]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * "$Id: socket.c 7583 2008-05-16 17:47:16Z mike $"
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 int recoverable
; /* Recoverable error shown? */
77 int contimeout
; /* Connection timeout */
78 int waiteof
; /* Wait for end-of-file? */
79 int port
; /* Port number */
80 char portname
[255]; /* Port name */
81 int delay
; /* Delay for retries... */
82 int device_fd
; /* AppSocket */
83 int error
; /* Error code (if any) */
84 http_addrlist_t
*addrlist
, /* Address list */
85 *addr
; /* Connected address */
86 char addrname
[256]; /* Address name */
87 int snmp_fd
, /* SNMP socket */
88 start_count
, /* Page count via SNMP at start */
89 page_count
; /* Page count via SNMP */
90 ssize_t tbytes
; /* Total number of bytes written */
91 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
92 struct sigaction action
; /* Actions for POSIX signals */
93 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
97 * Make sure status messages are not buffered...
100 setbuf(stderr
, NULL
);
103 * Ignore SIGPIPE signals...
107 sigset(SIGPIPE
, SIG_IGN
);
108 #elif defined(HAVE_SIGACTION)
109 memset(&action
, 0, sizeof(action
));
110 action
.sa_handler
= SIG_IGN
;
111 sigaction(SIGPIPE
, &action
, NULL
);
113 signal(SIGPIPE
, SIG_IGN
);
114 #endif /* HAVE_SIGSET */
117 * Check command-line...
122 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
123 return (CUPS_BACKEND_OK
);
125 else if (argc
< 6 || argc
> 7)
127 _cupsLangPrintf(stderr
,
128 _("Usage: %s job-id user title copies options [file]\n"),
130 return (CUPS_BACKEND_FAILED
);
134 * If we have 7 arguments, print the file named on the command-line.
135 * Otherwise, send stdin instead...
146 * Try to open the print file...
149 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
151 _cupsLangPrintf(stderr
,
152 _("ERROR: Unable to open print file \"%s\": %s\n"),
153 argv
[6], strerror(errno
));
154 return (CUPS_BACKEND_FAILED
);
157 copies
= atoi(argv
[4]);
161 * Extract the hostname and port number from the URI...
164 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
165 return (CUPS_BACKEND_FAILED
);
167 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
,
168 method
, sizeof(method
), username
, sizeof(username
),
169 hostname
, sizeof(hostname
), &port
,
170 resource
, sizeof(resource
));
173 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
176 * Get options, if any...
180 contimeout
= 7 * 24 * 60 * 60;
182 if ((options
= strchr(resource
, '?')) != NULL
)
185 * Yup, terminate the device name string and move to the first
186 * character of the options...
203 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
206 if ((sep
= *options
) != '\0')
217 while (*options
&& *options
!= '+' && *options
!= '&')
227 * Process the option...
230 if (!strcasecmp(name
, "waiteof"))
233 * Set the wait-for-eof value...
236 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
237 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
239 else if (!strcasecmp(name
, "contimeout"))
242 * Set the connection timeout...
246 contimeout
= atoi(value
);
252 * Then try to connect to the remote host...
256 start_time
= time(NULL
);
258 sprintf(portname
, "%d", port
);
260 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
262 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
264 return (CUPS_BACKEND_STOP
);
267 _cupsLangPrintf(stderr
,
268 _("INFO: Attempting to connect to host %s on port %d\n"),
271 fputs("STATE: +connecting-to-device\n", stderr
);
275 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
280 if (getenv("CLASS") != NULL
)
283 * If the CLASS environment variable is set, the job was submitted
284 * to a class and not to a specific queue. In this case, we want
285 * to abort immediately so that the job can be requeued on the next
286 * available printer in the class.
289 _cupsLangPuts(stderr
,
290 _("INFO: Unable to contact printer, queuing on next "
291 "printer in class...\n"));
294 * Sleep 5 seconds to keep the job from requeuing too rapidly...
299 return (CUPS_BACKEND_FAILED
);
302 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
303 error
== EHOSTUNREACH
)
305 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
307 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
308 return (CUPS_BACKEND_FAILED
);
313 _cupsLangPrintf(stderr
,
314 _("WARNING: recoverable: Network host \'%s\' is busy; "
315 "will retry in %d seconds...\n"),
327 _cupsLangPrintf(stderr
, "DEBUG: Connection error: %s\n",
329 _cupsLangPuts(stderr
,
330 _("ERROR: recoverable: Unable to connect to printer; "
331 "will retry in 30 seconds...\n"));
342 * If we've shown a recoverable error make sure the printer proxies
343 * have a chance to see the recovered message. Not pretty but
344 * necessary for now...
347 fputs("INFO: recovered: \n", stderr
);
351 fputs("STATE: -connecting-to-device\n", stderr
);
352 _cupsLangPrintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
355 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
356 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
357 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
358 ntohs(addr
->addr
.ipv6
.sin6_port
));
360 #endif /* AF_INET6 */
361 if (addr
->addr
.addr
.sa_family
== AF_INET
)
362 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
363 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
364 ntohs(addr
->addr
.ipv4
.sin_port
));
367 * See if the printer supports SNMP...
370 if ((snmp_fd
= _cupsSNMPOpen(addr
->addr
.addr
.sa_family
)) >= 0)
372 if (backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &start_count
, NULL
))
378 _cupsSNMPClose(snmp_fd
);
386 * Print everything...
391 while (copies
> 0 && tbytes
>= 0)
397 fputs("PAGE: 1 1\n", stderr
);
398 lseek(print_fd
, 0, SEEK_SET
);
401 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addr
->addr
), 1,
402 backendNetworkSideCB
);
404 if (print_fd
!= 0 && tbytes
>= 0)
405 _cupsLangPrintf(stderr
,
406 #ifdef HAVE_LONG_LONG
407 _("INFO: Sent print file, %lld bytes...\n"),
409 _("INFO: Sent print file, %ld bytes...\n"),
410 #endif /* HAVE_LONG_LONG */
415 * Get any pending back-channel data...
418 while (wait_bc(device_fd
, 5) > 0);
423 * Shutdown the socket and wait for the other end to finish...
426 _cupsLangPuts(stderr
,
427 _("INFO: Print file sent, waiting for printer to finish...\n"));
429 shutdown(device_fd
, 1);
431 while (wait_bc(device_fd
, 90) > 0);
435 * Collect the final page count as needed...
439 !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &page_count
, NULL
) &&
440 page_count
> start_count
)
441 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
444 * Close the socket connection...
449 httpAddrFreeList(addrlist
);
452 * Close the input file and return...
459 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
461 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
466 * 'wait_bc()' - Wait for back-channel data...
469 static int /* O - # bytes read or -1 on error */
470 wait_bc(int device_fd
, /* I - Socket */
471 int secs
) /* I - Seconds to wait */
473 struct timeval timeout
; /* Timeout for select() */
474 fd_set input
; /* Input set for select() */
475 ssize_t bytes
; /* Number of back-channel bytes read */
476 char buffer
[1024]; /* Back-channel buffer */
480 * Wait up to "secs" seconds for backchannel data...
483 timeout
.tv_sec
= secs
;
487 FD_SET(device_fd
, &input
);
489 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
492 * Grab the data coming back and spit it out to stderr...
495 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
497 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
499 cupsBackChannelWrite(buffer
, bytes
, 1.0);
510 * End of "$Id: socket.c 7583 2008-05-16 17:47:16Z mike $".