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