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