]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/socket.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / socket.c
CommitLineData
ef416fc2 1/*
09a101d6 2 * "$Id: socket.c 6591 2007-06-21 20:35:28Z 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...
323c5de1 30 * wait_bc() - Wait for back-channel data...
ef416fc2 31 */
32
33/*
34 * Include necessary headers.
35 */
36
ef416fc2 37#include <cups/http-private.h>
ed486911 38#include "backend-private.h"
ef416fc2 39#include <stdarg.h>
ef416fc2 40#include <sys/types.h>
41#include <sys/stat.h>
ef416fc2 42
43#ifdef WIN32
44# include <winsock.h>
45#else
46# include <unistd.h>
47# include <fcntl.h>
48# include <sys/socket.h>
49# include <netinet/in.h>
50# include <arpa/inet.h>
51# include <netdb.h>
52#endif /* WIN32 */
53
54
f7deaa1a 55/*
56 * Local functions...
57 */
58
59static void side_cb(int print_fd, int device_fd, int use_bc);
323c5de1 60static int wait_bc(int device_fd, int secs);
f7deaa1a 61
62
ef416fc2 63/*
64 * 'main()' - Send a file to the printer or server.
65 *
66 * Usage:
67 *
68 * printer-uri job-id user title copies options [file]
69 */
70
71int /* O - Exit status */
72main(int argc, /* I - Number of command-line arguments (6 or 7) */
73 char *argv[]) /* I - Command-line arguments */
74{
75 char method[255], /* Method in URI */
76 hostname[1024], /* Hostname */
77 username[255], /* Username info (not used) */
fa73b229 78 resource[1024], /* Resource info (not used) */
79 *options, /* Pointer to options */
80 name[255], /* Name of option */
81 value[255], /* Value of option */
82 *ptr; /* Pointer into name or value */
ed486911 83 int print_fd; /* Print file */
ef416fc2 84 int copies; /* Number of copies to print */
c0e1af83 85 time_t start_time; /* Time of first connect */
86 int recoverable; /* Recoverable error shown? */
87 int contimeout; /* Connection timeout */
fa73b229 88 int waiteof; /* Wait for end-of-file? */
ef416fc2 89 int port; /* Port number */
90 char portname[255]; /* Port name */
91 int delay; /* Delay for retries... */
ed486911 92 int device_fd; /* AppSocket */
ef416fc2 93 int error; /* Error code (if any) */
26d47ec6 94 http_addrlist_t *addrlist, /* Address list */
95 *addr; /* Connected address */
96 char addrname[256]; /* Address name */
ed486911 97 ssize_t tbytes; /* Total number of bytes written */
ef416fc2 98#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
99 struct sigaction action; /* Actions for POSIX signals */
100#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
101
102
103 /*
104 * Make sure status messages are not buffered...
105 */
106
107 setbuf(stderr, NULL);
108
109 /*
110 * Ignore SIGPIPE signals...
111 */
112
113#ifdef HAVE_SIGSET
114 sigset(SIGPIPE, SIG_IGN);
115#elif defined(HAVE_SIGACTION)
116 memset(&action, 0, sizeof(action));
117 action.sa_handler = SIG_IGN;
118 sigaction(SIGPIPE, &action, NULL);
119#else
120 signal(SIGPIPE, SIG_IGN);
121#endif /* HAVE_SIGSET */
122
123 /*
124 * Check command-line...
125 */
126
127 if (argc == 1)
128 {
129 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
130 return (CUPS_BACKEND_OK);
131 }
132 else if (argc < 6 || argc > 7)
133 {
c0e1af83 134 fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
ef416fc2 135 argv[0]);
136 return (CUPS_BACKEND_FAILED);
137 }
138
139 /*
140 * If we have 7 arguments, print the file named on the command-line.
141 * Otherwise, send stdin instead...
142 */
143
144 if (argc == 6)
145 {
ed486911 146 print_fd = 0;
147 copies = 1;
ef416fc2 148 }
149 else
150 {
151 /*
152 * Try to open the print file...
153 */
154
ed486911 155 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
ef416fc2 156 {
157 perror("ERROR: unable to open print file");
158 return (CUPS_BACKEND_FAILED);
159 }
160
161 copies = atoi(argv[4]);
162 }
163
164 /*
165 * Extract the hostname and port number from the URI...
166 */
167
a4d04587 168 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
169 method, sizeof(method), username, sizeof(username),
170 hostname, sizeof(hostname), &port,
ef416fc2 171 resource, sizeof(resource));
172
173 if (port == 0)
174 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
175
fa73b229 176 /*
177 * Get options, if any...
178 */
179
c0e1af83 180 waiteof = 1;
181 contimeout = 7 * 24 * 60 * 60;
fa73b229 182
183 if ((options = strchr(resource, '?')) != NULL)
184 {
185 /*
186 * Yup, terminate the device name string and move to the first
187 * character of the options...
188 */
189
190 *options++ = '\0';
191
192 /*
193 * Parse options...
194 */
195
196 while (*options)
197 {
198 /*
199 * Get the name...
200 */
201
202 for (ptr = name; *options && *options != '=';)
203 if (ptr < (name + sizeof(name) - 1))
204 *ptr++ = *options++;
205 *ptr = '\0';
206
207 if (*options == '=')
208 {
209 /*
210 * Get the value...
211 */
212
213 options ++;
214
215 for (ptr = value; *options && *options != '+' && *options != '&';)
216 if (ptr < (value + sizeof(value) - 1))
217 *ptr++ = *options++;
218 *ptr = '\0';
219
220 if (*options == '+' || *options == '&')
221 options ++;
222 }
223 else
224 value[0] = '\0';
225
226 /*
227 * Process the option...
228 */
229
230 if (!strcasecmp(name, "waiteof"))
231 {
232 /*
233 * Set the wait-for-eof value...
234 */
235
236 waiteof = !value[0] || !strcasecmp(value, "on") ||
237 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
238 }
c0e1af83 239 else if (!strcasecmp(name, "contimeout"))
240 {
241 /*
242 * Set the connection timeout...
243 */
244
245 if (atoi(value) > 0)
246 contimeout = atoi(value);
247 }
fa73b229 248 }
249 }
250
ef416fc2 251 /*
252 * Then try to connect to the remote host...
253 */
254
c0e1af83 255 recoverable = 0;
256 start_time = time(NULL);
257
ef416fc2 258 sprintf(portname, "%d", port);
259
260 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
261 {
c0e1af83 262 fprintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"), hostname);
ef416fc2 263 return (CUPS_BACKEND_STOP);
264 }
265
c0e1af83 266 fprintf(stderr, _("INFO: Attempting to connect to host %s on port %d\n"),
ef416fc2 267 hostname, port);
268
ed486911 269 fputs("STATE: +connecting-to-device\n", stderr);
ef416fc2 270
ed486911 271 for (delay = 5;;)
ef416fc2 272 {
26d47ec6 273 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
ef416fc2 274 {
ed486911 275 error = errno;
276 device_fd = -1;
ef416fc2 277
ed486911 278 if (getenv("CLASS") != NULL)
279 {
280 /*
281 * If the CLASS environment variable is set, the job was submitted
282 * to a class and not to a specific queue. In this case, we want
283 * to abort immediately so that the job can be requeued on the next
284 * available printer in the class.
285 */
ef416fc2 286
c0e1af83 287 fputs(_("INFO: Unable to contact printer, queuing on next "
288 "printer in class...\n"), stderr);
ef416fc2 289
ed486911 290 /*
291 * Sleep 5 seconds to keep the job from requeuing too rapidly...
292 */
ef416fc2 293
ed486911 294 sleep(5);
ef416fc2 295
ed486911 296 return (CUPS_BACKEND_FAILED);
297 }
ef416fc2 298
ed486911 299 if (error == ECONNREFUSED || error == EHOSTDOWN ||
300 error == EHOSTUNREACH)
301 {
c0e1af83 302 if (contimeout && (time(NULL) - start_time) > contimeout)
303 {
304 fputs(_("ERROR: Printer not responding!\n"), stderr);
305 return (CUPS_BACKEND_FAILED);
306 }
307
308 recoverable = 1;
309
ed486911 310 fprintf(stderr,
c0e1af83 311 _("WARNING: recoverable: Network host \'%s\' is busy; will "
312 "retry in %d seconds...\n"),
313 hostname, delay);
314
ed486911 315 sleep(delay);
ef416fc2 316
ed486911 317 if (delay < 30)
318 delay += 5;
ef416fc2 319 }
320 else
ed486911 321 {
c0e1af83 322 recoverable = 1;
323
324 fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
325 fputs(_("ERROR: recoverable: Unable to connect to printer; will "
326 "retry in 30 seconds...\n"), stderr);
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);
c0e1af83 347 fprintf(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)
c0e1af83 380 fprintf(stderr,
381#ifdef HAVE_LONG_LONG
382 _("INFO: Sent print file, %lld bytes...\n"),
383#else
384 _("INFO: Sent print file, %ld bytes...\n"),
385#endif /* HAVE_LONG_LONG */
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
c0e1af83 401 fputs(_("INFO: Print file sent, waiting for printer to finish...\n"),
402 stderr);
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)
c0e1af83 425 fputs(_("INFO: Ready to print.\n"), stderr);
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 */
444
445
446 datalen = sizeof(data);
447
448 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
449 {
c0e1af83 450 fputs(_("WARNING: Failed to read side-channel request!\n"), stderr);
f7deaa1a 451 return;
452 }
453
454 switch (command)
455 {
456 case CUPS_SC_CMD_DRAIN_OUTPUT :
457 /*
458 * Our sockets disable the Nagle algorithm and data is sent immediately.
459 */
460
09a101d6 461 if (backendDrainOutput(print_fd, device_fd))
462 status = CUPS_SC_STATUS_IO_ERROR;
463 else
464 status = CUPS_SC_STATUS_OK;
465
f7deaa1a 466 datalen = 0;
467 break;
468
469 case CUPS_SC_CMD_GET_BIDI :
470 data[0] = use_bc;
471 datalen = 1;
472 break;
473
474 default :
475 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
476 datalen = 0;
477 break;
478 }
479
480 cupsSideChannelWrite(command, status, data, datalen, 1.0);
481}
482
483
484/*
323c5de1 485 * 'wait_bc()' - Wait for back-channel data...
486 */
487
488static int /* O - # bytes read or -1 on error */
489wait_bc(int device_fd, /* I - Socket */
490 int secs) /* I - Seconds to wait */
491{
492 struct timeval timeout; /* Timeout for select() */
493 fd_set input; /* Input set for select() */
494 ssize_t bytes; /* Number of back-channel bytes read */
495 char buffer[1024]; /* Back-channel buffer */
496
497
498 /*
499 * Wait up to "secs" seconds for backchannel data...
500 */
501
502 timeout.tv_sec = secs;
503 timeout.tv_usec = 0;
504
505 FD_ZERO(&input);
506 FD_SET(device_fd, &input);
507
508 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
509 {
510 /*
511 * Grab the data coming back and spit it out to stderr...
512 */
513
514 if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0)
515 {
516 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
517 (int)bytes);
518 cupsBackChannelWrite(buffer, bytes, 1.0);
519 }
520
521 return (bytes);
522 }
523 else
524 return (-1);
525}
526
527
528/*
09a101d6 529 * End of "$Id: socket.c 6591 2007-06-21 20:35:28Z mike $".
ef416fc2 530 */