]>
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-2009 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 scheme
[255], /* Scheme 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 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 have_supplies
; /* Printer supports supply levels? */
92 ssize_t tbytes
; /* Total number of bytes written */
93 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
94 struct sigaction action
; /* Actions for POSIX signals */
95 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
99 * Make sure status messages are not buffered...
102 setbuf(stderr
, NULL
);
105 * Ignore SIGPIPE signals...
109 sigset(SIGPIPE
, SIG_IGN
);
110 #elif defined(HAVE_SIGACTION)
111 memset(&action
, 0, sizeof(action
));
112 action
.sa_handler
= SIG_IGN
;
113 sigaction(SIGPIPE
, &action
, NULL
);
115 signal(SIGPIPE
, SIG_IGN
);
116 #endif /* HAVE_SIGSET */
119 * Check command-line...
124 printf("network socket \"Unknown\" \"%s\"\n",
125 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
126 return (CUPS_BACKEND_OK
);
128 else if (argc
< 6 || argc
> 7)
130 _cupsLangPrintf(stderr
,
131 _("Usage: %s job-id user title copies options [file]\n"),
133 return (CUPS_BACKEND_FAILED
);
137 * If we have 7 arguments, print the file named on the command-line.
138 * Otherwise, send stdin instead...
149 * Try to open the print file...
152 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
154 _cupsLangPrintf(stderr
,
155 _("ERROR: Unable to open print file \"%s\": %s\n"),
156 argv
[6], strerror(errno
));
157 return (CUPS_BACKEND_FAILED
);
160 copies
= atoi(argv
[4]);
164 * Extract the hostname and port number from the URI...
167 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
168 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 (!strcasecmp(name
, "waiteof"))
235 * Set the wait-for-eof value...
238 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
239 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
241 else if (!strcasecmp(name
, "contimeout"))
244 * Set the connection timeout...
248 contimeout
= atoi(value
);
254 * Then try to connect to the remote host...
257 start_time
= time(NULL
);
259 sprintf(portname
, "%d", port
);
261 fputs("STATE: +connecting-to-device\n", stderr
);
262 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
264 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
266 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'\n"),
268 return (CUPS_BACKEND_STOP
);
271 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
272 _cupsLangPuts(stderr
, _("INFO: Connecting to printer...\n"));
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
);
312 _cupsLangPrintf(stderr
,
313 _("WARNING: Network host \'%s\' is busy; will retry in "
314 "%d seconds...\n"), hostname
, delay
);
323 _cupsLangPrintf(stderr
, "DEBUG: Connection error: %s\n",
325 _cupsLangPuts(stderr
,
326 _("ERROR: Unable to connect to printer; will retry in 30 "
335 fputs("STATE: -connecting-to-device\n", stderr
);
336 _cupsLangPuts(stderr
, _("INFO: Connected to printer...\n"));
339 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
340 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
341 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
342 ntohs(addr
->addr
.ipv6
.sin6_port
));
344 #endif /* AF_INET6 */
345 if (addr
->addr
.addr
.sa_family
== AF_INET
)
346 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
347 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
348 ntohs(addr
->addr
.ipv4
.sin_port
));
351 * See if the printer supports SNMP...
354 if ((snmp_fd
= _cupsSNMPOpen(addr
->addr
.addr
.sa_family
)) >= 0)
356 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &start_count
,
360 have_supplies
= start_count
= 0;
363 * Print everything...
368 while (copies
> 0 && tbytes
>= 0)
374 fputs("PAGE: 1 1\n", stderr
);
375 lseek(print_fd
, 0, SEEK_SET
);
378 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addr
->addr
), 1,
379 backendNetworkSideCB
);
381 if (print_fd
!= 0 && tbytes
>= 0)
382 _cupsLangPrintf(stderr
,
383 #ifdef HAVE_LONG_LONG
384 _("INFO: Sent print file, %lld bytes...\n"),
386 _("INFO: Sent print file, %ld bytes...\n"),
387 #endif /* HAVE_LONG_LONG */
392 * Wait up to 5 seconds to get any pending back-channel data...
395 wait_time
= time(NULL
) + 5;
396 while (wait_time
>= time(¤t_time
))
397 if (wait_bc(device_fd
, wait_time
- current_time
) <= 0)
403 * Shutdown the socket and wait for the other end to finish...
406 _cupsLangPuts(stderr
,
407 _("INFO: Print file sent, waiting for printer to finish...\n"));
409 shutdown(device_fd
, 1);
411 while (wait_bc(device_fd
, 90) > 0);
415 * Collect the final page count as needed...
419 !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &page_count
, NULL
) &&
420 page_count
> start_count
)
421 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
424 * Close the socket connection...
429 httpAddrFreeList(addrlist
);
432 * Close the input file and return...
439 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
441 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
446 * 'wait_bc()' - Wait for back-channel data...
449 static int /* O - # bytes read or -1 on error */
450 wait_bc(int device_fd
, /* I - Socket */
451 int secs
) /* I - Seconds to wait */
453 struct timeval timeout
; /* Timeout for select() */
454 fd_set input
; /* Input set for select() */
455 ssize_t bytes
; /* Number of back-channel bytes read */
456 char buffer
[1024]; /* Back-channel buffer */
460 * Wait up to "secs" seconds for backchannel data...
463 timeout
.tv_sec
= secs
;
467 FD_SET(device_fd
, &input
);
469 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
472 * Grab the data coming back and spit it out to stderr...
475 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
477 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data\n",
479 cupsBackChannelWrite(buffer
, bytes
, 1.0);
490 * End of "$Id: socket.c 7881 2008-08-28 20:21:56Z mike $".