]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * main() - Send a file to the printer or server.
20 * wait_bc() - Wait for back-channel data...
24 * Include necessary headers.
27 #include <cups/http-private.h>
28 #include "backend-private.h"
30 #include <sys/types.h>
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <arpa/inet.h>
49 static int wait_bc(int device_fd
, int secs
);
53 * 'main()' - Send a file to the printer or server.
57 * printer-uri job-id user title copies options [file]
60 int /* O - Exit status */
61 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
62 char *argv
[]) /* I - Command-line arguments */
64 char method
[255], /* Method in URI */
65 hostname
[1024], /* Hostname */
66 username
[255], /* Username info (not used) */
67 resource
[1024], /* Resource info (not used) */
68 *options
, /* Pointer to options */
69 *name
, /* Name of option */
70 *value
, /* Value of option */
71 sep
; /* Option separator */
72 int print_fd
; /* Print file */
73 int copies
; /* Number of copies to print */
74 time_t start_time
; /* Time of first connect */
75 int recoverable
; /* Recoverable error shown? */
76 int contimeout
; /* Connection timeout */
77 int waiteof
; /* Wait for end-of-file? */
78 int port
; /* Port number */
79 char portname
[255]; /* Port name */
80 int delay
; /* Delay for retries... */
81 int device_fd
; /* AppSocket */
82 int error
; /* Error code (if any) */
83 http_addrlist_t
*addrlist
, /* Address list */
84 *addr
; /* Connected address */
85 char addrname
[256]; /* Address name */
86 int snmp_fd
, /* SNMP socket */
87 start_count
, /* Page count via SNMP at start */
88 page_count
; /* Page count via SNMP */
89 ssize_t tbytes
; /* Total number of bytes written */
90 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
91 struct sigaction action
; /* Actions for POSIX signals */
92 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
96 * Make sure status messages are not buffered...
102 * Ignore SIGPIPE signals...
106 sigset(SIGPIPE
, SIG_IGN
);
107 #elif defined(HAVE_SIGACTION)
108 memset(&action
, 0, sizeof(action
));
109 action
.sa_handler
= SIG_IGN
;
110 sigaction(SIGPIPE
, &action
, NULL
);
112 signal(SIGPIPE
, SIG_IGN
);
113 #endif /* HAVE_SIGSET */
116 * Check command-line...
121 puts("network socket \"Unknown\" \"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]\n"),
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 _cupsLangPrintf(stderr
,
151 _("ERROR: Unable to open print file \"%s\": %s\n"),
152 argv
[6], strerror(errno
));
153 return (CUPS_BACKEND_FAILED
);
156 copies
= atoi(argv
[4]);
160 * Extract the hostname and port number from the URI...
163 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
164 method
, sizeof(method
), username
, sizeof(username
),
165 hostname
, sizeof(hostname
), &port
,
166 resource
, sizeof(resource
));
169 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
172 * Get options, if any...
176 contimeout
= 7 * 24 * 60 * 60;
178 if ((options
= strchr(resource
, '?')) != NULL
)
181 * Yup, terminate the device name string and move to the first
182 * character of the options...
199 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
202 if ((sep
= *options
) != '\0')
213 while (*options
&& *options
!= '+' && *options
!= '&')
223 * Process the option...
226 if (!strcasecmp(name
, "waiteof"))
229 * Set the wait-for-eof value...
232 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
233 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
235 else if (!strcasecmp(name
, "contimeout"))
238 * Set the connection timeout...
242 contimeout
= atoi(value
);
248 * Then try to connect to the remote host...
252 start_time
= time(NULL
);
254 sprintf(portname
, "%d", port
);
256 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
258 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
260 return (CUPS_BACKEND_STOP
);
263 _cupsLangPrintf(stderr
,
264 _("INFO: Attempting to connect to host %s on port %d\n"),
267 fputs("STATE: +connecting-to-device\n", stderr
);
271 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
276 if (getenv("CLASS") != NULL
)
279 * If the CLASS environment variable is set, the job was submitted
280 * to a class and not to a specific queue. In this case, we want
281 * to abort immediately so that the job can be requeued on the next
282 * available printer in the class.
285 _cupsLangPuts(stderr
,
286 _("INFO: Unable to contact printer, queuing on next "
287 "printer in class...\n"));
290 * Sleep 5 seconds to keep the job from requeuing too rapidly...
295 return (CUPS_BACKEND_FAILED
);
298 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
299 error
== EHOSTUNREACH
)
301 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
303 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
304 return (CUPS_BACKEND_FAILED
);
309 _cupsLangPrintf(stderr
,
310 _("WARNING: recoverable: Network host \'%s\' is busy; "
311 "will retry in %d seconds...\n"),
323 _cupsLangPrintf(stderr
, "DEBUG: Connection error: %s\n",
325 _cupsLangPuts(stderr
,
326 _("ERROR: recoverable: Unable to connect to printer; "
327 "will retry in 30 seconds...\n"));
338 * If we've shown a recoverable error make sure the printer proxies
339 * have a chance to see the recovered message. Not pretty but
340 * necessary for now...
343 fputs("INFO: recovered: \n", stderr
);
347 fputs("STATE: -connecting-to-device\n", stderr
);
348 _cupsLangPrintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
351 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
352 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
353 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
354 ntohs(addr
->addr
.ipv6
.sin6_port
));
356 #endif /* AF_INET6 */
357 if (addr
->addr
.addr
.sa_family
== AF_INET
)
358 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
359 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
360 ntohs(addr
->addr
.ipv4
.sin_port
));
363 * See if the printer supports SNMP...
366 if ((snmp_fd
= _cupsSNMPOpen(addr
->addr
.addr
.sa_family
)) >= 0)
367 if (backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &start_count
, NULL
))
373 _cupsSNMPClose(snmp_fd
);
378 * Print everything...
383 while (copies
> 0 && tbytes
>= 0)
389 fputs("PAGE: 1 1\n", stderr
);
390 lseek(print_fd
, 0, SEEK_SET
);
393 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addr
->addr
), 1,
394 backendNetworkSideCB
);
396 if (print_fd
!= 0 && tbytes
>= 0)
397 _cupsLangPrintf(stderr
,
398 #ifdef HAVE_LONG_LONG
399 _("INFO: Sent print file, %lld bytes...\n"),
401 _("INFO: Sent print file, %ld bytes...\n"),
402 #endif /* HAVE_LONG_LONG */
407 * Get any pending back-channel data...
410 while (wait_bc(device_fd
, 5) > 0);
415 * Shutdown the socket and wait for the other end to finish...
418 _cupsLangPuts(stderr
,
419 _("INFO: Print file sent, waiting for printer to finish...\n"));
421 shutdown(device_fd
, 1);
423 while (wait_bc(device_fd
, 90) > 0);
427 * Collect the final page count as needed...
431 !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &page_count
, NULL
) &&
432 page_count
> start_count
)
433 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
436 * Close the socket connection...
441 httpAddrFreeList(addrlist
);
444 * Close the input file and return...
451 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
453 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
458 * 'wait_bc()' - Wait for back-channel data...
461 static int /* O - # bytes read or -1 on error */
462 wait_bc(int device_fd
, /* I - Socket */
463 int secs
) /* I - Seconds to wait */
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 */
472 * Wait up to "secs" seconds for backchannel data...
475 timeout
.tv_sec
= secs
;
479 FD_SET(device_fd
, &input
);
481 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
484 * Grab the data coming back and spit it out to stderr...
487 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
489 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
491 cupsBackChannelWrite(buffer
, bytes
, 1.0);