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