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