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