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