]>
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 const char *device_uri
; /* Device URI */
65 char method
[255], /* Method in URI */
66 hostname
[1024], /* Hostname */
67 username
[255], /* Username info (not used) */
68 resource
[1024], /* Resource info (not used) */
69 *options
, /* Pointer to options */
70 *name
, /* Name of option */
71 *value
, /* Value of option */
72 sep
; /* Option separator */
73 int print_fd
; /* Print file */
74 int copies
; /* Number of copies to print */
75 time_t start_time
, /* Time of first connect */
76 wait_time
; /* Time to wait before shutting down socket */
77 int recoverable
; /* Recoverable error shown? */
78 int contimeout
; /* Connection timeout */
79 int waiteof
; /* Wait for end-of-file? */
80 int port
; /* Port number */
81 char portname
[255]; /* Port name */
82 int delay
; /* Delay for retries... */
83 int device_fd
; /* AppSocket */
84 int error
; /* Error code (if any) */
85 http_addrlist_t
*addrlist
, /* Address list */
86 *addr
; /* Connected address */
87 char addrname
[256]; /* Address name */
88 int snmp_fd
, /* SNMP socket */
89 start_count
, /* Page count via SNMP at start */
90 page_count
; /* Page count via SNMP */
91 ssize_t tbytes
; /* Total number of bytes written */
92 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
93 struct sigaction action
; /* Actions for POSIX signals */
94 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
98 * Make sure status messages are not buffered...
101 setbuf(stderr
, NULL
);
104 * Ignore SIGPIPE signals...
108 sigset(SIGPIPE
, SIG_IGN
);
109 #elif defined(HAVE_SIGACTION)
110 memset(&action
, 0, sizeof(action
));
111 action
.sa_handler
= SIG_IGN
;
112 sigaction(SIGPIPE
, &action
, NULL
);
114 signal(SIGPIPE
, SIG_IGN
);
115 #endif /* HAVE_SIGSET */
118 * Check command-line...
123 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
124 return (CUPS_BACKEND_OK
);
126 else if (argc
< 6 || argc
> 7)
128 _cupsLangPrintf(stderr
,
129 _("Usage: %s job-id user title copies options [file]\n"),
131 return (CUPS_BACKEND_FAILED
);
135 * If we have 7 arguments, print the file named on the command-line.
136 * Otherwise, send stdin instead...
147 * Try to open the print file...
150 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
152 _cupsLangPrintf(stderr
,
153 _("ERROR: Unable to open print file \"%s\": %s\n"),
154 argv
[6], strerror(errno
));
155 return (CUPS_BACKEND_FAILED
);
158 copies
= atoi(argv
[4]);
162 * Extract the hostname and port number from the URI...
165 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
166 return (CUPS_BACKEND_FAILED
);
168 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
,
169 method
, sizeof(method
), username
, sizeof(username
),
170 hostname
, sizeof(hostname
), &port
,
171 resource
, sizeof(resource
));
174 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
177 * Get options, if any...
181 contimeout
= 7 * 24 * 60 * 60;
183 if ((options
= strchr(resource
, '?')) != NULL
)
186 * Yup, terminate the device name string and move to the first
187 * character of the options...
204 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
207 if ((sep
= *options
) != '\0')
218 while (*options
&& *options
!= '+' && *options
!= '&')
228 * Process the option...
231 if (!strcasecmp(name
, "waiteof"))
234 * Set the wait-for-eof value...
237 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
238 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
240 else if (!strcasecmp(name
, "contimeout"))
243 * Set the connection timeout...
247 contimeout
= atoi(value
);
253 * Then try to connect to the remote host...
257 start_time
= time(NULL
);
259 sprintf(portname
, "%d", port
);
261 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
263 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
265 return (CUPS_BACKEND_STOP
);
268 _cupsLangPrintf(stderr
,
269 _("INFO: Attempting to connect to host %s on port %d\n"),
272 fputs("STATE: +connecting-to-device\n", stderr
);
276 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
281 if (getenv("CLASS") != NULL
)
284 * If the CLASS environment variable is set, the job was submitted
285 * to a class and not to a specific queue. In this case, we want
286 * to abort immediately so that the job can be requeued on the next
287 * available printer in the class.
290 _cupsLangPuts(stderr
,
291 _("INFO: Unable to contact printer, queuing on next "
292 "printer in class...\n"));
295 * Sleep 5 seconds to keep the job from requeuing too rapidly...
300 return (CUPS_BACKEND_FAILED
);
303 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
304 error
== EHOSTUNREACH
)
306 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
308 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
309 return (CUPS_BACKEND_FAILED
);
314 _cupsLangPrintf(stderr
,
315 _("WARNING: recoverable: Network host \'%s\' is busy; "
316 "will retry in %d seconds...\n"),
328 _cupsLangPrintf(stderr
, "DEBUG: Connection error: %s\n",
330 _cupsLangPuts(stderr
,
331 _("ERROR: recoverable: Unable to connect to printer; "
332 "will retry in 30 seconds...\n"));
343 * If we've shown a recoverable error make sure the printer proxies
344 * have a chance to see the recovered message. Not pretty but
345 * necessary for now...
348 fputs("INFO: recovered: \n", stderr
);
352 fputs("STATE: -connecting-to-device\n", stderr
);
353 _cupsLangPrintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
356 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
357 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
358 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
359 ntohs(addr
->addr
.ipv6
.sin6_port
));
361 #endif /* AF_INET6 */
362 if (addr
->addr
.addr
.sa_family
== AF_INET
)
363 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
364 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
365 ntohs(addr
->addr
.ipv4
.sin_port
));
368 * See if the printer supports SNMP...
371 if ((snmp_fd
= _cupsSNMPOpen(addr
->addr
.addr
.sa_family
)) >= 0)
373 if (backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &start_count
, NULL
))
379 _cupsSNMPClose(snmp_fd
);
387 * Print everything...
392 while (copies
> 0 && tbytes
>= 0)
398 fputs("PAGE: 1 1\n", stderr
);
399 lseek(print_fd
, 0, SEEK_SET
);
402 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addr
->addr
), 1,
403 backendNetworkSideCB
);
405 if (print_fd
!= 0 && tbytes
>= 0)
406 _cupsLangPrintf(stderr
,
407 #ifdef HAVE_LONG_LONG
408 _("INFO: Sent print file, %lld bytes...\n"),
410 _("INFO: Sent print file, %ld bytes...\n"),
411 #endif /* HAVE_LONG_LONG */
416 * Wait up to 5 seconds to get any pending back-channel data...
419 wait_time
= time(NULL
) + 5;
420 while (wait_time
>= time(NULL
))
421 if (wait_bc(device_fd
, 1) <= 0)
427 * Shutdown the socket and wait for the other end to finish...
430 _cupsLangPuts(stderr
,
431 _("INFO: Print file sent, waiting for printer to finish...\n"));
433 shutdown(device_fd
, 1);
435 while (wait_bc(device_fd
, 90) > 0);
439 * Collect the final page count as needed...
443 !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &page_count
, NULL
) &&
444 page_count
> start_count
)
445 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
448 * Close the socket connection...
453 httpAddrFreeList(addrlist
);
456 * Close the input file and return...
463 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
465 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
470 * 'wait_bc()' - Wait for back-channel data...
473 static int /* O - # bytes read or -1 on error */
474 wait_bc(int device_fd
, /* I - Socket */
475 int secs
) /* I - Seconds to wait */
477 struct timeval timeout
; /* Timeout for select() */
478 fd_set input
; /* Input set for select() */
479 ssize_t bytes
; /* Number of back-channel bytes read */
480 char buffer
[1024]; /* Back-channel buffer */
484 * Wait up to "secs" seconds for backchannel data...
487 timeout
.tv_sec
= secs
;
491 FD_SET(device_fd
, &input
);
493 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
496 * Grab the data coming back and spit it out to stderr...
499 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
501 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
503 cupsBackChannelWrite(buffer
, bytes
, 1.0);