]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * "$Id: socket.c 7881 2008-08-28 20:21:56Z 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 current_time
, /* Current time */
77 wait_time
; /* Time to wait before shutting down socket */
78 int recoverable
; /* Recoverable error shown? */
79 int contimeout
; /* Connection timeout */
80 int waiteof
; /* Wait for end-of-file? */
81 int port
; /* Port number */
82 char portname
[255]; /* Port name */
83 int delay
; /* Delay for retries... */
84 int device_fd
; /* AppSocket */
85 int error
; /* Error code (if any) */
86 http_addrlist_t
*addrlist
, /* Address list */
87 *addr
; /* Connected address */
88 char addrname
[256]; /* Address name */
89 int snmp_fd
, /* SNMP socket */
90 start_count
, /* Page count via SNMP at start */
91 page_count
, /* Page count via SNMP */
92 have_supplies
; /* Printer supports supply levels? */
93 ssize_t tbytes
; /* Total number of bytes written */
94 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
95 struct sigaction action
; /* Actions for POSIX signals */
96 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
100 * Make sure status messages are not buffered...
103 setbuf(stderr
, NULL
);
106 * Ignore SIGPIPE signals...
110 sigset(SIGPIPE
, SIG_IGN
);
111 #elif defined(HAVE_SIGACTION)
112 memset(&action
, 0, sizeof(action
));
113 action
.sa_handler
= SIG_IGN
;
114 sigaction(SIGPIPE
, &action
, NULL
);
116 signal(SIGPIPE
, SIG_IGN
);
117 #endif /* HAVE_SIGSET */
120 * Check command-line...
125 printf("network socket \"Unknown\" \"%s\"\n",
126 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
127 return (CUPS_BACKEND_OK
);
129 else if (argc
< 6 || argc
> 7)
131 _cupsLangPrintf(stderr
,
132 _("Usage: %s job-id user title copies options [file]\n"),
134 return (CUPS_BACKEND_FAILED
);
138 * If we have 7 arguments, print the file named on the command-line.
139 * Otherwise, send stdin instead...
150 * Try to open the print file...
153 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
155 _cupsLangPrintf(stderr
,
156 _("ERROR: Unable to open print file \"%s\": %s\n"),
157 argv
[6], strerror(errno
));
158 return (CUPS_BACKEND_FAILED
);
161 copies
= atoi(argv
[4]);
165 * Extract the hostname and port number from the URI...
168 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
169 return (CUPS_BACKEND_FAILED
);
171 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
,
172 method
, sizeof(method
), username
, sizeof(username
),
173 hostname
, sizeof(hostname
), &port
,
174 resource
, sizeof(resource
));
177 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
180 * Get options, if any...
184 contimeout
= 7 * 24 * 60 * 60;
186 if ((options
= strchr(resource
, '?')) != NULL
)
189 * Yup, terminate the device name string and move to the first
190 * character of the options...
207 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
210 if ((sep
= *options
) != '\0')
221 while (*options
&& *options
!= '+' && *options
!= '&')
231 * Process the option...
234 if (!strcasecmp(name
, "waiteof"))
237 * Set the wait-for-eof value...
240 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
241 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
243 else if (!strcasecmp(name
, "contimeout"))
246 * Set the connection timeout...
250 contimeout
= atoi(value
);
256 * Then try to connect to the remote host...
260 start_time
= time(NULL
);
262 sprintf(portname
, "%d", port
);
264 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
266 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
268 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
270 return (CUPS_BACKEND_STOP
);
273 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n",
275 _cupsLangPuts(stderr
, _("INFO: Connecting to printer...\n"));
277 fputs("STATE: +connecting-to-device\n", stderr
);
281 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
286 if (getenv("CLASS") != NULL
)
289 * If the CLASS environment variable is set, the job was submitted
290 * to a class and not to a specific queue. In this case, we want
291 * to abort immediately so that the job can be requeued on the next
292 * available printer in the class.
295 _cupsLangPuts(stderr
,
296 _("INFO: Unable to contact printer, queuing on next "
297 "printer in class...\n"));
300 * Sleep 5 seconds to keep the job from requeuing too rapidly...
305 return (CUPS_BACKEND_FAILED
);
308 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
309 error
== EHOSTUNREACH
)
311 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
313 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
314 return (CUPS_BACKEND_FAILED
);
319 _cupsLangPrintf(stderr
,
320 _("WARNING: recoverable: Network host \'%s\' is busy; "
321 "will retry in %d seconds...\n"),
333 _cupsLangPrintf(stderr
, "DEBUG: Connection error: %s\n",
335 _cupsLangPuts(stderr
,
336 _("ERROR: recoverable: Unable to connect to printer; "
337 "will retry in 30 seconds...\n"));
348 * If we've shown a recoverable error make sure the printer proxies
349 * have a chance to see the recovered message. Not pretty but
350 * necessary for now...
353 fputs("INFO: recovered: \n", stderr
);
357 fputs("STATE: -connecting-to-device\n", stderr
);
358 _cupsLangPuts(stderr
, _("INFO: Connected to printer...\n"));
361 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
362 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
363 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
364 ntohs(addr
->addr
.ipv6
.sin6_port
));
366 #endif /* AF_INET6 */
367 if (addr
->addr
.addr
.sa_family
== AF_INET
)
368 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
369 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
370 ntohs(addr
->addr
.ipv4
.sin_port
));
373 * See if the printer supports SNMP...
376 if ((snmp_fd
= _cupsSNMPOpen(addr
->addr
.addr
.sa_family
)) >= 0)
378 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &start_count
,
382 have_supplies
= start_count
= 0;
385 * Print everything...
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
, &(addr
->addr
), 1,
401 backendNetworkSideCB
);
403 if (print_fd
!= 0 && tbytes
>= 0)
404 _cupsLangPrintf(stderr
,
405 #ifdef HAVE_LONG_LONG
406 _("INFO: Sent print file, %lld bytes...\n"),
408 _("INFO: Sent print file, %ld bytes...\n"),
409 #endif /* HAVE_LONG_LONG */
414 * Wait up to 5 seconds to get any pending back-channel data...
417 wait_time
= time(NULL
) + 5;
418 while (wait_time
>= time(¤t_time
))
419 if (wait_bc(device_fd
, wait_time
- current_time
) <= 0)
425 * Shutdown the socket and wait for the other end to finish...
428 _cupsLangPuts(stderr
,
429 _("INFO: Print file sent, waiting for printer to finish...\n"));
431 shutdown(device_fd
, 1);
433 while (wait_bc(device_fd
, 90) > 0);
437 * Collect the final page count as needed...
441 !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &page_count
, NULL
) &&
442 page_count
> start_count
)
443 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
446 * Close the socket connection...
451 httpAddrFreeList(addrlist
);
454 * Close the input file and return...
461 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
463 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
468 * 'wait_bc()' - Wait for back-channel data...
471 static int /* O - # bytes read or -1 on error */
472 wait_bc(int device_fd
, /* I - Socket */
473 int secs
) /* I - Seconds to wait */
475 struct timeval timeout
; /* Timeout for select() */
476 fd_set input
; /* Input set for select() */
477 ssize_t bytes
; /* Number of back-channel bytes read */
478 char buffer
[1024]; /* Back-channel buffer */
482 * Wait up to "secs" seconds for backchannel data...
485 timeout
.tv_sec
= secs
;
489 FD_SET(device_fd
, &input
);
491 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
494 * Grab the data coming back and spit it out to stderr...
497 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
499 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
501 cupsBackChannelWrite(buffer
, bytes
, 1.0);
512 * End of "$Id: socket.c 7881 2008-08-28 20:21:56Z mike $".