]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/socket.c
Merge changes from CUPS 1.4svn-r8722 (tentative 1.4.0 GM)
[thirdparty/cups.git] / backend / socket.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: socket.c 7881 2008-08-28 20:21:56Z mike $"
ef416fc2 3 *
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
5 *
acb056cb 6 * Copyright 2007-2009 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 */
acb056cb 65 char scheme[255], /* Scheme in URI */
ef416fc2 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 75 time_t start_time, /* Time of first connect */
9a4f8274 76 current_time, /* Current time */
5f64df29 77 wait_time; /* Time to wait before shutting down socket */
c0e1af83 78 int recoverable; /* Recoverable error shown? */
79 int contimeout; /* Connection timeout */
fa73b229 80 int waiteof; /* Wait for end-of-file? */
ef416fc2 81 int port; /* Port number */
82 char portname[255]; /* Port name */
83 int delay; /* Delay for retries... */
ed486911 84 int device_fd; /* AppSocket */
ef416fc2 85 int error; /* Error code (if any) */
26d47ec6 86 http_addrlist_t *addrlist, /* Address list */
db1f069b 87 *addr; /* Connected address */
26d47ec6 88 char addrname[256]; /* Address name */
568fa3fa
MS
89 int snmp_fd, /* SNMP socket */
90 start_count, /* Page count via SNMP at start */
426c6a59
MS
91 page_count, /* Page count via SNMP */
92 have_supplies; /* Printer supports supply levels? */
ed486911 93 ssize_t tbytes; /* Total number of bytes written */
ef416fc2 94#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
95 struct sigaction action; /* Actions for POSIX signals */
96#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
97
98
99 /*
100 * Make sure status messages are not buffered...
101 */
102
103 setbuf(stderr, NULL);
104
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
119 /*
120 * Check command-line...
121 */
122
123 if (argc == 1)
124 {
8b450588
MS
125 printf("network socket \"Unknown\" \"%s\"\n",
126 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
ef416fc2 127 return (CUPS_BACKEND_OK);
128 }
129 else if (argc < 6 || argc > 7)
130 {
db1f069b
MS
131 _cupsLangPrintf(stderr,
132 _("Usage: %s job-id user title copies options [file]\n"),
133 argv[0]);
ef416fc2 134 return (CUPS_BACKEND_FAILED);
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)
143 {
ed486911 144 print_fd = 0;
145 copies = 1;
ef416fc2 146 }
147 else
148 {
149 /*
150 * Try to open the print file...
151 */
152
ed486911 153 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
ef416fc2 154 {
080811b1
MS
155 _cupsLangPrintf(stderr,
156 _("ERROR: Unable to open print file \"%s\": %s\n"),
157 argv[6], strerror(errno));
ef416fc2 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
1f0275e3
MS
168 if ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
169 return (CUPS_BACKEND_FAILED);
170
acb056cb
MS
171 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
172 username, sizeof(username), hostname, sizeof(hostname), &port,
ef416fc2 173 resource, sizeof(resource));
174
175 if (port == 0)
176 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
177
fa73b229 178 /*
179 * Get options, if any...
180 */
181
c0e1af83 182 waiteof = 1;
183 contimeout = 7 * 24 * 60 * 60;
fa73b229 184
185 if ((options = strchr(resource, '?')) != NULL)
186 {
187 /*
188 * Yup, terminate the device name string and move to the first
189 * character of the options...
190 */
191
192 *options++ = '\0';
193
194 /*
195 * Parse options...
196 */
197
198 while (*options)
199 {
200 /*
201 * Get the name...
202 */
203
db1f069b 204 name = options;
fa73b229 205
db1f069b
MS
206 while (*options && *options != '=' && *options != '+' && *options != '&')
207 options ++;
208
209 if ((sep = *options) != '\0')
210 *options++ = '\0';
211
212 if (sep == '=')
fa73b229 213 {
214 /*
215 * Get the value...
216 */
217
db1f069b 218 value = options;
fa73b229 219
db1f069b 220 while (*options && *options != '+' && *options != '&')
fa73b229 221 options ++;
db1f069b
MS
222
223 if (*options)
224 *options++ = '\0';
fa73b229 225 }
226 else
db1f069b 227 value = (char *)"";
fa73b229 228
229 /*
230 * Process the option...
231 */
232
233 if (!strcasecmp(name, "waiteof"))
234 {
235 /*
236 * Set the wait-for-eof value...
237 */
238
239 waiteof = !value[0] || !strcasecmp(value, "on") ||
240 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
241 }
c0e1af83 242 else if (!strcasecmp(name, "contimeout"))
243 {
244 /*
245 * Set the connection timeout...
246 */
247
248 if (atoi(value) > 0)
249 contimeout = atoi(value);
250 }
fa73b229 251 }
252 }
253
ef416fc2 254 /*
255 * Then try to connect to the remote host...
256 */
257
c0e1af83 258 recoverable = 0;
259 start_time = time(NULL);
260
ef416fc2 261 sprintf(portname, "%d", port);
262
acb056cb 263 fputs("STATE: +connecting-to-device\n", stderr);
1340db2d
MS
264 fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
265
ef416fc2 266 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
267 {
db1f069b
MS
268 _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"),
269 hostname);
ef416fc2 270 return (CUPS_BACKEND_STOP);
271 }
272
acb056cb 273 fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
8b450588 274 _cupsLangPuts(stderr, _("INFO: Connecting to printer...\n"));
ef416fc2 275
ed486911 276 for (delay = 5;;)
ef416fc2 277 {
26d47ec6 278 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
ef416fc2 279 {
ed486911 280 error = errno;
281 device_fd = -1;
ef416fc2 282
ed486911 283 if (getenv("CLASS") != NULL)
284 {
285 /*
286 * If the CLASS environment variable is set, the job was submitted
287 * to a class and not to a specific queue. In this case, we want
288 * to abort immediately so that the job can be requeued on the next
289 * available printer in the class.
290 */
ef416fc2 291
db1f069b
MS
292 _cupsLangPuts(stderr,
293 _("INFO: Unable to contact printer, queuing on next "
294 "printer in class...\n"));
ef416fc2 295
ed486911 296 /*
297 * Sleep 5 seconds to keep the job from requeuing too rapidly...
298 */
ef416fc2 299
ed486911 300 sleep(5);
ef416fc2 301
ed486911 302 return (CUPS_BACKEND_FAILED);
303 }
ef416fc2 304
ed486911 305 if (error == ECONNREFUSED || error == EHOSTDOWN ||
306 error == EHOSTUNREACH)
307 {
c0e1af83 308 if (contimeout && (time(NULL) - start_time) > contimeout)
309 {
db1f069b 310 _cupsLangPuts(stderr, _("ERROR: Printer not responding!\n"));
c0e1af83 311 return (CUPS_BACKEND_FAILED);
312 }
313
314 recoverable = 1;
315
db1f069b
MS
316 _cupsLangPrintf(stderr,
317 _("WARNING: recoverable: Network host \'%s\' is busy; "
318 "will retry in %d seconds...\n"),
319 hostname, delay);
c0e1af83 320
ed486911 321 sleep(delay);
ef416fc2 322
ed486911 323 if (delay < 30)
324 delay += 5;
ef416fc2 325 }
326 else
ed486911 327 {
c0e1af83 328 recoverable = 1;
329
db1f069b
MS
330 _cupsLangPrintf(stderr, "DEBUG: Connection error: %s\n",
331 strerror(errno));
332 _cupsLangPuts(stderr,
333 _("ERROR: recoverable: Unable to connect to printer; "
334 "will retry in 30 seconds...\n"));
ed486911 335 sleep(30);
336 }
ef416fc2 337 }
ed486911 338 else
339 break;
340 }
ef416fc2 341
c0e1af83 342 if (recoverable)
343 {
344 /*
acb056cb
MS
345 * If we've shown a recoverable error make sure the printer proxies have a
346 * chance to see the recovered message. Not pretty but necessary for now...
c0e1af83 347 */
348
349 fputs("INFO: recovered: \n", stderr);
350 sleep(5);
351 }
352
ed486911 353 fputs("STATE: -connecting-to-device\n", stderr);
8b450588 354 _cupsLangPuts(stderr, _("INFO: Connected to printer...\n"));
26d47ec6 355
356#ifdef AF_INET6
357 if (addr->addr.addr.sa_family == AF_INET6)
358 fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
c0e1af83 359 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
360 ntohs(addr->addr.ipv6.sin6_port));
26d47ec6 361 else
362#endif /* AF_INET6 */
363 if (addr->addr.addr.sa_family == AF_INET)
364 fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4)...\n",
c0e1af83 365 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
366 ntohs(addr->addr.ipv4.sin_port));
ef416fc2 367
568fa3fa
MS
368 /*
369 * See if the printer supports SNMP...
370 */
371
7a14d768 372 if ((snmp_fd = _cupsSNMPOpen(addr->addr.addr.sa_family)) >= 0)
1f0275e3 373 {
426c6a59
MS
374 have_supplies = !backendSNMPSupplies(snmp_fd, &(addr->addr), &start_count,
375 NULL);
1f0275e3
MS
376 }
377 else
426c6a59 378 have_supplies = start_count = 0;
568fa3fa 379
ed486911 380 /*
381 * Print everything...
382 */
ef416fc2 383
ed486911 384 tbytes = 0;
ef416fc2 385
ed486911 386 while (copies > 0 && tbytes >= 0)
387 {
ef416fc2 388 copies --;
389
ed486911 390 if (print_fd != 0)
ef416fc2 391 {
392 fputs("PAGE: 1 1\n", stderr);
ed486911 393 lseek(print_fd, 0, SEEK_SET);
ef416fc2 394 }
395
568fa3fa
MS
396 tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addr->addr), 1,
397 backendNetworkSideCB);
ef416fc2 398
ed486911 399 if (print_fd != 0 && tbytes >= 0)
db1f069b 400 _cupsLangPrintf(stderr,
c0e1af83 401#ifdef HAVE_LONG_LONG
db1f069b 402 _("INFO: Sent print file, %lld bytes...\n"),
c0e1af83 403#else
db1f069b 404 _("INFO: Sent print file, %ld bytes...\n"),
c0e1af83 405#endif /* HAVE_LONG_LONG */
db1f069b 406 CUPS_LLCAST tbytes);
ed486911 407 }
ef416fc2 408
323c5de1 409 /*
5f64df29 410 * Wait up to 5 seconds to get any pending back-channel data...
323c5de1 411 */
412
5f64df29 413 wait_time = time(NULL) + 5;
9a4f8274
MS
414 while (wait_time >= time(&current_time))
415 if (wait_bc(device_fd, wait_time - current_time) <= 0)
5f64df29 416 break;
323c5de1 417
ed486911 418 if (waiteof)
419 {
420 /*
421 * Shutdown the socket and wait for the other end to finish...
422 */
ef416fc2 423
db1f069b
MS
424 _cupsLangPuts(stderr,
425 _("INFO: Print file sent, waiting for printer to finish...\n"));
ef416fc2 426
ed486911 427 shutdown(device_fd, 1);
ef416fc2 428
323c5de1 429 while (wait_bc(device_fd, 90) > 0);
ed486911 430 }
ef416fc2 431
568fa3fa
MS
432 /*
433 * Collect the final page count as needed...
434 */
435
426c6a59 436 if (have_supplies &&
568fa3fa
MS
437 !backendSNMPSupplies(snmp_fd, &(addr->addr), &page_count, NULL) &&
438 page_count > start_count)
439 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
440
ed486911 441 /*
442 * Close the socket connection...
443 */
ef416fc2 444
ed486911 445 close(device_fd);
ef416fc2 446
447 httpAddrFreeList(addrlist);
448
449 /*
450 * Close the input file and return...
451 */
452
ed486911 453 if (print_fd != 0)
454 close(print_fd);
ef416fc2 455
ed486911 456 if (tbytes >= 0)
db1f069b 457 _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
ef416fc2 458
ed486911 459 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
ef416fc2 460}
461
462
f7deaa1a 463/*
323c5de1 464 * 'wait_bc()' - Wait for back-channel data...
465 */
466
467static int /* O - # bytes read or -1 on error */
468wait_bc(int device_fd, /* I - Socket */
469 int secs) /* I - Seconds to wait */
470{
471 struct timeval timeout; /* Timeout for select() */
472 fd_set input; /* Input set for select() */
473 ssize_t bytes; /* Number of back-channel bytes read */
474 char buffer[1024]; /* Back-channel buffer */
475
476
477 /*
478 * Wait up to "secs" seconds for backchannel data...
479 */
480
481 timeout.tv_sec = secs;
482 timeout.tv_usec = 0;
483
484 FD_ZERO(&input);
485 FD_SET(device_fd, &input);
486
487 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
488 {
489 /*
490 * Grab the data coming back and spit it out to stderr...
491 */
492
493 if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0)
494 {
495 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
496 (int)bytes);
497 cupsBackChannelWrite(buffer, bytes, 1.0);
498 }
499
500 return (bytes);
501 }
502 else
503 return (-1);
504}
505
506
507/*
b19ccc9e 508 * End of "$Id: socket.c 7881 2008-08-28 20:21:56Z mike $".
ef416fc2 509 */