1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 1997 Andreas Beck
3    This file is part of the SANE package.
4 
5    SANE is free software; you can redistribute it and/or modify it under
6    the terms of the GNU General Public License as published by the Free
7    Software Foundation; either version 2 of the License, or (at your
8    option) any later version.
9 
10    SANE is distributed in the hope that it will be useful, but WITHOUT
11    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13    for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with sane; see the file COPYING.
17    If not, see <https://www.gnu.org/licenses/>.
18 
19    This file implements a simple SANE frontend (well it rather is a
20    transport layer, but seen from libsane it is a frontend) which acts
21    as a NETSANE server. The NETSANE specifications should have come
22    with this package.
23    Feel free to enhance this program ! It needs extension especially
24    regarding crypto-support and authentication.
25  */
26 
27 #include <ctype.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 
37 #include <netdb.h>
38 #include <netinet/in.h>
39 
40 #include "../include/sane/sane.h"
41 
42 void
auth_callback(SANE_String_Const domain,SANE_Char * username,SANE_Char * password)43 auth_callback (SANE_String_Const domain,
44 	       SANE_Char *username,
45 	       SANE_Char *password)
46 {
47   printf ("Client '%s' requested authorization.\nUser:\n", domain);
48   scanf ("%s", username);
49   printf ("Password:\n");
50   scanf ("%s", password);
51   return;
52 }
53 
54 void
testsane(const char * dev_name)55 testsane (const char *dev_name)
56 {
57   int hlp, x;
58   SANE_Status bla;
59   SANE_Int blubb;
60   SANE_Handle hand;
61   SANE_Parameters pars;
62   const SANE_Option_Descriptor *sod;
63   const SANE_Device **device_list;
64   char buffer[2048];
65 
66   bla = sane_init (&blubb, auth_callback);
67   fprintf (stderr, "Init : stat=%d ver=%x\nPress Enter to continue...",
68 	   bla, blubb);
69   getchar ();
70   if (bla != SANE_STATUS_GOOD)
71     return;
72 
73   bla = sane_get_devices (&device_list, SANE_FALSE);
74   fprintf (stderr, "GetDev : stat=%s\n", sane_strstatus (bla));
75   if (bla != SANE_STATUS_GOOD)
76     return;
77 
78   bla = sane_open (dev_name, &hand);
79   fprintf (stderr, "Open : stat=%s hand=%p\n", sane_strstatus (bla), hand);
80   if (bla != SANE_STATUS_GOOD)
81     return;
82 
83   bla = sane_set_io_mode (hand, 0);
84   fprintf (stderr, "SetIoMode : stat=%s\n", sane_strstatus (bla));
85 
86   for (hlp = 0; hlp < 9999; hlp++)
87     {
88       sod = sane_get_option_descriptor (hand, hlp);
89       if (sod == NULL)
90 	break;
91       fprintf (stderr, "Gopt(%d) : stat=%p\n", hlp, sod);
92       fprintf (stderr, "name : %s\n", sod->name);
93       fprintf (stderr, "title: %s\n", sod->title);
94       fprintf (stderr, "desc : %s\n", sod->desc);
95 
96       fprintf (stderr, "type : %d\n", sod->type);
97       fprintf (stderr, "unit : %d\n", sod->unit);
98       fprintf (stderr, "size : %d\n", sod->size);
99       fprintf (stderr, "cap  : %d\n", sod->cap);
100       fprintf (stderr, "ctyp : %d\n", sod->constraint_type);
101       switch (sod->constraint_type)
102 	{
103 	case SANE_CONSTRAINT_NONE:
104 	  break;
105 	case SANE_CONSTRAINT_STRING_LIST:
106 	  fprintf (stderr, "stringlist:\n");
107 	  break;
108 	case SANE_CONSTRAINT_WORD_LIST:
109 	  fprintf (stderr, "wordlist (%d) : ", sod->constraint.word_list[0]);
110 	  for (x = 1; x <= sod->constraint.word_list[0]; x++)
111 	    fprintf (stderr, " %d ", sod->constraint.word_list[x]);
112 	  fprintf (stderr, "\n");
113 	  break;
114 	case SANE_CONSTRAINT_RANGE:
115 	  fprintf (stderr, "range: %d-%d %d \n", sod->constraint.range->min,
116 		   sod->constraint.range->max, sod->constraint.range->quant);
117 	  break;
118 	}
119     }
120 
121   bla = sane_get_parameters (hand, &pars);
122   fprintf (stderr,
123 	   "Parm : stat=%s form=%d,lf=%d,bpl=%d,pixpl=%d,lin=%d,dep=%d\n",
124 	   sane_strstatus (bla),
125 	   pars.format, pars.last_frame,
126 	   pars.bytes_per_line, pars.pixels_per_line,
127 	   pars.lines, pars.depth);
128   if (bla != SANE_STATUS_GOOD)
129     return;
130 
131   bla = sane_start (hand);
132   fprintf (stderr, "Start : stat=%s\n", sane_strstatus (bla));
133   if (bla != SANE_STATUS_GOOD)
134     return;
135 
136   do
137     {
138       bla = sane_read (hand, buffer, sizeof (buffer), &blubb);
139       /*printf("Read : stat=%s len=%d\n",sane_strstatus (bla),blubb); */
140       if (bla != SANE_STATUS_GOOD)
141 	{
142 	  if (bla == SANE_STATUS_EOF)
143 	    break;
144 	  return;
145 	}
146       fwrite (buffer, 1, blubb, stdout);
147     }
148   while (1);
149 
150   sane_cancel (hand);
151   fprintf (stderr, "Cancel.\n");
152 
153   sane_close (hand);
154   fprintf (stderr, "Close\n");
155 
156   for (hlp = 0; hlp < 20; hlp++)
157     fprintf (stderr, "STRS %d=%s\n", hlp, sane_strstatus (hlp));
158 
159   fprintf (stderr, "Exit.\n");
160 }
161 
162 int
main(int argc,char * argv[])163 main (int argc, char *argv[])
164 {
165   if (argc != 2 && argc != 3)
166     {
167       fprintf (stderr, "Usage: %s devicename [hostname]\n", argv[0]);
168       exit (0);
169     }
170   if (argc == 3)
171     {
172       char envbuf[1024];
173       sprintf (envbuf, "SANE_NET_HOST=%s", argv[2]);
174       putenv (envbuf);
175     }
176 
177   fprintf (stderr, "This is a SANE test application.\n"
178 	   "Now connecting to device %s.\n", argv[1]);
179   testsane (argv[1]);
180   sane_exit ();
181   return 0;
182 }
183