2 * "$Id: socket.c 6438 2007-04-03 17:52:41Z mike $"
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the printer or server.
29 * side_cb() - Handle side-channel requests...
30 * wait_bc() - Wait for back-channel data...
34 * Include necessary headers.
37 #include <cups/http-private.h>
38 #include "backend-private.h"
40 #include <sys/types.h>
48 # include <sys/socket.h>
49 # include <netinet/in.h>
50 # include <arpa/inet.h>
59 static void side_cb(int print_fd
, int device_fd
, int use_bc
);
60 static int wait_bc(int device_fd
, int secs
);
64 * 'main()' - Send a file to the printer or server.
68 * printer-uri job-id user title copies options [file]
71 int /* O - Exit status */
72 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
73 char *argv
[]) /* I - Command-line arguments */
75 char method
[255], /* Method in URI */
76 hostname
[1024], /* Hostname */
77 username
[255], /* Username info (not used) */
78 resource
[1024], /* Resource info (not used) */
79 *options
, /* Pointer to options */
80 name
[255], /* Name of option */
81 value
[255], /* Value of option */
82 *ptr
; /* Pointer into name or value */
83 int print_fd
; /* Print file */
84 int copies
; /* Number of copies to print */
85 time_t start_time
; /* Time of first connect */
86 int recoverable
; /* Recoverable error shown? */
87 int contimeout
; /* Connection timeout */
88 int waiteof
; /* Wait for end-of-file? */
89 int port
; /* Port number */
90 char portname
[255]; /* Port name */
91 int delay
; /* Delay for retries... */
92 int device_fd
; /* AppSocket */
93 int error
; /* Error code (if any) */
94 http_addrlist_t
*addrlist
, /* Address list */
95 *addr
; /* Connected address */
96 char addrname
[256]; /* Address name */
97 ssize_t tbytes
; /* Total number of bytes written */
98 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
99 struct sigaction action
; /* Actions for POSIX signals */
100 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
104 * Make sure status messages are not buffered...
107 setbuf(stderr
, NULL
);
110 * Ignore SIGPIPE signals...
114 sigset(SIGPIPE
, SIG_IGN
);
115 #elif defined(HAVE_SIGACTION)
116 memset(&action
, 0, sizeof(action
));
117 action
.sa_handler
= SIG_IGN
;
118 sigaction(SIGPIPE
, &action
, NULL
);
120 signal(SIGPIPE
, SIG_IGN
);
121 #endif /* HAVE_SIGSET */
124 * Check command-line...
129 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
130 return (CUPS_BACKEND_OK
);
132 else if (argc
< 6 || argc
> 7)
134 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
136 return (CUPS_BACKEND_FAILED
);
140 * If we have 7 arguments, print the file named on the command-line.
141 * Otherwise, send stdin instead...
152 * Try to open the print file...
155 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
157 perror("ERROR: unable to open print file");
158 return (CUPS_BACKEND_FAILED
);
161 copies
= atoi(argv
[4]);
165 * Extract the hostname and port number from the URI...
168 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
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...
202 for (ptr
= name
; *options
&& *options
!= '=';)
203 if (ptr
< (name
+ sizeof(name
) - 1))
215 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
216 if (ptr
< (value
+ sizeof(value
) - 1))
220 if (*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 fprintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"), hostname
);
263 return (CUPS_BACKEND_STOP
);
266 fprintf(stderr
, _("INFO: Attempting to connect to host %s on port %d\n"),
269 fputs("STATE: +connecting-to-device\n", stderr
);
273 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
278 if (getenv("CLASS") != NULL
)
281 * If the CLASS environment variable is set, the job was submitted
282 * to a class and not to a specific queue. In this case, we want
283 * to abort immediately so that the job can be requeued on the next
284 * available printer in the class.
287 fputs(_("INFO: Unable to contact printer, queuing on next "
288 "printer in class...\n"), stderr
);
291 * Sleep 5 seconds to keep the job from requeuing too rapidly...
296 return (CUPS_BACKEND_FAILED
);
299 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
300 error
== EHOSTUNREACH
)
302 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
304 fputs(_("ERROR: Printer not responding!\n"), stderr
);
305 return (CUPS_BACKEND_FAILED
);
311 _("WARNING: recoverable: Network host \'%s\' is busy; will "
312 "retry in %d seconds...\n"),
324 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
325 fputs(_("ERROR: recoverable: Unable to connect to printer; will "
326 "retry in 30 seconds...\n"), stderr
);
337 * If we've shown a recoverable error make sure the printer proxies
338 * have a chance to see the recovered message. Not pretty but
339 * necessary for now...
342 fputs("INFO: recovered: \n", stderr
);
346 fputs("STATE: -connecting-to-device\n", stderr
);
347 fprintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
350 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
351 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
352 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
353 ntohs(addr
->addr
.ipv6
.sin6_port
));
355 #endif /* AF_INET6 */
356 if (addr
->addr
.addr
.sa_family
== AF_INET
)
357 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
358 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
359 ntohs(addr
->addr
.ipv4
.sin_port
));
362 * Print everything...
367 while (copies
> 0 && tbytes
>= 0)
373 fputs("PAGE: 1 1\n", stderr
);
374 lseek(print_fd
, 0, SEEK_SET
);
377 tbytes
= backendRunLoop(print_fd
, device_fd
, 1, side_cb
);
379 if (print_fd
!= 0 && tbytes
>= 0)
381 #ifdef HAVE_LONG_LONG
382 _("INFO: Sent print file, %lld bytes...\n"),
384 _("INFO: Sent print file, %ld bytes...\n"),
385 #endif /* HAVE_LONG_LONG */
390 * Get any pending back-channel data...
393 while (wait_bc(device_fd
, 5) > 0);
398 * Shutdown the socket and wait for the other end to finish...
401 fputs(_("INFO: Print file sent, waiting for printer to finish...\n"),
404 shutdown(device_fd
, 1);
406 while (wait_bc(device_fd
, 90) > 0);
410 * Close the socket connection...
415 httpAddrFreeList(addrlist
);
418 * Close the input file and return...
425 fputs(_("INFO: Ready to print.\n"), stderr
);
427 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
432 * 'side_cb()' - Handle side-channel requests...
436 side_cb(int print_fd
, /* I - Print file */
437 int device_fd
, /* I - Device file */
438 int use_bc
) /* I - Using back-channel? */
440 cups_sc_command_t command
; /* Request command */
441 cups_sc_status_t status
; /* Request/response status */
442 char data
[2048]; /* Request/response data */
443 int datalen
; /* Request/response data size */
446 datalen
= sizeof(data
);
448 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
450 fputs(_("WARNING: Failed to read side-channel request!\n"), stderr
);
456 case CUPS_SC_CMD_DRAIN_OUTPUT
:
458 * Our sockets disable the Nagle algorithm and data is sent immediately.
461 status
= CUPS_SC_STATUS_OK
;
465 case CUPS_SC_CMD_GET_BIDI
:
471 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
476 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
481 * 'wait_bc()' - Wait for back-channel data...
484 static int /* O - # bytes read or -1 on error */
485 wait_bc(int device_fd
, /* I - Socket */
486 int secs
) /* I - Seconds to wait */
488 struct timeval timeout
; /* Timeout for select() */
489 fd_set input
; /* Input set for select() */
490 ssize_t bytes
; /* Number of back-channel bytes read */
491 char buffer
[1024]; /* Back-channel buffer */
495 * Wait up to "secs" seconds for backchannel data...
498 timeout
.tv_sec
= secs
;
502 FD_SET(device_fd
, &input
);
504 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
507 * Grab the data coming back and spit it out to stderr...
510 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
512 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
514 cupsBackChannelWrite(buffer
, bytes
, 1.0);
525 * End of "$Id: socket.c 6438 2007-04-03 17:52:41Z mike $".