summaryrefslogtreecommitdiff
path: root/backend/net.c
blob: a3ebded272e51fce7a42da740cdeac38578e8dbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
/* sane - Scanner Access Now Easy.
   Copyright (C) 1997 David Mosberger-Tang
   Copyright (C) 2003, 2008 Julien BLACHE <jb@jblache.org>
      AF-independent code + IPv6, Avahi support

   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice.

   This file implements a SANE network-based meta backend.  */

#ifdef _AIX
# include "../include/lalloca.h" /* MUST come first for AIX! */
#endif

#include "../include/sane/config.h"
#include "../include/lalloca.h"
#include "../include/_stdint.h"

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_LIBC_H
# include <libc.h> /* NeXTStep/OpenStep */
#endif

#include <sys/time.h>
#include <sys/types.h>

#include <netinet/in.h>
#include <netdb.h> /* OS/2 needs this _after_ <netinet/in.h>, grrr... */

#ifdef WITH_AVAHI
# include <avahi-client/client.h>
# include <avahi-client/lookup.h>

# include <avahi-common/thread-watch.h>
# include <avahi-common/malloc.h>
# include <avahi-common/error.h>

# define SANED_SERVICE_DNS "_sane-port._tcp"

static AvahiClient *avahi_client = NULL;
static AvahiThreadedPoll *avahi_thread = NULL;
static AvahiServiceBrowser *avahi_browser = NULL;
#endif /* WITH_AVAHI */

#include "../include/sane/sane.h"
#include "../include/sane/sanei.h"
#include "../include/sane/sanei_net.h"
#include "../include/sane/sanei_codec_bin.h"
#include "net.h"

#define BACKEND_NAME    net
#include "../include/sane/sanei_backend.h"

#ifndef PATH_MAX
# define PATH_MAX       1024
#endif

#include "../include/sane/sanei_config.h"
#define NET_CONFIG_FILE "net.conf"

/* Please increase version number with every change
   (don't forget to update net.desc) */

/* define the version string depending on which network code is used */
#if defined (HAVE_GETADDRINFO) && defined (HAVE_GETNAMEINFO)
# define NET_USES_AF_INDEP
# ifdef ENABLE_IPV6
#  define NET_VERSION "1.0.14 (AF-indep+IPv6)"
# else
#  define NET_VERSION "1.0.14 (AF-indep)"
# endif /* ENABLE_IPV6 */
#else
# undef ENABLE_IPV6
# define NET_VERSION "1.0.14"
#endif /* HAVE_GETADDRINFO && HAVE_GETNAMEINFO */

static SANE_Auth_Callback auth_callback;
static Net_Device *first_device;
static Net_Scanner *first_handle;
static const SANE_Device **devlist;
static int client_big_endian; /* 1 == big endian; 0 == little endian */
static int server_big_endian; /* 1 == big endian; 0 == little endian */
static int depth; /* bits per pixel */
static int connect_timeout = -1; /* timeout for connection to saned */

#ifndef NET_USES_AF_INDEP
static int saned_port;
#endif /* !NET_USES_AF_INDEP */

/* This variable is only needed, if the depth is 16bit/channel and
   client/server have different endianness.  A value of -1 means, that there's
   no hang over; otherwise the value has to be casted to SANE_Byte.  hang_over
   means, that there is a remaining byte from a previous call to sane_read,
   which could not be byte-swapped, e.g. because the frontend requested an odd
   number of bytes.
*/
static int hang_over;

/* This variable is only needed, if the depth is 16bit/channel and
   client/server have different endianness.  A value of -1 means, that there's
   no left over; otherwise the value has to be casted to SANE_Byte.  left_over
   means, that there is a remaining byte from a previous call to sane_read,
   which already is in the correct byte order, but could not be returned,
   e.g.  because the frontend requested only one byte per call.
*/
static int left_over;


#ifdef NET_USES_AF_INDEP
static SANE_Status
add_device (const char *name, Net_Device ** ndp)
{
  struct addrinfo hints;
  struct addrinfo *res;
  struct addrinfo *resp;
  struct sockaddr_in *sin;
#ifdef ENABLE_IPV6
  struct sockaddr_in6 *sin6;
#endif /* ENABLE_IPV6 */

  Net_Device *nd = NULL;

  int error;
  short sane_port = htons (6566);

  DBG (1, "add_device: adding backend %s\n", name);

  for (nd = first_device; nd; nd = nd->next)
    if (strcmp (nd->name, name) == 0)
      {
	DBG (1, "add_device: already in list\n");

	if (ndp)
	  *ndp = nd;

	return SANE_STATUS_GOOD;
      }

  memset (&hints, 0, sizeof(hints));

# ifdef ENABLE_IPV6
  hints.ai_family = PF_UNSPEC;
# else
  hints.ai_family = PF_INET;
# endif /* ENABLE_IPV6 */

  error = getaddrinfo (name, "sane-port", &hints, &res);
  if (error)
    {
      error = getaddrinfo (name, NULL, &hints, &res);
      if (error)
	{
	  DBG (1, "add_device: error while getting address of host %s: %s\n",
	       name, gai_strerror (error));

	  return SANE_STATUS_IO_ERROR;
	}
      else
	{
          for (resp = res; resp != NULL; resp = resp->ai_next) 
            { 
              switch (resp->ai_family) 
                { 
                  case AF_INET: 
                    sin = (struct sockaddr_in *) resp->ai_addr; 
                    sin->sin_port = sane_port; 
                    break; 
#ifdef ENABLE_IPV6 
		  case AF_INET6: 
		    sin6 = (struct sockaddr_in6 *) resp->ai_addr; 
		    sin6->sin6_port = sane_port; 
		    break; 
#endif /* ENABLE_IPV6 */ 
                }
	    } 
	}
    }

  nd = malloc (sizeof (Net_Device));
  if (!nd)
    {
      DBG (1, "add_device: not enough memory for Net_Device struct\n");
      
      freeaddrinfo (res);
      return SANE_STATUS_NO_MEM;
    }

  memset (nd, 0, sizeof (Net_Device));
  nd->name = strdup (name);
  if (!nd->name)
    {
      DBG (1, "add_device: not enough memory to duplicate name\n");
      free(nd);
      return SANE_STATUS_NO_MEM;
    }
      
  nd->addr = res;
  nd->ctl = -1;

  nd->next = first_device;

  first_device = nd;
      
  if (ndp)
    *ndp = nd;
  DBG (2, "add_device: backend %s added\n", name);
  return SANE_STATUS_GOOD;
}

#else /* !NET_USES_AF_INDEP */

static SANE_Status
add_device (const char *name, Net_Device ** ndp)
{
  struct hostent *he;
  Net_Device *nd;
  struct sockaddr_in *sin;

  DBG (1, "add_device: adding backend %s\n", name);

  for (nd = first_device; nd; nd = nd->next)
    if (strcmp (nd->name, name) == 0)
      {
	DBG (1, "add_device: already in list\n");

	if (ndp)
	  *ndp = nd;

	return SANE_STATUS_GOOD;
      }

  he = gethostbyname (name);
  if (!he)
    {
      DBG (1, "add_device: can't get address of host %s\n", name);
      return SANE_STATUS_IO_ERROR;
    }

  if (he->h_addrtype != AF_INET)
    {
      DBG (1, "add_device: don't know how to deal with addr family %d\n",
	   he->h_addrtype);
      return SANE_STATUS_INVAL;
    }

  nd = malloc (sizeof (*nd));
  if (!nd)
    {
      DBG (1, "add_device: not enough memory for Net_Device struct\n");
      return SANE_STATUS_NO_MEM;
    }

  memset (nd, 0, sizeof (*nd));
  nd->name = strdup (name);
  if (!nd->name)
    {
      DBG (1, "add_device: not enough memory to duplicate name\n");
      free (nd);
      return SANE_STATUS_NO_MEM;
    }
  nd->addr.sa_family = he->h_addrtype;

  sin = (struct sockaddr_in *) &nd->addr;
  memcpy (&sin->sin_addr, he->h_addr_list[0], he->h_length);

  nd->ctl = -1;
  nd->next = first_device;
  first_device = nd;
  if (ndp)
    *ndp = nd;
  DBG (2, "add_device: backend %s added\n", name);
  return SANE_STATUS_GOOD;
}
#endif /* NET_USES_AF_INDEP */


#ifdef NET_USES_AF_INDEP
static SANE_Status
connect_dev (Net_Device * dev)
{
  struct addrinfo *addrp;

  SANE_Word version_code;
  SANE_Init_Reply reply;
  SANE_Status status = SANE_STATUS_IO_ERROR;
  SANE_Init_Req req;
  SANE_Bool connected = SANE_FALSE;
#ifdef TCP_NODELAY
  int on = 1;
  int level = -1;
#endif
  struct timeval tv;

  int i;

  DBG (2, "connect_dev: trying to connect to %s\n", dev->name);

  for (addrp = dev->addr, i = 0; (addrp != NULL) && (connected == SANE_FALSE); addrp = addrp->ai_next, i++)
    {
# ifdef ENABLE_IPV6
      if ((addrp->ai_family != AF_INET) && (addrp->ai_family != AF_INET6))
# else /* !ENABLE_IPV6 */
      if (addrp->ai_family != AF_INET)
# endif /* ENABLE_IPV6 */
	{
	  DBG (1, "connect_dev: [%d] don't know how to deal with addr family %d\n",
	       i, addrp->ai_family);
	  continue;
	}
      
      dev->ctl = socket (addrp->ai_family, SOCK_STREAM, 0);
      if (dev->ctl < 0)
	{
	  DBG (1, "connect_dev: [%d] failed to obtain socket (%s)\n",
	       i, strerror (errno));
	  dev->ctl = -1;
	  continue;
	}

      /* Set SO_SNDTIMEO for the connection to saned */
      if (connect_timeout > 0)
	{
	  tv.tv_sec = connect_timeout;
	  tv.tv_usec = 0;

	  if (setsockopt (dev->ctl, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
	    {
	      DBG (1, "connect_dev: [%d] failed to set SO_SNDTIMEO (%s)\n", i, strerror (errno));
	    }
	}

      if (connect (dev->ctl, addrp->ai_addr, addrp->ai_addrlen) < 0)
	{
	  DBG (1, "connect_dev: [%d] failed to connect (%s)\n", i, strerror (errno));
	  dev->ctl = -1;
	  continue;
	}
      DBG (3, "connect_dev: [%d] connection succeeded (%s)\n", i, (addrp->ai_family == AF_INET6) ? "IPv6" : "IPv4");
      dev->addr_used = addrp;
      connected = SANE_TRUE;
    }

  if (connected != SANE_TRUE)
    {
      DBG (1, "connect_dev: couldn't connect to host (see messages above)\n");
      return SANE_STATUS_IO_ERROR;
    }

#else /* !NET_USES_AF_INDEP */

static SANE_Status
connect_dev (Net_Device * dev)
{
  struct sockaddr_in *sin;
  SANE_Word version_code;
  SANE_Init_Reply reply;
  SANE_Status status = SANE_STATUS_IO_ERROR;
  SANE_Init_Req req;
#ifdef TCP_NODELAY
  int on = 1;
  int level = -1;
#endif
  struct timeval tv;

  DBG (2, "connect_dev: trying to connect to %s\n", dev->name);

  if (dev->addr.sa_family != AF_INET)
    {
      DBG (1, "connect_dev: don't know how to deal with addr family %d\n",
	   dev->addr.sa_family);
      return SANE_STATUS_IO_ERROR;
    }

  dev->ctl = socket (dev->addr.sa_family, SOCK_STREAM, 0);
  if (dev->ctl < 0)
    {
      DBG (1, "connect_dev: failed to obtain socket (%s)\n",
	   strerror (errno));
      dev->ctl = -1;
      return SANE_STATUS_IO_ERROR;
    }
  sin = (struct sockaddr_in *) &dev->addr;
  sin->sin_port = saned_port;


  /* Set SO_SNDTIMEO for the connection to saned */
  if (connect_timeout > 0)
    {
      tv.tv_sec = connect_timeout;
      tv.tv_usec = 0;

      if (setsockopt (dev->ctl, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
	{
	  DBG (1, "connect_dev: failed to set SO_SNDTIMEO (%s)\n", strerror (errno));
	}
    }

  if (connect (dev->ctl, &dev->addr, sizeof (dev->addr)) < 0)
    {
      DBG (1, "connect_dev: failed to connect (%s)\n", strerror (errno));
      dev->ctl = -1;
      return SANE_STATUS_IO_ERROR;
    }
  DBG (3, "connect_dev: connection succeeded\n");
#endif /* NET_USES_AF_INDEP */

  /* We're connected now, so reset SO_SNDTIMEO to the default value of 0 */
  if (connect_timeout > 0)
    {
      tv.tv_sec = 0;
      tv.tv_usec = 0;

      if (setsockopt (dev->ctl, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
	{
	  DBG (1, "connect_dev: failed to reset SO_SNDTIMEO (%s)\n", strerror (errno));
	}
    }

#ifdef TCP_NODELAY
# ifdef SOL_TCP
  level = SOL_TCP;
# else /* !SOL_TCP */
  /* Look up the protocol level in the protocols database. */
  {
    struct protoent *p;
    p = getprotobyname ("tcp");
    if (p == 0)
      DBG (1, "connect_dev: cannot look up `tcp' protocol number");
    else
      level = p->p_proto;
  }
# endif	/* SOL_TCP */

  if (level == -1 ||
      setsockopt (dev->ctl, level, TCP_NODELAY, &on, sizeof (on)))
    DBG (1, "connect_dev: failed to put send socket in TCP_NODELAY mode (%s)",
	 strerror (errno));
#endif /* !TCP_NODELAY */

  DBG (2, "connect_dev: sanei_w_init\n");
  sanei_w_init (&dev->wire, sanei_codec_bin_init);
  dev->wire.io.fd = dev->ctl;
  dev->wire.io.read = read;
  dev->wire.io.write = write;

  /* exchange version codes with the server: */
  req.version_code = SANE_VERSION_CODE (V_MAJOR, V_MINOR,
					SANEI_NET_PROTOCOL_VERSION);
  req.username = getlogin ();
  DBG (2, "connect_dev: net_init (user=%s, local version=%d.%d.%d)\n",
       req.username, V_MAJOR, V_MINOR, SANEI_NET_PROTOCOL_VERSION);
  sanei_w_call (&dev->wire, SANE_NET_INIT,
		(WireCodecFunc) sanei_w_init_req, &req,
		(WireCodecFunc) sanei_w_init_reply, &reply);

  if (dev->wire.status != 0)
    {
      DBG (1, "connect_dev: argument marshalling error (%s)\n",
	   strerror (dev->wire.status));
      status = SANE_STATUS_IO_ERROR;
      goto fail;
    }

  status = reply.status;
  version_code = reply.version_code;
  DBG (2, "connect_dev: freeing init reply (status=%s, remote "
       "version=%d.%d.%d)\n", sane_strstatus (status),
       SANE_VERSION_MAJOR (version_code),
       SANE_VERSION_MINOR (version_code), SANE_VERSION_BUILD (version_code));
  sanei_w_free (&dev->wire, (WireCodecFunc) sanei_w_init_reply, &reply);

  if (status != 0)
    {
      DBG (1, "connect_dev: access to %s denied\n", dev->name);
      goto fail;
    }
  if (SANE_VERSION_MAJOR (version_code) != V_MAJOR)
    {
      DBG (1, "connect_dev: major version mismatch: got %d, expected %d\n",
	   SANE_VERSION_MAJOR (version_code), V_MAJOR);
      status = SANE_STATUS_IO_ERROR;
      goto fail;
    }
  if (SANE_VERSION_BUILD (version_code) != SANEI_NET_PROTOCOL_VERSION
      && SANE_VERSION_BUILD (version_code) != 2)
    {
      DBG (1, "connect_dev: network protocol version mismatch: "
	   "got %d, expected %d\n",
	   SANE_VERSION_BUILD (version_code), SANEI_NET_PROTOCOL_VERSION);
      status = SANE_STATUS_IO_ERROR;
      goto fail;
    }
  dev->wire.version = SANE_VERSION_BUILD (version_code);
  DBG (4, "connect_dev: done\n");
  return SANE_STATUS_GOOD;

fail:
  DBG (2, "connect_dev: closing connection to %s\n", dev->name);
  close (dev->ctl);
  dev->ctl = -1;
  return status;
}


static SANE_Status
fetch_options (Net_Scanner * s)
{
  int option_number;
  DBG (3, "fetch_options: %p\n", (void *) s);

  if (s->opt.num_options)
    {
      DBG (2, "fetch_options: %d option descriptors cached... freeing\n",
	   s->opt.num_options);
      sanei_w_set_dir (&s->hw->wire, WIRE_FREE);
      s->hw->wire.status = 0;
      sanei_w_option_descriptor_array (&s->hw->wire, &s->opt);
      if (s->hw->wire.status)
	{
	  DBG (1, "fetch_options: failed to free old list (%s)\n",
	       strerror (s->hw->wire.status));
	  return SANE_STATUS_IO_ERROR;
	}
    }
  DBG (3, "fetch_options: get_option_descriptors\n");
  sanei_w_call (&s->hw->wire, SANE_NET_GET_OPTION_DESCRIPTORS,
		(WireCodecFunc) sanei_w_word, &s->handle,
		(WireCodecFunc) sanei_w_option_descriptor_array, &s->opt);
  if (s->hw->wire.status)
    {
      DBG (1, "fetch_options: failed to get option descriptors (%s)\n",
	   strerror (s->hw->wire.status));
      return SANE_STATUS_IO_ERROR;
    }

  if (s->local_opt.num_options == 0)
    {
      DBG (3, "fetch_options: creating %d local option descriptors\n",
	   s->opt.num_options);
      s->local_opt.desc = 
	malloc (s->opt.num_options * sizeof (s->local_opt.desc));
      if (!s->local_opt.desc)
	{
	  DBG (1, "fetch_options: couldn't malloc s->local_opt.desc\n");
	  return SANE_STATUS_NO_MEM;
	}
      for (option_number = 0; 
	   option_number < s->opt.num_options; 
	   option_number++)
	{
	  s->local_opt.desc[option_number] =
	    malloc (sizeof (SANE_Option_Descriptor));
	  if (!s->local_opt.desc[option_number])
	    {
	      DBG (1, "fetch_options: couldn't malloc "
		   "s->local_opt.desc[%d]\n", option_number);
	      return SANE_STATUS_NO_MEM;
	    }
	}
      s->local_opt.num_options = s->opt.num_options;
    }
  else if (s->local_opt.num_options != s->opt.num_options)
    {
      DBG (1, "fetch_options: option number count changed during runtime?\n");
      return SANE_STATUS_INVAL;
    }

  DBG (3, "fetch_options: copying %d option descriptors\n", 
       s->opt.num_options);
      
  for (option_number = 0; option_number < s->opt.num_options; option_number++)
    {
      memcpy (s->local_opt.desc[option_number], s->opt.desc[option_number],
	      sizeof (SANE_Option_Descriptor));
    }
  
  s->options_valid = 1;
  DBG (3, "fetch_options: %d options fetched\n", s->opt.num_options);
  return SANE_STATUS_GOOD;
}

static SANE_Status
do_cancel (Net_Scanner * s)
{
  DBG (2, "do_cancel: %p\n", (void *) s);
  s->hw->auth_active = 0;
  if (s->data >= 0)
    {
      DBG (3, "do_cancel: closing data pipe\n");
      close (s->data);
      s->data = -1;
    }
  return SANE_STATUS_CANCELLED;
}

static void
do_authorization (Net_Device * dev, SANE_String resource)
{
  SANE_Authorization_Req req;
  SANE_Char username[SANE_MAX_USERNAME_LEN];
  SANE_Char password[SANE_MAX_PASSWORD_LEN];
  char *net_resource;

  DBG (2, "do_authorization: dev=%p resource=%s\n", (void *) dev, resource);

  dev->auth_active = 1;

  memset (&req, 0, sizeof (req));
  memset (username, 0, sizeof (SANE_Char) * SANE_MAX_USERNAME_LEN);
  memset (password, 0, sizeof (SANE_Char) * SANE_MAX_PASSWORD_LEN);

  net_resource = malloc (strlen (resource) + 6 + strlen (dev->name));

  if (net_resource != NULL)
    {
      sprintf (net_resource, "net:%s:%s", dev->name, resource);
      if (auth_callback)
	{
	  DBG (2, "do_authorization: invoking auth_callback, resource = %s\n",
	       net_resource);
	  (*auth_callback) (net_resource, username, password);
	}
      else
	DBG (1, "do_authorization: no auth_callback present\n");
      free (net_resource);
    }
  else /* Is this necessary? If we don't have these few bytes we will get
	  in trouble later anyway */
    {
      DBG (1, "do_authorization: not enough memory for net_resource\n");
      if (auth_callback)
	{
	  DBG (2, "do_authorization: invoking auth_callback, resource = %s\n",
	       resource);
	  (*auth_callback) (resource, username, password);
	}
      else
	DBG (1, "do_authorization: no auth_callback present\n");
    }

  if (dev->auth_active)
    {
      SANE_Word ack;

      req.resource = resource;
      req.username = username;
      req.password = password;
      DBG (2, "do_authorization: relaying authentication data\n");
      sanei_w_call (&dev->wire, SANE_NET_AUTHORIZE,
		    (WireCodecFunc) sanei_w_authorization_req, &req,
		    (WireCodecFunc) sanei_w_word, &ack);
    }
  else
    DBG (1, "do_authorization: auth_active is false... strange\n");
}


#ifdef WITH_AVAHI
static void
net_avahi_resolve_callback (AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol,
			    AvahiResolverEvent event, const char *name, const char *type,
			    const char *domain, const char *host_name, const AvahiAddress *address,
			    uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags,
			    void *userdata)
{
  char a[AVAHI_ADDRESS_STR_MAX];
  char *t;

  /* unused */
  interface = interface;
  protocol = protocol;
  userdata = userdata;

  if (!r)
    return;

  switch (event)
    {
      case AVAHI_RESOLVER_FAILURE:
	DBG (1, "net_avahi_resolve_callback: failed to resolve service '%s' of type '%s' in domain '%s': %s\n",
	     name, type, domain, avahi_strerror (avahi_client_errno (avahi_service_resolver_get_client (r))));
	break;

      case AVAHI_RESOLVER_FOUND:
	DBG (3, "net_avahi_resolve_callback: service '%s' of type '%s' in domain '%s':\n", name, type, domain);

	avahi_address_snprint(a, sizeof (a), address);
	t = avahi_string_list_to_string (txt);

	DBG (3, "\t%s:%u (%s)\n\tTXT=%s\n\tcookie is %u\n\tis_local: %i\n\tour_own: %i\n"
	     "\twide_area: %i\n\tmulticast: %i\n\tcached: %i\n",
	     host_name, port, a, t, avahi_string_list_get_service_cookie (txt),
	     !!(flags & AVAHI_LOOKUP_RESULT_LOCAL), !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
	     !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA), !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
	     !!(flags & AVAHI_LOOKUP_RESULT_CACHED));

	/* TODO: evaluate TXT record */

	/* Try first with the name */
	if (add_device (host_name, NULL) != SANE_STATUS_GOOD)
	  {
	    DBG (1, "net_avahi_resolve_callback: couldn't add backend with name %s\n", host_name);

	    /* Then try the raw IP address */
	    if (add_device (t, NULL) != SANE_STATUS_GOOD)
	      DBG (1, "net_avahi_resolve_callback: couldn't add backend with IP address %s either\n", t);
	  }

	avahi_free (t);
	break;
    }

  avahi_service_resolver_free(r);
}

static void
net_avahi_browse_callback (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol,
			   AvahiBrowserEvent event, const char *name, const char *type,
			   const char *domain, AvahiLookupResultFlags flags, void *userdata)
{
  AvahiProtocol proto;

  /* unused */
  flags = flags;
  userdata = userdata;

  if (!b)
    return;

  switch (event)
    {
      case AVAHI_BROWSER_FAILURE:
	DBG (1, "net_avahi_browse_callback: %s\n", avahi_strerror (avahi_client_errno (avahi_service_browser_get_client (b))));
	avahi_threaded_poll_quit (avahi_thread);
	return;

      case AVAHI_BROWSER_NEW:
	DBG (3, "net_avahi_browse_callback: NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);

	/* The server will actually be added to our list in the resolver callback */

	/* The resolver object will be freed in the resolver callback, or by
	 * the server if it terminates before the callback is called.
	 */
#ifdef ENABLE_IPV6
	proto = AVAHI_PROTO_UNSPEC;
#else
	proto = AVAHI_PROTO_INET;
#endif /* ENABLE_IPV6 */
	if (!(avahi_service_resolver_new (avahi_client, interface, protocol, name, type, domain, proto, 0, net_avahi_resolve_callback, NULL)))
	  DBG (2, "net_avahi_browse_callback: failed to resolve service '%s': %s\n", name, avahi_strerror (avahi_client_errno (avahi_client)));
	break;

      case AVAHI_BROWSER_REMOVE:
	DBG (3, "net_avahi_browse_callback: REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
	/* With the current architecture, we cannot safely remove a server from the list */
	break;

      case AVAHI_BROWSER_ALL_FOR_NOW:
      case AVAHI_BROWSER_CACHE_EXHAUSTED:
	DBG (3, "net_avahi_browse_callback: %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
	break;
    }
}

static void
net_avahi_callback (AvahiClient *c, AvahiClientState state, void * userdata)
{
  AvahiProtocol proto;
  int error;

  /* unused */
  userdata = userdata;

  if (!c)
    return;

  switch (state)
    {
      case AVAHI_CLIENT_CONNECTING:
	break;

      case AVAHI_CLIENT_S_COLLISION:
      case AVAHI_CLIENT_S_REGISTERING:
      case AVAHI_CLIENT_S_RUNNING:
	if (avahi_browser)
	  return;

#ifdef ENABLE_IPV6
	proto = AVAHI_PROTO_UNSPEC;
#else
	proto = AVAHI_PROTO_INET;
#endif /* ENABLE_IPV6 */

	avahi_browser = avahi_service_browser_new (c, AVAHI_IF_UNSPEC, proto, SANED_SERVICE_DNS, NULL, 0, net_avahi_browse_callback, NULL);
	if (avahi_browser == NULL)
	  {
	    DBG (1, "net_avahi_callback: could not create service browser: %s\n", avahi_strerror (avahi_client_errno (c)));
	    avahi_threaded_poll_quit (avahi_thread);
	  }
	break;

      case AVAHI_CLIENT_FAILURE:
	error = avahi_client_errno (c);

	if (error == AVAHI_ERR_DISCONNECTED)
	  {
	    /* Server disappeared - try to reconnect */
	    avahi_client_free (avahi_client);
	    avahi_client = NULL;

	    if (avahi_browser)
	      {
		avahi_service_browser_free (avahi_browser);
		avahi_browser = NULL;
	      }

	    avahi_client = avahi_client_new (avahi_threaded_poll_get (avahi_thread), AVAHI_CLIENT_NO_FAIL, net_avahi_callback, NULL, &error);
	    if (avahi_client == NULL)
	      {
		DBG (1, "net_avahi_init: could not create Avahi client: %s\n", avahi_strerror (error));
		avahi_threaded_poll_quit (avahi_thread);
	      }
	  }
	else
	  {
	    /* Another error happened - game over */
	    DBG (1, "net_avahi_callback: server connection failure: %s\n", avahi_strerror (error));
	    avahi_threaded_poll_quit (avahi_thread);
	  }
	break;
    }
}


static void
net_avahi_init (void)
{
  int error;

  avahi_thread = avahi_threaded_poll_new ();
  if (avahi_thread == NULL)
    {
      DBG (1, "net_avahi_init: could not create threaded poll object\n");
      goto fail;
    }

  avahi_client = avahi_client_new (avahi_threaded_poll_get (avahi_thread), AVAHI_CLIENT_NO_FAIL, net_avahi_callback, NULL, &error);
  if (avahi_client == NULL)
    {
      DBG (1, "net_avahi_init: could not create Avahi client: %s\n", avahi_strerror (error));
      goto fail;
    }

  if (avahi_threaded_poll_start (avahi_thread) < 0)
    {
      DBG (1, "net_avahi_init: Avahi thread failed to start\n");
      goto fail;
    }

  /* All done */
  return;

 fail:
  DBG (1, "net_avahi_init: Avahi init failed, support disabled\n");

  if (avahi_client)
    {
      avahi_client_free (avahi_client);
      avahi_client = NULL;
    }

  if (avahi_thread)
    {
      avahi_threaded_poll_free (avahi_thread);
      avahi_thread = NULL;
    }
}

static void
net_avahi_cleanup (void)
{
  if (!avahi_thread)
    return;

  DBG (1, "net_avahi_cleanup: stopping thread\n");

  avahi_threaded_poll_stop (avahi_thread);

  if (avahi_browser)
    avahi_service_browser_free (avahi_browser);

  if (avahi_client)
    avahi_client_free (avahi_client);

  avahi_threaded_poll_free (avahi_thread);

  DBG (1, "net_avahi_cleanup: done\n");
}
#endif /* WITH_AVAHI */


SANE_Status
sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
{
  char device_name[PATH_MAX];
  const char *optval;
  const char *env;
  size_t len;
  FILE *fp;
  short ns = 0x1234;
  unsigned char *p = (unsigned char *)(&ns);

#ifndef NET_USES_AF_INDEP
  struct servent *serv;
#endif /* !NET_USES_AF_INDEP */

  DBG_INIT ();

  DBG (2, "sane_init: authorize %s null, version_code %s null\n", (authorize) ? "!=" : "==",
       (version_code) ? "!=" : "==");

  devlist = NULL;
  first_device = NULL;
  first_handle = NULL;

#ifdef WITH_AVAHI
  net_avahi_init ();
#endif /* WITH_AVAHI */

  auth_callback = authorize;

  /* Return the version number of the sane-backends package to allow
     the frontend to print them. This is done only for net and dll,
     because these backends are usually called by the frontend. */
  if (version_code)
    *version_code = SANE_VERSION_CODE (SANE_DLL_V_MAJOR, SANE_DLL_V_MINOR,
				       SANE_DLL_V_BUILD);

  DBG (1, "sane_init: SANE net backend version %s from %s\n", NET_VERSION,
       PACKAGE_STRING);

  /* determine (client) machine byte order */
  if (*p == 0x12)
    {
      client_big_endian = 1;
      DBG (3, "sane_init: Client has big endian byte order\n");
    }
  else
    {
      client_big_endian = 0;
      DBG (3, "sane_init: Client has little endian byte order\n");
    }

#ifndef NET_USES_AF_INDEP
  DBG (2, "sane_init: determining sane service port\n");
  serv = getservbyname ("sane-port", "tcp");

  if (serv)
    {
      DBG (2, "sane_init: found port %d\n", ntohs (serv->s_port));
      saned_port = serv->s_port;
    }
  else
    {
      saned_port = htons (6566);
      DBG (1, "sane_init: could not find `sane-port' service (%s); using default "
	   "port %d\n", strerror (errno), ntohs (saned_port));
    }
#endif /* !NET_USES_AF_INDEP */

  DBG (2, "sane_init: searching for config file\n");
  fp = sanei_config_open (NET_CONFIG_FILE);
  if (fp)
    {
      while (sanei_config_read (device_name, sizeof (device_name), fp))
	{
	  if (device_name[0] == '#')	/* ignore line comments */
	    continue;
	  len = strlen (device_name);

	  if (!len)
	    continue;		/* ignore empty lines */

	  /*
	   * Check for net backend options.
	   * Anything that isn't an option is a saned host.
	   */
	  if (strstr(device_name, "connect_timeout") != NULL)
	    {
	      /* Look for the = sign; if it's not there, error out */
	      optval = strchr(device_name, '=');

	      if (!optval)
		continue;

	      optval = sanei_config_skip_whitespace (++optval);
	      if ((optval != NULL) && (*optval != '\0'))
		{
		  connect_timeout = atoi(optval);

		  DBG (2, "sane_init: connect timeout set to %d seconds\n", connect_timeout);
		}

	      continue;
	    }

	  DBG (2, "sane_init: trying to add %s\n", device_name);
	  add_device (device_name, 0);
	}

      fclose (fp);
      DBG (2, "sane_init: done reading config\n");
    }
  else
    DBG (1, "sane_init: could not open config file (%s): %s\n",
	 NET_CONFIG_FILE, strerror (errno));

  DBG (2, "sane_init: evaluating environment variable SANE_NET_HOSTS\n");
  env = getenv ("SANE_NET_HOSTS");
  if (env)
    {
      char *copy, *next, *host;
      if ((copy = strdup (env)) != NULL)
	{
	  next = copy;
	  while ((host = strsep (&next, ":")))
	    {
#ifdef ENABLE_IPV6
	      if (host[0] == '[')
		{
		  /* skip '[' (host[0]) */
		  host++;
		  /* get the rest of the IPv6 addr (we're screwed if ] is missing)
		   * Is it worth checking for the matching ] ? Not for now. */
		  strsep (&next, "]");
		  /* add back the ":" that got removed by the strsep() */
		  host[strlen (host)] = ':';
		  /* host now holds the IPv6 address */
		  
		  /* skip the ':' that could be after ] (avoids a call to strsep() */
		  if (next[0] == ':')
		    next++;
		}

	      /* 
	       * if the IPv6 is last in the list, the strsep() call in the while()
	       * will return a string with the first char being '\0'. Skip it.
	       */
	      if (host[0] == '\0')
		  continue;
#endif /* ENABLE_IPV6 */
	      DBG (2, "sane_init: trying to add %s\n", host);
	      add_device (host, 0);
	    }
	  free (copy);
	}
      else
	DBG (1, "sane_init: not enough memory to duplicate "
	     "environment variable\n");
    }

  DBG (2, "sane_init: evaluating environment variable SANE_NET_TIMEOUT\n");
  env = getenv ("SANE_NET_TIMEOUT");
  if (env)
    {
      connect_timeout = atoi(env);
      DBG (2, "sane_init: connect timeout set to %d seconds from env\n", connect_timeout);
    }

  DBG (2, "sane_init: done\n");
  return SANE_STATUS_GOOD;
}

void
sane_exit (void)
{
  Net_Scanner *handle, *next_handle;
  Net_Device *dev, *next_device;
  int i;

  DBG (1, "sane_exit: exiting\n");

#ifdef WITH_AVAHI
  net_avahi_cleanup ();
#endif /* WITH_AVAHI */

  /* first, close all handles: */
  for (handle = first_handle; handle; handle = next_handle)
    {
      next_handle = handle->next;
      sane_close (handle);
    }
  first_handle = 0;

  /* now close all devices: */
  for (dev = first_device; dev; dev = next_device)
    {
      next_device = dev->next;

      DBG (2, "sane_exit: closing dev %p, ctl=%d\n", (void *) dev, dev->ctl);

      if (dev->ctl >= 0)
	{
	  sanei_w_call (&dev->wire, SANE_NET_EXIT,
			(WireCodecFunc) sanei_w_void, 0,
			(WireCodecFunc) sanei_w_void, 0);
	  sanei_w_exit (&dev->wire);
	  close (dev->ctl);
	}
      if (dev->name)
	free ((void *) dev->name);

#ifdef NET_USES_AF_INDEP      
      if (dev->addr)
	freeaddrinfo(dev->addr);
#endif /* NET_USES_AF_INDEP */

      free (dev);
    }
  if (devlist)
    {
      for (i = 0; devlist[i]; ++i)
	{
	  if (devlist[i]->vendor)
	    free ((void *) devlist[i]->vendor);
	  if (devlist[i]->model)
	    free ((void *) devlist[i]->model);
	  if (devlist[i]->type)
	    free ((void *) devlist[i]->type);
	  free ((void *) devlist[i]);
	}
      free (devlist);
    }
  DBG (3, "sane_exit: finished.\n");
}

/* Note that a call to get_devices() implies that we'll have to
   connect to all remote hosts.  To avoid this, you can call
   sane_open() directly (assuming you know the name of the
   backend/device).  This is appropriate for the command-line
   interface of SANE, for example.
 */
SANE_Status
sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
{
  static int devlist_size = 0, devlist_len = 0;
  static const SANE_Device *empty_devlist[1] = { 0 };
  SANE_Get_Devices_Reply reply;
  SANE_Status status;
  Net_Device *dev;
  char *full_name;
  int i, num_devs;
  size_t len;
#define ASSERT_SPACE(n)                                                    \
  {                                                                        \
    if (devlist_len + (n) > devlist_size)                                  \
      {                                                                    \
        devlist_size += (n) + 15;                                          \
        if (devlist)                                                       \
          devlist = realloc (devlist, devlist_size * sizeof (devlist[0])); \
        else                                                               \
          devlist = malloc (devlist_size * sizeof (devlist[0]));           \
        if (!devlist)                                                      \
          {                                                                \
             DBG (1, "sane_get_devices: not enough memory\n");	           \
             return SANE_STATUS_NO_MEM;                                    \
          }                                                                \
      }                                                                    \
  }

  DBG (3, "sane_get_devices: local_only = %d\n", local_only);

  if (local_only)
    {
      *device_list = empty_devlist;
      return SANE_STATUS_GOOD;
    }

  if (devlist)
    {
      DBG (2, "sane_get_devices: freeing devlist\n");
      for (i = 0; devlist[i]; ++i)
	{
	  if (devlist[i]->vendor)
	    free ((void *) devlist[i]->vendor);
	  if (devlist[i]->model)
	    free ((void *) devlist[i]->model);
	  if (devlist[i]->type)
	    free ((void *) devlist[i]->type);
	  free ((void *) devlist[i]);
	}
      free (devlist);
      devlist = 0;
    }
  devlist_len = 0;
  devlist_size = 0;

  for (dev = first_device; dev; dev = dev->next)
    {
      if (dev->ctl < 0)
	{
	  status = connect_dev (dev);
	  if (status != SANE_STATUS_GOOD)
	    {
	      DBG (1, "sane_get_devices: ignoring failure to connect to %s\n",
		   dev->name);
	      continue;
	    }
	}
      sanei_w_call (&dev->wire, SANE_NET_GET_DEVICES,
		    (WireCodecFunc) sanei_w_void, 0,
		    (WireCodecFunc) sanei_w_get_devices_reply, &reply);
      if (reply.status != SANE_STATUS_GOOD)
	{
	  DBG (1, "sane_get_devices: ignoring rpc-returned status %s\n",
	       sane_strstatus (reply.status));
	  sanei_w_free (&dev->wire,
			(WireCodecFunc) sanei_w_get_devices_reply, &reply);
	  continue;
	}

      /* count the number of devices for this backend: */
      for (num_devs = 0; reply.device_list[num_devs]; ++num_devs);

      ASSERT_SPACE (num_devs);

      for (i = 0; i < num_devs; ++i)
	{
	  SANE_Device *rdev;
	  char *mem;
#ifdef ENABLE_IPV6
	  SANE_Bool IPv6 = SANE_FALSE;
#endif /* ENABLE_IPV6 */

	  /* create a new device entry with a device name that is the
	     sum of the backend name a colon and the backend's device
	     name: */
	  len = strlen (dev->name) + 1 + strlen (reply.device_list[i]->name);

#ifdef ENABLE_IPV6
	  if (strchr (dev->name, ':') != NULL)
	    {
	      len += 2;
	      IPv6 = SANE_TRUE;
	    }
#endif /* ENABLE_IPV6 */

	  mem = malloc (sizeof (*dev) + len + 1);
	  if (!mem)
	    {
	      DBG (1, "sane_get_devices: not enough free memory\n");
	      sanei_w_free (&dev->wire,
			    (WireCodecFunc) sanei_w_get_devices_reply,
			    &reply);
	      return SANE_STATUS_NO_MEM;
	    }

	  memset (mem, 0, sizeof (*dev) + len);
	  full_name = mem + sizeof (*dev);
	  
#ifdef ENABLE_IPV6
	  if (IPv6 == SANE_TRUE)
	    strcat (full_name, "[");
#endif /* ENABLE_IPV6 */

	  strcat (full_name, dev->name);

#ifdef ENABLE_IPV6	  
	  if (IPv6 == SANE_TRUE)
	    strcat (full_name, "]");
#endif /* ENABLE_IPV6 */

	  strcat (full_name, ":");
	  strcat (full_name, reply.device_list[i]->name);
	  DBG (3, "sane_get_devices: got %s\n", full_name);

	  rdev = (SANE_Device *) mem;
	  rdev->name = full_name;
	  rdev->vendor = strdup (reply.device_list[i]->vendor);
	  rdev->model = strdup (reply.device_list[i]->model);
	  rdev->type = strdup (reply.device_list[i]->type);

	  if ((!rdev->vendor) || (!rdev->model) || (!rdev->type))
	    {
	      DBG (1, "sane_get_devices: not enough free memory\n");
	      if (rdev->vendor)
		free ((void *) rdev->vendor);
	      if (rdev->model)
		free ((void *) rdev->model);
	      if (rdev->type)
		free ((void *) rdev->type);
	      free (rdev);
	      sanei_w_free (&dev->wire,
			    (WireCodecFunc) sanei_w_get_devices_reply,
			    &reply);
	      return SANE_STATUS_NO_MEM;
	    }

	  devlist[devlist_len++] = rdev;
	}
      /* now free up the rpc return value: */
      sanei_w_free (&dev->wire,
		    (WireCodecFunc) sanei_w_get_devices_reply, &reply);
    }

  /* terminate device list with NULL entry: */
  ASSERT_SPACE (1);
  devlist[devlist_len++] = 0;

  *device_list = devlist;
  DBG (2, "sane_get_devices: finished (%d devices)\n", devlist_len - 1);
  return SANE_STATUS_GOOD;
}

SANE_Status
sane_open (SANE_String_Const full_name, SANE_Handle * meta_handle)
{
  SANE_Open_Reply reply;
  const char *dev_name;
#ifdef ENABLE_IPV6
  const char *tmp_name;
  SANE_Bool v6addr = SANE_FALSE;
#endif /* ENABLE_IPV6 */
  SANE_String nd_name;
  SANE_Status status;
  SANE_Word handle;
  SANE_Word ack;
  Net_Device *dev;
  Net_Scanner *s;
  int need_auth;

  DBG (3, "sane_open(\"%s\")\n", full_name);
  
#ifdef ENABLE_IPV6
  /*
   * Check whether a numerical IPv6 host was specified
   * [2001:42:42::12] <== check for '[' as full_name[0]
   * ex: [2001:42:42::12]:test:0 (syntax taken from Apache 2)
   */
  if (full_name[0] == '[')
    {
      v6addr = SANE_TRUE;
      tmp_name = strchr (full_name, ']');
      if (!tmp_name)
	{
	  DBG (1, "sane_open: incorrect host address: missing matching ']'\n");
	  return SANE_STATUS_INVAL;
	}
    }
  else
    tmp_name = full_name;

  dev_name = strchr (tmp_name, ':');
#else /* !ENABLE_IPV6 */

  dev_name = strchr (full_name, ':');  
#endif /* ENABLE_IPV6 */

  if (dev_name)
    {
#ifdef strndupa
# ifdef ENABLE_IPV6
      if (v6addr == SANE_TRUE)
	nd_name = strndupa (full_name + 1, dev_name - full_name - 2);
      else
	nd_name = strndupa (full_name, dev_name - full_name);

# else /* !ENABLE_IPV6 */

      nd_name = strndupa (full_name, dev_name - full_name);
# endif /* ENABLE_IPV6 */

      if (!nd_name)
	{
	  DBG (1, "sane_open: not enough free memory\n");
	  return SANE_STATUS_NO_MEM;
	}
#else
      char *tmp;

# ifdef ENABLE_IPV6
      if (v6addr == SANE_TRUE)
	tmp = alloca (dev_name - full_name - 2 + 1);
      else
	tmp = alloca (dev_name - full_name + 1);

# else /* !ENABLE_IPV6 */

      tmp = alloca (dev_name - full_name + 1);
# endif /* ENABLE_IPV6 */

      if (!tmp)
	{
	  DBG (1, "sane_open: not enough free memory\n");
	  return SANE_STATUS_NO_MEM;
	}

# ifdef ENABLE_IPV6
      if (v6addr == SANE_TRUE)
	{
	  memcpy (tmp, full_name + 1, dev_name - full_name - 2);
	  tmp[dev_name - full_name - 2] = '\0';
	}
      else
	{
	  memcpy (tmp, full_name, dev_name - full_name);
	  tmp[dev_name - full_name] = '\0';
	}

# else /* !ENABLE_IPV6 */

      memcpy (tmp, full_name, dev_name - full_name);
      tmp[dev_name - full_name] = '\0';
# endif /* ENABLE_IPV6 */

      nd_name = tmp;
#endif
      ++dev_name;		/* skip colon */
    }
  else
    {
      /* if no colon interpret full_name as the host name; an empty
         device name will cause us to open the first device of that
         host.  */
#ifdef ENABLE_IPV6
      if (v6addr == SANE_TRUE)
	{
	  nd_name = alloca (strlen (full_name) - 2 + 1);
	  if (!nd_name)
	    {
	      DBG (1, "sane_open: not enough free memory\n");
	      return SANE_STATUS_NO_MEM;
	    }
	  memcpy (nd_name, full_name + 1, strlen (full_name) - 2);
	  nd_name[strlen (full_name) - 2] = '\0';
	}
      else
	nd_name = (char *) full_name;

#else /* !ENABLE_IPV6 */

      nd_name = (char *) full_name;
#endif /* ENABLE_IPV6 */

      dev_name = "";
    }
  DBG (2, "sane_open: host = %s, device = %s\n", nd_name, dev_name);

  if (!nd_name[0])
    {
      /* Unlike other backends, we never allow an empty backend-name.
         Otherwise, it's possible that sane_open("") will result in
         endless looping (consider the case where NET is the first
         backend...) */

      DBG (1, "sane_open: empty backend name is not allowed\n");
      return SANE_STATUS_INVAL;
    }
  else
    for (dev = first_device; dev; dev = dev->next)
      if (strcmp (dev->name, nd_name) == 0)
	break;

  if (!dev)
    {
      DBG (1,
	   "sane_open: device %s not found, trying to register it anyway\n",
	   nd_name);
      status = add_device (nd_name, &dev);
      if (status != SANE_STATUS_GOOD)
	{
	  DBG (1, "sane_open: could not open device\n");
	  return status;
	}
    }
  else
    DBG (2, "sane_open: device found in list\n");

  if (dev->ctl < 0)
    {
      DBG (2, "sane_open: device not connected yet...\n");
      status = connect_dev (dev);
      if (status != SANE_STATUS_GOOD)
	{
	  DBG (1, "sane_open: could not connect to device\n");
	  return status;
	}
    }

  DBG (3, "sane_open: net_open\n");
  sanei_w_call (&dev->wire, SANE_NET_OPEN,
		(WireCodecFunc) sanei_w_string, &dev_name,
		(WireCodecFunc) sanei_w_open_reply, &reply);
  do
    {
      if (dev->wire.status != 0)
	{
	  DBG (1, "sane_open: open rpc call failed (%s)\n",
	       strerror (dev->wire.status));
	  return SANE_STATUS_IO_ERROR;
	}

      status = reply.status;
      handle = reply.handle;
      need_auth = (reply.resource_to_authorize != 0);

      if (need_auth)
	{
	  DBG (3, "sane_open: authorization required\n");
	  do_authorization (dev, reply.resource_to_authorize);

	  sanei_w_free (&dev->wire, (WireCodecFunc) sanei_w_open_reply,
			&reply);

	  if (dev->wire.direction != WIRE_DECODE)
	    sanei_w_set_dir (&dev->wire, WIRE_DECODE);
	  sanei_w_open_reply (&dev->wire, &reply);

	  continue;
	}
      else
	sanei_w_free (&dev->wire, (WireCodecFunc) sanei_w_open_reply, &reply);

      if (need_auth && !dev->auth_active)
	{
	  DBG (2, "sane_open: open cancelled\n");
	  return SANE_STATUS_CANCELLED;
	}

      if (status != SANE_STATUS_GOOD)
	{
	  DBG (1, "sane_open: remote open failed\n");
	  return reply.status;
	}
    }
  while (need_auth);

  s = malloc (sizeof (*s));
  if (!s)
    {
      DBG (1, "sane_open: not enough free memory\n");
      return SANE_STATUS_NO_MEM;
    }

  memset (s, 0, sizeof (*s));
  s->hw = dev;
  s->handle = handle;
  s->data = -1;
  s->next = first_handle;
  s->local_opt.desc = 0;
  s->local_opt.num_options = 0;

  DBG (3, "sane_open: getting option descriptors\n");
  status = fetch_options (s);
  if (status != SANE_STATUS_GOOD)
    {
      DBG (1, "sane_open: fetch_options failed (%s), closing device again\n",
	   sane_strstatus (status));

      sanei_w_call (&s->hw->wire, SANE_NET_CLOSE,
		    (WireCodecFunc) sanei_w_word, &s->handle,
		    (WireCodecFunc) sanei_w_word, &ack);

      free (s);

      return status;
    }

  first_handle = s;
  *meta_handle = s;

  DBG (3, "sane_open: success\n");
  return SANE_STATUS_GOOD;
}

void
sane_close (SANE_Handle handle)
{
  Net_Scanner *prev, *s;
  SANE_Word ack;
  int option_number;

  DBG (3, "sane_close: handle %p\n", handle);

  prev = 0;
  for (s = first_handle; s; s = s->next)
    {
      if (s == handle)
	break;
      prev = s;
    }
  if (!s)
    {
      DBG (1, "sane_close: invalid handle %p\n", handle);
      return;			/* oops, not a handle we know about */
    }
  if (prev)
    prev->next = s->next;
  else
    first_handle = s->next;

  if (s->opt.num_options)
    {
      DBG (2, "sane_close: removing cached option descriptors\n");
      sanei_w_set_dir (&s->hw->wire, WIRE_FREE);
      s->hw->wire.status = 0;
      sanei_w_option_descriptor_array (&s->hw->wire, &s->opt);
      if (s->hw->wire.status)
	DBG (1, "sane_close: couldn't free sanei_w_option_descriptor_array "
	     "(%s)\n", sane_strstatus (s->hw->wire.status));
    }

  DBG (2, "sane_close: removing local option descriptors\n");
  for (option_number = 0; option_number < s->local_opt.num_options;
       option_number++)
    free (s->local_opt.desc[option_number]);
  if (s->local_opt.desc)
    free (s->local_opt.desc);

  DBG (2, "sane_close: net_close\n");
  sanei_w_call (&s->hw->wire, SANE_NET_CLOSE,
		(WireCodecFunc) sanei_w_word, &s->handle,
		(WireCodecFunc) sanei_w_word, &ack);
  if (s->data >= 0)
    {
      DBG (2, "sane_close: closing data pipe\n");
      close (s->data);
    }
  free (s);
  DBG (2, "sane_close: done\n");
}

const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
  Net_Scanner *s = handle;
  SANE_Status status;

  DBG (3, "sane_get_option_descriptor: option %d\n", option);

  if (!s->options_valid)
    {
      DBG (3, "sane_get_option_descriptor: getting option descriptors\n");
      status = fetch_options (s);
      if (status != SANE_STATUS_GOOD)
	{
	  DBG (1, "sane_get_option_descriptor: fetch_options failed (%s)\n",
	       sane_strstatus (status));
	  return 0;
	}
    }

  if (((SANE_Word) option >= s->opt.num_options) || (option < 0))
    {
      DBG (2, "sane_get_option_descriptor: invalid option number\n");
      return 0;
    }
  return s->local_opt.desc[option];
}

SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
		     SANE_Action action, void *value, SANE_Word * info)
{
  Net_Scanner *s = handle;
  SANE_Control_Option_Req req;
  SANE_Control_Option_Reply reply;
  SANE_Status status;
  size_t value_size;
  int need_auth;
  SANE_Word local_info;

  DBG (3, "sane_control_option: option %d, action %d\n", option, action);

  if (!s->options_valid)
    {
      DBG (1, "sane_control_option: FRONTEND BUG: option descriptors reload needed\n");
      return SANE_STATUS_INVAL;
    }

  if (((SANE_Word) option >= s->opt.num_options) || (option < 0))
    {
      DBG (1, "sane_control_option: invalid option number\n");
      return SANE_STATUS_INVAL;
    }

  switch (s->opt.desc[option]->type)
    {
    case SANE_TYPE_BUTTON:
    case SANE_TYPE_GROUP:	/* shouldn't happen... */
      /* the SANE standard defines that the option size of a BUTTON or
         GROUP is IGNORED.  */
      value_size = 0;
      break;
    case SANE_TYPE_STRING:	/* strings can be smaller than size */
      value_size = s->opt.desc[option]->size;
      if ((action == SANE_ACTION_SET_VALUE)
	  && (((SANE_Int) strlen ((SANE_String) value) + 1)
	      < s->opt.desc[option]->size))
	value_size = strlen ((SANE_String) value) + 1;
      break;
    default:
      value_size = s->opt.desc[option]->size;
      break;
    }

  /* Avoid leaking memory bits */
  if (value && (action != SANE_ACTION_SET_VALUE))
    memset (value, 0, value_size);

  /* for SET_AUTO the parameter ``value'' is ignored */
  if (action == SANE_ACTION_SET_AUTO)
    value_size = 0;

  req.handle = s->handle;
  req.option = option;
  req.action = action;
  req.value_type = s->opt.desc[option]->type;
  req.value_size = value_size;
  req.value = value;

  local_info = 0;

  DBG (3, "sane_control_option: remote control option\n");
  sanei_w_call (&s->hw->wire, SANE_NET_CONTROL_OPTION,
		(WireCodecFunc) sanei_w_control_option_req, &req,
		(WireCodecFunc) sanei_w_control_option_reply, &reply);

  do
    {
      status = reply.status;
      need_auth = (reply.resource_to_authorize != 0);
      if (need_auth)
	{
	  DBG (3, "sane_control_option: auth required\n");
	  do_authorization (s->hw, reply.resource_to_authorize);
	  sanei_w_free (&s->hw->wire,
			(WireCodecFunc) sanei_w_control_option_reply, &reply);

	  sanei_w_set_dir (&s->hw->wire, WIRE_DECODE);

	  sanei_w_control_option_reply (&s->hw->wire, &reply);
	  continue;

	}
      else if (status == SANE_STATUS_GOOD)
	{
	  local_info = reply.info;

	  if (info)
	    *info = reply.info;
	  if (value_size > 0)
	    {
	      if ((SANE_Word) value_size == reply.value_size)
		memcpy (value, reply.value, reply.value_size);
	      else
		DBG (1, "sane_control_option: size changed from %d to %d\n",
		     s->opt.desc[option]->size, reply.value_size);
	    }

	  if (reply.info & SANE_INFO_RELOAD_OPTIONS)
	    s->options_valid = 0;
	}
      sanei_w_free (&s->hw->wire,
		    (WireCodecFunc) sanei_w_control_option_reply, &reply);
      if (need_auth && !s->hw->auth_active)
	return SANE_STATUS_CANCELLED;
    }
  while (need_auth);

  DBG (2, "sane_control_option: remote done (%s, info %x)\n", sane_strstatus (status), local_info);

  if ((status == SANE_STATUS_GOOD) && (info == NULL) && (local_info & SANE_INFO_RELOAD_OPTIONS))
    {
      DBG (2, "sane_control_option: reloading options as frontend does not care\n");

      status = fetch_options (s);

      DBG (2, "sane_control_option: reload done (%s)\n", sane_strstatus (status));
    }

  DBG (2, "sane_control_option: done (%s, info %x)\n", sane_strstatus (status), local_info);

  return status;
}

SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
  Net_Scanner *s = handle;
  SANE_Get_Parameters_Reply reply;
  SANE_Status status;

  DBG (3, "sane_get_parameters\n");

  if (!params)
    {
      DBG (1, "sane_get_parameters: parameter params not supplied\n");
      return SANE_STATUS_INVAL;
    }

  DBG (3, "sane_get_parameters: remote get parameters\n");
  sanei_w_call (&s->hw->wire, SANE_NET_GET_PARAMETERS,
		(WireCodecFunc) sanei_w_word, &s->handle,
		(WireCodecFunc) sanei_w_get_parameters_reply, &reply);

  status = reply.status;
  *params = reply.params;
  depth = reply.params.depth;
  sanei_w_free (&s->hw->wire,
		(WireCodecFunc) sanei_w_get_parameters_reply, &reply);

  DBG (3, "sane_get_parameters: returned status %s\n",
       sane_strstatus (status));
  return status;
}

#ifdef NET_USES_AF_INDEP
SANE_Status
sane_start (SANE_Handle handle)
{
  Net_Scanner *s = handle;
  SANE_Start_Reply reply;
  struct sockaddr_in sin;
  struct sockaddr *sa;
#ifdef ENABLE_IPV6
  struct sockaddr_in6 sin6;
#endif /* ENABLE_IPV6 */
  SANE_Status status;
  int fd, need_auth;
  socklen_t len;
  uint16_t port;			/* Internet-specific */


  DBG (3, "sane_start\n");

  hang_over = -1;
  left_over = -1;

  if (s->data >= 0)
    {
      DBG (2, "sane_start: data pipe already exists\n");
      return SANE_STATUS_INVAL;
    }

  /* Do this ahead of time so in case anything fails, we can
     recover gracefully (without hanging our server).  */

  switch (s->hw->addr_used->ai_family)
    {
      case AF_INET:
	len = sizeof (sin);
	sa = (struct sockaddr *) &sin;
	break;
#ifdef ENABLE_IPV6
      case AF_INET6:
	len = sizeof (sin6);
	sa = (struct sockaddr *) &sin6;
	break;
#endif /* ENABLE_IPV6 */
      default:
	DBG (1, "sane_start: unknown address family : %d\n",
	     s->hw->addr_used->ai_family);
	return SANE_STATUS_INVAL;
    }

  if (getpeername (s->hw->ctl, sa, &len) < 0)
    {
      DBG (1, "sane_start: getpeername() failed (%s)\n", strerror (errno));
      return SANE_STATUS_IO_ERROR;
    }

  fd = socket (s->hw->addr_used->ai_family, SOCK_STREAM, 0);
  if (fd < 0)
    {
      DBG (1, "sane_start: socket() failed (%s)\n", strerror (errno));
      return SANE_STATUS_IO_ERROR;
    }

  DBG (3, "sane_start: remote start\n");
  sanei_w_call (&s->hw->wire, SANE_NET_START,
		(WireCodecFunc) sanei_w_word, &s->handle,
		(WireCodecFunc) sanei_w_start_reply, &reply);
  do
    {
      status = reply.status;
      port = reply.port;
      if (reply.byte_order == 0x1234)
	{
	  server_big_endian = 0;
	  DBG (1, "sane_start: server has little endian byte order\n");
	}
      else
	{
	  server_big_endian = 1;
	  DBG (1, "sane_start: server has big endian byte order\n");
	}

      need_auth = (reply.resource_to_authorize != 0);
      if (need_auth)
	{
	  DBG (3, "sane_start: auth required\n");
	  do_authorization (s->hw, reply.resource_to_authorize);

	  sanei_w_free (&s->hw->wire,
			(WireCodecFunc) sanei_w_start_reply, &reply);

	  sanei_w_set_dir (&s->hw->wire, WIRE_DECODE);

	  sanei_w_start_reply (&s->hw->wire, &reply);

	  continue;
	}
      sanei_w_free (&s->hw->wire, (WireCodecFunc) sanei_w_start_reply,
		    &reply);
      if (need_auth && !s->hw->auth_active)
	return SANE_STATUS_CANCELLED;

      if (status != SANE_STATUS_GOOD)
	{
	  DBG (1, "sane_start: remote start failed (%s)\n",
	       sane_strstatus (status));
	  close (fd);
	  return status;
	}
    }
  while (need_auth);
  DBG (3, "sane_start: remote start finished, data at port %hu\n", port);

  switch (s->hw->addr_used->ai_family)
    {
      case AF_INET:
	sin.sin_port = htons (port);
	break;
#ifdef ENABLE_IPV6
      case AF_INET6:
	sin6.sin6_port = htons (port);
	break;
#endif /* ENABLE_IPV6 */
    }

  if (connect (fd, sa, len) < 0)
    {
      DBG (1, "sane_start: connect() failed (%s)\n", strerror (errno));
      close (fd);
      return SANE_STATUS_IO_ERROR;
    }
  shutdown (fd, 1);
  s->data = fd;
  s->reclen_buf_offset = 0;
  s->bytes_remaining = 0;
  DBG (3, "sane_start: done (%s)\n", sane_strstatus (status));
  return status;
}

#else /* !NET_USES_AF_INDEP */

SANE_Status
sane_start (SANE_Handle handle)
{
  Net_Scanner *s = handle;
  SANE_Start_Reply reply;
  struct sockaddr_in sin;
  SANE_Status status;
  int fd, need_auth;
  socklen_t len;
  uint16_t port;			/* Internet-specific */


  DBG (3, "sane_start\n");

  hang_over = -1;
  left_over = -1;

  if (s->data >= 0)
    {
      DBG (2, "sane_start: data pipe already exists\n");
      return SANE_STATUS_INVAL;
    }

  /* Do this ahead of time so in case anything fails, we can
     recover gracefully (without hanging our server).  */
  len = sizeof (sin);
  if (getpeername (s->hw->ctl, (struct sockaddr *) &sin, &len) < 0)
    {
      DBG (1, "sane_start: getpeername() failed (%s)\n", strerror (errno));
      return SANE_STATUS_IO_ERROR;
    }

  fd = socket (s->hw->addr.sa_family, SOCK_STREAM, 0);
  if (fd < 0)
    {
      DBG (1, "sane_start: socket() failed (%s)\n", strerror (errno));
      return SANE_STATUS_IO_ERROR;
    }

  DBG (3, "sane_start: remote start\n");
  sanei_w_call (&s->hw->wire, SANE_NET_START,
		(WireCodecFunc) sanei_w_word, &s->handle,
		(WireCodecFunc) sanei_w_start_reply, &reply);
  do
    {

      status = reply.status;
      port = reply.port;
      if (reply.byte_order == 0x1234)
	{
	  server_big_endian = 0;
	  DBG (1, "sane_start: server has little endian byte order\n");
	}
      else
	{
	  server_big_endian = 1;
	  DBG (1, "sane_start: server has big endian byte order\n");
	}

      need_auth = (reply.resource_to_authorize != 0);
      if (need_auth)
	{
	  DBG (3, "sane_start: auth required\n");
	  do_authorization (s->hw, reply.resource_to_authorize);

	  sanei_w_free (&s->hw->wire,
			(WireCodecFunc) sanei_w_start_reply, &reply);

	  sanei_w_set_dir (&s->hw->wire, WIRE_DECODE);

	  sanei_w_start_reply (&s->hw->wire, &reply);

	  continue;
	}
      sanei_w_free (&s->hw->wire, (WireCodecFunc) sanei_w_start_reply,
		    &reply);
      if (need_auth && !s->hw->auth_active)
	return SANE_STATUS_CANCELLED;

      if (status != SANE_STATUS_GOOD)
	{
	  DBG (1, "sane_start: remote start failed (%s)\n",
	       sane_strstatus (status));
	  close (fd);
	  return status;
	}
    }
  while (need_auth);
  DBG (3, "sane_start: remote start finished, data at port %hu\n", port);
  sin.sin_port = htons (port);

  if (connect (fd, (struct sockaddr *) &sin, len) < 0)
    {
      DBG (1, "sane_start: connect() failed (%s)\n", strerror (errno));
      close (fd);
      return SANE_STATUS_IO_ERROR;
    }
  shutdown (fd, 1);
  s->data = fd;
  s->reclen_buf_offset = 0;
  s->bytes_remaining = 0;
  DBG (3, "sane_start: done (%s)\n", sane_strstatus (status));
  return status;
}
#endif /* NET_USES_AF_INDEP */


SANE_Status
sane_read (SANE_Handle handle, SANE_Byte * data, SANE_Int max_length,
	   SANE_Int * length)
{
  Net_Scanner *s = handle;
  ssize_t nread;
  SANE_Int cnt;
  SANE_Int start_cnt;
  SANE_Int end_cnt;
  SANE_Byte swap_buf;
  SANE_Byte temp_hang_over;
  int is_even;

  DBG (3, "sane_read: handle=%p, data=%p, max_length=%d, length=%p\n",
       handle, data, max_length, (void *) length);
  if (!length)
    {
      DBG (1, "sane_read: length == NULL\n");
      return SANE_STATUS_INVAL;
    }

  is_even = 1;
  *length = 0;

  /* If there's a left over, i.e. a byte already in the correct byte order,
     return it immediately; otherwise read may fail with a SANE_STATUS_EOF and
     the caller never can read the last byte */
  if ((depth == 16) && (server_big_endian != client_big_endian))
    {
      if (left_over > -1)
	{
	  DBG (3, "sane_read: left_over from previous call, return "
	       "immediately\n");
	  /* return the byte, we've currently scanned; hang_over becomes 
	     left_over */
	  *data = (SANE_Byte) left_over;
	  left_over = -1;
	  *length = 1;
	  return SANE_STATUS_GOOD;
	}
    }

  if (s->data < 0)
    {
      DBG (1, "sane_read: data pipe doesn't exist, scan cancelled?\n");
      return SANE_STATUS_CANCELLED;
    }

  if (s->bytes_remaining == 0)
    {
      /* boy, is this painful or what? */
      
      DBG (4, "sane_read: reading packet length\n");
      nread = read (s->data, s->reclen_buf + s->reclen_buf_offset,
		    4 - s->reclen_buf_offset);
      if (nread < 0)
	{
	  DBG (3, "sane_read: read failed (%s)\n", strerror (errno));
	  if (errno == EAGAIN)
	    {
	      DBG (3, "sane_read: try again later\n");
	      return SANE_STATUS_GOOD;
	    }
	  else
	    {
	      DBG (1, "sane_read: cancelling read\n");
	      do_cancel (s);
	      return SANE_STATUS_IO_ERROR;
	    }
	}
      DBG (4, "sane_read: read %lu bytes, %d from 4 total\n", (u_long) nread,
	   s->reclen_buf_offset);
      s->reclen_buf_offset += nread;
      if (s->reclen_buf_offset < 4)
	{
	  DBG (4, "sane_read: enough for now\n");
	  return SANE_STATUS_GOOD;
	}

      s->reclen_buf_offset = 0;
      s->bytes_remaining = (((u_long) s->reclen_buf[0] << 24)
			    | ((u_long) s->reclen_buf[1] << 16)
			    | ((u_long) s->reclen_buf[2] << 8)
			    | ((u_long) s->reclen_buf[3] << 0));
      DBG (3, "sane_read: next record length=%ld bytes\n",
	   (long) s->bytes_remaining);
      if (s->bytes_remaining == 0xffffffff)
	{
	  char ch;

	  DBG (2, "sane_read: received error signal\n");

	  /* turn off non-blocking I/O (s->data will be closed anyhow): */
	  fcntl (s->data, F_SETFL, 0);

	  /* read the status byte: */
	  if (read (s->data, &ch, sizeof (ch)) != 1)
	    {
	      DBG (1, "sane_read: failed to read error code\n");
	      ch = SANE_STATUS_IO_ERROR;
	    }
	  DBG (1, "sane_read: error code %s\n",
	       sane_strstatus ((SANE_Status) ch));
	  do_cancel (s);
	  return (SANE_Status) ch;
	}
    }

  if (max_length > (SANE_Int) s->bytes_remaining)
    max_length = s->bytes_remaining;

  nread = read (s->data, data, max_length);
  
  if (nread < 0)
    {
      DBG (2, "sane_read: error code %s\n", strerror (errno));
      if (errno == EAGAIN)
	return SANE_STATUS_GOOD;
      else
	{
	  DBG (1, "sane_read: cancelling scan\n");
	  do_cancel (s);
	  return SANE_STATUS_IO_ERROR;
	}
    }

  s->bytes_remaining -= nread;

  *length = nread;
  /* Check whether we are scanning with a depth of 16 bits/pixel and whether
     server and client have different byte order. If this is true, then it's
     neccessary to check whether read returned an odd number. If an odd number
     has been returned, we must save the last byte.
  */
  if ((depth == 16) && (server_big_endian != client_big_endian))
    {
      DBG (1,"sane_read: client/server have different byte order; "
	   "must swap\n");
      /* special case: 1 byte scanned and hang_over */
      if ((nread == 1) && (hang_over > -1))
	{
	  /* return the byte, we've currently scanned; hang_over becomes 
	     left_over */
	  left_over = hang_over;
	  hang_over = -1;
	  return SANE_STATUS_GOOD;
	}
      /* check whether an even or an odd number of bytes has been scanned */
      if ((nread % 2) == 0)
        is_even = 1;
      else
        is_even = 0;
      /* check, whether there's a hang over from a previous call;
	 in this case we memcopy the data up one byte */
      if ((nread > 1) && (hang_over > -1))
	{
	  /* store last byte */
	  temp_hang_over = *(data + nread - 1);
	  memmove (data + 1, data, nread - 1);
	  *data = (SANE_Byte) hang_over;
	  /* what happens with the last byte depends on whether the number
	     of bytes is even or odd */
	  if (is_even == 1)
	    {
	      /* number of bytes is even; no new hang_over, exchange last
		 byte with hang over; last byte becomes left_over */
	      left_over = *(data + nread - 1);
	      *(data + nread - 1) = temp_hang_over;
	      hang_over = -1;
	      start_cnt = 0;
	      /* last byte already swapped */
	      end_cnt = nread - 2;
	    }
	  else
	    {
	      /* number of bytes is odd; last byte becomes new hang_over */
	      hang_over = temp_hang_over;
	      left_over = -1;
	      start_cnt = 0;
	      end_cnt = nread - 1;
	    }
	}
      else if (nread == 1)
	{
	  /* if only one byte has been read, save it as hang_over and return
	     length=0 */
	  hang_over = (int) *data;
	  *length = 0;
	  return SANE_STATUS_GOOD;
	}
      else
	{
	  /* no hang_over; test for even or odd byte number */
	  if(is_even == 1)
	    {
	      start_cnt = 0;
	      end_cnt = *length;
	    }
	  else
	    {
	      start_cnt = 0;
	      hang_over = *(data + *length - 1);
	      *length -= 1;
	      end_cnt = *length;
	    }
	}
      /* swap the bytes */
      for (cnt = start_cnt; cnt < end_cnt - 1; cnt += 2)
	{
	  swap_buf = *(data + cnt);
	  *(data + cnt) = *(data + cnt + 1);
	  *(data + cnt + 1) = swap_buf;
	}
    }
  DBG (3, "sane_read: %lu bytes read, %lu remaining\n", (u_long) nread,
       (u_long) s->bytes_remaining);

  return SANE_STATUS_GOOD;
}

void
sane_cancel (SANE_Handle handle)
{
  Net_Scanner *s = handle;
  SANE_Word ack;

  DBG (3, "sane_cancel: sending net_cancel\n");

  sanei_w_call (&s->hw->wire, SANE_NET_CANCEL,
		(WireCodecFunc) sanei_w_word, &s->handle,
		(WireCodecFunc) sanei_w_word, &ack);
  do_cancel (s);
  DBG (4, "sane_cancel: done\n");
}

SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
  Net_Scanner *s = handle;

  DBG (3, "sane_set_io_mode: non_blocking = %d\n", non_blocking);
  if (s->data < 0)
    {
      DBG (1, "sane_set_io_mode: pipe doesn't exist\n");
      return SANE_STATUS_INVAL;
    }

  if (fcntl (s->data, F_SETFL, non_blocking ? O_NONBLOCK : 0) < 0)
    {
      DBG (1, "sane_set_io_mode: fcntl failed (%s)\n", strerror (errno));
      return SANE_STATUS_IO_ERROR;
    }

  return SANE_STATUS_GOOD;
}

SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
{
  Net_Scanner *s = handle;

  DBG (3, "sane_get_select_fd\n");

  if (s->data < 0)
    {
      DBG (1, "sane_get_select_fd: pipe doesn't exist\n");
      return SANE_STATUS_INVAL;
    }

  *fd = s->data;
  DBG (3, "sane_get_select_fd: done; *fd = %d\n", *fd);
  return SANE_STATUS_GOOD;
}