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