]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/socket.c
Changelog.
[thirdparty/cups.git] / backend / socket.c
CommitLineData
43e2fc22 1/*
b2e10895 2 * "$Id$"
43e2fc22 3 *
7b64ad64 4 * AppSocket backend for CUPS.
43e2fc22 5 *
955a918d 6 * Copyright 2007-2012 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 */
9fa9ba8f 65 char scheme[255], /* Scheme in URI */
5baa731f 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 */
e3682167 75 time_t start_time; /* Time of first connect */
ff0295f0 76 int contimeout; /* Connection timeout */
41ed0c21 77 int waiteof; /* Wait for end-of-file? */
5baa731f 78 int port; /* Port number */
086c584d 79 char portname[255]; /* Port name */
5baa731f 80 int delay; /* Delay for retries... */
43b5ad0d 81 int device_fd; /* AppSocket */
5baa731f 82 int error; /* Error code (if any) */
a0c0bbe7 83 http_addrlist_t *addrlist, /* Address list */
abd1f85f 84 *addr; /* Connected address */
a0c0bbe7 85 char addrname[256]; /* Address name */
bd08ee89 86 int snmp_enabled = 1; /* Is SNMP enabled? */
5ba153ab 87 int snmp_fd, /* SNMP socket */
88 start_count, /* Page count via SNMP at start */
6bffe4a6 89 page_count, /* Page count via SNMP */
90 have_supplies; /* Printer supports supply levels? */
0663fa1c 91 ssize_t bytes = 0, /* Initial bytes read */
92 tbytes; /* Total number of bytes written */
7b64ad64 93 char buffer[1024]; /* Initial print buffer */
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,
4c4eea89 132 _("Usage: %s job-id user title copies options [file]"),
472af6f3 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 {
bd2147fb 155 _cupsLangPrintError("ERROR", _("Unable to open print file"));
6248387b 156 return (CUPS_BACKEND_FAILED);
e73c6c0a 157 }
3f9cb6c6 158
159 copies = atoi(argv[4]);
e73c6c0a 160 }
161
162 /*
163 * Extract the hostname and port number from the URI...
164 */
165
04c7bec1 166 while ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
167 {
168 _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer."));
169 sleep(10);
170
171 if (getenv("CLASS") != NULL)
172 return (CUPS_BACKEND_FAILED);
173 }
cda2b561 174
9fa9ba8f 175 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
176 username, sizeof(username), hostname, sizeof(hostname), &port,
0e66904d 177 resource, sizeof(resource));
e73c6c0a 178
179 if (port == 0)
180 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
181
41ed0c21 182 /*
183 * Get options, if any...
184 */
185
ff0295f0 186 waiteof = 1;
187 contimeout = 7 * 24 * 60 * 60;
41ed0c21 188
189 if ((options = strchr(resource, '?')) != NULL)
190 {
191 /*
192 * Yup, terminate the device name string and move to the first
193 * character of the options...
194 */
195
196 *options++ = '\0';
197
198 /*
199 * Parse options...
200 */
201
202 while (*options)
203 {
204 /*
205 * Get the name...
206 */
207
abd1f85f 208 name = options;
41ed0c21 209
abd1f85f 210 while (*options && *options != '=' && *options != '+' && *options != '&')
211 options ++;
212
213 if ((sep = *options) != '\0')
214 *options++ = '\0';
215
216 if (sep == '=')
41ed0c21 217 {
218 /*
219 * Get the value...
220 */
221
abd1f85f 222 value = options;
41ed0c21 223
abd1f85f 224 while (*options && *options != '+' && *options != '&')
41ed0c21 225 options ++;
abd1f85f 226
227 if (*options)
228 *options++ = '\0';
41ed0c21 229 }
230 else
abd1f85f 231 value = (char *)"";
41ed0c21 232
233 /*
234 * Process the option...
235 */
236
c6fab96f 237 if (!_cups_strcasecmp(name, "waiteof"))
41ed0c21 238 {
239 /*
240 * Set the wait-for-eof value...
241 */
242
c6fab96f 243 waiteof = !value[0] || !_cups_strcasecmp(value, "on") ||
244 !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true");
41ed0c21 245 }
bd08ee89 246 else if (!_cups_strcasecmp(name, "snmp"))
247 {
248 /*
249 * Enable/disable SNMP stuff...
250 */
251
252 snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") ||
253 _cups_strcasecmp(value, "yes") ||
254 _cups_strcasecmp(value, "true");
255 }
c6fab96f 256 else if (!_cups_strcasecmp(name, "contimeout"))
ff0295f0 257 {
258 /*
259 * Set the connection timeout...
260 */
261
262 if (atoi(value) > 0)
263 contimeout = atoi(value);
264 }
41ed0c21 265 }
266 }
267
e73c6c0a 268 /*
cce0044f 269 * Then try finding the remote host...
e73c6c0a 270 */
271
f831f3d8 272 start_time = time(NULL);
ff0295f0 273
086c584d 274 sprintf(portname, "%d", port);
275
9fa9ba8f 276 fputs("STATE: +connecting-to-device\n", stderr);
bd8ccc10 277 fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
278
04c7bec1 279 while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
e73c6c0a 280 {
04c7bec1 281 _cupsLangPrintFilter(stderr, "INFO",
bd2147fb 282 _("Unable to locate printer \"%s\"."), hostname);
04c7bec1 283 sleep(10);
284
285 if (getenv("CLASS") != NULL)
cce0044f 286 {
287 fputs("STATE: -connecting-to-device\n", stderr);
04c7bec1 288 return (CUPS_BACKEND_STOP);
cce0044f 289 }
290 }
291
292 /*
293 * See if the printer supports SNMP...
294 */
295
bd08ee89 296 if (snmp_enabled)
297 snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family);
298 else
299 snmp_fd = -1;
300
301 if (snmp_fd >= 0)
1109fd69 302 have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr),
303 &start_count, NULL);
cce0044f 304 else
305 have_supplies = start_count = 0;
306
307 /*
308 * Wait for data from the filter...
309 */
310
311 if (print_fd == 0)
0663fa1c 312 {
ad6d7ec9 313 if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 1, backendNetworkSideCB))
cce0044f 314 return (CUPS_BACKEND_OK);
0663fa1c 315 else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0)
316 return (CUPS_BACKEND_OK);
317 }
cce0044f 318
319 /*
320 * Connect to the printer...
321 */
e73c6c0a 322
9fa9ba8f 323 fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
bd2147fb 324 _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer."));
e73c6c0a 325
43b5ad0d 326 for (delay = 5;;)
e73c6c0a 327 {
a0c0bbe7 328 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
e73c6c0a 329 {
43b5ad0d 330 error = errno;
331 device_fd = -1;
3f9cb6c6 332
43b5ad0d 333 if (getenv("CLASS") != NULL)
334 {
335 /*
336 * If the CLASS environment variable is set, the job was submitted
337 * to a class and not to a specific queue. In this case, we want
338 * to abort immediately so that the job can be requeued on the next
339 * available printer in the class.
340 */
b7b63780 341
2e3228ef 342 _cupsLangPrintFilter(stderr, "INFO",
343 _("Unable to contact printer, queuing on next "
bd2147fb 344 "printer in class."));
b7b63780 345
43b5ad0d 346 /*
347 * Sleep 5 seconds to keep the job from requeuing too rapidly...
348 */
b7b63780 349
43b5ad0d 350 sleep(5);
b7b63780 351
43b5ad0d 352 return (CUPS_BACKEND_FAILED);
353 }
b7b63780 354
03a8680a 355 fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error));
356
43b5ad0d 357 if (error == ECONNREFUSED || error == EHOSTDOWN ||
358 error == EHOSTUNREACH)
359 {
ff0295f0 360 if (contimeout && (time(NULL) - start_time) > contimeout)
361 {
2e3228ef 362 _cupsLangPrintFilter(stderr, "ERROR",
bd2147fb 363 _("The printer is not responding."));
ff0295f0 364 return (CUPS_BACKEND_FAILED);
365 }
366
03a8680a 367 switch (error)
368 {
369 case EHOSTDOWN :
2e3228ef 370 _cupsLangPrintFilter(stderr, "WARNING",
7cfa02ab 371 _("The printer may not exist or "
372 "is unavailable at this time."));
03a8680a 373 break;
374
375 case EHOSTUNREACH :
2e3228ef 376 _cupsLangPrintFilter(stderr, "WARNING",
7cfa02ab 377 _("The printer is unreachable at this "
378 "time."));
03a8680a 379 break;
380
381 case ECONNREFUSED :
382 default :
2e3228ef 383 _cupsLangPrintFilter(stderr, "WARNING",
d326343a 384 _("The printer is in use."));
03a8680a 385 break;
386 }
ff0295f0 387
43b5ad0d 388 sleep(delay);
10f725af 389
43b5ad0d 390 if (delay < 30)
391 delay += 5;
e73c6c0a 392 }
3f9cb6c6 393 else
43b5ad0d 394 {
2e3228ef 395 _cupsLangPrintFilter(stderr, "ERROR",
7cfa02ab 396 _("The printer is not responding."));
43b5ad0d 397 sleep(30);
398 }
e73c6c0a 399 }
43b5ad0d 400 else
401 break;
402 }
e73c6c0a 403
43b5ad0d 404 fputs("STATE: -connecting-to-device\n", stderr);
bd2147fb 405 _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer."));
a0c0bbe7 406
f0aa54a1 407 fprintf(stderr, "DEBUG: Connected to %s:%d...\n",
408 httpAddrString(&(addr->addr), addrname, sizeof(addrname)),
2d3aac27 409 httpAddrPort(&(addr->addr)));
4ff40357 410
43b5ad0d 411 /*
412 * Print everything...
413 */
4ff40357 414
43b5ad0d 415 tbytes = 0;
e73c6c0a 416
0663fa1c 417 if (bytes > 0)
418 tbytes += write(device_fd, buffer, bytes);
419
43b5ad0d 420 while (copies > 0 && tbytes >= 0)
421 {
3f9cb6c6 422 copies --;
7c88bf41 423
43b5ad0d 424 if (print_fd != 0)
e73c6c0a 425 {
3f9cb6c6 426 fputs("PAGE: 1 1\n", stderr);
43b5ad0d 427 lseek(print_fd, 0, SEEK_SET);
e73c6c0a 428 }
e73c6c0a 429
1109fd69 430 tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addrlist->addr), 1,
431 0, backendNetworkSideCB);
5baa731f 432
43b5ad0d 433 if (print_fd != 0 && tbytes >= 0)
bd2147fb 434 _cupsLangPrintFilter(stderr, "INFO", _("Print file sent."));
43b5ad0d 435 }
3f9cb6c6 436
43b5ad0d 437 if (waiteof)
438 {
439 /*
440 * Shutdown the socket and wait for the other end to finish...
441 */
3f9cb6c6 442
bd2147fb 443 _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to finish."));
0cd4970e 444
43b5ad0d 445 shutdown(device_fd, 1);
3f9cb6c6 446
313509aa 447 while (wait_bc(device_fd, 90) > 0);
43b5ad0d 448 }
b5cb0608 449
5ba153ab 450 /*
451 * Collect the final page count as needed...
452 */
453
1109fd69 454 if (have_supplies &&
455 !backendSNMPSupplies(snmp_fd, &(addrlist->addr), &page_count, NULL) &&
5ba153ab 456 page_count > start_count)
457 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
458
43b5ad0d 459 /*
460 * Close the socket connection...
461 */
3f9cb6c6 462
43b5ad0d 463 close(device_fd);
e73c6c0a 464
086c584d 465 httpAddrFreeList(addrlist);
466
e73c6c0a 467 /*
3f9cb6c6 468 * Close the input file and return...
e73c6c0a 469 */
470
43b5ad0d 471 if (print_fd != 0)
472 close(print_fd);
e73c6c0a 473
cce0044f 474 return (CUPS_BACKEND_OK);
e73c6c0a 475}
43e2fc22 476
477
313509aa 478/*
479 * 'wait_bc()' - Wait for back-channel data...
480 */
481
482static int /* O - # bytes read or -1 on error */
483wait_bc(int device_fd, /* I - Socket */
484 int secs) /* I - Seconds to wait */
485{
486 struct timeval timeout; /* Timeout for select() */
487 fd_set input; /* Input set for select() */
488 ssize_t bytes; /* Number of back-channel bytes read */
489 char buffer[1024]; /* Back-channel buffer */
490
491
492 /*
493 * Wait up to "secs" seconds for backchannel data...
494 */
495
496 timeout.tv_sec = secs;
497 timeout.tv_usec = 0;
498
499 FD_ZERO(&input);
500 FD_SET(device_fd, &input);
501
502 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
503 {
504 /*
505 * Grab the data coming back and spit it out to stderr...
506 */
507
508 if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0)
509 {
157fcea9 510 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data\n",
313509aa 511 (int)bytes);
512 cupsBackChannelWrite(buffer, bytes, 1.0);
513 }
514
515 return (bytes);
516 }
517 else
518 return (-1);
519}
520
521
b2e10895 522/*
523 * End of "$Id$".
43e2fc22 524 */