]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
4 * AppSocket backend for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers.
22 #include <cups/http-private.h>
23 #include "backend-private.h"
25 #include <sys/types.h>
33 # include <sys/socket.h>
34 # include <netinet/in.h>
35 # include <arpa/inet.h>
44 static ssize_t
wait_bc(int device_fd
, int secs
);
48 * 'main()' - Send a file to the printer or server.
52 * printer-uri job-id user title copies options [file]
55 int /* O - Exit status */
56 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
57 char *argv
[]) /* I - Command-line arguments */
59 const char *device_uri
; /* Device URI */
60 char scheme
[255], /* Scheme in URI */
61 hostname
[1024], /* Hostname */
62 username
[255], /* Username info (not used) */
63 resource
[1024], /* Resource info (not used) */
64 *options
, /* Pointer to options */
65 *name
, /* Name of option */
66 *value
, /* Value of option */
67 sep
; /* Option separator */
68 int print_fd
; /* Print file */
69 int copies
; /* Number of copies to print */
70 time_t start_time
; /* Time of first connect */
71 int contimeout
; /* Connection timeout */
72 int waiteof
; /* Wait for end-of-file? */
73 int port
; /* Port number */
74 char portname
[255]; /* Port name */
75 int delay
; /* Delay for retries... */
76 int device_fd
; /* AppSocket */
77 int error
; /* Error code (if any) */
78 http_addrlist_t
*addrlist
, /* Address list */
79 *addr
; /* Connected address */
80 char addrname
[256]; /* Address name */
81 int snmp_enabled
= 1; /* Is SNMP enabled? */
82 int snmp_fd
, /* SNMP socket */
83 start_count
, /* Page count via SNMP at start */
84 page_count
, /* Page count via SNMP */
85 have_supplies
; /* Printer supports supply levels? */
86 ssize_t bytes
= 0, /* Initial bytes read */
87 tbytes
; /* Total number of bytes written */
88 char buffer
[1024]; /* Initial print buffer */
89 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
90 struct sigaction action
; /* Actions for POSIX signals */
91 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
95 * Make sure status messages are not buffered...
101 * Ignore SIGPIPE signals...
105 sigset(SIGPIPE
, SIG_IGN
);
106 #elif defined(HAVE_SIGACTION)
107 memset(&action
, 0, sizeof(action
));
108 action
.sa_handler
= SIG_IGN
;
109 sigaction(SIGPIPE
, &action
, NULL
);
111 signal(SIGPIPE
, SIG_IGN
);
112 #endif /* HAVE_SIGSET */
115 * Check command-line...
120 printf("network socket \"Unknown\" \"%s\"\n",
121 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
122 return (CUPS_BACKEND_OK
);
124 else if (argc
< 6 || argc
> 7)
126 _cupsLangPrintf(stderr
,
127 _("Usage: %s job-id user title copies options [file]"),
129 return (CUPS_BACKEND_FAILED
);
133 * If we have 7 arguments, print the file named on the command-line.
134 * Otherwise, send stdin instead...
145 * Try to open the print file...
148 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
150 _cupsLangPrintError("ERROR", _("Unable to open print file"));
151 return (CUPS_BACKEND_FAILED
);
154 copies
= atoi(argv
[4]);
158 * Extract the hostname and port number from the URI...
161 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
163 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
166 if (getenv("CLASS") != NULL
)
167 return (CUPS_BACKEND_FAILED
);
170 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
171 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
172 resource
, sizeof(resource
));
175 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
178 * Get options, if any...
182 contimeout
= 7 * 24 * 60 * 60;
184 if ((options
= strchr(resource
, '?')) != NULL
)
187 * Yup, terminate the device name string and move to the first
188 * character of the options...
205 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
208 if ((sep
= *options
) != '\0')
219 while (*options
&& *options
!= '+' && *options
!= '&')
229 * Process the option...
232 if (!_cups_strcasecmp(name
, "waiteof"))
235 * Set the wait-for-eof value...
238 waiteof
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
239 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
241 else if (!_cups_strcasecmp(name
, "snmp"))
244 * Enable/disable SNMP stuff...
247 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
248 !_cups_strcasecmp(value
, "yes") ||
249 !_cups_strcasecmp(value
, "true");
251 else if (!_cups_strcasecmp(name
, "contimeout"))
254 * Set the connection timeout...
258 contimeout
= atoi(value
);
264 * Then try finding the remote host...
267 start_time
= time(NULL
);
269 sprintf(portname
, "%d", port
);
271 fputs("STATE: +connecting-to-device\n", stderr
);
272 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
274 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
276 _cupsLangPrintFilter(stderr
, "INFO",
277 _("Unable to locate printer \"%s\"."), hostname
);
280 if (getenv("CLASS") != NULL
)
282 fputs("STATE: -connecting-to-device\n", stderr
);
283 return (CUPS_BACKEND_STOP
);
288 * See if the printer supports SNMP...
292 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
297 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
300 have_supplies
= start_count
= 0;
303 * Wait for data from the filter...
308 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 1, backendNetworkSideCB
))
309 return (CUPS_BACKEND_OK
);
310 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
311 return (CUPS_BACKEND_OK
);
315 * Connect to the printer...
318 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
319 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
323 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
328 if (getenv("CLASS") != NULL
)
331 * If the CLASS environment variable is set, the job was submitted
332 * to a class and not to a specific queue. In this case, we want
333 * to abort immediately so that the job can be requeued on the next
334 * available printer in the class.
337 _cupsLangPrintFilter(stderr
, "INFO",
338 _("Unable to contact printer, queuing on next "
339 "printer in class."));
342 * Sleep 5 seconds to keep the job from requeuing too rapidly...
347 return (CUPS_BACKEND_FAILED
);
350 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
352 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
353 error
== EHOSTUNREACH
)
355 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
357 _cupsLangPrintFilter(stderr
, "ERROR",
358 _("The printer is not responding."));
359 return (CUPS_BACKEND_FAILED
);
365 _cupsLangPrintFilter(stderr
, "WARNING",
366 _("The printer may not exist or "
367 "is unavailable at this time."));
371 _cupsLangPrintFilter(stderr
, "WARNING",
372 _("The printer is unreachable at this "
378 _cupsLangPrintFilter(stderr
, "WARNING",
379 _("The printer is in use."));
383 sleep((unsigned)delay
);
390 _cupsLangPrintFilter(stderr
, "ERROR",
391 _("The printer is not responding."));
399 fputs("STATE: -connecting-to-device\n", stderr
);
400 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
402 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
403 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
404 httpAddrPort(&(addr
->addr
)));
407 * Print everything...
413 tbytes
+= write(device_fd
, buffer
, (size_t)bytes
);
415 while (copies
> 0 && tbytes
>= 0)
421 fputs("PAGE: 1 1\n", stderr
);
422 lseek(print_fd
, 0, SEEK_SET
);
425 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addrlist
->addr
), 1,
426 0, backendNetworkSideCB
);
428 if (print_fd
!= 0 && tbytes
>= 0)
429 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
432 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
437 * Shutdown the socket and wait for the other end to finish...
440 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for printer to finish."));
442 shutdown(device_fd
, 1);
444 while (wait_bc(device_fd
, 90) > 0);
448 * Collect the final page count as needed...
452 !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), &page_count
, NULL
) &&
453 page_count
> start_count
)
454 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
457 * Close the socket connection...
462 httpAddrFreeList(addrlist
);
465 * Close the input file and return...
471 return (CUPS_BACKEND_OK
);
476 * 'wait_bc()' - Wait for back-channel data...
479 static ssize_t
/* O - # bytes read or -1 on error */
480 wait_bc(int device_fd
, /* I - Socket */
481 int secs
) /* I - Seconds to wait */
483 struct timeval timeout
; /* Timeout for select() */
484 fd_set input
; /* Input set for select() */
485 ssize_t bytes
; /* Number of back-channel bytes read */
486 char buffer
[1024]; /* Back-channel buffer */
490 * Wait up to "secs" seconds for backchannel data...
493 timeout
.tv_sec
= secs
;
497 FD_SET(device_fd
, &input
);
499 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
502 * Grab the data coming back and spit it out to stderr...
505 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
507 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data\n",
509 cupsBackChannelWrite(buffer
, (size_t)bytes
, 1.0);