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