]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2186a2ba1a831b3a3bb02153a15ebcfaec29c80f
2 * AppSocket backend for CUPS.
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright © 2007-2018 by Apple Inc.
6 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
13 * Include necessary headers.
16 #include <cups/http-private.h>
17 #include "backend-private.h"
19 #include <sys/types.h>
27 # include <sys/socket.h>
28 # include <netinet/in.h>
29 # include <arpa/inet.h>
38 static ssize_t
wait_bc(int device_fd
, int secs
);
42 * 'main()' - Send a file to the printer or server.
46 * printer-uri job-id user title copies options [file]
49 int /* O - Exit status */
50 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
51 char *argv
[]) /* I - Command-line arguments */
53 const char *device_uri
; /* Device URI */
54 char scheme
[255], /* Scheme in URI */
55 hostname
[1024], /* Hostname */
56 username
[255], /* Username info (not used) */
57 resource
[1024], /* Resource info (not used) */
58 *options
, /* Pointer to options */
59 *name
, /* Name of option */
60 *value
, /* Value of option */
61 sep
; /* Option separator */
62 int print_fd
; /* Print file */
63 int copies
; /* Number of copies to print */
64 time_t start_time
; /* Time of first connect */
65 int contimeout
; /* Connection timeout */
66 int waiteof
; /* Wait for end-of-file? */
67 int port
; /* Port number */
68 int delay
; /* Delay for retries... */
69 int device_fd
; /* AppSocket */
70 int error
; /* Error code (if any) */
71 http_addrlist_t
*addrlist
, /* Address list */
72 *addr
; /* Connected address */
73 char addrname
[256]; /* Address name */
74 int snmp_enabled
= 1; /* Is SNMP enabled? */
75 int snmp_fd
, /* SNMP socket */
76 start_count
, /* Page count via SNMP at start */
77 page_count
, /* Page count via SNMP */
78 have_supplies
; /* Printer supports supply levels? */
79 ssize_t bytes
= 0, /* Initial bytes read */
80 tbytes
; /* Total number of bytes written */
81 char buffer
[1024]; /* Initial print buffer */
82 struct sigaction action
; /* Actions for POSIX signals */
86 * Make sure status messages are not buffered...
92 * Ignore SIGPIPE signals...
95 memset(&action
, 0, sizeof(action
));
96 action
.sa_handler
= SIG_IGN
;
97 sigaction(SIGPIPE
, &action
, NULL
);
100 * Check command-line...
105 printf("network socket \"Unknown\" \"%s\"\n",
106 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
107 return (CUPS_BACKEND_OK
);
109 else if (argc
!= 6 && argc
!= 7)
111 _cupsLangPrintf(stderr
,
112 _("Usage: %s job-id user title copies options [file]"),
114 return (CUPS_BACKEND_FAILED
);
118 * If we have 7 arguments, print the file named on the command-line.
119 * Otherwise, send stdin instead...
130 * Try to open the print file...
133 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
135 _cupsLangPrintError("ERROR", _("Unable to open print file"));
136 return (CUPS_BACKEND_FAILED
);
139 copies
= atoi(argv
[4]);
143 * Extract the hostname and port number from the URI...
146 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
148 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
151 if (getenv("CLASS") != NULL
)
152 return (CUPS_BACKEND_FAILED
);
155 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
156 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
157 resource
, sizeof(resource
));
160 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
163 * Get options, if any...
167 contimeout
= 7 * 24 * 60 * 60;
169 if ((options
= strchr(resource
, '?')) != NULL
)
172 * Yup, terminate the device name string and move to the first
173 * character of the options...
190 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
193 if ((sep
= *options
) != '\0')
204 while (*options
&& *options
!= '+' && *options
!= '&')
214 * Process the option...
217 if (!_cups_strcasecmp(name
, "waiteof"))
220 * Set the wait-for-eof value...
223 waiteof
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
224 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
226 else if (!_cups_strcasecmp(name
, "snmp"))
229 * Enable/disable SNMP stuff...
232 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
233 !_cups_strcasecmp(value
, "yes") ||
234 !_cups_strcasecmp(value
, "true");
236 else if (!_cups_strcasecmp(name
, "contimeout"))
239 * Set the connection timeout...
243 contimeout
= atoi(value
);
249 * Then try finding the remote host...
252 start_time
= time(NULL
);
254 addrlist
= backendLookup(hostname
, port
, NULL
);
257 * See if the printer supports SNMP...
261 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
266 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
269 have_supplies
= start_count
= 0;
272 * Wait for data from the filter...
277 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 1, backendNetworkSideCB
))
278 return (CUPS_BACKEND_OK
);
279 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
280 return (CUPS_BACKEND_OK
);
284 * Connect to the printer...
287 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
288 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
292 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
297 if (getenv("CLASS") != NULL
)
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.
306 _cupsLangPrintFilter(stderr
, "INFO",
307 _("Unable to contact printer, queuing on next "
308 "printer in class."));
311 * Sleep 5 seconds to keep the job from requeuing too rapidly...
316 return (CUPS_BACKEND_FAILED
);
319 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
321 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
|| errno
== EHOSTUNREACH
|| errno
== ETIMEDOUT
|| errno
== ENOTCONN
)
323 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
325 _cupsLangPrintFilter(stderr
, "ERROR",
326 _("The printer is not responding."));
327 return (CUPS_BACKEND_FAILED
);
333 _cupsLangPrintFilter(stderr
, "WARNING",
334 _("The printer may not exist or "
335 "is unavailable at this time."));
340 _cupsLangPrintFilter(stderr
, "WARNING",
341 _("The printer is unreachable at this "
346 _cupsLangPrintFilter(stderr
, "WARNING",
347 _("The printer is in use."));
351 sleep((unsigned)delay
);
358 _cupsLangPrintFilter(stderr
, "ERROR",
359 _("The printer is not responding."));
367 fputs("STATE: -connecting-to-device\n", stderr
);
368 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
370 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
371 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
372 httpAddrPort(&(addr
->addr
)));
375 * Print everything...
381 tbytes
+= write(device_fd
, buffer
, (size_t)bytes
);
383 while (copies
> 0 && tbytes
>= 0)
389 fputs("PAGE: 1 1\n", stderr
);
390 lseek(print_fd
, 0, SEEK_SET
);
393 if ((bytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addrlist
->addr
), 1, 0, backendNetworkSideCB
)) < 0)
398 if (print_fd
!= 0 && tbytes
>= 0)
399 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
402 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
404 if (waiteof
&& tbytes
>= 0)
407 * Shutdown the socket and wait for the other end to finish...
410 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for printer to finish."));
412 shutdown(device_fd
, 1);
414 while (wait_bc(device_fd
, 90) > 0);
418 * Collect the final page count as needed...
422 !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), &page_count
, NULL
) &&
423 page_count
> start_count
)
424 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
427 * Close the socket connection...
432 httpAddrFreeList(addrlist
);
435 * Close the input file and return...
441 return (tbytes
>= 0 ? CUPS_BACKEND_OK
: CUPS_BACKEND_FAILED
);
446 * 'wait_bc()' - Wait for back-channel data...
449 static ssize_t
/* O - # bytes read or -1 on error */
450 wait_bc(int device_fd
, /* I - Socket */
451 int secs
) /* I - Seconds to wait */
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 */
460 * Wait up to "secs" seconds for backchannel data...
463 timeout
.tv_sec
= secs
;
467 FD_SET(device_fd
, &input
);
469 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
472 * Grab the data coming back and spit it out to stderr...
475 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
477 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data\n",
479 cupsBackChannelWrite(buffer
, (size_t)bytes
, 1.0);