]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: socket.c 7881 2008-08-28 20:21:56Z mike $" | |
3 | * | |
4 | * AppSocket backend for CUPS. | |
5 | * | |
6 | * Copyright 2007-2012 by Apple Inc. | |
7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. | |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
10 | * property of Apple Inc. and are protected by Federal copyright | |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * "LICENSE" which should have been included with this file. If this | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
14 | * | |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * main() - Send a file to the printer or server. | |
20 | * wait_bc() - Wait for back-channel data... | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Include necessary headers. | |
25 | */ | |
26 | ||
27 | #include <cups/http-private.h> | |
28 | #include "backend-private.h" | |
29 | #include <stdarg.h> | |
30 | #include <sys/types.h> | |
31 | #include <sys/stat.h> | |
32 | ||
33 | #ifdef WIN32 | |
34 | # include <winsock.h> | |
35 | #else | |
36 | # include <unistd.h> | |
37 | # include <fcntl.h> | |
38 | # include <sys/socket.h> | |
39 | # include <netinet/in.h> | |
40 | # include <arpa/inet.h> | |
41 | # include <netdb.h> | |
42 | #endif /* WIN32 */ | |
43 | ||
44 | ||
45 | /* | |
46 | * Local functions... | |
47 | */ | |
48 | ||
49 | static int wait_bc(int device_fd, int secs); | |
50 | ||
51 | ||
52 | /* | |
53 | * 'main()' - Send a file to the printer or server. | |
54 | * | |
55 | * Usage: | |
56 | * | |
57 | * printer-uri job-id user title copies options [file] | |
58 | */ | |
59 | ||
60 | int /* O - Exit status */ | |
61 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
62 | char *argv[]) /* I - Command-line arguments */ | |
63 | { | |
64 | const char *device_uri; /* Device URI */ | |
65 | char scheme[255], /* Scheme in URI */ | |
66 | hostname[1024], /* Hostname */ | |
67 | username[255], /* Username info (not used) */ | |
68 | resource[1024], /* Resource info (not used) */ | |
69 | *options, /* Pointer to options */ | |
70 | *name, /* Name of option */ | |
71 | *value, /* Value of option */ | |
72 | sep; /* Option separator */ | |
73 | int print_fd; /* Print file */ | |
74 | int copies; /* Number of copies to print */ | |
75 | time_t start_time; /* Time of first connect */ | |
76 | #ifdef __APPLE__ | |
77 | time_t current_time, /* Current time */ | |
78 | wait_time; /* Time to wait before shutting down socket */ | |
79 | #endif /* __APPLE__ */ | |
80 | int contimeout; /* Connection timeout */ | |
81 | int waiteof; /* Wait for end-of-file? */ | |
82 | int port; /* Port number */ | |
83 | char portname[255]; /* Port name */ | |
84 | int delay; /* Delay for retries... */ | |
85 | int device_fd; /* AppSocket */ | |
86 | int error; /* Error code (if any) */ | |
87 | http_addrlist_t *addrlist, /* Address list */ | |
88 | *addr; /* Connected address */ | |
89 | char addrname[256]; /* Address name */ | |
90 | int snmp_enabled = 1; /* Is SNMP enabled? */ | |
91 | int snmp_fd, /* SNMP socket */ | |
92 | start_count, /* Page count via SNMP at start */ | |
93 | page_count, /* Page count via SNMP */ | |
94 | have_supplies; /* Printer supports supply levels? */ | |
95 | ssize_t bytes = 0, /* Initial bytes read */ | |
96 | tbytes; /* Total number of bytes written */ | |
97 | char buffer[1024]; /* Initial print buffer */ | |
98 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
99 | struct sigaction action; /* Actions for POSIX signals */ | |
100 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
101 | ||
102 | ||
103 | /* | |
104 | * Make sure status messages are not buffered... | |
105 | */ | |
106 | ||
107 | setbuf(stderr, NULL); | |
108 | ||
109 | /* | |
110 | * Ignore SIGPIPE signals... | |
111 | */ | |
112 | ||
113 | #ifdef HAVE_SIGSET | |
114 | sigset(SIGPIPE, SIG_IGN); | |
115 | #elif defined(HAVE_SIGACTION) | |
116 | memset(&action, 0, sizeof(action)); | |
117 | action.sa_handler = SIG_IGN; | |
118 | sigaction(SIGPIPE, &action, NULL); | |
119 | #else | |
120 | signal(SIGPIPE, SIG_IGN); | |
121 | #endif /* HAVE_SIGSET */ | |
122 | ||
123 | /* | |
124 | * Check command-line... | |
125 | */ | |
126 | ||
127 | if (argc == 1) | |
128 | { | |
129 | printf("network socket \"Unknown\" \"%s\"\n", | |
130 | _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect"))); | |
131 | return (CUPS_BACKEND_OK); | |
132 | } | |
133 | else if (argc < 6 || argc > 7) | |
134 | { | |
135 | _cupsLangPrintf(stderr, | |
136 | _("Usage: %s job-id user title copies options [file]"), | |
137 | argv[0]); | |
138 | return (CUPS_BACKEND_FAILED); | |
139 | } | |
140 | ||
141 | /* | |
142 | * If we have 7 arguments, print the file named on the command-line. | |
143 | * Otherwise, send stdin instead... | |
144 | */ | |
145 | ||
146 | if (argc == 6) | |
147 | { | |
148 | print_fd = 0; | |
149 | copies = 1; | |
150 | } | |
151 | else | |
152 | { | |
153 | /* | |
154 | * Try to open the print file... | |
155 | */ | |
156 | ||
157 | if ((print_fd = open(argv[6], O_RDONLY)) < 0) | |
158 | { | |
159 | _cupsLangPrintError("ERROR", _("Unable to open print file")); | |
160 | return (CUPS_BACKEND_FAILED); | |
161 | } | |
162 | ||
163 | copies = atoi(argv[4]); | |
164 | } | |
165 | ||
166 | /* | |
167 | * Extract the hostname and port number from the URI... | |
168 | */ | |
169 | ||
170 | while ((device_uri = cupsBackendDeviceURI(argv)) == NULL) | |
171 | { | |
172 | _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer.")); | |
173 | sleep(10); | |
174 | ||
175 | if (getenv("CLASS") != NULL) | |
176 | return (CUPS_BACKEND_FAILED); | |
177 | } | |
178 | ||
179 | httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), | |
180 | username, sizeof(username), hostname, sizeof(hostname), &port, | |
181 | resource, sizeof(resource)); | |
182 | ||
183 | if (port == 0) | |
184 | port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */ | |
185 | ||
186 | /* | |
187 | * Get options, if any... | |
188 | */ | |
189 | ||
190 | waiteof = 1; | |
191 | contimeout = 7 * 24 * 60 * 60; | |
192 | ||
193 | if ((options = strchr(resource, '?')) != NULL) | |
194 | { | |
195 | /* | |
196 | * Yup, terminate the device name string and move to the first | |
197 | * character of the options... | |
198 | */ | |
199 | ||
200 | *options++ = '\0'; | |
201 | ||
202 | /* | |
203 | * Parse options... | |
204 | */ | |
205 | ||
206 | while (*options) | |
207 | { | |
208 | /* | |
209 | * Get the name... | |
210 | */ | |
211 | ||
212 | name = options; | |
213 | ||
214 | while (*options && *options != '=' && *options != '+' && *options != '&') | |
215 | options ++; | |
216 | ||
217 | if ((sep = *options) != '\0') | |
218 | *options++ = '\0'; | |
219 | ||
220 | if (sep == '=') | |
221 | { | |
222 | /* | |
223 | * Get the value... | |
224 | */ | |
225 | ||
226 | value = options; | |
227 | ||
228 | while (*options && *options != '+' && *options != '&') | |
229 | options ++; | |
230 | ||
231 | if (*options) | |
232 | *options++ = '\0'; | |
233 | } | |
234 | else | |
235 | value = (char *)""; | |
236 | ||
237 | /* | |
238 | * Process the option... | |
239 | */ | |
240 | ||
241 | if (!_cups_strcasecmp(name, "waiteof")) | |
242 | { | |
243 | /* | |
244 | * Set the wait-for-eof value... | |
245 | */ | |
246 | ||
247 | waiteof = !value[0] || !_cups_strcasecmp(value, "on") || | |
248 | !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true"); | |
249 | } | |
250 | else if (!_cups_strcasecmp(name, "snmp")) | |
251 | { | |
252 | /* | |
253 | * Enable/disable SNMP stuff... | |
254 | */ | |
255 | ||
256 | snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") || | |
257 | _cups_strcasecmp(value, "yes") || | |
258 | _cups_strcasecmp(value, "true"); | |
259 | } | |
260 | else if (!_cups_strcasecmp(name, "contimeout")) | |
261 | { | |
262 | /* | |
263 | * Set the connection timeout... | |
264 | */ | |
265 | ||
266 | if (atoi(value) > 0) | |
267 | contimeout = atoi(value); | |
268 | } | |
269 | } | |
270 | } | |
271 | ||
272 | /* | |
273 | * Then try finding the remote host... | |
274 | */ | |
275 | ||
276 | start_time = time(NULL); | |
277 | ||
278 | sprintf(portname, "%d", port); | |
279 | ||
280 | fputs("STATE: +connecting-to-device\n", stderr); | |
281 | fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname); | |
282 | ||
283 | while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) | |
284 | { | |
285 | _cupsLangPrintFilter(stderr, "INFO", | |
286 | _("Unable to locate printer \"%s\"."), hostname); | |
287 | sleep(10); | |
288 | ||
289 | if (getenv("CLASS") != NULL) | |
290 | { | |
291 | fputs("STATE: -connecting-to-device\n", stderr); | |
292 | return (CUPS_BACKEND_STOP); | |
293 | } | |
294 | } | |
295 | ||
296 | /* | |
297 | * See if the printer supports SNMP... | |
298 | */ | |
299 | ||
300 | if (snmp_enabled) | |
301 | snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family); | |
302 | else | |
303 | snmp_fd = -1; | |
304 | ||
305 | if (snmp_fd >= 0) | |
306 | have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr), | |
307 | &start_count, NULL); | |
308 | else | |
309 | have_supplies = start_count = 0; | |
310 | ||
311 | /* | |
312 | * Wait for data from the filter... | |
313 | */ | |
314 | ||
315 | if (print_fd == 0) | |
316 | { | |
317 | if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 1, backendNetworkSideCB)) | |
318 | return (CUPS_BACKEND_OK); | |
319 | else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0) | |
320 | return (CUPS_BACKEND_OK); | |
321 | } | |
322 | ||
323 | /* | |
324 | * Connect to the printer... | |
325 | */ | |
326 | ||
327 | fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port); | |
328 | _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer.")); | |
329 | ||
330 | for (delay = 5;;) | |
331 | { | |
332 | if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL) | |
333 | { | |
334 | error = errno; | |
335 | device_fd = -1; | |
336 | ||
337 | if (getenv("CLASS") != NULL) | |
338 | { | |
339 | /* | |
340 | * If the CLASS environment variable is set, the job was submitted | |
341 | * to a class and not to a specific queue. In this case, we want | |
342 | * to abort immediately so that the job can be requeued on the next | |
343 | * available printer in the class. | |
344 | */ | |
345 | ||
346 | _cupsLangPrintFilter(stderr, "INFO", | |
347 | _("Unable to contact printer, queuing on next " | |
348 | "printer in class.")); | |
349 | ||
350 | /* | |
351 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
352 | */ | |
353 | ||
354 | sleep(5); | |
355 | ||
356 | return (CUPS_BACKEND_FAILED); | |
357 | } | |
358 | ||
359 | fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error)); | |
360 | ||
361 | if (error == ECONNREFUSED || error == EHOSTDOWN || | |
362 | error == EHOSTUNREACH) | |
363 | { | |
364 | if (contimeout && (time(NULL) - start_time) > contimeout) | |
365 | { | |
366 | _cupsLangPrintFilter(stderr, "ERROR", | |
367 | _("The printer is not responding.")); | |
368 | return (CUPS_BACKEND_FAILED); | |
369 | } | |
370 | ||
371 | switch (error) | |
372 | { | |
373 | case EHOSTDOWN : | |
374 | _cupsLangPrintFilter(stderr, "WARNING", | |
375 | _("The printer may not exist or " | |
376 | "is unavailable at this time.")); | |
377 | break; | |
378 | ||
379 | case EHOSTUNREACH : | |
380 | _cupsLangPrintFilter(stderr, "WARNING", | |
381 | _("The printer is unreachable at this " | |
382 | "time.")); | |
383 | break; | |
384 | ||
385 | case ECONNREFUSED : | |
386 | default : | |
387 | _cupsLangPrintFilter(stderr, "WARNING", | |
388 | _("The printer is in use.")); | |
389 | break; | |
390 | } | |
391 | ||
392 | sleep(delay); | |
393 | ||
394 | if (delay < 30) | |
395 | delay += 5; | |
396 | } | |
397 | else | |
398 | { | |
399 | _cupsLangPrintFilter(stderr, "ERROR", | |
400 | _("The printer is not responding.")); | |
401 | sleep(30); | |
402 | } | |
403 | } | |
404 | else | |
405 | break; | |
406 | } | |
407 | ||
408 | fputs("STATE: -connecting-to-device\n", stderr); | |
409 | _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer.")); | |
410 | ||
411 | fprintf(stderr, "DEBUG: Connected to %s:%d...\n", | |
412 | httpAddrString(&(addr->addr), addrname, sizeof(addrname)), | |
413 | _httpAddrPort(&(addr->addr))); | |
414 | ||
415 | /* | |
416 | * Print everything... | |
417 | */ | |
418 | ||
419 | tbytes = 0; | |
420 | ||
421 | if (bytes > 0) | |
422 | tbytes += write(device_fd, buffer, bytes); | |
423 | ||
424 | while (copies > 0 && tbytes >= 0) | |
425 | { | |
426 | copies --; | |
427 | ||
428 | if (print_fd != 0) | |
429 | { | |
430 | fputs("PAGE: 1 1\n", stderr); | |
431 | lseek(print_fd, 0, SEEK_SET); | |
432 | } | |
433 | ||
434 | tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addrlist->addr), 1, | |
435 | 0, backendNetworkSideCB); | |
436 | ||
437 | if (print_fd != 0 && tbytes >= 0) | |
438 | _cupsLangPrintFilter(stderr, "INFO", _("Print file sent.")); | |
439 | } | |
440 | ||
441 | #ifdef __APPLE__ | |
442 | /* | |
443 | * Wait up to 5 seconds to get any pending back-channel data... | |
444 | */ | |
445 | ||
446 | wait_time = time(NULL) + 5; | |
447 | while (wait_time >= time(¤t_time)) | |
448 | if (wait_bc(device_fd, wait_time - current_time) <= 0) | |
449 | break; | |
450 | #endif /* __APPLE__ */ | |
451 | ||
452 | if (waiteof) | |
453 | { | |
454 | /* | |
455 | * Shutdown the socket and wait for the other end to finish... | |
456 | */ | |
457 | ||
458 | _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to finish.")); | |
459 | ||
460 | shutdown(device_fd, 1); | |
461 | ||
462 | while (wait_bc(device_fd, 90) > 0); | |
463 | } | |
464 | ||
465 | /* | |
466 | * Collect the final page count as needed... | |
467 | */ | |
468 | ||
469 | if (have_supplies && | |
470 | !backendSNMPSupplies(snmp_fd, &(addrlist->addr), &page_count, NULL) && | |
471 | page_count > start_count) | |
472 | fprintf(stderr, "PAGE: total %d\n", page_count - start_count); | |
473 | ||
474 | /* | |
475 | * Close the socket connection... | |
476 | */ | |
477 | ||
478 | close(device_fd); | |
479 | ||
480 | httpAddrFreeList(addrlist); | |
481 | ||
482 | /* | |
483 | * Close the input file and return... | |
484 | */ | |
485 | ||
486 | if (print_fd != 0) | |
487 | close(print_fd); | |
488 | ||
489 | return (CUPS_BACKEND_OK); | |
490 | } | |
491 | ||
492 | ||
493 | /* | |
494 | * 'wait_bc()' - Wait for back-channel data... | |
495 | */ | |
496 | ||
497 | static int /* O - # bytes read or -1 on error */ | |
498 | wait_bc(int device_fd, /* I - Socket */ | |
499 | int secs) /* I - Seconds to wait */ | |
500 | { | |
501 | struct timeval timeout; /* Timeout for select() */ | |
502 | fd_set input; /* Input set for select() */ | |
503 | ssize_t bytes; /* Number of back-channel bytes read */ | |
504 | char buffer[1024]; /* Back-channel buffer */ | |
505 | ||
506 | ||
507 | /* | |
508 | * Wait up to "secs" seconds for backchannel data... | |
509 | */ | |
510 | ||
511 | timeout.tv_sec = secs; | |
512 | timeout.tv_usec = 0; | |
513 | ||
514 | FD_ZERO(&input); | |
515 | FD_SET(device_fd, &input); | |
516 | ||
517 | if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0) | |
518 | { | |
519 | /* | |
520 | * Grab the data coming back and spit it out to stderr... | |
521 | */ | |
522 | ||
523 | if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0) | |
524 | { | |
525 | fprintf(stderr, "DEBUG: Received %d bytes of back-channel data\n", | |
526 | (int)bytes); | |
527 | cupsBackChannelWrite(buffer, bytes, 1.0); | |
528 | } | |
529 | ||
530 | return (bytes); | |
531 | } | |
532 | else | |
533 | return (-1); | |
534 | } | |
535 | ||
536 | ||
537 | /* | |
538 | * End of "$Id: socket.c 7881 2008-08-28 20:21:56Z mike $". | |
539 | */ |