]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
07725fee | 2 | * "$Id: socket.c 5948 2006-09-12 13:58:39Z mike $" |
ef416fc2 | 3 | * |
4 | * AppSocket backend for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * This file is subject to the Apple OS-Developed Software exception. | |
25 | * | |
26 | * Contents: | |
27 | * | |
28 | * main() - Send a file to the printer or server. | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Include necessary headers. | |
33 | */ | |
34 | ||
ef416fc2 | 35 | #include <cups/http-private.h> |
ed486911 | 36 | #include "backend-private.h" |
ef416fc2 | 37 | #include <stdarg.h> |
ef416fc2 | 38 | #include <sys/types.h> |
39 | #include <sys/stat.h> | |
ef416fc2 | 40 | |
41 | #ifdef WIN32 | |
42 | # include <winsock.h> | |
43 | #else | |
44 | # include <unistd.h> | |
45 | # include <fcntl.h> | |
46 | # include <sys/socket.h> | |
47 | # include <netinet/in.h> | |
48 | # include <arpa/inet.h> | |
49 | # include <netdb.h> | |
50 | #endif /* WIN32 */ | |
51 | ||
52 | ||
53 | /* | |
54 | * 'main()' - Send a file to the printer or server. | |
55 | * | |
56 | * Usage: | |
57 | * | |
58 | * printer-uri job-id user title copies options [file] | |
59 | */ | |
60 | ||
61 | int /* O - Exit status */ | |
62 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
63 | char *argv[]) /* I - Command-line arguments */ | |
64 | { | |
65 | char method[255], /* Method in URI */ | |
66 | hostname[1024], /* Hostname */ | |
67 | username[255], /* Username info (not used) */ | |
fa73b229 | 68 | resource[1024], /* Resource info (not used) */ |
69 | *options, /* Pointer to options */ | |
70 | name[255], /* Name of option */ | |
71 | value[255], /* Value of option */ | |
72 | *ptr; /* Pointer into name or value */ | |
ed486911 | 73 | int print_fd; /* Print file */ |
ef416fc2 | 74 | int copies; /* Number of copies to print */ |
fa73b229 | 75 | int waiteof; /* Wait for end-of-file? */ |
ef416fc2 | 76 | int port; /* Port number */ |
77 | char portname[255]; /* Port name */ | |
78 | int delay; /* Delay for retries... */ | |
ed486911 | 79 | int device_fd; /* AppSocket */ |
ef416fc2 | 80 | int error; /* Error code (if any) */ |
81 | http_addrlist_t *addrlist; /* Address list */ | |
ed486911 | 82 | ssize_t tbytes; /* Total number of bytes written */ |
ef416fc2 | 83 | struct timeval timeout; /* Timeout for select() */ |
ed486911 | 84 | fd_set input; /* Input set for select() */ |
85 | ssize_t bc_bytes; /* Number of back-channel bytes read */ | |
ef416fc2 | 86 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
87 | struct sigaction action; /* Actions for POSIX signals */ | |
88 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
89 | ||
90 | ||
91 | /* | |
92 | * Make sure status messages are not buffered... | |
93 | */ | |
94 | ||
95 | setbuf(stderr, NULL); | |
96 | ||
97 | /* | |
98 | * Ignore SIGPIPE signals... | |
99 | */ | |
100 | ||
101 | #ifdef HAVE_SIGSET | |
102 | sigset(SIGPIPE, SIG_IGN); | |
103 | #elif defined(HAVE_SIGACTION) | |
104 | memset(&action, 0, sizeof(action)); | |
105 | action.sa_handler = SIG_IGN; | |
106 | sigaction(SIGPIPE, &action, NULL); | |
107 | #else | |
108 | signal(SIGPIPE, SIG_IGN); | |
109 | #endif /* HAVE_SIGSET */ | |
110 | ||
111 | /* | |
112 | * Check command-line... | |
113 | */ | |
114 | ||
115 | if (argc == 1) | |
116 | { | |
117 | puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\""); | |
118 | return (CUPS_BACKEND_OK); | |
119 | } | |
120 | else if (argc < 6 || argc > 7) | |
121 | { | |
122 | fprintf(stderr, "Usage: %s job-id user title copies options [file]\n", | |
123 | argv[0]); | |
124 | return (CUPS_BACKEND_FAILED); | |
125 | } | |
126 | ||
127 | /* | |
128 | * If we have 7 arguments, print the file named on the command-line. | |
129 | * Otherwise, send stdin instead... | |
130 | */ | |
131 | ||
132 | if (argc == 6) | |
133 | { | |
ed486911 | 134 | print_fd = 0; |
135 | copies = 1; | |
ef416fc2 | 136 | } |
137 | else | |
138 | { | |
139 | /* | |
140 | * Try to open the print file... | |
141 | */ | |
142 | ||
ed486911 | 143 | if ((print_fd = open(argv[6], O_RDONLY)) < 0) |
ef416fc2 | 144 | { |
145 | perror("ERROR: unable to open print file"); | |
146 | return (CUPS_BACKEND_FAILED); | |
147 | } | |
148 | ||
149 | copies = atoi(argv[4]); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Extract the hostname and port number from the URI... | |
154 | */ | |
155 | ||
a4d04587 | 156 | httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv), |
157 | method, sizeof(method), username, sizeof(username), | |
158 | hostname, sizeof(hostname), &port, | |
ef416fc2 | 159 | resource, sizeof(resource)); |
160 | ||
161 | if (port == 0) | |
162 | port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */ | |
163 | ||
fa73b229 | 164 | /* |
165 | * Get options, if any... | |
166 | */ | |
167 | ||
168 | waiteof = 1; | |
169 | ||
170 | if ((options = strchr(resource, '?')) != NULL) | |
171 | { | |
172 | /* | |
173 | * Yup, terminate the device name string and move to the first | |
174 | * character of the options... | |
175 | */ | |
176 | ||
177 | *options++ = '\0'; | |
178 | ||
179 | /* | |
180 | * Parse options... | |
181 | */ | |
182 | ||
183 | while (*options) | |
184 | { | |
185 | /* | |
186 | * Get the name... | |
187 | */ | |
188 | ||
189 | for (ptr = name; *options && *options != '=';) | |
190 | if (ptr < (name + sizeof(name) - 1)) | |
191 | *ptr++ = *options++; | |
192 | *ptr = '\0'; | |
193 | ||
194 | if (*options == '=') | |
195 | { | |
196 | /* | |
197 | * Get the value... | |
198 | */ | |
199 | ||
200 | options ++; | |
201 | ||
202 | for (ptr = value; *options && *options != '+' && *options != '&';) | |
203 | if (ptr < (value + sizeof(value) - 1)) | |
204 | *ptr++ = *options++; | |
205 | *ptr = '\0'; | |
206 | ||
207 | if (*options == '+' || *options == '&') | |
208 | options ++; | |
209 | } | |
210 | else | |
211 | value[0] = '\0'; | |
212 | ||
213 | /* | |
214 | * Process the option... | |
215 | */ | |
216 | ||
217 | if (!strcasecmp(name, "waiteof")) | |
218 | { | |
219 | /* | |
220 | * Set the wait-for-eof value... | |
221 | */ | |
222 | ||
223 | waiteof = !value[0] || !strcasecmp(value, "on") || | |
224 | !strcasecmp(value, "yes") || !strcasecmp(value, "true"); | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
ef416fc2 | 229 | /* |
230 | * Then try to connect to the remote host... | |
231 | */ | |
232 | ||
233 | sprintf(portname, "%d", port); | |
234 | ||
235 | if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) | |
236 | { | |
237 | fprintf(stderr, "ERROR: Unable to locate printer \'%s\'!\n", hostname); | |
238 | return (CUPS_BACKEND_STOP); | |
239 | } | |
240 | ||
241 | fprintf(stderr, "INFO: Attempting to connect to host %s on port %d\n", | |
242 | hostname, port); | |
243 | ||
ed486911 | 244 | fputs("STATE: +connecting-to-device\n", stderr); |
ef416fc2 | 245 | |
ed486911 | 246 | for (delay = 5;;) |
ef416fc2 | 247 | { |
ed486911 | 248 | if (!httpAddrConnect(addrlist, &device_fd)) |
ef416fc2 | 249 | { |
ed486911 | 250 | error = errno; |
251 | device_fd = -1; | |
ef416fc2 | 252 | |
ed486911 | 253 | if (getenv("CLASS") != NULL) |
254 | { | |
255 | /* | |
256 | * If the CLASS environment variable is set, the job was submitted | |
257 | * to a class and not to a specific queue. In this case, we want | |
258 | * to abort immediately so that the job can be requeued on the next | |
259 | * available printer in the class. | |
260 | */ | |
ef416fc2 | 261 | |
ed486911 | 262 | fprintf(stderr, "INFO: Unable to connect to \"%s\", queuing on next printer in class...\n", |
263 | hostname); | |
ef416fc2 | 264 | |
ed486911 | 265 | /* |
266 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
267 | */ | |
ef416fc2 | 268 | |
ed486911 | 269 | sleep(5); |
ef416fc2 | 270 | |
ed486911 | 271 | return (CUPS_BACKEND_FAILED); |
272 | } | |
ef416fc2 | 273 | |
ed486911 | 274 | if (error == ECONNREFUSED || error == EHOSTDOWN || |
275 | error == EHOSTUNREACH) | |
276 | { | |
277 | fprintf(stderr, | |
278 | "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n", | |
279 | hostname, delay); | |
280 | sleep(delay); | |
ef416fc2 | 281 | |
ed486911 | 282 | if (delay < 30) |
283 | delay += 5; | |
ef416fc2 | 284 | } |
285 | else | |
ed486911 | 286 | { |
287 | perror("ERROR: Unable to connect to printer (retrying in 30 seconds)"); | |
288 | sleep(30); | |
289 | } | |
ef416fc2 | 290 | } |
ed486911 | 291 | else |
292 | break; | |
293 | } | |
ef416fc2 | 294 | |
ed486911 | 295 | fputs("STATE: -connecting-to-device\n", stderr); |
ef416fc2 | 296 | |
ed486911 | 297 | /* |
298 | * Print everything... | |
299 | */ | |
ef416fc2 | 300 | |
ed486911 | 301 | tbytes = 0; |
ef416fc2 | 302 | |
ed486911 | 303 | while (copies > 0 && tbytes >= 0) |
304 | { | |
ef416fc2 | 305 | copies --; |
306 | ||
ed486911 | 307 | if (print_fd != 0) |
ef416fc2 | 308 | { |
309 | fputs("PAGE: 1 1\n", stderr); | |
ed486911 | 310 | lseek(print_fd, 0, SEEK_SET); |
ef416fc2 | 311 | } |
312 | ||
ed486911 | 313 | tbytes = backendRunLoop(print_fd, device_fd, 1); |
ef416fc2 | 314 | |
ed486911 | 315 | if (print_fd != 0 && tbytes >= 0) |
316 | fprintf(stderr, "INFO: Sent print file, " CUPS_LLFMT " bytes...\n", | |
317 | CUPS_LLCAST tbytes); | |
318 | } | |
ef416fc2 | 319 | |
ed486911 | 320 | if (waiteof) |
321 | { | |
322 | /* | |
323 | * Shutdown the socket and wait for the other end to finish... | |
324 | */ | |
ef416fc2 | 325 | |
ed486911 | 326 | fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr); |
ef416fc2 | 327 | |
ed486911 | 328 | shutdown(device_fd, 1); |
ef416fc2 | 329 | |
ed486911 | 330 | for (;;) |
ef416fc2 | 331 | { |
332 | /* | |
ed486911 | 333 | * Wait a maximum of 90 seconds for backchannel data or a closed |
334 | * connection... | |
ef416fc2 | 335 | */ |
336 | ||
ed486911 | 337 | timeout.tv_sec = 90; |
338 | timeout.tv_usec = 0; | |
ef416fc2 | 339 | |
ed486911 | 340 | FD_ZERO(&input); |
341 | FD_SET(device_fd, &input); | |
ef416fc2 | 342 | |
ed486911 | 343 | if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0) |
ef416fc2 | 344 | { |
345 | /* | |
ed486911 | 346 | * Grab the data coming back and spit it out to stderr... |
ef416fc2 | 347 | */ |
348 | ||
ed486911 | 349 | if ((bc_bytes = read(device_fd, resource, sizeof(resource))) > 0) |
ef416fc2 | 350 | { |
ed486911 | 351 | fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n", |
352 | (int)bc_bytes); | |
353 | cupsBackChannelWrite(resource, bc_bytes, 1.0); | |
fa73b229 | 354 | } |
ef416fc2 | 355 | else |
356 | break; | |
357 | } | |
ed486911 | 358 | else |
359 | break; | |
ef416fc2 | 360 | } |
ed486911 | 361 | } |
ef416fc2 | 362 | |
ed486911 | 363 | /* |
364 | * Close the socket connection... | |
365 | */ | |
ef416fc2 | 366 | |
ed486911 | 367 | close(device_fd); |
ef416fc2 | 368 | |
369 | httpAddrFreeList(addrlist); | |
370 | ||
371 | /* | |
372 | * Close the input file and return... | |
373 | */ | |
374 | ||
ed486911 | 375 | if (print_fd != 0) |
376 | close(print_fd); | |
ef416fc2 | 377 | |
ed486911 | 378 | if (tbytes >= 0) |
ef416fc2 | 379 | fputs("INFO: Ready to print.\n", stderr); |
380 | ||
ed486911 | 381 | return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK); |
ef416fc2 | 382 | } |
383 | ||
384 | ||
385 | /* | |
07725fee | 386 | * End of "$Id: socket.c 5948 2006-09-12 13:58:39Z mike $". |
ef416fc2 | 387 | */ |