• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 1997 David Mosberger-Tang
3    Copyright (C) 2003, 2008 Julien BLACHE <jb@jblache.org>
4       AF-independent code + IPv6, Avahi support
5 
6    This file is part of the SANE package.
7 
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 
21    As a special exception, the authors of SANE give permission for
22    additional uses of the libraries contained in this release of SANE.
23 
24    The exception is that, if you link a SANE library with other files
25    to produce an executable, this does not by itself cause the
26    resulting executable to be covered by the GNU General Public
27    License.  Your use of that executable is in no way restricted on
28    account of linking the SANE library code into it.
29 
30    This exception does not, however, invalidate any other reasons why
31    the executable file might be covered by the GNU General Public
32    License.
33 
34    If you submit changes to SANE to the maintainers to be included in
35    a subsequent release, you agree by submitting the changes that
36    those changes may be distributed with this exception intact.
37 
38    If you write modifications of your own for SANE, it is your choice
39    whether to permit this exception to apply to your modifications.
40    If you do not wish that, delete this exception notice.
41 
42    This file implements a SANE network-based meta backend.  */
43 
44 #ifdef _AIX
45 # include "../include/lalloca.h" /* MUST come first for AIX! */
46 #endif
47 
48 #include "../include/sane/config.h"
49 #include "../include/lalloca.h"
50 #include "../include/_stdint.h"
51 
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <limits.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <pwd.h>
59 #ifdef HAVE_LIBC_H
60 # include <libc.h> /* NeXTStep/OpenStep */
61 #endif
62 
63 #include <sys/time.h>
64 #include <sys/types.h>
65 
66 #include <netinet/in.h>
67 #include <netdb.h> /* OS/2 needs this _after_ <netinet/in.h>, grrr... */
68 
69 #if WITH_AVAHI
70 # include <avahi-client/client.h>
71 # include <avahi-client/lookup.h>
72 
73 # include <avahi-common/thread-watch.h>
74 # include <avahi-common/malloc.h>
75 # include <avahi-common/error.h>
76 
77 # define SANED_SERVICE_DNS "_sane-port._tcp"
78 
79 static AvahiClient *avahi_client = NULL;
80 static AvahiThreadedPoll *avahi_thread = NULL;
81 static AvahiServiceBrowser *avahi_browser = NULL;
82 #endif /* WITH_AVAHI */
83 
84 #include "../include/sane/sane.h"
85 #include "../include/sane/sanei.h"
86 #include "../include/sane/sanei_net.h"
87 #include "../include/sane/sanei_codec_bin.h"
88 #include "net.h"
89 
90 #define BACKEND_NAME    net
91 #include "../include/sane/sanei_backend.h"
92 
93 #ifndef PATH_MAX
94 # define PATH_MAX       1024
95 #endif
96 
97 #include "../include/sane/sanei_config.h"
98 #define NET_CONFIG_FILE "net.conf"
99 
100 /* Please increase version number with every change
101    (don't forget to update net.desc) */
102 
103 /* define the version string depending on which network code is used */
104 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GETNAMEINFO)
105 # define NET_USES_AF_INDEP
106 # ifdef ENABLE_IPV6
107 #  define NET_VERSION "1.0.14 (AF-indep+IPv6)"
108 # else
109 #  define NET_VERSION "1.0.14 (AF-indep)"
110 # endif /* ENABLE_IPV6 */
111 #else
112 # undef ENABLE_IPV6
113 # define NET_VERSION "1.0.14"
114 #endif /* HAVE_GETADDRINFO && HAVE_GETNAMEINFO */
115 
116 static SANE_Auth_Callback auth_callback;
117 static Net_Device *first_device;
118 static Net_Scanner *first_handle;
119 static const SANE_Device **devlist;
120 static int client_big_endian; /* 1 == big endian; 0 == little endian */
121 static int server_big_endian; /* 1 == big endian; 0 == little endian */
122 static int depth; /* bits per pixel */
123 static int connect_timeout = -1; /* timeout for connection to saned */
124 
125 #ifndef NET_USES_AF_INDEP
126 static int saned_port;
127 #endif /* !NET_USES_AF_INDEP */
128 
129 /* This variable is only needed, if the depth is 16bit/channel and
130    client/server have different endianness.  A value of -1 means, that there's
131    no hang over; otherwise the value has to be casted to SANE_Byte.  hang_over
132    means, that there is a remaining byte from a previous call to sane_read,
133    which could not be byte-swapped, e.g. because the frontend requested an odd
134    number of bytes.
135 */
136 static int hang_over;
137 
138 /* This variable is only needed, if the depth is 16bit/channel and
139    client/server have different endianness.  A value of -1 means, that there's
140    no left over; otherwise the value has to be casted to SANE_Byte.  left_over
141    means, that there is a remaining byte from a previous call to sane_read,
142    which already is in the correct byte order, but could not be returned,
143    e.g.  because the frontend requested only one byte per call.
144 */
145 static int left_over;
146 
147 
148 #ifdef NET_USES_AF_INDEP
149 static SANE_Status
add_device(const char * name,Net_Device ** ndp)150 add_device (const char *name, Net_Device ** ndp)
151 {
152   struct addrinfo hints;
153   struct addrinfo *res;
154   struct addrinfo *resp;
155   struct sockaddr_in *sin;
156 #ifdef ENABLE_IPV6
157   struct sockaddr_in6 *sin6;
158 #endif /* ENABLE_IPV6 */
159 
160   Net_Device *nd = NULL;
161 
162   int error;
163   short sane_port = htons (6566);
164 
165   DBG (1, "add_device: adding backend %s\n", name);
166 
167   for (nd = first_device; nd; nd = nd->next)
168     if (strcmp (nd->name, name) == 0)
169       {
170 	DBG (1, "add_device: already in list\n");
171 
172 	if (ndp)
173 	  *ndp = nd;
174 
175 	return SANE_STATUS_GOOD;
176       }
177 
178   memset (&hints, 0, sizeof(hints));
179 
180 # ifdef ENABLE_IPV6
181   hints.ai_family = PF_UNSPEC;
182 # else
183   hints.ai_family = PF_INET;
184 # endif /* ENABLE_IPV6 */
185 
186   error = getaddrinfo (name, "sane-port", &hints, &res);
187   if (error)
188     {
189       error = getaddrinfo (name, NULL, &hints, &res);
190       if (error)
191 	{
192 	  DBG (1, "add_device: error while getting address of host %s: %s\n",
193 	       name, gai_strerror (error));
194 
195 	  return SANE_STATUS_IO_ERROR;
196 	}
197       else
198 	{
199           for (resp = res; resp != NULL; resp = resp->ai_next)
200             {
201               switch (resp->ai_family)
202                 {
203                   case AF_INET:
204                     sin = (struct sockaddr_in *) resp->ai_addr;
205                     sin->sin_port = sane_port;
206                     break;
207 #ifdef ENABLE_IPV6
208 		  case AF_INET6:
209 		    sin6 = (struct sockaddr_in6 *) resp->ai_addr;
210 		    sin6->sin6_port = sane_port;
211 		    break;
212 #endif /* ENABLE_IPV6 */
213                 }
214 	    }
215 	}
216     }
217 
218   nd = malloc (sizeof (Net_Device));
219   if (!nd)
220     {
221       DBG (1, "add_device: not enough memory for Net_Device struct\n");
222 
223       freeaddrinfo (res);
224       return SANE_STATUS_NO_MEM;
225     }
226 
227   memset (nd, 0, sizeof (Net_Device));
228   nd->name = strdup (name);
229   if (!nd->name)
230     {
231       DBG (1, "add_device: not enough memory to duplicate name\n");
232       free(nd);
233       return SANE_STATUS_NO_MEM;
234     }
235 
236   nd->addr = res;
237   nd->ctl = -1;
238 
239   nd->next = first_device;
240 
241   first_device = nd;
242 
243   if (ndp)
244     *ndp = nd;
245   DBG (2, "add_device: backend %s added\n", name);
246   return SANE_STATUS_GOOD;
247 }
248 
249 #else /* !NET_USES_AF_INDEP */
250 
251 static SANE_Status
add_device(const char * name,Net_Device ** ndp)252 add_device (const char *name, Net_Device ** ndp)
253 {
254   struct hostent *he;
255   Net_Device *nd;
256   struct sockaddr_in *sin;
257 
258   DBG (1, "add_device: adding backend %s\n", name);
259 
260   for (nd = first_device; nd; nd = nd->next)
261     if (strcmp (nd->name, name) == 0)
262       {
263 	DBG (1, "add_device: already in list\n");
264 
265 	if (ndp)
266 	  *ndp = nd;
267 
268 	return SANE_STATUS_GOOD;
269       }
270 
271   he = gethostbyname (name);
272   if (!he)
273     {
274       DBG (1, "add_device: can't get address of host %s\n", name);
275       return SANE_STATUS_IO_ERROR;
276     }
277 
278   if (he->h_addrtype != AF_INET)
279     {
280       DBG (1, "add_device: don't know how to deal with addr family %d\n",
281 	   he->h_addrtype);
282       return SANE_STATUS_INVAL;
283     }
284 
285   nd = malloc (sizeof (*nd));
286   if (!nd)
287     {
288       DBG (1, "add_device: not enough memory for Net_Device struct\n");
289       return SANE_STATUS_NO_MEM;
290     }
291 
292   memset (nd, 0, sizeof (*nd));
293   nd->name = strdup (name);
294   if (!nd->name)
295     {
296       DBG (1, "add_device: not enough memory to duplicate name\n");
297       free (nd);
298       return SANE_STATUS_NO_MEM;
299     }
300   nd->addr.sa_family = he->h_addrtype;
301 
302   sin = (struct sockaddr_in *) &nd->addr;
303   memcpy (&sin->sin_addr, he->h_addr_list[0], he->h_length);
304 
305   nd->ctl = -1;
306   nd->next = first_device;
307   first_device = nd;
308   if (ndp)
309     *ndp = nd;
310   DBG (2, "add_device: backend %s added\n", name);
311   return SANE_STATUS_GOOD;
312 }
313 #endif /* NET_USES_AF_INDEP */
314 
315 /* Calls getpwuid_r(). The return value must be freed by the caller. */
get_current_username()316 char* get_current_username()
317 {
318   long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
319   if (bufsize == -1)
320   {
321     return NULL;
322   }
323 
324   char* buf = (char*) malloc(bufsize);
325   if (buf == NULL)
326   {
327     return NULL;
328   }
329 
330   struct passwd pwd;
331   struct passwd *result;
332   if (getpwuid_r(getuid(), &pwd, buf, bufsize, &result) != 0 || result == NULL)
333   {
334     return NULL;
335   }
336 
337   /* pw_name is allocated somewhere within buf, so we use memmove() */
338   memmove(buf, pwd.pw_name, strlen(pwd.pw_name));
339   return buf;
340 }
341 
342 #ifdef NET_USES_AF_INDEP
343 static SANE_Status
connect_dev(Net_Device * dev)344 connect_dev (Net_Device * dev)
345 {
346   struct addrinfo *addrp;
347 
348   SANE_Word version_code;
349   SANE_Init_Reply reply;
350   SANE_Status status = SANE_STATUS_IO_ERROR;
351   SANE_Init_Req req;
352   SANE_Bool connected = SANE_FALSE;
353 #ifdef TCP_NODELAY
354   int on = 1;
355   int level = -1;
356 #endif
357   struct timeval tv;
358 
359   int i;
360 
361   DBG (2, "connect_dev: trying to connect to %s\n", dev->name);
362 
363   for (addrp = dev->addr, i = 0; (addrp != NULL) && (connected == SANE_FALSE); addrp = addrp->ai_next, i++)
364     {
365 # ifdef ENABLE_IPV6
366       if ((addrp->ai_family != AF_INET) && (addrp->ai_family != AF_INET6))
367 # else /* !ENABLE_IPV6 */
368       if (addrp->ai_family != AF_INET)
369 # endif /* ENABLE_IPV6 */
370 	{
371 	  DBG (1, "connect_dev: [%d] don't know how to deal with addr family %d\n",
372 	       i, addrp->ai_family);
373 	  continue;
374 	}
375 
376       dev->ctl = socket (addrp->ai_family, SOCK_STREAM, 0);
377       if (dev->ctl < 0)
378 	{
379 	  DBG (1, "connect_dev: [%d] failed to obtain socket (%s)\n",
380 	       i, strerror (errno));
381 	  dev->ctl = -1;
382 	  continue;
383 	}
384 
385       /* Set SO_SNDTIMEO for the connection to saned */
386       if (connect_timeout > 0)
387 	{
388 	  tv.tv_sec = connect_timeout;
389 	  tv.tv_usec = 0;
390 
391 	  if (setsockopt (dev->ctl, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
392 	    {
393 	      DBG (1, "connect_dev: [%d] failed to set SO_SNDTIMEO (%s)\n", i, strerror (errno));
394 	    }
395 	}
396 
397       if (connect (dev->ctl, addrp->ai_addr, addrp->ai_addrlen) < 0)
398 	{
399 	  DBG (1, "connect_dev: [%d] failed to connect (%s)\n", i, strerror (errno));
400 	  dev->ctl = -1;
401 	  continue;
402 	}
403       DBG (3, "connect_dev: [%d] connection succeeded (%s)\n", i, (addrp->ai_family == AF_INET6) ? "IPv6" : "IPv4");
404       dev->addr_used = addrp;
405       connected = SANE_TRUE;
406     }
407 
408   if (connected != SANE_TRUE)
409     {
410       DBG (1, "connect_dev: couldn't connect to host (see messages above)\n");
411       return SANE_STATUS_IO_ERROR;
412     }
413 
414 #else /* !NET_USES_AF_INDEP */
415 
416 static SANE_Status
417 connect_dev (Net_Device * dev)
418 {
419   struct sockaddr_in *sin;
420   SANE_Word version_code;
421   SANE_Init_Reply reply;
422   SANE_Status status = SANE_STATUS_IO_ERROR;
423   SANE_Init_Req req;
424 #ifdef TCP_NODELAY
425   int on = 1;
426   int level = -1;
427 #endif
428   struct timeval tv;
429 
430   DBG (2, "connect_dev: trying to connect to %s\n", dev->name);
431 
432   if (dev->addr.sa_family != AF_INET)
433     {
434       DBG (1, "connect_dev: don't know how to deal with addr family %d\n",
435 	   dev->addr.sa_family);
436       return SANE_STATUS_IO_ERROR;
437     }
438 
439   dev->ctl = socket (dev->addr.sa_family, SOCK_STREAM, 0);
440   if (dev->ctl < 0)
441     {
442       DBG (1, "connect_dev: failed to obtain socket (%s)\n",
443 	   strerror (errno));
444       dev->ctl = -1;
445       return SANE_STATUS_IO_ERROR;
446     }
447   sin = (struct sockaddr_in *) &dev->addr;
448   sin->sin_port = saned_port;
449 
450 
451   /* Set SO_SNDTIMEO for the connection to saned */
452   if (connect_timeout > 0)
453     {
454       tv.tv_sec = connect_timeout;
455       tv.tv_usec = 0;
456 
457       if (setsockopt (dev->ctl, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
458 	{
459 	  DBG (1, "connect_dev: failed to set SO_SNDTIMEO (%s)\n", strerror (errno));
460 	}
461     }
462 
463   if (connect (dev->ctl, &dev->addr, sizeof (dev->addr)) < 0)
464     {
465       DBG (1, "connect_dev: failed to connect (%s)\n", strerror (errno));
466       dev->ctl = -1;
467       return SANE_STATUS_IO_ERROR;
468     }
469   DBG (3, "connect_dev: connection succeeded\n");
470 #endif /* NET_USES_AF_INDEP */
471 
472   /* We're connected now, so reset SO_SNDTIMEO to the default value of 0 */
473   if (connect_timeout > 0)
474     {
475       tv.tv_sec = 0;
476       tv.tv_usec = 0;
477 
478       if (setsockopt (dev->ctl, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
479 	{
480 	  DBG (1, "connect_dev: failed to reset SO_SNDTIMEO (%s)\n", strerror (errno));
481 	}
482     }
483 
484 #ifdef TCP_NODELAY
485 # ifdef SOL_TCP
486   level = SOL_TCP;
487 # else /* !SOL_TCP */
488   /* Look up the protocol level in the protocols database. */
489   {
490     struct protoent *p;
491     p = getprotobyname ("tcp");
492     if (p == 0)
493       DBG (1, "connect_dev: cannot look up `tcp' protocol number");
494     else
495       level = p->p_proto;
496   }
497 # endif	/* SOL_TCP */
498 
499   if (level == -1 ||
500       setsockopt (dev->ctl, level, TCP_NODELAY, &on, sizeof (on)))
501     DBG (1, "connect_dev: failed to put send socket in TCP_NODELAY mode (%s)",
502 	 strerror (errno));
503 #endif /* !TCP_NODELAY */
504 
505   DBG (2, "connect_dev: sanei_w_init\n");
506   sanei_w_init (&dev->wire, sanei_codec_bin_init);
507   dev->wire.io.fd = dev->ctl;
508   dev->wire.io.read = read;
509   dev->wire.io.write = write;
510 
511   /* exchange version codes with the server: */
512   req.version_code = SANE_VERSION_CODE (V_MAJOR, V_MINOR,
513 					SANEI_NET_PROTOCOL_VERSION);
514   req.username = get_current_username();
515   DBG (2, "connect_dev: net_init (user=%s, local version=%d.%d.%d)\n",
516        req.username, V_MAJOR, V_MINOR, SANEI_NET_PROTOCOL_VERSION);
517   sanei_w_call (&dev->wire, SANE_NET_INIT,
518 		(WireCodecFunc) sanei_w_init_req, &req,
519 		(WireCodecFunc) sanei_w_init_reply, &reply);
520   free(req.username);
521   req.username = NULL;
522 
523   if (dev->wire.status != 0)
524     {
525       DBG (1, "connect_dev: argument marshalling error (%s)\n",
526 	   strerror (dev->wire.status));
527       status = SANE_STATUS_IO_ERROR;
528       goto fail;
529     }
530 
531   status = reply.status;
532   version_code = reply.version_code;
533   DBG (2, "connect_dev: freeing init reply (status=%s, remote "
534        "version=%d.%d.%d)\n", sane_strstatus (status),
535        SANE_VERSION_MAJOR (version_code),
536        SANE_VERSION_MINOR (version_code), SANE_VERSION_BUILD (version_code));
537   sanei_w_free (&dev->wire, (WireCodecFunc) sanei_w_init_reply, &reply);
538 
539   if (status != 0)
540     {
541       DBG (1, "connect_dev: access to %s denied\n", dev->name);
542       goto fail;
543     }
544   if (SANE_VERSION_MAJOR (version_code) != V_MAJOR)
545     {
546       DBG (1, "connect_dev: major version mismatch: got %d, expected %d\n",
547 	   SANE_VERSION_MAJOR (version_code), V_MAJOR);
548       status = SANE_STATUS_IO_ERROR;
549       goto fail;
550     }
551   if (SANE_VERSION_BUILD (version_code) != SANEI_NET_PROTOCOL_VERSION
552       && SANE_VERSION_BUILD (version_code) != 2)
553     {
554       DBG (1, "connect_dev: network protocol version mismatch: "
555 	   "got %d, expected %d\n",
556 	   SANE_VERSION_BUILD (version_code), SANEI_NET_PROTOCOL_VERSION);
557       status = SANE_STATUS_IO_ERROR;
558       goto fail;
559     }
560   dev->wire.version = SANE_VERSION_BUILD (version_code);
561   DBG (4, "connect_dev: done\n");
562   return SANE_STATUS_GOOD;
563 
564 fail:
565   DBG (2, "connect_dev: closing connection to %s\n", dev->name);
566   close (dev->ctl);
567   dev->ctl = -1;
568   return status;
569 }
570 
571 
572 static SANE_Status
573 fetch_options (Net_Scanner * s)
574 {
575   int option_number;
576   DBG (3, "fetch_options: %p\n", (void *) s);
577 
578   if (s->opt.num_options)
579     {
580       DBG (2, "fetch_options: %d option descriptors cached... freeing\n",
581 	   s->opt.num_options);
582       sanei_w_set_dir (&s->hw->wire, WIRE_FREE);
583       s->hw->wire.status = 0;
584       sanei_w_option_descriptor_array (&s->hw->wire, &s->opt);
585       if (s->hw->wire.status)
586 	{
587 	  DBG (1, "fetch_options: failed to free old list (%s)\n",
588 	       strerror (s->hw->wire.status));
589 	  return SANE_STATUS_IO_ERROR;
590 	}
591     }
592   DBG (3, "fetch_options: get_option_descriptors\n");
593   sanei_w_call (&s->hw->wire, SANE_NET_GET_OPTION_DESCRIPTORS,
594 		(WireCodecFunc) sanei_w_word, &s->handle,
595 		(WireCodecFunc) sanei_w_option_descriptor_array, &s->opt);
596   if (s->hw->wire.status)
597     {
598       DBG (1, "fetch_options: failed to get option descriptors (%s)\n",
599 	   strerror (s->hw->wire.status));
600       return SANE_STATUS_IO_ERROR;
601     }
602 
603   if (s->local_opt.num_options == 0)
604     {
605       DBG (3, "fetch_options: creating %d local option descriptors\n",
606 	   s->opt.num_options);
607       s->local_opt.desc =
608 	malloc (s->opt.num_options * sizeof (s->local_opt.desc));
609       if (!s->local_opt.desc)
610 	{
611 	  DBG (1, "fetch_options: couldn't malloc s->local_opt.desc\n");
612 	  return SANE_STATUS_NO_MEM;
613 	}
614       for (option_number = 0;
615 	   option_number < s->opt.num_options;
616 	   option_number++)
617 	{
618 	  s->local_opt.desc[option_number] =
619 	    malloc (sizeof (SANE_Option_Descriptor));
620 	  if (!s->local_opt.desc[option_number])
621 	    {
622 	      DBG (1, "fetch_options: couldn't malloc "
623 		   "s->local_opt.desc[%d]\n", option_number);
624 	      return SANE_STATUS_NO_MEM;
625 	    }
626 	}
627       s->local_opt.num_options = s->opt.num_options;
628     }
629   else if (s->local_opt.num_options != s->opt.num_options)
630     {
631       DBG (1, "fetch_options: option number count changed during runtime?\n");
632       return SANE_STATUS_INVAL;
633     }
634 
635   DBG (3, "fetch_options: copying %d option descriptors\n",
636        s->opt.num_options);
637 
638   for (option_number = 0; option_number < s->opt.num_options; option_number++)
639     {
640       memcpy (s->local_opt.desc[option_number], s->opt.desc[option_number],
641 	      sizeof (SANE_Option_Descriptor));
642     }
643 
644   s->options_valid = 1;
645   DBG (3, "fetch_options: %d options fetched\n", s->opt.num_options);
646   return SANE_STATUS_GOOD;
647 }
648 
649 static SANE_Status
650 do_cancel (Net_Scanner * s)
651 {
652   DBG (2, "do_cancel: %p\n", (void *) s);
653   s->hw->auth_active = 0;
654   if (s->data >= 0)
655     {
656       DBG (3, "do_cancel: closing data pipe\n");
657       close (s->data);
658       s->data = -1;
659     }
660   return SANE_STATUS_CANCELLED;
661 }
662 
663 static void
664 do_authorization (Net_Device * dev, SANE_String resource)
665 {
666   SANE_Authorization_Req req;
667   SANE_Char username[SANE_MAX_USERNAME_LEN];
668   SANE_Char password[SANE_MAX_PASSWORD_LEN];
669   char *net_resource;
670 
671   DBG (2, "do_authorization: dev=%p resource=%s\n", (void *) dev, resource);
672 
673   dev->auth_active = 1;
674 
675   memset (&req, 0, sizeof (req));
676   memset (username, 0, sizeof (SANE_Char) * SANE_MAX_USERNAME_LEN);
677   memset (password, 0, sizeof (SANE_Char) * SANE_MAX_PASSWORD_LEN);
678 
679   net_resource = malloc (strlen (resource) + 6 + strlen (dev->name));
680 
681   if (net_resource != NULL)
682     {
683       sprintf (net_resource, "net:%s:%s", dev->name, resource);
684       if (auth_callback)
685 	{
686 	  DBG (2, "do_authorization: invoking auth_callback, resource = %s\n",
687 	       net_resource);
688 	  (*auth_callback) (net_resource, username, password);
689 	}
690       else
691 	DBG (1, "do_authorization: no auth_callback present\n");
692       free (net_resource);
693     }
694   else /* Is this necessary? If we don't have these few bytes we will get
695 	  in trouble later anyway */
696     {
697       DBG (1, "do_authorization: not enough memory for net_resource\n");
698       if (auth_callback)
699 	{
700 	  DBG (2, "do_authorization: invoking auth_callback, resource = %s\n",
701 	       resource);
702 	  (*auth_callback) (resource, username, password);
703 	}
704       else
705 	DBG (1, "do_authorization: no auth_callback present\n");
706     }
707 
708   if (dev->auth_active)
709     {
710       SANE_Word ack;
711 
712       req.resource = resource;
713       req.username = username;
714       req.password = password;
715       DBG (2, "do_authorization: relaying authentication data\n");
716       sanei_w_call (&dev->wire, SANE_NET_AUTHORIZE,
717 		    (WireCodecFunc) sanei_w_authorization_req, &req,
718 		    (WireCodecFunc) sanei_w_word, &ack);
719     }
720   else
721     DBG (1, "do_authorization: auth_active is false... strange\n");
722 }
723 
724 
725 #if WITH_AVAHI
726 static void
727 net_avahi_resolve_callback (AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol,
728 			    AvahiResolverEvent event, const char *name, const char *type,
729 			    const char *domain, const char *host_name, const AvahiAddress *address,
730 			    uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags,
731 			    void *userdata)
732 {
733   char a[AVAHI_ADDRESS_STR_MAX];
734   char *t;
735 
736   /* unused */
737   (void) interface;
738   (void) protocol;
739   (void) userdata;
740 
741   if (!r)
742     return;
743 
744   switch (event)
745     {
746       case AVAHI_RESOLVER_FAILURE:
747 	DBG (1, "net_avahi_resolve_callback: failed to resolve service '%s' of type '%s' in domain '%s': %s\n",
748 	     name, type, domain, avahi_strerror (avahi_client_errno (avahi_service_resolver_get_client (r))));
749 	break;
750 
751       case AVAHI_RESOLVER_FOUND:
752 	DBG (3, "net_avahi_resolve_callback: service '%s' of type '%s' in domain '%s':\n", name, type, domain);
753 
754 	avahi_address_snprint(a, sizeof (a), address);
755 	t = avahi_string_list_to_string (txt);
756 
757 	DBG (3, "\t%s:%u (%s)\n\tTXT=%s\n\tcookie is %u\n\tis_local: %i\n\tour_own: %i\n"
758 	     "\twide_area: %i\n\tmulticast: %i\n\tcached: %i\n",
759 	     host_name, port, a, t, avahi_string_list_get_service_cookie (txt),
760 	     !!(flags & AVAHI_LOOKUP_RESULT_LOCAL), !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
761 	     !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA), !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
762 	     !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
763 
764 	/* TODO: evaluate TXT record */
765 
766 	/* Try first with the name */
767 	if (add_device (host_name, NULL) != SANE_STATUS_GOOD)
768 	  {
769 	    DBG (1, "net_avahi_resolve_callback: couldn't add backend with name %s\n", host_name);
770 
771 	    /* Then try the raw IP address */
772 	    if (add_device (t, NULL) != SANE_STATUS_GOOD)
773 	      DBG (1, "net_avahi_resolve_callback: couldn't add backend with IP address %s either\n", t);
774 	  }
775 
776 	avahi_free (t);
777 	break;
778     }
779 
780   avahi_service_resolver_free(r);
781 }
782 
783 static void
784 net_avahi_browse_callback (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol,
785 			   AvahiBrowserEvent event, const char *name, const char *type,
786 			   const char *domain, AvahiLookupResultFlags flags, void *userdata)
787 {
788   AvahiProtocol proto;
789 
790   /* unused */
791   (void) flags;
792   (void) userdata;
793 
794   if (!b)
795     return;
796 
797   switch (event)
798     {
799       case AVAHI_BROWSER_FAILURE:
800 	DBG (1, "net_avahi_browse_callback: %s\n", avahi_strerror (avahi_client_errno (avahi_service_browser_get_client (b))));
801 	avahi_threaded_poll_quit (avahi_thread);
802 	return;
803 
804       case AVAHI_BROWSER_NEW:
805 	DBG (3, "net_avahi_browse_callback: NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
806 
807 	/* The server will actually be added to our list in the resolver callback */
808 
809 	/* The resolver object will be freed in the resolver callback, or by
810 	 * the server if it terminates before the callback is called.
811 	 */
812 #ifdef ENABLE_IPV6
813 	proto = AVAHI_PROTO_UNSPEC;
814 #else
815 	proto = AVAHI_PROTO_INET;
816 #endif /* ENABLE_IPV6 */
817 	if (!(avahi_service_resolver_new (avahi_client, interface, protocol, name, type, domain, proto, 0, net_avahi_resolve_callback, NULL)))
818 	  DBG (2, "net_avahi_browse_callback: failed to resolve service '%s': %s\n", name, avahi_strerror (avahi_client_errno (avahi_client)));
819 	break;
820 
821       case AVAHI_BROWSER_REMOVE:
822 	DBG (3, "net_avahi_browse_callback: REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
823 	/* With the current architecture, we cannot safely remove a server from the list */
824 	break;
825 
826       case AVAHI_BROWSER_ALL_FOR_NOW:
827       case AVAHI_BROWSER_CACHE_EXHAUSTED:
828 	DBG (3, "net_avahi_browse_callback: %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
829 	break;
830     }
831 }
832 
833 static void
834 net_avahi_callback (AvahiClient *c, AvahiClientState state, void * userdata)
835 {
836   AvahiProtocol proto;
837   int error;
838 
839   /* unused */
840   (void) userdata;
841 
842   if (!c)
843     return;
844 
845   switch (state)
846     {
847       case AVAHI_CLIENT_CONNECTING:
848 	break;
849 
850       case AVAHI_CLIENT_S_COLLISION:
851       case AVAHI_CLIENT_S_REGISTERING:
852       case AVAHI_CLIENT_S_RUNNING:
853 	if (avahi_browser)
854 	  return;
855 
856 #ifdef ENABLE_IPV6
857 	proto = AVAHI_PROTO_UNSPEC;
858 #else
859 	proto = AVAHI_PROTO_INET;
860 #endif /* ENABLE_IPV6 */
861 
862 	avahi_browser = avahi_service_browser_new (c, AVAHI_IF_UNSPEC, proto, SANED_SERVICE_DNS, NULL, 0, net_avahi_browse_callback, NULL);
863 	if (avahi_browser == NULL)
864 	  {
865 	    DBG (1, "net_avahi_callback: could not create service browser: %s\n", avahi_strerror (avahi_client_errno (c)));
866 	    avahi_threaded_poll_quit (avahi_thread);
867 	  }
868 	break;
869 
870       case AVAHI_CLIENT_FAILURE:
871 	error = avahi_client_errno (c);
872 
873 	if (error == AVAHI_ERR_DISCONNECTED)
874 	  {
875 	    /* Server disappeared - try to reconnect */
876 	    if (avahi_browser)
877 	      {
878 		avahi_service_browser_free (avahi_browser);
879 		avahi_browser = NULL;
880 	      }
881 
882 	    avahi_client_free (avahi_client);
883 	    avahi_client = NULL;
884 
885 	    avahi_client = avahi_client_new (avahi_threaded_poll_get (avahi_thread), AVAHI_CLIENT_NO_FAIL, net_avahi_callback, NULL, &error);
886 	    if (avahi_client == NULL)
887 	      {
888 		DBG (1, "net_avahi_init: could not create Avahi client: %s\n", avahi_strerror (error));
889 		avahi_threaded_poll_quit (avahi_thread);
890 	      }
891 	  }
892 	else
893 	  {
894 	    /* Another error happened - game over */
895 	    DBG (1, "net_avahi_callback: server connection failure: %s\n", avahi_strerror (error));
896 	    avahi_threaded_poll_quit (avahi_thread);
897 	  }
898 	break;
899     }
900 }
901 
902 
903 static void
904 net_avahi_init (void)
905 {
906   int error;
907 
908   avahi_thread = avahi_threaded_poll_new ();
909   if (avahi_thread == NULL)
910     {
911       DBG (1, "net_avahi_init: could not create threaded poll object\n");
912       goto fail;
913     }
914 
915   avahi_client = avahi_client_new (avahi_threaded_poll_get (avahi_thread), AVAHI_CLIENT_NO_FAIL, net_avahi_callback, NULL, &error);
916   if (avahi_client == NULL)
917     {
918       DBG (1, "net_avahi_init: could not create Avahi client: %s\n", avahi_strerror (error));
919       goto fail;
920     }
921 
922   if (avahi_threaded_poll_start (avahi_thread) < 0)
923     {
924       DBG (1, "net_avahi_init: Avahi thread failed to start\n");
925       goto fail;
926     }
927 
928   /* All done */
929   return;
930 
931  fail:
932   DBG (1, "net_avahi_init: Avahi init failed, support disabled\n");
933 
934   if (avahi_client)
935     {
936       avahi_client_free (avahi_client);
937       avahi_client = NULL;
938     }
939 
940   if (avahi_thread)
941     {
942       avahi_threaded_poll_free (avahi_thread);
943       avahi_thread = NULL;
944     }
945 }
946 
947 static void
948 net_avahi_cleanup (void)
949 {
950   if (!avahi_thread)
951     return;
952 
953   DBG (1, "net_avahi_cleanup: stopping thread\n");
954 
955   avahi_threaded_poll_stop (avahi_thread);
956 
957   if (avahi_browser)
958     avahi_service_browser_free (avahi_browser);
959 
960   if (avahi_client)
961     avahi_client_free (avahi_client);
962 
963   avahi_threaded_poll_free (avahi_thread);
964 
965   DBG (1, "net_avahi_cleanup: done\n");
966 }
967 #endif /* WITH_AVAHI */
968 
969 
970 SANE_Status
971 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
972 {
973   char device_name[PATH_MAX];
974   const char *optval;
975   const char *env;
976   size_t len;
977   FILE *fp;
978   short ns = 0x1234;
979   unsigned char *p = (unsigned char *)(&ns);
980 
981 #ifndef NET_USES_AF_INDEP
982   struct servent *serv;
983 #endif /* !NET_USES_AF_INDEP */
984 
985   DBG_INIT ();
986 
987   DBG (2, "sane_init: authorize %s null, version_code %s null\n", (authorize) ? "!=" : "==",
988        (version_code) ? "!=" : "==");
989 
990   devlist = NULL;
991   first_device = NULL;
992   first_handle = NULL;
993 
994 #if WITH_AVAHI
995   net_avahi_init ();
996 #endif /* WITH_AVAHI */
997 
998   auth_callback = authorize;
999 
1000   /* Return the version number of the sane-backends package to allow
1001      the frontend to print them. This is done only for net and dll,
1002      because these backends are usually called by the frontend. */
1003   if (version_code)
1004     *version_code = SANE_VERSION_CODE (SANE_DLL_V_MAJOR, SANE_DLL_V_MINOR,
1005 				       SANE_DLL_V_BUILD);
1006 
1007   DBG (1, "sane_init: SANE net backend version %s from %s\n", NET_VERSION,
1008        PACKAGE_STRING);
1009 
1010   /* determine (client) machine byte order */
1011   if (*p == 0x12)
1012     {
1013       client_big_endian = 1;
1014       DBG (3, "sane_init: Client has big endian byte order\n");
1015     }
1016   else
1017     {
1018       client_big_endian = 0;
1019       DBG (3, "sane_init: Client has little endian byte order\n");
1020     }
1021 
1022 #ifndef NET_USES_AF_INDEP
1023   DBG (2, "sane_init: determining sane service port\n");
1024   serv = getservbyname ("sane-port", "tcp");
1025 
1026   if (serv)
1027     {
1028       DBG (2, "sane_init: found port %d\n", ntohs (serv->s_port));
1029       saned_port = serv->s_port;
1030     }
1031   else
1032     {
1033       saned_port = htons (6566);
1034       DBG (1, "sane_init: could not find `sane-port' service (%s); using default "
1035 	   "port %d\n", strerror (errno), ntohs (saned_port));
1036     }
1037 #endif /* !NET_USES_AF_INDEP */
1038 
1039   DBG (2, "sane_init: searching for config file\n");
1040   fp = sanei_config_open (NET_CONFIG_FILE);
1041   if (fp)
1042     {
1043       while (sanei_config_read (device_name, sizeof (device_name), fp))
1044 	{
1045 	  if (device_name[0] == '#')	/* ignore line comments */
1046 	    continue;
1047 	  len = strlen (device_name);
1048 
1049 	  if (!len)
1050 	    continue;		/* ignore empty lines */
1051 
1052 	  /*
1053 	   * Check for net backend options.
1054 	   * Anything that isn't an option is a saned host.
1055 	   */
1056 	  if (strstr(device_name, "connect_timeout") != NULL)
1057 	    {
1058 	      /* Look for the = sign; if it's not there, error out */
1059 	      optval = strchr(device_name, '=');
1060 
1061 	      if (!optval)
1062 		continue;
1063 
1064 	      optval = sanei_config_skip_whitespace (++optval);
1065 	      if ((optval != NULL) && (*optval != '\0'))
1066 		{
1067 		  connect_timeout = atoi(optval);
1068 
1069 		  DBG (2, "sane_init: connect timeout set to %d seconds\n", connect_timeout);
1070 		}
1071 
1072 	      continue;
1073 	    }
1074 #if WITH_AVAHI
1075 	  avahi_threaded_poll_lock (avahi_thread);
1076 #endif /* WITH_AVAHI */
1077 	  DBG (2, "sane_init: trying to add %s\n", device_name);
1078 	  add_device (device_name, 0);
1079 #if WITH_AVAHI
1080 	  avahi_threaded_poll_unlock (avahi_thread);
1081 #endif /* WITH_AVAHI */
1082 	}
1083 
1084       fclose (fp);
1085       DBG (2, "sane_init: done reading config\n");
1086     }
1087   else
1088     DBG (1, "sane_init: could not open config file (%s): %s\n",
1089 	 NET_CONFIG_FILE, strerror (errno));
1090 
1091   DBG (2, "sane_init: evaluating environment variable SANE_NET_HOSTS\n");
1092   env = getenv ("SANE_NET_HOSTS");
1093   if (env)
1094     {
1095       char *copy, *next, *host;
1096       if ((copy = strdup (env)) != NULL)
1097 	{
1098 	  next = copy;
1099 	  while ((host = strsep (&next, ":")))
1100 	    {
1101 #ifdef ENABLE_IPV6
1102 	      if (host[0] == '[')
1103 		{
1104 		  /* skip '[' (host[0]) */
1105 		  host++;
1106 		  /* get the rest of the IPv6 addr (we're screwed if ] is missing)
1107 		   * Is it worth checking for the matching ] ? Not for now. */
1108 		  strsep (&next, "]");
1109 		  /* add back the ":" that got removed by the strsep() */
1110 		  host[strlen (host)] = ':';
1111 		  /* host now holds the IPv6 address */
1112 
1113 		  /* skip the ':' that could be after ] (avoids a call to strsep() */
1114 		  if (next[0] == ':')
1115 		    next++;
1116 		}
1117 
1118 	      /*
1119 	       * if the IPv6 is last in the list, the strsep() call in the while()
1120 	       * will return a string with the first char being '\0'. Skip it.
1121 	       */
1122 	      if (host[0] == '\0')
1123 		  continue;
1124 #endif /* ENABLE_IPV6 */
1125 #if WITH_AVAHI
1126 	      avahi_threaded_poll_lock (avahi_thread);
1127 #endif /* WITH_AVAHI */
1128 	      DBG (2, "sane_init: trying to add %s\n", host);
1129 	      add_device (host, 0);
1130 #if WITH_AVAHI
1131 	      avahi_threaded_poll_unlock (avahi_thread);
1132 #endif /* WITH_AVAHI */
1133 	    }
1134 	  free (copy);
1135 	}
1136       else
1137 	DBG (1, "sane_init: not enough memory to duplicate "
1138 	     "environment variable\n");
1139     }
1140 
1141   DBG (2, "sane_init: evaluating environment variable SANE_NET_TIMEOUT\n");
1142   env = getenv ("SANE_NET_TIMEOUT");
1143   if (env)
1144     {
1145       connect_timeout = atoi(env);
1146       DBG (2, "sane_init: connect timeout set to %d seconds from env\n", connect_timeout);
1147     }
1148 
1149   DBG (2, "sane_init: done\n");
1150   return SANE_STATUS_GOOD;
1151 }
1152 
1153 void
1154 sane_exit (void)
1155 {
1156   Net_Scanner *handle, *next_handle;
1157   Net_Device *dev, *next_device;
1158   int i;
1159 
1160   DBG (1, "sane_exit: exiting\n");
1161 
1162 #if WITH_AVAHI
1163   net_avahi_cleanup ();
1164 #endif /* WITH_AVAHI */
1165 
1166   /* first, close all handles: */
1167   for (handle = first_handle; handle; handle = next_handle)
1168     {
1169       next_handle = handle->next;
1170       sane_close (handle);
1171     }
1172   first_handle = 0;
1173 
1174   /* now close all devices: */
1175   for (dev = first_device; dev; dev = next_device)
1176     {
1177       next_device = dev->next;
1178 
1179       DBG (2, "sane_exit: closing dev %p, ctl=%d\n", (void *) dev, dev->ctl);
1180 
1181       if (dev->ctl >= 0)
1182 	{
1183 	  sanei_w_call (&dev->wire, SANE_NET_EXIT,
1184 			(WireCodecFunc) sanei_w_void, 0,
1185 			(WireCodecFunc) sanei_w_void, 0);
1186 	  sanei_w_exit (&dev->wire);
1187 	  close (dev->ctl);
1188 	}
1189       if (dev->name)
1190 	free ((void *) dev->name);
1191 
1192 #ifdef NET_USES_AF_INDEP
1193       if (dev->addr)
1194 	freeaddrinfo(dev->addr);
1195 #endif /* NET_USES_AF_INDEP */
1196 
1197       free (dev);
1198     }
1199   if (devlist)
1200     {
1201       for (i = 0; devlist[i]; ++i)
1202 	{
1203 	  if (devlist[i]->vendor)
1204 	    free ((void *) devlist[i]->vendor);
1205 	  if (devlist[i]->model)
1206 	    free ((void *) devlist[i]->model);
1207 	  if (devlist[i]->type)
1208 	    free ((void *) devlist[i]->type);
1209 	  free ((void *) devlist[i]);
1210 	}
1211       free (devlist);
1212     }
1213   DBG (3, "sane_exit: finished.\n");
1214 }
1215 
1216 /* Note that a call to get_devices() implies that we'll have to
1217    connect to all remote hosts.  To avoid this, you can call
1218    sane_open() directly (assuming you know the name of the
1219    backend/device).  This is appropriate for the command-line
1220    interface of SANE, for example.
1221  */
1222 SANE_Status
1223 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
1224 {
1225   static int devlist_size = 0, devlist_len = 0;
1226   static const SANE_Device *empty_devlist[1] = { 0 };
1227   SANE_Get_Devices_Reply reply;
1228   SANE_Status status;
1229   Net_Device *dev;
1230   char *full_name;
1231   int i, num_devs;
1232   size_t len;
1233 #define ASSERT_SPACE(n) do                                                 \
1234   {                                                                        \
1235     if (devlist_len + (n) > devlist_size)                                  \
1236       {                                                                    \
1237         devlist_size += (n) + 15;                                          \
1238         if (devlist)                                                       \
1239           devlist = realloc (devlist, devlist_size * sizeof (devlist[0])); \
1240         else                                                               \
1241           devlist = malloc (devlist_size * sizeof (devlist[0]));           \
1242         if (!devlist)                                                      \
1243           {                                                                \
1244              DBG (1, "sane_get_devices: not enough memory\n");	           \
1245              return SANE_STATUS_NO_MEM;                                    \
1246           }                                                                \
1247       }                                                                    \
1248   } while (0)
1249 
1250   DBG (3, "sane_get_devices: local_only = %d\n", local_only);
1251 
1252   if (local_only)
1253     {
1254       *device_list = empty_devlist;
1255       return SANE_STATUS_GOOD;
1256     }
1257 
1258   if (devlist)
1259     {
1260       DBG (2, "sane_get_devices: freeing devlist\n");
1261       for (i = 0; devlist[i]; ++i)
1262 	{
1263 	  if (devlist[i]->vendor)
1264 	    free ((void *) devlist[i]->vendor);
1265 	  if (devlist[i]->model)
1266 	    free ((void *) devlist[i]->model);
1267 	  if (devlist[i]->type)
1268 	    free ((void *) devlist[i]->type);
1269 	  free ((void *) devlist[i]);
1270 	}
1271       free (devlist);
1272       devlist = 0;
1273     }
1274   devlist_len = 0;
1275   devlist_size = 0;
1276 
1277   for (dev = first_device; dev; dev = dev->next)
1278     {
1279       if (dev->ctl < 0)
1280 	{
1281 	  status = connect_dev (dev);
1282 	  if (status != SANE_STATUS_GOOD)
1283 	    {
1284 	      DBG (1, "sane_get_devices: ignoring failure to connect to %s\n",
1285 		   dev->name);
1286 	      continue;
1287 	    }
1288 	}
1289       sanei_w_call (&dev->wire, SANE_NET_GET_DEVICES,
1290 		    (WireCodecFunc) sanei_w_void, 0,
1291 		    (WireCodecFunc) sanei_w_get_devices_reply, &reply);
1292       if (reply.status != SANE_STATUS_GOOD)
1293 	{
1294 	  DBG (1, "sane_get_devices: ignoring rpc-returned status %s\n",
1295 	       sane_strstatus (reply.status));
1296 	  sanei_w_free (&dev->wire,
1297 			(WireCodecFunc) sanei_w_get_devices_reply, &reply);
1298 	  continue;
1299 	}
1300 
1301       /* count the number of devices for this backend: */
1302       for (num_devs = 0; reply.device_list[num_devs]; ++num_devs);
1303 
1304       ASSERT_SPACE (num_devs);
1305 
1306       for (i = 0; i < num_devs; ++i)
1307 	{
1308 	  SANE_Device *rdev;
1309 	  char *mem;
1310 #ifdef ENABLE_IPV6
1311 	  SANE_Bool IPv6 = SANE_FALSE;
1312 #endif /* ENABLE_IPV6 */
1313 
1314 	  /* create a new device entry with a device name that is the
1315 	     sum of the backend name a colon and the backend's device
1316 	     name: */
1317 	  len = strlen (dev->name) + 1 + strlen (reply.device_list[i]->name);
1318 
1319 #ifdef ENABLE_IPV6
1320 	  if (strchr (dev->name, ':') != NULL)
1321 	    {
1322 	      len += 2;
1323 	      IPv6 = SANE_TRUE;
1324 	    }
1325 #endif /* ENABLE_IPV6 */
1326 
1327 	  mem = malloc (sizeof (*dev) + len + 1);
1328 	  if (!mem)
1329 	    {
1330 	      DBG (1, "sane_get_devices: not enough free memory\n");
1331 	      sanei_w_free (&dev->wire,
1332 			    (WireCodecFunc) sanei_w_get_devices_reply,
1333 			    &reply);
1334 	      return SANE_STATUS_NO_MEM;
1335 	    }
1336 
1337 	  memset (mem, 0, sizeof (*dev) + len);
1338 	  full_name = mem + sizeof (*dev);
1339 
1340 #ifdef ENABLE_IPV6
1341 	  if (IPv6 == SANE_TRUE)
1342 	    strcat (full_name, "[");
1343 #endif /* ENABLE_IPV6 */
1344 
1345 	  strcat (full_name, dev->name);
1346 
1347 #ifdef ENABLE_IPV6
1348 	  if (IPv6 == SANE_TRUE)
1349 	    strcat (full_name, "]");
1350 #endif /* ENABLE_IPV6 */
1351 
1352 	  strcat (full_name, ":");
1353 	  strcat (full_name, reply.device_list[i]->name);
1354 	  DBG (3, "sane_get_devices: got %s\n", full_name);
1355 
1356 	  rdev = (SANE_Device *) mem;
1357 	  rdev->name = full_name;
1358 	  rdev->vendor = strdup (reply.device_list[i]->vendor);
1359 	  rdev->model = strdup (reply.device_list[i]->model);
1360 	  rdev->type = strdup (reply.device_list[i]->type);
1361 
1362 	  if ((!rdev->vendor) || (!rdev->model) || (!rdev->type))
1363 	    {
1364 	      DBG (1, "sane_get_devices: not enough free memory\n");
1365 	      if (rdev->vendor)
1366 		free ((void *) rdev->vendor);
1367 	      if (rdev->model)
1368 		free ((void *) rdev->model);
1369 	      if (rdev->type)
1370 		free ((void *) rdev->type);
1371 	      free (rdev);
1372 	      sanei_w_free (&dev->wire,
1373 			    (WireCodecFunc) sanei_w_get_devices_reply,
1374 			    &reply);
1375 	      return SANE_STATUS_NO_MEM;
1376 	    }
1377 
1378 	  devlist[devlist_len++] = rdev;
1379 	}
1380       /* now free up the rpc return value: */
1381       sanei_w_free (&dev->wire,
1382 		    (WireCodecFunc) sanei_w_get_devices_reply, &reply);
1383     }
1384 
1385   /* terminate device list with NULL entry: */
1386   ASSERT_SPACE (1);
1387   devlist[devlist_len++] = 0;
1388 
1389   *device_list = devlist;
1390   DBG (2, "sane_get_devices: finished (%d devices)\n", devlist_len - 1);
1391   return SANE_STATUS_GOOD;
1392 }
1393 
1394 SANE_Status
1395 sane_open (SANE_String_Const full_name, SANE_Handle * meta_handle)
1396 {
1397   SANE_Open_Reply reply;
1398   const char *dev_name;
1399 #ifdef ENABLE_IPV6
1400   const char *tmp_name;
1401   SANE_Bool v6addr = SANE_FALSE;
1402 #endif /* ENABLE_IPV6 */
1403   SANE_String nd_name;
1404   SANE_Status status;
1405   SANE_Word handle;
1406   SANE_Word ack;
1407   Net_Device *dev;
1408   Net_Scanner *s;
1409   int need_auth;
1410 
1411   DBG (3, "sane_open(\"%s\")\n", full_name);
1412 
1413 #ifdef ENABLE_IPV6
1414   /*
1415    * Check whether a numerical IPv6 host was specified
1416    * [2001:42:42::12] <== check for '[' as full_name[0]
1417    * ex: [2001:42:42::12]:test:0 (syntax taken from Apache 2)
1418    */
1419   if (full_name[0] == '[')
1420     {
1421       v6addr = SANE_TRUE;
1422       tmp_name = strchr (full_name, ']');
1423       if (!tmp_name)
1424 	{
1425 	  DBG (1, "sane_open: incorrect host address: missing matching ']'\n");
1426 	  return SANE_STATUS_INVAL;
1427 	}
1428     }
1429   else
1430     tmp_name = full_name;
1431 
1432   dev_name = strchr (tmp_name, ':');
1433 #else /* !ENABLE_IPV6 */
1434 
1435   dev_name = strchr (full_name, ':');
1436 #endif /* ENABLE_IPV6 */
1437 
1438   if (dev_name)
1439     {
1440 #ifdef strndupa
1441 # ifdef ENABLE_IPV6
1442       if (v6addr == SANE_TRUE)
1443 	nd_name = strndupa (full_name + 1, dev_name - full_name - 2);
1444       else
1445 	nd_name = strndupa (full_name, dev_name - full_name);
1446 
1447 # else /* !ENABLE_IPV6 */
1448 
1449       nd_name = strndupa (full_name, dev_name - full_name);
1450 # endif /* ENABLE_IPV6 */
1451 
1452       if (!nd_name)
1453 	{
1454 	  DBG (1, "sane_open: not enough free memory\n");
1455 	  return SANE_STATUS_NO_MEM;
1456 	}
1457 #else
1458       char *tmp;
1459 
1460 # ifdef ENABLE_IPV6
1461       if (v6addr == SANE_TRUE)
1462 	tmp = alloca (dev_name - full_name - 2 + 1);
1463       else
1464 	tmp = alloca (dev_name - full_name + 1);
1465 
1466 # else /* !ENABLE_IPV6 */
1467 
1468       tmp = alloca (dev_name - full_name + 1);
1469 # endif /* ENABLE_IPV6 */
1470 
1471       if (!tmp)
1472 	{
1473 	  DBG (1, "sane_open: not enough free memory\n");
1474 	  return SANE_STATUS_NO_MEM;
1475 	}
1476 
1477 # ifdef ENABLE_IPV6
1478       if (v6addr == SANE_TRUE)
1479 	{
1480 	  memcpy (tmp, full_name + 1, dev_name - full_name - 2);
1481 	  tmp[dev_name - full_name - 2] = '\0';
1482 	}
1483       else
1484 	{
1485 	  memcpy (tmp, full_name, dev_name - full_name);
1486 	  tmp[dev_name - full_name] = '\0';
1487 	}
1488 
1489 # else /* !ENABLE_IPV6 */
1490 
1491       memcpy (tmp, full_name, dev_name - full_name);
1492       tmp[dev_name - full_name] = '\0';
1493 # endif /* ENABLE_IPV6 */
1494 
1495       nd_name = tmp;
1496 #endif
1497       ++dev_name;		/* skip colon */
1498     }
1499   else
1500     {
1501       /* if no colon interpret full_name as the host name; an empty
1502          device name will cause us to open the first device of that
1503          host.  */
1504 #ifdef ENABLE_IPV6
1505       if (v6addr == SANE_TRUE)
1506 	{
1507 	  nd_name = alloca (strlen (full_name) - 2 + 1);
1508 	  if (!nd_name)
1509 	    {
1510 	      DBG (1, "sane_open: not enough free memory\n");
1511 	      return SANE_STATUS_NO_MEM;
1512 	    }
1513 	  memcpy (nd_name, full_name + 1, strlen (full_name) - 2);
1514 	  nd_name[strlen (full_name) - 2] = '\0';
1515 	}
1516       else
1517 	nd_name = (char *) full_name;
1518 
1519 #else /* !ENABLE_IPV6 */
1520 
1521       nd_name = (char *) full_name;
1522 #endif /* ENABLE_IPV6 */
1523 
1524       dev_name = "";
1525     }
1526   DBG (2, "sane_open: host = %s, device = %s\n", nd_name, dev_name);
1527 
1528   if (!nd_name[0])
1529     {
1530       /* Unlike other backends, we never allow an empty backend-name.
1531          Otherwise, it's possible that sane_open("") will result in
1532          endless looping (consider the case where NET is the first
1533          backend...) */
1534 
1535       DBG (1, "sane_open: empty backend name is not allowed\n");
1536       return SANE_STATUS_INVAL;
1537     }
1538   else
1539     for (dev = first_device; dev; dev = dev->next)
1540       if (strcmp (dev->name, nd_name) == 0)
1541 	break;
1542 
1543   if (!dev)
1544     {
1545       DBG (1,
1546 	   "sane_open: device %s not found, trying to register it anyway\n",
1547 	   nd_name);
1548 #if WITH_AVAHI
1549       avahi_threaded_poll_lock (avahi_thread);
1550 #endif /* WITH_AVAHI */
1551       status = add_device (nd_name, &dev);
1552 #if WITH_AVAHI
1553       avahi_threaded_poll_unlock (avahi_thread);
1554 #endif /* WITH_AVAHI */
1555       if (status != SANE_STATUS_GOOD)
1556 	{
1557 	  DBG (1, "sane_open: could not open device\n");
1558 	  return status;
1559 	}
1560     }
1561   else
1562     DBG (2, "sane_open: device found in list\n");
1563 
1564   if (dev->ctl < 0)
1565     {
1566       DBG (2, "sane_open: device not connected yet...\n");
1567       status = connect_dev (dev);
1568       if (status != SANE_STATUS_GOOD)
1569 	{
1570 	  DBG (1, "sane_open: could not connect to device\n");
1571 	  return status;
1572 	}
1573     }
1574 
1575   DBG (3, "sane_open: net_open\n");
1576   sanei_w_call (&dev->wire, SANE_NET_OPEN,
1577 		(WireCodecFunc) sanei_w_string, &dev_name,
1578 		(WireCodecFunc) sanei_w_open_reply, &reply);
1579   do
1580     {
1581       if (dev->wire.status != 0)
1582 	{
1583 	  DBG (1, "sane_open: open rpc call failed (%s)\n",
1584 	       strerror (dev->wire.status));
1585 	  return SANE_STATUS_IO_ERROR;
1586 	}
1587 
1588       status = reply.status;
1589       handle = reply.handle;
1590       need_auth = (reply.resource_to_authorize != 0);
1591 
1592       if (need_auth)
1593 	{
1594 	  DBG (3, "sane_open: authorization required\n");
1595 	  do_authorization (dev, reply.resource_to_authorize);
1596 
1597 	  sanei_w_free (&dev->wire, (WireCodecFunc) sanei_w_open_reply,
1598 			&reply);
1599 
1600 	  if (dev->wire.direction != WIRE_DECODE)
1601 	    sanei_w_set_dir (&dev->wire, WIRE_DECODE);
1602 	  sanei_w_open_reply (&dev->wire, &reply);
1603 
1604 	  continue;
1605 	}
1606       else
1607 	sanei_w_free (&dev->wire, (WireCodecFunc) sanei_w_open_reply, &reply);
1608 
1609       if (need_auth && !dev->auth_active)
1610 	{
1611 	  DBG (2, "sane_open: open cancelled\n");
1612 	  return SANE_STATUS_CANCELLED;
1613 	}
1614 
1615       if (status != SANE_STATUS_GOOD)
1616 	{
1617 	  DBG (1, "sane_open: remote open failed\n");
1618 	  return reply.status;
1619 	}
1620     }
1621   while (need_auth);
1622 
1623   s = malloc (sizeof (*s));
1624   if (!s)
1625     {
1626       DBG (1, "sane_open: not enough free memory\n");
1627       return SANE_STATUS_NO_MEM;
1628     }
1629 
1630   memset (s, 0, sizeof (*s));
1631   s->hw = dev;
1632   s->handle = handle;
1633   s->data = -1;
1634   s->next = first_handle;
1635   s->local_opt.desc = 0;
1636   s->local_opt.num_options = 0;
1637 
1638   DBG (3, "sane_open: getting option descriptors\n");
1639   status = fetch_options (s);
1640   if (status != SANE_STATUS_GOOD)
1641     {
1642       DBG (1, "sane_open: fetch_options failed (%s), closing device again\n",
1643 	   sane_strstatus (status));
1644 
1645       sanei_w_call (&s->hw->wire, SANE_NET_CLOSE,
1646 		    (WireCodecFunc) sanei_w_word, &s->handle,
1647 		    (WireCodecFunc) sanei_w_word, &ack);
1648 
1649       free (s);
1650 
1651       return status;
1652     }
1653 
1654   first_handle = s;
1655   *meta_handle = s;
1656 
1657   DBG (3, "sane_open: success\n");
1658   return SANE_STATUS_GOOD;
1659 }
1660 
1661 void
1662 sane_close (SANE_Handle handle)
1663 {
1664   Net_Scanner *prev, *s;
1665   SANE_Word ack;
1666   int option_number;
1667 
1668   DBG (3, "sane_close: handle %p\n", handle);
1669 
1670   prev = 0;
1671   for (s = first_handle; s; s = s->next)
1672     {
1673       if (s == handle)
1674 	break;
1675       prev = s;
1676     }
1677   if (!s)
1678     {
1679       DBG (1, "sane_close: invalid handle %p\n", handle);
1680       return;			/* oops, not a handle we know about */
1681     }
1682   if (prev)
1683     prev->next = s->next;
1684   else
1685     first_handle = s->next;
1686 
1687   if (s->opt.num_options)
1688     {
1689       DBG (2, "sane_close: removing cached option descriptors\n");
1690       sanei_w_set_dir (&s->hw->wire, WIRE_FREE);
1691       s->hw->wire.status = 0;
1692       sanei_w_option_descriptor_array (&s->hw->wire, &s->opt);
1693       if (s->hw->wire.status)
1694 	DBG (1, "sane_close: couldn't free sanei_w_option_descriptor_array "
1695 	     "(%s)\n", sane_strstatus (s->hw->wire.status));
1696     }
1697 
1698   DBG (2, "sane_close: removing local option descriptors\n");
1699   for (option_number = 0; option_number < s->local_opt.num_options;
1700        option_number++)
1701     free (s->local_opt.desc[option_number]);
1702   if (s->local_opt.desc)
1703     free (s->local_opt.desc);
1704 
1705   DBG (2, "sane_close: net_close\n");
1706   sanei_w_call (&s->hw->wire, SANE_NET_CLOSE,
1707 		(WireCodecFunc) sanei_w_word, &s->handle,
1708 		(WireCodecFunc) sanei_w_word, &ack);
1709   if (s->data >= 0)
1710     {
1711       DBG (2, "sane_close: closing data pipe\n");
1712       close (s->data);
1713     }
1714   free (s);
1715   DBG (2, "sane_close: done\n");
1716 }
1717 
1718 const SANE_Option_Descriptor *
1719 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
1720 {
1721   Net_Scanner *s = handle;
1722   SANE_Status status;
1723 
1724   DBG (3, "sane_get_option_descriptor: option %d\n", option);
1725 
1726   if (!s->options_valid)
1727     {
1728       DBG (3, "sane_get_option_descriptor: getting option descriptors\n");
1729       status = fetch_options (s);
1730       if (status != SANE_STATUS_GOOD)
1731 	{
1732 	  DBG (1, "sane_get_option_descriptor: fetch_options failed (%s)\n",
1733 	       sane_strstatus (status));
1734 	  return 0;
1735 	}
1736     }
1737 
1738   if (((SANE_Word) option >= s->opt.num_options) || (option < 0))
1739     {
1740       DBG (2, "sane_get_option_descriptor: invalid option number\n");
1741       return 0;
1742     }
1743   return s->local_opt.desc[option];
1744 }
1745 
1746 SANE_Status
1747 sane_control_option (SANE_Handle handle, SANE_Int option,
1748 		     SANE_Action action, void *value, SANE_Word * info)
1749 {
1750   Net_Scanner *s = handle;
1751   SANE_Control_Option_Req req;
1752   SANE_Control_Option_Reply reply;
1753   SANE_Status status;
1754   size_t value_size;
1755   int need_auth;
1756   SANE_Word local_info;
1757 
1758   DBG (3, "sane_control_option: option %d, action %d\n", option, action);
1759 
1760   if (!s->options_valid)
1761     {
1762       DBG (1, "sane_control_option: FRONTEND BUG: option descriptors reload needed\n");
1763       return SANE_STATUS_INVAL;
1764     }
1765 
1766   if (((SANE_Word) option >= s->opt.num_options) || (option < 0))
1767     {
1768       DBG (1, "sane_control_option: invalid option number\n");
1769       return SANE_STATUS_INVAL;
1770     }
1771 
1772   switch (s->opt.desc[option]->type)
1773     {
1774     case SANE_TYPE_BUTTON:
1775     case SANE_TYPE_GROUP:	/* shouldn't happen... */
1776       /* the SANE standard defines that the option size of a BUTTON or
1777          GROUP is IGNORED.  */
1778       value_size = 0;
1779       break;
1780     case SANE_TYPE_STRING:	/* strings can be smaller than size */
1781       value_size = s->opt.desc[option]->size;
1782       if ((action == SANE_ACTION_SET_VALUE)
1783 	  && (((SANE_Int) strlen ((SANE_String) value) + 1)
1784 	      < s->opt.desc[option]->size))
1785 	value_size = strlen ((SANE_String) value) + 1;
1786       break;
1787     default:
1788       value_size = s->opt.desc[option]->size;
1789       break;
1790     }
1791 
1792   /* Avoid leaking memory bits */
1793   if (value && (action != SANE_ACTION_SET_VALUE))
1794     memset (value, 0, value_size);
1795 
1796   /* for SET_AUTO the parameter ``value'' is ignored */
1797   if (action == SANE_ACTION_SET_AUTO)
1798     value_size = 0;
1799 
1800   req.handle = s->handle;
1801   req.option = option;
1802   req.action = action;
1803   req.value_type = s->opt.desc[option]->type;
1804   req.value_size = value_size;
1805   req.value = value;
1806 
1807   local_info = 0;
1808 
1809   DBG (3, "sane_control_option: remote control option\n");
1810   sanei_w_call (&s->hw->wire, SANE_NET_CONTROL_OPTION,
1811 		(WireCodecFunc) sanei_w_control_option_req, &req,
1812 		(WireCodecFunc) sanei_w_control_option_reply, &reply);
1813 
1814   do
1815     {
1816       status = reply.status;
1817       need_auth = (reply.resource_to_authorize != 0);
1818       if (need_auth)
1819 	{
1820 	  DBG (3, "sane_control_option: auth required\n");
1821 	  do_authorization (s->hw, reply.resource_to_authorize);
1822 	  sanei_w_free (&s->hw->wire,
1823 			(WireCodecFunc) sanei_w_control_option_reply, &reply);
1824 
1825 	  sanei_w_set_dir (&s->hw->wire, WIRE_DECODE);
1826 
1827 	  sanei_w_control_option_reply (&s->hw->wire, &reply);
1828 	  continue;
1829 
1830 	}
1831       else if (status == SANE_STATUS_GOOD)
1832 	{
1833 	  local_info = reply.info;
1834 
1835 	  if (info)
1836 	    *info = reply.info;
1837 	  if (value_size > 0)
1838 	    {
1839 	      if ((SANE_Word) value_size == reply.value_size)
1840 		memcpy (value, reply.value, reply.value_size);
1841 	      else
1842 		DBG (1, "sane_control_option: size changed from %d to %d\n",
1843 		     s->opt.desc[option]->size, reply.value_size);
1844 	    }
1845 
1846 	  if (reply.info & SANE_INFO_RELOAD_OPTIONS)
1847 	    s->options_valid = 0;
1848 	}
1849       sanei_w_free (&s->hw->wire,
1850 		    (WireCodecFunc) sanei_w_control_option_reply, &reply);
1851       if (need_auth && !s->hw->auth_active)
1852 	return SANE_STATUS_CANCELLED;
1853     }
1854   while (need_auth);
1855 
1856   DBG (2, "sane_control_option: remote done (%s, info %x)\n", sane_strstatus (status), local_info);
1857 
1858   if ((status == SANE_STATUS_GOOD) && (info == NULL) && (local_info & SANE_INFO_RELOAD_OPTIONS))
1859     {
1860       DBG (2, "sane_control_option: reloading options as frontend does not care\n");
1861 
1862       status = fetch_options (s);
1863 
1864       DBG (2, "sane_control_option: reload done (%s)\n", sane_strstatus (status));
1865     }
1866 
1867   DBG (2, "sane_control_option: done (%s, info %x)\n", sane_strstatus (status), local_info);
1868 
1869   return status;
1870 }
1871 
1872 SANE_Status
1873 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
1874 {
1875   Net_Scanner *s = handle;
1876   SANE_Get_Parameters_Reply reply;
1877   SANE_Status status;
1878 
1879   DBG (3, "sane_get_parameters\n");
1880 
1881   if (!params)
1882     {
1883       DBG (1, "sane_get_parameters: parameter params not supplied\n");
1884       return SANE_STATUS_INVAL;
1885     }
1886 
1887   DBG (3, "sane_get_parameters: remote get parameters\n");
1888   sanei_w_call (&s->hw->wire, SANE_NET_GET_PARAMETERS,
1889 		(WireCodecFunc) sanei_w_word, &s->handle,
1890 		(WireCodecFunc) sanei_w_get_parameters_reply, &reply);
1891 
1892   status = reply.status;
1893   *params = reply.params;
1894   depth = reply.params.depth;
1895   sanei_w_free (&s->hw->wire,
1896 		(WireCodecFunc) sanei_w_get_parameters_reply, &reply);
1897 
1898   DBG (3, "sane_get_parameters: returned status %s\n",
1899        sane_strstatus (status));
1900   return status;
1901 }
1902 
1903 #ifdef NET_USES_AF_INDEP
1904 SANE_Status
1905 sane_start (SANE_Handle handle)
1906 {
1907   Net_Scanner *s = handle;
1908   SANE_Start_Reply reply;
1909   struct sockaddr_in sin;
1910   struct sockaddr *sa;
1911 #ifdef ENABLE_IPV6
1912   struct sockaddr_in6 sin6;
1913 #endif /* ENABLE_IPV6 */
1914   SANE_Status status;
1915   int fd, need_auth;
1916   socklen_t len;
1917   uint16_t port;			/* Internet-specific */
1918 
1919 
1920   DBG (3, "sane_start\n");
1921 
1922   hang_over = -1;
1923   left_over = -1;
1924 
1925   if (s->data >= 0)
1926     {
1927       DBG (2, "sane_start: data pipe already exists\n");
1928       return SANE_STATUS_INVAL;
1929     }
1930 
1931   /* Do this ahead of time so in case anything fails, we can
1932      recover gracefully (without hanging our server).  */
1933 
1934   switch (s->hw->addr_used->ai_family)
1935     {
1936       case AF_INET:
1937 	len = sizeof (sin);
1938 	sa = (struct sockaddr *) &sin;
1939 	break;
1940 #ifdef ENABLE_IPV6
1941       case AF_INET6:
1942 	len = sizeof (sin6);
1943 	sa = (struct sockaddr *) &sin6;
1944 	break;
1945 #endif /* ENABLE_IPV6 */
1946       default:
1947 	DBG (1, "sane_start: unknown address family : %d\n",
1948 	     s->hw->addr_used->ai_family);
1949 	return SANE_STATUS_INVAL;
1950     }
1951 
1952   if (getpeername (s->hw->ctl, sa, &len) < 0)
1953     {
1954       DBG (1, "sane_start: getpeername() failed (%s)\n", strerror (errno));
1955       return SANE_STATUS_IO_ERROR;
1956     }
1957 
1958   fd = socket (s->hw->addr_used->ai_family, SOCK_STREAM, 0);
1959   if (fd < 0)
1960     {
1961       DBG (1, "sane_start: socket() failed (%s)\n", strerror (errno));
1962       return SANE_STATUS_IO_ERROR;
1963     }
1964 
1965   DBG (3, "sane_start: remote start\n");
1966   sanei_w_call (&s->hw->wire, SANE_NET_START,
1967 		(WireCodecFunc) sanei_w_word, &s->handle,
1968 		(WireCodecFunc) sanei_w_start_reply, &reply);
1969   do
1970     {
1971       status = reply.status;
1972       port = reply.port;
1973       if (reply.byte_order == 0x1234)
1974 	{
1975 	  server_big_endian = 0;
1976 	  DBG (1, "sane_start: server has little endian byte order\n");
1977 	}
1978       else
1979 	{
1980 	  server_big_endian = 1;
1981 	  DBG (1, "sane_start: server has big endian byte order\n");
1982 	}
1983 
1984       need_auth = (reply.resource_to_authorize != 0);
1985       if (need_auth)
1986 	{
1987 	  DBG (3, "sane_start: auth required\n");
1988 	  do_authorization (s->hw, reply.resource_to_authorize);
1989 
1990 	  sanei_w_free (&s->hw->wire,
1991 			(WireCodecFunc) sanei_w_start_reply, &reply);
1992 
1993 	  sanei_w_set_dir (&s->hw->wire, WIRE_DECODE);
1994 
1995 	  sanei_w_start_reply (&s->hw->wire, &reply);
1996 
1997 	  continue;
1998 	}
1999       sanei_w_free (&s->hw->wire, (WireCodecFunc) sanei_w_start_reply,
2000 		    &reply);
2001       if (need_auth && !s->hw->auth_active)
2002 	return SANE_STATUS_CANCELLED;
2003 
2004       if (status != SANE_STATUS_GOOD)
2005 	{
2006 	  DBG (1, "sane_start: remote start failed (%s)\n",
2007 	       sane_strstatus (status));
2008 	  close (fd);
2009 	  return status;
2010 	}
2011     }
2012   while (need_auth);
2013   DBG (3, "sane_start: remote start finished, data at port %hu\n", port);
2014 
2015   switch (s->hw->addr_used->ai_family)
2016     {
2017       case AF_INET:
2018 	sin.sin_port = htons (port);
2019 	break;
2020 #ifdef ENABLE_IPV6
2021       case AF_INET6:
2022 	sin6.sin6_port = htons (port);
2023 	break;
2024 #endif /* ENABLE_IPV6 */
2025     }
2026 
2027   if (connect (fd, sa, len) < 0)
2028     {
2029       DBG (1, "sane_start: connect() failed (%s)\n", strerror (errno));
2030       close (fd);
2031       return SANE_STATUS_IO_ERROR;
2032     }
2033   shutdown (fd, 1);
2034   s->data = fd;
2035   s->reclen_buf_offset = 0;
2036   s->bytes_remaining = 0;
2037   DBG (3, "sane_start: done (%s)\n", sane_strstatus (status));
2038   return status;
2039 }
2040 
2041 #else /* !NET_USES_AF_INDEP */
2042 
2043 SANE_Status
2044 sane_start (SANE_Handle handle)
2045 {
2046   Net_Scanner *s = handle;
2047   SANE_Start_Reply reply;
2048   struct sockaddr_in sin;
2049   SANE_Status status;
2050   int fd, need_auth;
2051   socklen_t len;
2052   uint16_t port;			/* Internet-specific */
2053 
2054 
2055   DBG (3, "sane_start\n");
2056 
2057   hang_over = -1;
2058   left_over = -1;
2059 
2060   if (s->data >= 0)
2061     {
2062       DBG (2, "sane_start: data pipe already exists\n");
2063       return SANE_STATUS_INVAL;
2064     }
2065 
2066   /* Do this ahead of time so in case anything fails, we can
2067      recover gracefully (without hanging our server).  */
2068   len = sizeof (sin);
2069   if (getpeername (s->hw->ctl, (struct sockaddr *) &sin, &len) < 0)
2070     {
2071       DBG (1, "sane_start: getpeername() failed (%s)\n", strerror (errno));
2072       return SANE_STATUS_IO_ERROR;
2073     }
2074 
2075   fd = socket (s->hw->addr.sa_family, SOCK_STREAM, 0);
2076   if (fd < 0)
2077     {
2078       DBG (1, "sane_start: socket() failed (%s)\n", strerror (errno));
2079       return SANE_STATUS_IO_ERROR;
2080     }
2081 
2082   DBG (3, "sane_start: remote start\n");
2083   sanei_w_call (&s->hw->wire, SANE_NET_START,
2084 		(WireCodecFunc) sanei_w_word, &s->handle,
2085 		(WireCodecFunc) sanei_w_start_reply, &reply);
2086   do
2087     {
2088 
2089       status = reply.status;
2090       port = reply.port;
2091       if (reply.byte_order == 0x1234)
2092 	{
2093 	  server_big_endian = 0;
2094 	  DBG (1, "sane_start: server has little endian byte order\n");
2095 	}
2096       else
2097 	{
2098 	  server_big_endian = 1;
2099 	  DBG (1, "sane_start: server has big endian byte order\n");
2100 	}
2101 
2102       need_auth = (reply.resource_to_authorize != 0);
2103       if (need_auth)
2104 	{
2105 	  DBG (3, "sane_start: auth required\n");
2106 	  do_authorization (s->hw, reply.resource_to_authorize);
2107 
2108 	  sanei_w_free (&s->hw->wire,
2109 			(WireCodecFunc) sanei_w_start_reply, &reply);
2110 
2111 	  sanei_w_set_dir (&s->hw->wire, WIRE_DECODE);
2112 
2113 	  sanei_w_start_reply (&s->hw->wire, &reply);
2114 
2115 	  continue;
2116 	}
2117       sanei_w_free (&s->hw->wire, (WireCodecFunc) sanei_w_start_reply,
2118 		    &reply);
2119       if (need_auth && !s->hw->auth_active)
2120 	return SANE_STATUS_CANCELLED;
2121 
2122       if (status != SANE_STATUS_GOOD)
2123 	{
2124 	  DBG (1, "sane_start: remote start failed (%s)\n",
2125 	       sane_strstatus (status));
2126 	  close (fd);
2127 	  return status;
2128 	}
2129     }
2130   while (need_auth);
2131   DBG (3, "sane_start: remote start finished, data at port %hu\n", port);
2132   sin.sin_port = htons (port);
2133 
2134   if (connect (fd, (struct sockaddr *) &sin, len) < 0)
2135     {
2136       DBG (1, "sane_start: connect() failed (%s)\n", strerror (errno));
2137       close (fd);
2138       return SANE_STATUS_IO_ERROR;
2139     }
2140   shutdown (fd, 1);
2141   s->data = fd;
2142   s->reclen_buf_offset = 0;
2143   s->bytes_remaining = 0;
2144   DBG (3, "sane_start: done (%s)\n", sane_strstatus (status));
2145   return status;
2146 }
2147 #endif /* NET_USES_AF_INDEP */
2148 
2149 
2150 SANE_Status
2151 sane_read (SANE_Handle handle, SANE_Byte * data, SANE_Int max_length,
2152 	   SANE_Int * length)
2153 {
2154   Net_Scanner *s = handle;
2155   ssize_t nread;
2156   SANE_Int cnt;
2157   SANE_Int start_cnt;
2158   SANE_Int end_cnt;
2159   SANE_Byte swap_buf;
2160   SANE_Byte temp_hang_over;
2161   int is_even;
2162 
2163   DBG (3, "sane_read: handle=%p, data=%p, max_length=%d, length=%p\n",
2164        handle, (void *) data, max_length, (void *) length);
2165   if (!length)
2166     {
2167       DBG (1, "sane_read: length == NULL\n");
2168       return SANE_STATUS_INVAL;
2169     }
2170 
2171   is_even = 1;
2172   *length = 0;
2173 
2174   /* If there's a left over, i.e. a byte already in the correct byte order,
2175      return it immediately; otherwise read may fail with a SANE_STATUS_EOF and
2176      the caller never can read the last byte */
2177   if ((depth == 16) && (server_big_endian != client_big_endian))
2178     {
2179       if (left_over > -1)
2180 	{
2181 	  DBG (3, "sane_read: left_over from previous call, return "
2182 	       "immediately\n");
2183 	  /* return the byte, we've currently scanned; hang_over becomes
2184 	     left_over */
2185 	  *data = (SANE_Byte) left_over;
2186 	  left_over = -1;
2187 	  *length = 1;
2188 	  return SANE_STATUS_GOOD;
2189 	}
2190     }
2191 
2192   if (s->data < 0)
2193     {
2194       DBG (1, "sane_read: data pipe doesn't exist, scan cancelled?\n");
2195       return SANE_STATUS_CANCELLED;
2196     }
2197 
2198   if (s->bytes_remaining == 0)
2199     {
2200       /* boy, is this painful or what? */
2201 
2202       DBG (4, "sane_read: reading packet length\n");
2203       nread = read (s->data, s->reclen_buf + s->reclen_buf_offset,
2204 		    4 - s->reclen_buf_offset);
2205       if (nread < 0)
2206 	{
2207 	  DBG (3, "sane_read: read failed (%s)\n", strerror (errno));
2208 	  if (errno == EAGAIN)
2209 	    {
2210 	      DBG (3, "sane_read: try again later\n");
2211 	      return SANE_STATUS_GOOD;
2212 	    }
2213 	  else
2214 	    {
2215 	      DBG (1, "sane_read: cancelling read\n");
2216 	      do_cancel (s);
2217 	      return SANE_STATUS_IO_ERROR;
2218 	    }
2219 	}
2220       DBG (4, "sane_read: read %lu bytes, %d from 4 total\n", (u_long) nread,
2221 	   s->reclen_buf_offset);
2222       s->reclen_buf_offset += nread;
2223       if (s->reclen_buf_offset < 4)
2224 	{
2225 	  DBG (4, "sane_read: enough for now\n");
2226 	  return SANE_STATUS_GOOD;
2227 	}
2228 
2229       s->reclen_buf_offset = 0;
2230       s->bytes_remaining = (((u_long) s->reclen_buf[0] << 24)
2231 			    | ((u_long) s->reclen_buf[1] << 16)
2232 			    | ((u_long) s->reclen_buf[2] << 8)
2233 			    | ((u_long) s->reclen_buf[3] << 0));
2234       DBG (3, "sane_read: next record length=%ld bytes\n",
2235 	   (long) s->bytes_remaining);
2236       if (s->bytes_remaining == 0xffffffff)
2237 	{
2238 	  char ch;
2239 
2240 	  DBG (2, "sane_read: received error signal\n");
2241 
2242 	  /* turn off non-blocking I/O (s->data will be closed anyhow): */
2243 	  fcntl (s->data, F_SETFL, 0);
2244 
2245 	  /* read the status byte: */
2246 	  if (read (s->data, &ch, sizeof (ch)) != 1)
2247 	    {
2248 	      DBG (1, "sane_read: failed to read error code\n");
2249 	      ch = SANE_STATUS_IO_ERROR;
2250 	    }
2251 	  DBG (1, "sane_read: error code %s\n",
2252 	       sane_strstatus ((SANE_Status) ch));
2253 	  do_cancel (s);
2254 	  return (SANE_Status) ch;
2255 	}
2256     }
2257 
2258   if (max_length > (SANE_Int) s->bytes_remaining)
2259     max_length = s->bytes_remaining;
2260 
2261   nread = read (s->data, data, max_length);
2262 
2263   if (nread < 0)
2264     {
2265       DBG (2, "sane_read: error code %s\n", strerror (errno));
2266       if (errno == EAGAIN)
2267 	return SANE_STATUS_GOOD;
2268       else
2269 	{
2270 	  DBG (1, "sane_read: cancelling scan\n");
2271 	  do_cancel (s);
2272 	  return SANE_STATUS_IO_ERROR;
2273 	}
2274     }
2275 
2276   s->bytes_remaining -= nread;
2277 
2278   *length = nread;
2279   /* Check whether we are scanning with a depth of 16 bits/pixel and whether
2280      server and client have different byte order. If this is true, then it's
2281      necessary to check whether read returned an odd number. If an odd number
2282      has been returned, we must save the last byte.
2283   */
2284   if ((depth == 16) && (server_big_endian != client_big_endian))
2285     {
2286       DBG (1,"sane_read: client/server have different byte order; "
2287 	   "must swap\n");
2288       /* special case: 1 byte scanned and hang_over */
2289       if ((nread == 1) && (hang_over > -1))
2290 	{
2291 	  /* return the byte, we've currently scanned; hang_over becomes
2292 	     left_over */
2293 	  left_over = hang_over;
2294 	  hang_over = -1;
2295 	  return SANE_STATUS_GOOD;
2296 	}
2297       /* check whether an even or an odd number of bytes has been scanned */
2298       if ((nread % 2) == 0)
2299         is_even = 1;
2300       else
2301         is_even = 0;
2302       /* check, whether there's a hang over from a previous call;
2303 	 in this case we memcopy the data up one byte */
2304       if ((nread > 1) && (hang_over > -1))
2305 	{
2306 	  /* store last byte */
2307 	  temp_hang_over = *(data + nread - 1);
2308 	  memmove (data + 1, data, nread - 1);
2309 	  *data = (SANE_Byte) hang_over;
2310 	  /* what happens with the last byte depends on whether the number
2311 	     of bytes is even or odd */
2312 	  if (is_even == 1)
2313 	    {
2314 	      /* number of bytes is even; no new hang_over, exchange last
2315 		 byte with hang over; last byte becomes left_over */
2316 	      left_over = *(data + nread - 1);
2317 	      *(data + nread - 1) = temp_hang_over;
2318 	      hang_over = -1;
2319 	      start_cnt = 0;
2320 	      /* last byte already swapped */
2321 	      end_cnt = nread - 2;
2322 	    }
2323 	  else
2324 	    {
2325 	      /* number of bytes is odd; last byte becomes new hang_over */
2326 	      hang_over = temp_hang_over;
2327 	      left_over = -1;
2328 	      start_cnt = 0;
2329 	      end_cnt = nread - 1;
2330 	    }
2331 	}
2332       else if (nread == 1)
2333 	{
2334 	  /* if only one byte has been read, save it as hang_over and return
2335 	     length=0 */
2336 	  hang_over = (int) *data;
2337 	  *length = 0;
2338 	  return SANE_STATUS_GOOD;
2339 	}
2340       else
2341 	{
2342 	  /* no hang_over; test for even or odd byte number */
2343 	  if(is_even == 1)
2344 	    {
2345 	      start_cnt = 0;
2346 	      end_cnt = *length;
2347 	    }
2348 	  else
2349 	    {
2350 	      start_cnt = 0;
2351 	      hang_over = *(data + *length - 1);
2352 	      *length -= 1;
2353 	      end_cnt = *length;
2354 	    }
2355 	}
2356       /* swap the bytes */
2357       for (cnt = start_cnt; cnt < end_cnt - 1; cnt += 2)
2358 	{
2359 	  swap_buf = *(data + cnt);
2360 	  *(data + cnt) = *(data + cnt + 1);
2361 	  *(data + cnt + 1) = swap_buf;
2362 	}
2363     }
2364   DBG (3, "sane_read: %lu bytes read, %lu remaining\n", (u_long) nread,
2365        (u_long) s->bytes_remaining);
2366 
2367   return SANE_STATUS_GOOD;
2368 }
2369 
2370 void
2371 sane_cancel (SANE_Handle handle)
2372 {
2373   Net_Scanner *s = handle;
2374   SANE_Word ack;
2375 
2376   DBG (3, "sane_cancel: sending net_cancel\n");
2377 
2378   sanei_w_call (&s->hw->wire, SANE_NET_CANCEL,
2379 		(WireCodecFunc) sanei_w_word, &s->handle,
2380 		(WireCodecFunc) sanei_w_word, &ack);
2381   do_cancel (s);
2382   DBG (4, "sane_cancel: done\n");
2383 }
2384 
2385 SANE_Status
2386 sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
2387 {
2388   Net_Scanner *s = handle;
2389 
2390   DBG (3, "sane_set_io_mode: non_blocking = %d\n", non_blocking);
2391   if (s->data < 0)
2392     {
2393       DBG (1, "sane_set_io_mode: pipe doesn't exist\n");
2394       return SANE_STATUS_INVAL;
2395     }
2396 
2397   if (fcntl (s->data, F_SETFL, non_blocking ? O_NONBLOCK : 0) < 0)
2398     {
2399       DBG (1, "sane_set_io_mode: fcntl failed (%s)\n", strerror (errno));
2400       return SANE_STATUS_IO_ERROR;
2401     }
2402 
2403   return SANE_STATUS_GOOD;
2404 }
2405 
2406 SANE_Status
2407 sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
2408 {
2409   Net_Scanner *s = handle;
2410 
2411   DBG (3, "sane_get_select_fd\n");
2412 
2413   if (s->data < 0)
2414     {
2415       DBG (1, "sane_get_select_fd: pipe doesn't exist\n");
2416       return SANE_STATUS_INVAL;
2417     }
2418 
2419   *fd = s->data;
2420   DBG (3, "sane_get_select_fd: done; *fd = %d\n", *fd);
2421   return SANE_STATUS_GOOD;
2422 }
2423