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