]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/socket.c
Import CUPS 1.4svn-r7170.
[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 *
bc44d920 6 * Copyright 2007 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 {
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
a4d04587 160 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
161 method, sizeof(method), username, sizeof(username),
162 hostname, sizeof(hostname), &port,
ef416fc2 163 resource, sizeof(resource));
164
165 if (port == 0)
166 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
167
fa73b229 168 /*
169 * Get options, if any...
170 */
171
c0e1af83 172 waiteof = 1;
173 contimeout = 7 * 24 * 60 * 60;
fa73b229 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
db1f069b 194 name = options;
fa73b229 195
db1f069b
MS
196 while (*options && *options != '=' && *options != '+' && *options != '&')
197 options ++;
198
199 if ((sep = *options) != '\0')
200 *options++ = '\0';
201
202 if (sep == '=')
fa73b229 203 {
204 /*
205 * Get the value...
206 */
207
db1f069b 208 value = options;
fa73b229 209
db1f069b 210 while (*options && *options != '+' && *options != '&')
fa73b229 211 options ++;
db1f069b
MS
212
213 if (*options)
214 *options++ = '\0';
fa73b229 215 }
216 else
db1f069b 217 value = (char *)"";
fa73b229 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 }
c0e1af83 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 }
fa73b229 241 }
242 }
243
ef416fc2 244 /*
245 * Then try to connect to the remote host...
246 */
247
c0e1af83 248 recoverable = 0;
249 start_time = time(NULL);
250
ef416fc2 251 sprintf(portname, "%d", port);
252
253 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
254 {
db1f069b
MS
255 _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"),
256 hostname);
ef416fc2 257 return (CUPS_BACKEND_STOP);
258 }
259
db1f069b
MS
260 _cupsLangPrintf(stderr,
261 _("INFO: Attempting to connect to host %s on port %d\n"),
262 hostname, port);
ef416fc2 263
ed486911 264 fputs("STATE: +connecting-to-device\n", stderr);
ef416fc2 265
ed486911 266 for (delay = 5;;)
ef416fc2 267 {
26d47ec6 268 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
ef416fc2 269 {
ed486911 270 error = errno;
271 device_fd = -1;
ef416fc2 272
ed486911 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 */
ef416fc2 281
db1f069b
MS
282 _cupsLangPuts(stderr,
283 _("INFO: Unable to contact printer, queuing on next "
284 "printer in class...\n"));
ef416fc2 285
ed486911 286 /*
287 * Sleep 5 seconds to keep the job from requeuing too rapidly...
288 */
ef416fc2 289
ed486911 290 sleep(5);
ef416fc2 291
ed486911 292 return (CUPS_BACKEND_FAILED);
293 }
ef416fc2 294
ed486911 295 if (error == ECONNREFUSED || error == EHOSTDOWN ||
296 error == EHOSTUNREACH)
297 {
c0e1af83 298 if (contimeout && (time(NULL) - start_time) > contimeout)
299 {
db1f069b 300 _cupsLangPuts(stderr, _("ERROR: Printer not responding!\n"));
c0e1af83 301 return (CUPS_BACKEND_FAILED);
302 }
303
304 recoverable = 1;
305
db1f069b
MS
306 _cupsLangPrintf(stderr,
307 _("WARNING: recoverable: Network host \'%s\' is busy; "
308 "will retry in %d seconds...\n"),
309 hostname, delay);
c0e1af83 310
ed486911 311 sleep(delay);
ef416fc2 312
ed486911 313 if (delay < 30)
314 delay += 5;
ef416fc2 315 }
316 else
ed486911 317 {
c0e1af83 318 recoverable = 1;
319
db1f069b
MS
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"));
ed486911 325 sleep(30);
326 }
ef416fc2 327 }
ed486911 328 else
329 break;
330 }
ef416fc2 331
c0e1af83 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
ed486911 344 fputs("STATE: -connecting-to-device\n", stderr);
db1f069b 345 _cupsLangPrintf(stderr, _("INFO: Connected to %s...\n"), hostname);
26d47ec6 346
347#ifdef AF_INET6
348 if (addr->addr.addr.sa_family == AF_INET6)
349 fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
c0e1af83 350 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
351 ntohs(addr->addr.ipv6.sin6_port));
26d47ec6 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",
c0e1af83 356 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
357 ntohs(addr->addr.ipv4.sin_port));
ef416fc2 358
ed486911 359 /*
360 * Print everything...
361 */
ef416fc2 362
ed486911 363 tbytes = 0;
ef416fc2 364
ed486911 365 while (copies > 0 && tbytes >= 0)
366 {
ef416fc2 367 copies --;
368
ed486911 369 if (print_fd != 0)
ef416fc2 370 {
371 fputs("PAGE: 1 1\n", stderr);
ed486911 372 lseek(print_fd, 0, SEEK_SET);
ef416fc2 373 }
374
f7deaa1a 375 tbytes = backendRunLoop(print_fd, device_fd, 1, side_cb);
ef416fc2 376
ed486911 377 if (print_fd != 0 && tbytes >= 0)
db1f069b 378 _cupsLangPrintf(stderr,
c0e1af83 379#ifdef HAVE_LONG_LONG
db1f069b 380 _("INFO: Sent print file, %lld bytes...\n"),
c0e1af83 381#else
db1f069b 382 _("INFO: Sent print file, %ld bytes...\n"),
c0e1af83 383#endif /* HAVE_LONG_LONG */
db1f069b 384 CUPS_LLCAST tbytes);
ed486911 385 }
ef416fc2 386
323c5de1 387 /*
388 * Get any pending back-channel data...
389 */
390
391 while (wait_bc(device_fd, 5) > 0);
392
ed486911 393 if (waiteof)
394 {
395 /*
396 * Shutdown the socket and wait for the other end to finish...
397 */
ef416fc2 398
db1f069b
MS
399 _cupsLangPuts(stderr,
400 _("INFO: Print file sent, waiting for printer to finish...\n"));
ef416fc2 401
ed486911 402 shutdown(device_fd, 1);
ef416fc2 403
323c5de1 404 while (wait_bc(device_fd, 90) > 0);
ed486911 405 }
ef416fc2 406
ed486911 407 /*
408 * Close the socket connection...
409 */
ef416fc2 410
ed486911 411 close(device_fd);
ef416fc2 412
413 httpAddrFreeList(addrlist);
414
415 /*
416 * Close the input file and return...
417 */
418
ed486911 419 if (print_fd != 0)
420 close(print_fd);
ef416fc2 421
ed486911 422 if (tbytes >= 0)
db1f069b 423 _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
ef416fc2 424
ed486911 425 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
ef416fc2 426}
427
428
429/*
f7deaa1a 430 * 'side_cb()' - Handle side-channel requests...
431 */
432
433static void
434side_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 */
76cd9e37 442 const char *device_id; /* 1284DEVICEID env var */
f7deaa1a 443
444
445 datalen = sizeof(data);
446
447 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
448 {
db1f069b 449 _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n"));
f7deaa1a 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
09a101d6 460 if (backendDrainOutput(print_fd, device_fd))
461 status = CUPS_SC_STATUS_IO_ERROR;
462 else
463 status = CUPS_SC_STATUS_OK;
464
f7deaa1a 465 datalen = 0;
466 break;
467
468 case CUPS_SC_CMD_GET_BIDI :
469 data[0] = use_bc;
470 datalen = 1;
471 break;
472
76cd9e37
MS
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
f7deaa1a 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/*
323c5de1 492 * 'wait_bc()' - Wait for back-channel data...
493 */
494
495static int /* O - # bytes read or -1 on error */
496wait_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/*
2e4ff8af 536 * End of "$Id: socket.c 6910 2007-09-04 20:34:29Z mike $".
ef416fc2 537 */