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