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