]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * AppSocket backend for CUPS.
4 * Copyright © 2007-2016 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers.
15 #include <cups/http-private.h>
16 #include "backend-private.h"
18 #include <sys/types.h>
26 # include <sys/socket.h>
27 # include <netinet/in.h>
28 # include <arpa/inet.h>
37 static ssize_t
wait_bc(int device_fd
, int secs
);
41 * 'main()' - Send a file to the printer or server.
45 * printer-uri job-id user title copies options [file]
48 int /* O - Exit status */
49 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
50 char *argv
[]) /* I - Command-line arguments */
52 const char *device_uri
; /* Device URI */
53 char scheme
[255], /* Scheme in URI */
54 hostname
[1024], /* Hostname */
55 username
[255], /* Username info (not used) */
56 resource
[1024], /* Resource info (not used) */
57 *options
, /* Pointer to options */
58 *name
, /* Name of option */
59 *value
, /* Value of option */
60 sep
; /* Option separator */
61 int print_fd
; /* Print file */
62 int copies
; /* Number of copies to print */
63 time_t start_time
; /* Time of first connect */
64 int contimeout
; /* Connection timeout */
65 int waiteof
; /* Wait for end-of-file? */
66 int port
; /* Port number */
67 int delay
; /* Delay for retries... */
68 int device_fd
; /* AppSocket */
69 int error
; /* Error code (if any) */
70 http_addrlist_t
*addrlist
, /* Address list */
71 *addr
; /* Connected address */
72 char addrname
[256]; /* Address name */
73 int snmp_enabled
= 1; /* Is SNMP enabled? */
74 int snmp_fd
, /* SNMP socket */
75 start_count
, /* Page count via SNMP at start */
76 page_count
, /* Page count via SNMP */
77 have_supplies
; /* Printer supports supply levels? */
78 ssize_t bytes
= 0, /* Initial bytes read */
79 tbytes
; /* Total number of bytes written */
80 char buffer
[1024]; /* Initial print buffer */
81 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
82 struct sigaction action
; /* Actions for POSIX signals */
83 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
87 * Make sure status messages are not buffered...
93 * Ignore SIGPIPE signals...
97 sigset(SIGPIPE
, SIG_IGN
);
98 #elif defined(HAVE_SIGACTION)
99 memset(&action
, 0, sizeof(action
));
100 action
.sa_handler
= SIG_IGN
;
101 sigaction(SIGPIPE
, &action
, NULL
);
103 signal(SIGPIPE
, SIG_IGN
);
104 #endif /* HAVE_SIGSET */
107 * Check command-line...
112 printf("network socket \"Unknown\" \"%s\"\n",
113 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
114 return (CUPS_BACKEND_OK
);
116 else if (argc
< 6 || argc
> 7)
118 _cupsLangPrintf(stderr
,
119 _("Usage: %s job-id user title copies options [file]"),
121 return (CUPS_BACKEND_FAILED
);
125 * If we have 7 arguments, print the file named on the command-line.
126 * Otherwise, send stdin instead...
137 * Try to open the print file...
140 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
142 _cupsLangPrintError("ERROR", _("Unable to open print file"));
143 return (CUPS_BACKEND_FAILED
);
146 copies
= atoi(argv
[4]);
150 * Extract the hostname and port number from the URI...
153 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
155 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
158 if (getenv("CLASS") != NULL
)
159 return (CUPS_BACKEND_FAILED
);
162 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
163 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
164 resource
, sizeof(resource
));
167 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
170 * Get options, if any...
174 contimeout
= 7 * 24 * 60 * 60;
176 if ((options
= strchr(resource
, '?')) != NULL
)
179 * Yup, terminate the device name string and move to the first
180 * character of the options...
197 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
200 if ((sep
= *options
) != '\0')
211 while (*options
&& *options
!= '+' && *options
!= '&')
221 * Process the option...
224 if (!_cups_strcasecmp(name
, "waiteof"))
227 * Set the wait-for-eof value...
230 waiteof
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
231 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
233 else if (!_cups_strcasecmp(name
, "snmp"))
236 * Enable/disable SNMP stuff...
239 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
240 !_cups_strcasecmp(value
, "yes") ||
241 !_cups_strcasecmp(value
, "true");
243 else if (!_cups_strcasecmp(name
, "contimeout"))
246 * Set the connection timeout...
250 contimeout
= atoi(value
);
256 * Then try finding the remote host...
259 start_time
= time(NULL
);
261 addrlist
= backendLookup(hostname
, port
, NULL
);
264 * See if the printer supports SNMP...
268 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
273 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
276 have_supplies
= start_count
= 0;
279 * Wait for data from the filter...
284 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 1, backendNetworkSideCB
))
285 return (CUPS_BACKEND_OK
);
286 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
287 return (CUPS_BACKEND_OK
);
291 * Connect to the printer...
294 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
295 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
299 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
304 if (getenv("CLASS") != NULL
)
307 * If the CLASS environment variable is set, the job was submitted
308 * to a class and not to a specific queue. In this case, we want
309 * to abort immediately so that the job can be requeued on the next
310 * available printer in the class.
313 _cupsLangPrintFilter(stderr
, "INFO",
314 _("Unable to contact printer, queuing on next "
315 "printer in class."));
318 * Sleep 5 seconds to keep the job from requeuing too rapidly...
323 return (CUPS_BACKEND_FAILED
);
326 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
328 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
|| errno
== EHOSTUNREACH
|| errno
== ETIMEDOUT
|| errno
== ENOTCONN
)
330 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
332 _cupsLangPrintFilter(stderr
, "ERROR",
333 _("The printer is not responding."));
334 return (CUPS_BACKEND_FAILED
);
340 _cupsLangPrintFilter(stderr
, "WARNING",
341 _("The printer may not exist or "
342 "is unavailable at this time."));
347 _cupsLangPrintFilter(stderr
, "WARNING",
348 _("The printer is unreachable at this "
353 _cupsLangPrintFilter(stderr
, "WARNING",
354 _("The printer is in use."));
358 sleep((unsigned)delay
);
365 _cupsLangPrintFilter(stderr
, "ERROR",
366 _("The printer is not responding."));
374 fputs("STATE: -connecting-to-device\n", stderr
);
375 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
377 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
378 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
379 httpAddrPort(&(addr
->addr
)));
382 * Print everything...
388 tbytes
+= write(device_fd
, buffer
, (size_t)bytes
);
390 while (copies
> 0 && tbytes
>= 0)
396 fputs("PAGE: 1 1\n", stderr
);
397 lseek(print_fd
, 0, SEEK_SET
);
400 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addrlist
->addr
), 1,
401 0, backendNetworkSideCB
);
403 if (print_fd
!= 0 && tbytes
>= 0)
404 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
407 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
412 * Shutdown the socket and wait for the other end to finish...
415 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for printer to finish."));
417 shutdown(device_fd
, 1);
419 while (wait_bc(device_fd
, 90) > 0);
423 * Collect the final page count as needed...
427 !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), &page_count
, NULL
) &&
428 page_count
> start_count
)
429 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
432 * Close the socket connection...
437 httpAddrFreeList(addrlist
);
440 * Close the input file and return...
446 return (CUPS_BACKEND_OK
);
451 * 'wait_bc()' - Wait for back-channel data...
454 static ssize_t
/* O - # bytes read or -1 on error */
455 wait_bc(int device_fd
, /* I - Socket */
456 int secs
) /* I - Seconds to wait */
458 struct timeval timeout
; /* Timeout for select() */
459 fd_set input
; /* Input set for select() */
460 ssize_t bytes
; /* Number of back-channel bytes read */
461 char buffer
[1024]; /* Back-channel buffer */
465 * Wait up to "secs" seconds for backchannel data...
468 timeout
.tv_sec
= secs
;
472 FD_SET(device_fd
, &input
);
474 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
477 * Grab the data coming back and spit it out to stderr...
480 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
482 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data\n",
484 cupsBackChannelWrite(buffer
, (size_t)bytes
, 1.0);