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