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