Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / backend / socket.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: socket.c 7583 2008-05-16 17:47:16Z 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.
323c5de1 20 * wait_bc() - Wait for back-channel data...
ef416fc2 21 */
22
23/*
24 * Include necessary headers.
25 */
26
ef416fc2 27#include <cups/http-private.h>
ed486911 28#include "backend-private.h"
ef416fc2 29#include <stdarg.h>
ef416fc2 30#include <sys/types.h>
31#include <sys/stat.h>
ef416fc2 32
33#ifdef WIN32
34# include <winsock.h>
35#else
36# include <unistd.h>
37# include <fcntl.h>
38# include <sys/socket.h>
39# include <netinet/in.h>
40# include <arpa/inet.h>
41# include <netdb.h>
42#endif /* WIN32 */
43
44
f7deaa1a 45/*
46 * Local functions...
47 */
48
323c5de1 49static int wait_bc(int device_fd, int secs);
f7deaa1a 50
51
ef416fc2 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
60int /* O - Exit status */
61main(int argc, /* I - Number of command-line arguments (6 or 7) */
62 char *argv[]) /* I - Command-line arguments */
63{
1f0275e3 64 const char *device_uri; /* Device URI */
ef416fc2 65 char method[255], /* Method in URI */
66 hostname[1024], /* Hostname */
67 username[255], /* Username info (not used) */
fa73b229 68 resource[1024], /* Resource info (not used) */
69 *options, /* Pointer to options */
db1f069b
MS
70 *name, /* Name of option */
71 *value, /* Value of option */
72 sep; /* Option separator */
ed486911 73 int print_fd; /* Print file */
ef416fc2 74 int copies; /* Number of copies to print */
5f64df29
MS
75 time_t start_time, /* Time of first connect */
76 wait_time; /* Time to wait before shutting down socket */
c0e1af83 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 */
568fa3fa
MS
88 int snmp_fd, /* SNMP socket */
89 start_count, /* Page count via SNMP at start */
90 page_count; /* Page count via SNMP */
ed486911 91 ssize_t tbytes; /* Total number of bytes written */
ef416fc2 92#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
93 struct sigaction action; /* Actions for POSIX signals */
94#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
95
96
97 /*
98 * Make sure status messages are not buffered...
99 */
100
101 setbuf(stderr, NULL);
102
103 /*
104 * Ignore SIGPIPE signals...
105 */
106
107#ifdef HAVE_SIGSET
108 sigset(SIGPIPE, SIG_IGN);
109#elif defined(HAVE_SIGACTION)
110 memset(&action, 0, sizeof(action));
111 action.sa_handler = SIG_IGN;
112 sigaction(SIGPIPE, &action, NULL);
113#else
114 signal(SIGPIPE, SIG_IGN);
115#endif /* HAVE_SIGSET */
116
117 /*
118 * Check command-line...
119 */
120
121 if (argc == 1)
122 {
123 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
124 return (CUPS_BACKEND_OK);
125 }
126 else if (argc < 6 || argc > 7)
127 {
db1f069b
MS
128 _cupsLangPrintf(stderr,
129 _("Usage: %s job-id user title copies options [file]\n"),
130 argv[0]);
ef416fc2 131 return (CUPS_BACKEND_FAILED);
132 }
133
134 /*
135 * If we have 7 arguments, print the file named on the command-line.
136 * Otherwise, send stdin instead...
137 */
138
139 if (argc == 6)
140 {
ed486911 141 print_fd = 0;
142 copies = 1;
ef416fc2 143 }
144 else
145 {
146 /*
147 * Try to open the print file...
148 */
149
ed486911 150 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
ef416fc2 151 {
080811b1
MS
152 _cupsLangPrintf(stderr,
153 _("ERROR: Unable to open print file \"%s\": %s\n"),
154 argv[6], strerror(errno));
ef416fc2 155 return (CUPS_BACKEND_FAILED);
156 }
157
158 copies = atoi(argv[4]);
159 }
160
161 /*
162 * Extract the hostname and port number from the URI...
163 */
164
1f0275e3
MS
165 if ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
166 return (CUPS_BACKEND_FAILED);
167
168 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri,
a4d04587 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
db1f069b 202 name = options;
fa73b229 203
db1f069b
MS
204 while (*options && *options != '=' && *options != '+' && *options != '&')
205 options ++;
206
207 if ((sep = *options) != '\0')
208 *options++ = '\0';
209
210 if (sep == '=')
fa73b229 211 {
212 /*
213 * Get the value...
214 */
215
db1f069b 216 value = options;
fa73b229 217
db1f069b 218 while (*options && *options != '+' && *options != '&')
fa73b229 219 options ++;
db1f069b
MS
220
221 if (*options)
222 *options++ = '\0';
fa73b229 223 }
224 else
db1f069b 225 value = (char *)"";
fa73b229 226
227 /*
228 * Process the option...
229 */
230
231 if (!strcasecmp(name, "waiteof"))
232 {
233 /*
234 * Set the wait-for-eof value...
235 */
236
237 waiteof = !value[0] || !strcasecmp(value, "on") ||
238 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
239 }
c0e1af83 240 else if (!strcasecmp(name, "contimeout"))
241 {
242 /*
243 * Set the connection timeout...
244 */
245
246 if (atoi(value) > 0)
247 contimeout = atoi(value);
248 }
fa73b229 249 }
250 }
251
ef416fc2 252 /*
253 * Then try to connect to the remote host...
254 */
255
c0e1af83 256 recoverable = 0;
257 start_time = time(NULL);
258
ef416fc2 259 sprintf(portname, "%d", port);
260
261 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
262 {
db1f069b
MS
263 _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"),
264 hostname);
ef416fc2 265 return (CUPS_BACKEND_STOP);
266 }
267
db1f069b
MS
268 _cupsLangPrintf(stderr,
269 _("INFO: Attempting to connect to host %s on port %d\n"),
270 hostname, port);
ef416fc2 271
ed486911 272 fputs("STATE: +connecting-to-device\n", stderr);
ef416fc2 273
ed486911 274 for (delay = 5;;)
ef416fc2 275 {
26d47ec6 276 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
ef416fc2 277 {
ed486911 278 error = errno;
279 device_fd = -1;
ef416fc2 280
ed486911 281 if (getenv("CLASS") != NULL)
282 {
283 /*
284 * If the CLASS environment variable is set, the job was submitted
285 * to a class and not to a specific queue. In this case, we want
286 * to abort immediately so that the job can be requeued on the next
287 * available printer in the class.
288 */
ef416fc2 289
db1f069b
MS
290 _cupsLangPuts(stderr,
291 _("INFO: Unable to contact printer, queuing on next "
292 "printer in class...\n"));
ef416fc2 293
ed486911 294 /*
295 * Sleep 5 seconds to keep the job from requeuing too rapidly...
296 */
ef416fc2 297
ed486911 298 sleep(5);
ef416fc2 299
ed486911 300 return (CUPS_BACKEND_FAILED);
301 }
ef416fc2 302
ed486911 303 if (error == ECONNREFUSED || error == EHOSTDOWN ||
304 error == EHOSTUNREACH)
305 {
c0e1af83 306 if (contimeout && (time(NULL) - start_time) > contimeout)
307 {
db1f069b 308 _cupsLangPuts(stderr, _("ERROR: Printer not responding!\n"));
c0e1af83 309 return (CUPS_BACKEND_FAILED);
310 }
311
312 recoverable = 1;
313
db1f069b
MS
314 _cupsLangPrintf(stderr,
315 _("WARNING: recoverable: Network host \'%s\' is busy; "
316 "will retry in %d seconds...\n"),
317 hostname, delay);
c0e1af83 318
ed486911 319 sleep(delay);
ef416fc2 320
ed486911 321 if (delay < 30)
322 delay += 5;
ef416fc2 323 }
324 else
ed486911 325 {
c0e1af83 326 recoverable = 1;
327
db1f069b
MS
328 _cupsLangPrintf(stderr, "DEBUG: Connection error: %s\n",
329 strerror(errno));
330 _cupsLangPuts(stderr,
331 _("ERROR: recoverable: Unable to connect to printer; "
332 "will retry in 30 seconds...\n"));
ed486911 333 sleep(30);
334 }
ef416fc2 335 }
ed486911 336 else
337 break;
338 }
ef416fc2 339
c0e1af83 340 if (recoverable)
341 {
342 /*
343 * If we've shown a recoverable error make sure the printer proxies
344 * have a chance to see the recovered message. Not pretty but
345 * necessary for now...
346 */
347
348 fputs("INFO: recovered: \n", stderr);
349 sleep(5);
350 }
351
ed486911 352 fputs("STATE: -connecting-to-device\n", stderr);
db1f069b 353 _cupsLangPrintf(stderr, _("INFO: Connected to %s...\n"), hostname);
26d47ec6 354
355#ifdef AF_INET6
356 if (addr->addr.addr.sa_family == AF_INET6)
357 fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
c0e1af83 358 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
359 ntohs(addr->addr.ipv6.sin6_port));
26d47ec6 360 else
361#endif /* AF_INET6 */
362 if (addr->addr.addr.sa_family == AF_INET)
363 fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4)...\n",
c0e1af83 364 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
365 ntohs(addr->addr.ipv4.sin_port));
ef416fc2 366
568fa3fa
MS
367 /*
368 * See if the printer supports SNMP...
369 */
370
7a14d768 371 if ((snmp_fd = _cupsSNMPOpen(addr->addr.addr.sa_family)) >= 0)
1f0275e3 372 {
568fa3fa
MS
373 if (backendSNMPSupplies(snmp_fd, &(addr->addr), &start_count, NULL))
374 {
375 /*
376 * No, close it...
377 */
378
7a14d768 379 _cupsSNMPClose(snmp_fd);
568fa3fa
MS
380 snmp_fd = -1;
381 }
1f0275e3
MS
382 }
383 else
384 start_count = 0;
568fa3fa 385
ed486911 386 /*
387 * Print everything...
388 */
ef416fc2 389
ed486911 390 tbytes = 0;
ef416fc2 391
ed486911 392 while (copies > 0 && tbytes >= 0)
393 {
ef416fc2 394 copies --;
395
ed486911 396 if (print_fd != 0)
ef416fc2 397 {
398 fputs("PAGE: 1 1\n", stderr);
ed486911 399 lseek(print_fd, 0, SEEK_SET);
ef416fc2 400 }
401
568fa3fa
MS
402 tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addr->addr), 1,
403 backendNetworkSideCB);
ef416fc2 404
ed486911 405 if (print_fd != 0 && tbytes >= 0)
db1f069b 406 _cupsLangPrintf(stderr,
c0e1af83 407#ifdef HAVE_LONG_LONG
db1f069b 408 _("INFO: Sent print file, %lld bytes...\n"),
c0e1af83 409#else
db1f069b 410 _("INFO: Sent print file, %ld bytes...\n"),
c0e1af83 411#endif /* HAVE_LONG_LONG */
db1f069b 412 CUPS_LLCAST tbytes);
ed486911 413 }
ef416fc2 414
323c5de1 415 /*
5f64df29 416 * Wait up to 5 seconds to get any pending back-channel data...
323c5de1 417 */
418
5f64df29
MS
419 wait_time = time(NULL) + 5;
420 while (wait_time >= time(NULL))
421 if (wait_bc(device_fd, 1) <= 0)
422 break;
323c5de1 423
ed486911 424 if (waiteof)
425 {
426 /*
427 * Shutdown the socket and wait for the other end to finish...
428 */
ef416fc2 429
db1f069b
MS
430 _cupsLangPuts(stderr,
431 _("INFO: Print file sent, waiting for printer to finish...\n"));
ef416fc2 432
ed486911 433 shutdown(device_fd, 1);
ef416fc2 434
323c5de1 435 while (wait_bc(device_fd, 90) > 0);
ed486911 436 }
ef416fc2 437
568fa3fa
MS
438 /*
439 * Collect the final page count as needed...
440 */
441
442 if (snmp_fd >= 0 &&
443 !backendSNMPSupplies(snmp_fd, &(addr->addr), &page_count, NULL) &&
444 page_count > start_count)
445 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
446
ed486911 447 /*
448 * Close the socket connection...
449 */
ef416fc2 450
ed486911 451 close(device_fd);
ef416fc2 452
453 httpAddrFreeList(addrlist);
454
455 /*
456 * Close the input file and return...
457 */
458
ed486911 459 if (print_fd != 0)
460 close(print_fd);
ef416fc2 461
ed486911 462 if (tbytes >= 0)
db1f069b 463 _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
ef416fc2 464
ed486911 465 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
ef416fc2 466}
467
468
f7deaa1a 469/*
323c5de1 470 * 'wait_bc()' - Wait for back-channel data...
471 */
472
473static int /* O - # bytes read or -1 on error */
474wait_bc(int device_fd, /* I - Socket */
475 int secs) /* I - Seconds to wait */
476{
477 struct timeval timeout; /* Timeout for select() */
478 fd_set input; /* Input set for select() */
479 ssize_t bytes; /* Number of back-channel bytes read */
480 char buffer[1024]; /* Back-channel buffer */
481
482
483 /*
484 * Wait up to "secs" seconds for backchannel data...
485 */
486
487 timeout.tv_sec = secs;
488 timeout.tv_usec = 0;
489
490 FD_ZERO(&input);
491 FD_SET(device_fd, &input);
492
493 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
494 {
495 /*
496 * Grab the data coming back and spit it out to stderr...
497 */
498
499 if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0)
500 {
501 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
502 (int)bytes);
503 cupsBackChannelWrite(buffer, bytes, 1.0);
504 }
505
506 return (bytes);
507 }
508 else
509 return (-1);
510}
511
512
513/*
75bd9771 514 * End of "$Id: socket.c 7583 2008-05-16 17:47:16Z mike $".
ef416fc2 515 */