]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
Remove all of the Subversion keywords from various source files.
[thirdparty/cups.git] / backend / socket.c
1 /*
2 * AppSocket backend for CUPS.
3 *
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
6 *
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/".
12 *
13 * This file is subject to the Apple OS-Developed Software exception.
14 */
15
16 /*
17 * Include necessary headers.
18 */
19
20 #include <cups/http-private.h>
21 #include "backend-private.h"
22 #include <stdarg.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
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
38 /*
39 * Local functions...
40 */
41
42 static ssize_t wait_bc(int device_fd, int secs);
43
44
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
53 int /* O - Exit status */
54 main(int argc, /* I - Number of command-line arguments (6 or 7) */
55 char *argv[]) /* I - Command-line arguments */
56 {
57 const char *device_uri; /* Device URI */
58 char scheme[255], /* Scheme in URI */
59 hostname[1024], /* Hostname */
60 username[255], /* Username info (not used) */
61 resource[1024], /* Resource info (not used) */
62 *options, /* Pointer to options */
63 *name, /* Name of option */
64 *value, /* Value of option */
65 sep; /* Option separator */
66 int print_fd; /* Print file */
67 int copies; /* Number of copies to print */
68 time_t start_time; /* Time of first connect */
69 int contimeout; /* Connection timeout */
70 int waiteof; /* Wait for end-of-file? */
71 int port; /* Port number */
72 char portname[255]; /* Port name */
73 int delay; /* Delay for retries... */
74 int device_fd; /* AppSocket */
75 int error; /* Error code (if any) */
76 http_addrlist_t *addrlist, /* Address list */
77 *addr; /* Connected address */
78 char addrname[256]; /* Address name */
79 int snmp_enabled = 1; /* Is SNMP enabled? */
80 int snmp_fd, /* SNMP socket */
81 start_count, /* Page count via SNMP at start */
82 page_count, /* Page count via SNMP */
83 have_supplies; /* Printer supports supply levels? */
84 ssize_t bytes = 0, /* Initial bytes read */
85 tbytes; /* Total number of bytes written */
86 char buffer[1024]; /* Initial print buffer */
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 {
118 printf("network socket \"Unknown\" \"%s\"\n",
119 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
120 return (CUPS_BACKEND_OK);
121 }
122 else if (argc < 6 || argc > 7)
123 {
124 _cupsLangPrintf(stderr,
125 _("Usage: %s job-id user title copies options [file]"),
126 argv[0]);
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 {
137 print_fd = 0;
138 copies = 1;
139 }
140 else
141 {
142 /*
143 * Try to open the print file...
144 */
145
146 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
147 {
148 _cupsLangPrintError("ERROR", _("Unable to open print file"));
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
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 }
167
168 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
169 username, sizeof(username), hostname, sizeof(hostname), &port,
170 resource, sizeof(resource));
171
172 if (port == 0)
173 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
174
175 /*
176 * Get options, if any...
177 */
178
179 waiteof = 1;
180 contimeout = 7 * 24 * 60 * 60;
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
201 name = options;
202
203 while (*options && *options != '=' && *options != '+' && *options != '&')
204 options ++;
205
206 if ((sep = *options) != '\0')
207 *options++ = '\0';
208
209 if (sep == '=')
210 {
211 /*
212 * Get the value...
213 */
214
215 value = options;
216
217 while (*options && *options != '+' && *options != '&')
218 options ++;
219
220 if (*options)
221 *options++ = '\0';
222 }
223 else
224 value = (char *)"";
225
226 /*
227 * Process the option...
228 */
229
230 if (!_cups_strcasecmp(name, "waiteof"))
231 {
232 /*
233 * Set the wait-for-eof value...
234 */
235
236 waiteof = !value[0] || !_cups_strcasecmp(value, "on") ||
237 !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true");
238 }
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") ||
246 !_cups_strcasecmp(value, "yes") ||
247 !_cups_strcasecmp(value, "true");
248 }
249 else if (!_cups_strcasecmp(name, "contimeout"))
250 {
251 /*
252 * Set the connection timeout...
253 */
254
255 if (atoi(value) > 0)
256 contimeout = atoi(value);
257 }
258 }
259 }
260
261 /*
262 * Then try finding the remote host...
263 */
264
265 start_time = time(NULL);
266
267 sprintf(portname, "%d", port);
268
269 fputs("STATE: +connecting-to-device\n", stderr);
270 fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
271
272 while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
273 {
274 _cupsLangPrintFilter(stderr, "INFO",
275 _("Unable to locate printer \"%s\"."), hostname);
276 sleep(10);
277
278 if (getenv("CLASS") != NULL)
279 {
280 fputs("STATE: -connecting-to-device\n", stderr);
281 return (CUPS_BACKEND_STOP);
282 }
283 }
284
285 /*
286 * See if the printer supports SNMP...
287 */
288
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)
295 have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr),
296 &start_count, NULL);
297 else
298 have_supplies = start_count = 0;
299
300 /*
301 * Wait for data from the filter...
302 */
303
304 if (print_fd == 0)
305 {
306 if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 1, backendNetworkSideCB))
307 return (CUPS_BACKEND_OK);
308 else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0)
309 return (CUPS_BACKEND_OK);
310 }
311
312 /*
313 * Connect to the printer...
314 */
315
316 fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
317 _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer."));
318
319 for (delay = 5;;)
320 {
321 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
322 {
323 error = errno;
324 device_fd = -1;
325
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 */
334
335 _cupsLangPrintFilter(stderr, "INFO",
336 _("Unable to contact printer, queuing on next "
337 "printer in class."));
338
339 /*
340 * Sleep 5 seconds to keep the job from requeuing too rapidly...
341 */
342
343 sleep(5);
344
345 return (CUPS_BACKEND_FAILED);
346 }
347
348 fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error));
349
350 if (error == ECONNREFUSED || error == EHOSTDOWN ||
351 error == EHOSTUNREACH)
352 {
353 if (contimeout && (time(NULL) - start_time) > contimeout)
354 {
355 _cupsLangPrintFilter(stderr, "ERROR",
356 _("The printer is not responding."));
357 return (CUPS_BACKEND_FAILED);
358 }
359
360 switch (error)
361 {
362 case EHOSTDOWN :
363 _cupsLangPrintFilter(stderr, "WARNING",
364 _("The printer may not exist or "
365 "is unavailable at this time."));
366 break;
367
368 case EHOSTUNREACH :
369 _cupsLangPrintFilter(stderr, "WARNING",
370 _("The printer is unreachable at this "
371 "time."));
372 break;
373
374 case ECONNREFUSED :
375 default :
376 _cupsLangPrintFilter(stderr, "WARNING",
377 _("The printer is in use."));
378 break;
379 }
380
381 sleep((unsigned)delay);
382
383 if (delay < 30)
384 delay += 5;
385 }
386 else
387 {
388 _cupsLangPrintFilter(stderr, "ERROR",
389 _("The printer is not responding."));
390 sleep(30);
391 }
392 }
393 else
394 break;
395 }
396
397 fputs("STATE: -connecting-to-device\n", stderr);
398 _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer."));
399
400 fprintf(stderr, "DEBUG: Connected to %s:%d...\n",
401 httpAddrString(&(addr->addr), addrname, sizeof(addrname)),
402 httpAddrPort(&(addr->addr)));
403
404 /*
405 * Print everything...
406 */
407
408 tbytes = 0;
409
410 if (bytes > 0)
411 tbytes += write(device_fd, buffer, (size_t)bytes);
412
413 while (copies > 0 && tbytes >= 0)
414 {
415 copies --;
416
417 if (print_fd != 0)
418 {
419 fputs("PAGE: 1 1\n", stderr);
420 lseek(print_fd, 0, SEEK_SET);
421 }
422
423 tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addrlist->addr), 1,
424 0, backendNetworkSideCB);
425
426 if (print_fd != 0 && tbytes >= 0)
427 _cupsLangPrintFilter(stderr, "INFO", _("Print file sent."));
428 }
429
430 fputs("STATE: +cups-waiting-for-job-completed\n", stderr);
431
432 if (waiteof)
433 {
434 /*
435 * Shutdown the socket and wait for the other end to finish...
436 */
437
438 _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to finish."));
439
440 shutdown(device_fd, 1);
441
442 while (wait_bc(device_fd, 90) > 0);
443 }
444
445 /*
446 * Collect the final page count as needed...
447 */
448
449 if (have_supplies &&
450 !backendSNMPSupplies(snmp_fd, &(addrlist->addr), &page_count, NULL) &&
451 page_count > start_count)
452 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
453
454 /*
455 * Close the socket connection...
456 */
457
458 close(device_fd);
459
460 httpAddrFreeList(addrlist);
461
462 /*
463 * Close the input file and return...
464 */
465
466 if (print_fd != 0)
467 close(print_fd);
468
469 return (CUPS_BACKEND_OK);
470 }
471
472
473 /*
474 * 'wait_bc()' - Wait for back-channel data...
475 */
476
477 static ssize_t /* O - # bytes read or -1 on error */
478 wait_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 {
505 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data\n",
506 (int)bytes);
507 cupsBackChannelWrite(buffer, (size_t)bytes, 1.0);
508 }
509
510 return (bytes);
511 }
512 else
513 return (-1);
514 }