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