2 * "$Id: socket.c 6911 2007-09-04 20:35:08Z 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
, /* Name of option */
72 *value
, /* Value of option */
73 sep
; /* Option separator */
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 _cupsLangPrintf(stderr
,
126 _("Usage: %s job-id user title copies options [file]\n"),
128 return (CUPS_BACKEND_FAILED
);
132 * If we have 7 arguments, print the file named on the command-line.
133 * Otherwise, send stdin instead...
144 * Try to open the print file...
147 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
149 perror("ERROR: unable to open print file");
150 return (CUPS_BACKEND_FAILED
);
153 copies
= atoi(argv
[4]);
157 * Extract the hostname and port number from the URI...
160 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
161 method
, sizeof(method
), username
, sizeof(username
),
162 hostname
, sizeof(hostname
), &port
,
163 resource
, sizeof(resource
));
166 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
169 * Get options, if any...
173 contimeout
= 7 * 24 * 60 * 60;
175 if ((options
= strchr(resource
, '?')) != NULL
)
178 * Yup, terminate the device name string and move to the first
179 * character of the options...
196 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
199 if ((sep
= *options
) != '\0')
210 while (*options
&& *options
!= '+' && *options
!= '&')
220 * Process the option...
223 if (!strcasecmp(name
, "waiteof"))
226 * Set the wait-for-eof value...
229 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
230 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
232 else if (!strcasecmp(name
, "contimeout"))
235 * Set the connection timeout...
239 contimeout
= atoi(value
);
245 * Then try to connect to the remote host...
249 start_time
= time(NULL
);
251 sprintf(portname
, "%d", port
);
253 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
255 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
257 return (CUPS_BACKEND_STOP
);
260 _cupsLangPrintf(stderr
,
261 _("INFO: Attempting to connect to host %s on port %d\n"),
264 fputs("STATE: +connecting-to-device\n", stderr
);
268 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
273 if (getenv("CLASS") != NULL
)
276 * If the CLASS environment variable is set, the job was submitted
277 * to a class and not to a specific queue. In this case, we want
278 * to abort immediately so that the job can be requeued on the next
279 * available printer in the class.
282 _cupsLangPuts(stderr
,
283 _("INFO: Unable to contact printer, queuing on next "
284 "printer in class...\n"));
287 * Sleep 5 seconds to keep the job from requeuing too rapidly...
292 return (CUPS_BACKEND_FAILED
);
295 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
296 error
== EHOSTUNREACH
)
298 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
300 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
301 return (CUPS_BACKEND_FAILED
);
306 _cupsLangPrintf(stderr
,
307 _("WARNING: recoverable: Network host \'%s\' is busy; "
308 "will retry in %d seconds...\n"),
320 _cupsLangPrintf(stderr
, "DEBUG: Connection error: %s\n",
322 _cupsLangPuts(stderr
,
323 _("ERROR: recoverable: Unable to connect to printer; "
324 "will retry in 30 seconds...\n"));
335 * If we've shown a recoverable error make sure the printer proxies
336 * have a chance to see the recovered message. Not pretty but
337 * necessary for now...
340 fputs("INFO: recovered: \n", stderr
);
344 fputs("STATE: -connecting-to-device\n", stderr
);
345 _cupsLangPrintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
348 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
349 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
350 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
351 ntohs(addr
->addr
.ipv6
.sin6_port
));
353 #endif /* AF_INET6 */
354 if (addr
->addr
.addr
.sa_family
== AF_INET
)
355 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
356 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
357 ntohs(addr
->addr
.ipv4
.sin_port
));
360 * Print everything...
365 while (copies
> 0 && tbytes
>= 0)
371 fputs("PAGE: 1 1\n", stderr
);
372 lseek(print_fd
, 0, SEEK_SET
);
375 tbytes
= backendRunLoop(print_fd
, device_fd
, 1, side_cb
);
377 if (print_fd
!= 0 && tbytes
>= 0)
378 _cupsLangPrintf(stderr
,
379 #ifdef HAVE_LONG_LONG
380 _("INFO: Sent print file, %lld bytes...\n"),
382 _("INFO: Sent print file, %ld bytes...\n"),
383 #endif /* HAVE_LONG_LONG */
388 * Get any pending back-channel data...
391 while (wait_bc(device_fd
, 5) > 0);
396 * Shutdown the socket and wait for the other end to finish...
399 _cupsLangPuts(stderr
,
400 _("INFO: Print file sent, waiting for printer to finish...\n"));
402 shutdown(device_fd
, 1);
404 while (wait_bc(device_fd
, 90) > 0);
408 * Close the socket connection...
413 httpAddrFreeList(addrlist
);
416 * Close the input file and return...
423 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
425 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
430 * 'side_cb()' - Handle side-channel requests...
434 side_cb(int print_fd
, /* I - Print file */
435 int device_fd
, /* I - Device file */
436 int use_bc
) /* I - Using back-channel? */
438 cups_sc_command_t command
; /* Request command */
439 cups_sc_status_t status
; /* Request/response status */
440 char data
[2048]; /* Request/response data */
441 int datalen
; /* Request/response data size */
442 const char *device_id
; /* 1284DEVICEID env var */
445 datalen
= sizeof(data
);
447 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
449 _cupsLangPuts(stderr
, _("WARNING: Failed to read side-channel request!\n"));
455 case CUPS_SC_CMD_DRAIN_OUTPUT
:
457 * Our sockets disable the Nagle algorithm and data is sent immediately.
460 if (backendDrainOutput(print_fd
, device_fd
))
461 status
= CUPS_SC_STATUS_IO_ERROR
;
463 status
= CUPS_SC_STATUS_OK
;
468 case CUPS_SC_CMD_GET_BIDI
:
473 case CUPS_SC_CMD_GET_DEVICE_ID
:
474 if ((device_id
= getenv("1284DEVICEID")) != NULL
)
476 strlcpy(data
, device_id
, sizeof(data
));
477 datalen
= (int)strlen(data
);
482 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
487 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
492 * 'wait_bc()' - Wait for back-channel data...
495 static int /* O - # bytes read or -1 on error */
496 wait_bc(int device_fd
, /* I - Socket */
497 int secs
) /* I - Seconds to wait */
499 struct timeval timeout
; /* Timeout for select() */
500 fd_set input
; /* Input set for select() */
501 ssize_t bytes
; /* Number of back-channel bytes read */
502 char buffer
[1024]; /* Back-channel buffer */
506 * Wait up to "secs" seconds for backchannel data...
509 timeout
.tv_sec
= secs
;
513 FD_SET(device_fd
, &input
);
515 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
518 * Grab the data coming back and spit it out to stderr...
521 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
523 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
525 cupsBackChannelWrite(buffer
, bytes
, 1.0);
536 * End of "$Id: socket.c 6911 2007-09-04 20:35:08Z mike $".