2 * "$Id: socket.c 6649 2007-07-11 21:46:42Z mike $"
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 * side_cb() - Handle side-channel requests...
21 * wait_bc() - Wait for back-channel data...
25 * Include necessary headers.
28 #include <cups/http-private.h>
29 #include "backend-private.h"
31 #include <sys/types.h>
39 # include <sys/socket.h>
40 # include <netinet/in.h>
41 # include <arpa/inet.h>
50 static void side_cb(int print_fd
, int device_fd
, int use_bc
);
51 static int wait_bc(int device_fd
, int secs
);
55 * 'main()' - Send a file to the printer or server.
59 * printer-uri job-id user title copies options [file]
62 int /* O - Exit status */
63 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
64 char *argv
[]) /* I - Command-line arguments */
66 char method
[255], /* Method in URI */
67 hostname
[1024], /* Hostname */
68 username
[255], /* Username info (not used) */
69 resource
[1024], /* Resource info (not used) */
70 *options
, /* Pointer to options */
71 name
[255], /* Name of option */
72 value
[255], /* Value of option */
73 *ptr
; /* Pointer into name or value */
74 int print_fd
; /* Print file */
75 int copies
; /* Number of copies to print */
76 time_t start_time
; /* Time of first connect */
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 ssize_t tbytes
; /* Total number of bytes written */
89 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
90 struct sigaction action
; /* Actions for POSIX signals */
91 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
95 * Make sure status messages are not buffered...
101 * Ignore SIGPIPE signals...
105 sigset(SIGPIPE
, SIG_IGN
);
106 #elif defined(HAVE_SIGACTION)
107 memset(&action
, 0, sizeof(action
));
108 action
.sa_handler
= SIG_IGN
;
109 sigaction(SIGPIPE
, &action
, NULL
);
111 signal(SIGPIPE
, SIG_IGN
);
112 #endif /* HAVE_SIGSET */
115 * Check command-line...
120 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
121 return (CUPS_BACKEND_OK
);
123 else if (argc
< 6 || argc
> 7)
125 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
127 return (CUPS_BACKEND_FAILED
);
131 * If we have 7 arguments, print the file named on the command-line.
132 * Otherwise, send stdin instead...
143 * Try to open the print file...
146 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
148 perror("ERROR: unable to open print file");
149 return (CUPS_BACKEND_FAILED
);
152 copies
= atoi(argv
[4]);
156 * Extract the hostname and port number from the URI...
159 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
160 method
, sizeof(method
), username
, sizeof(username
),
161 hostname
, sizeof(hostname
), &port
,
162 resource
, sizeof(resource
));
165 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
168 * Get options, if any...
172 contimeout
= 7 * 24 * 60 * 60;
174 if ((options
= strchr(resource
, '?')) != NULL
)
177 * Yup, terminate the device name string and move to the first
178 * character of the options...
193 for (ptr
= name
; *options
&& *options
!= '=';)
194 if (ptr
< (name
+ sizeof(name
) - 1))
206 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
207 if (ptr
< (value
+ sizeof(value
) - 1))
211 if (*options
== '+' || *options
== '&')
218 * Process the option...
221 if (!strcasecmp(name
, "waiteof"))
224 * Set the wait-for-eof value...
227 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
228 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
230 else if (!strcasecmp(name
, "contimeout"))
233 * Set the connection timeout...
237 contimeout
= atoi(value
);
243 * Then try to connect to the remote host...
247 start_time
= time(NULL
);
249 sprintf(portname
, "%d", port
);
251 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
253 fprintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"), hostname
);
254 return (CUPS_BACKEND_STOP
);
257 fprintf(stderr
, _("INFO: Attempting to connect to host %s on port %d\n"),
260 fputs("STATE: +connecting-to-device\n", stderr
);
264 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
269 if (getenv("CLASS") != NULL
)
272 * If the CLASS environment variable is set, the job was submitted
273 * to a class and not to a specific queue. In this case, we want
274 * to abort immediately so that the job can be requeued on the next
275 * available printer in the class.
278 fputs(_("INFO: Unable to contact printer, queuing on next "
279 "printer in class...\n"), stderr
);
282 * Sleep 5 seconds to keep the job from requeuing too rapidly...
287 return (CUPS_BACKEND_FAILED
);
290 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
291 error
== EHOSTUNREACH
)
293 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
295 fputs(_("ERROR: Printer not responding!\n"), stderr
);
296 return (CUPS_BACKEND_FAILED
);
302 _("WARNING: recoverable: Network host \'%s\' is busy; will "
303 "retry in %d seconds...\n"),
315 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
316 fputs(_("ERROR: recoverable: Unable to connect to printer; will "
317 "retry in 30 seconds...\n"), stderr
);
328 * If we've shown a recoverable error make sure the printer proxies
329 * have a chance to see the recovered message. Not pretty but
330 * necessary for now...
333 fputs("INFO: recovered: \n", stderr
);
337 fputs("STATE: -connecting-to-device\n", stderr
);
338 fprintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
341 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
342 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
343 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
344 ntohs(addr
->addr
.ipv6
.sin6_port
));
346 #endif /* AF_INET6 */
347 if (addr
->addr
.addr
.sa_family
== AF_INET
)
348 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
349 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
350 ntohs(addr
->addr
.ipv4
.sin_port
));
353 * Print everything...
358 while (copies
> 0 && tbytes
>= 0)
364 fputs("PAGE: 1 1\n", stderr
);
365 lseek(print_fd
, 0, SEEK_SET
);
368 tbytes
= backendRunLoop(print_fd
, device_fd
, 1, side_cb
);
370 if (print_fd
!= 0 && tbytes
>= 0)
372 #ifdef HAVE_LONG_LONG
373 _("INFO: Sent print file, %lld bytes...\n"),
375 _("INFO: Sent print file, %ld bytes...\n"),
376 #endif /* HAVE_LONG_LONG */
381 * Get any pending back-channel data...
384 while (wait_bc(device_fd
, 5) > 0);
389 * Shutdown the socket and wait for the other end to finish...
392 fputs(_("INFO: Print file sent, waiting for printer to finish...\n"),
395 shutdown(device_fd
, 1);
397 while (wait_bc(device_fd
, 90) > 0);
401 * Close the socket connection...
406 httpAddrFreeList(addrlist
);
409 * Close the input file and return...
416 fputs(_("INFO: Ready to print.\n"), stderr
);
418 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
423 * 'side_cb()' - Handle side-channel requests...
427 side_cb(int print_fd
, /* I - Print file */
428 int device_fd
, /* I - Device file */
429 int use_bc
) /* I - Using back-channel? */
431 cups_sc_command_t command
; /* Request command */
432 cups_sc_status_t status
; /* Request/response status */
433 char data
[2048]; /* Request/response data */
434 int datalen
; /* Request/response data size */
437 datalen
= sizeof(data
);
439 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
441 fputs(_("WARNING: Failed to read side-channel request!\n"), stderr
);
447 case CUPS_SC_CMD_DRAIN_OUTPUT
:
449 * Our sockets disable the Nagle algorithm and data is sent immediately.
452 if (backendDrainOutput(print_fd
, device_fd
))
453 status
= CUPS_SC_STATUS_IO_ERROR
;
455 status
= CUPS_SC_STATUS_OK
;
460 case CUPS_SC_CMD_GET_BIDI
:
466 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
471 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
476 * 'wait_bc()' - Wait for back-channel data...
479 static int /* O - # bytes read or -1 on error */
480 wait_bc(int device_fd
, /* I - Socket */
481 int secs
) /* I - Seconds to wait */
483 struct timeval timeout
; /* Timeout for select() */
484 fd_set input
; /* Input set for select() */
485 ssize_t bytes
; /* Number of back-channel bytes read */
486 char buffer
[1024]; /* Back-channel buffer */
490 * Wait up to "secs" seconds for backchannel data...
493 timeout
.tv_sec
= secs
;
497 FD_SET(device_fd
, &input
);
499 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
502 * Grab the data coming back and spit it out to stderr...
505 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
507 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
509 cupsBackChannelWrite(buffer
, bytes
, 1.0);
520 * End of "$Id: socket.c 6649 2007-07-11 21:46:42Z mike $".