2 * "$Id: socket.c 6910 2007-09-04 20:34:29Z 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 * 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 _cupsLangPrintf(stderr
,
150 _("ERROR: Unable to open print file \"%s\": %s\n"),
151 argv
[6], strerror(errno
));
152 return (CUPS_BACKEND_FAILED
);
155 copies
= atoi(argv
[4]);
159 * Extract the hostname and port number from the URI...
162 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
163 method
, sizeof(method
), username
, sizeof(username
),
164 hostname
, sizeof(hostname
), &port
,
165 resource
, sizeof(resource
));
168 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
171 * Get options, if any...
175 contimeout
= 7 * 24 * 60 * 60;
177 if ((options
= strchr(resource
, '?')) != NULL
)
180 * Yup, terminate the device name string and move to the first
181 * character of the options...
198 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
201 if ((sep
= *options
) != '\0')
212 while (*options
&& *options
!= '+' && *options
!= '&')
222 * Process the option...
225 if (!strcasecmp(name
, "waiteof"))
228 * Set the wait-for-eof value...
231 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
232 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
234 else if (!strcasecmp(name
, "contimeout"))
237 * Set the connection timeout...
241 contimeout
= atoi(value
);
247 * Then try to connect to the remote host...
251 start_time
= time(NULL
);
253 sprintf(portname
, "%d", port
);
255 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
257 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
259 return (CUPS_BACKEND_STOP
);
262 _cupsLangPrintf(stderr
,
263 _("INFO: Attempting to connect to host %s on port %d\n"),
266 fputs("STATE: +connecting-to-device\n", stderr
);
270 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
275 if (getenv("CLASS") != NULL
)
278 * If the CLASS environment variable is set, the job was submitted
279 * to a class and not to a specific queue. In this case, we want
280 * to abort immediately so that the job can be requeued on the next
281 * available printer in the class.
284 _cupsLangPuts(stderr
,
285 _("INFO: Unable to contact printer, queuing on next "
286 "printer in class...\n"));
289 * Sleep 5 seconds to keep the job from requeuing too rapidly...
294 return (CUPS_BACKEND_FAILED
);
297 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
298 error
== EHOSTUNREACH
)
300 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
302 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
303 return (CUPS_BACKEND_FAILED
);
308 _cupsLangPrintf(stderr
,
309 _("WARNING: recoverable: Network host \'%s\' is busy; "
310 "will retry in %d seconds...\n"),
322 _cupsLangPrintf(stderr
, "DEBUG: Connection error: %s\n",
324 _cupsLangPuts(stderr
,
325 _("ERROR: recoverable: Unable to connect to printer; "
326 "will retry in 30 seconds...\n"));
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 _cupsLangPrintf(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)
380 _cupsLangPrintf(stderr
,
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 _cupsLangPuts(stderr
,
402 _("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 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
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 */
444 const char *device_id
; /* 1284DEVICEID env var */
447 datalen
= sizeof(data
);
449 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
451 _cupsLangPuts(stderr
, _("WARNING: Failed to read side-channel request!\n"));
457 case CUPS_SC_CMD_DRAIN_OUTPUT
:
459 * Our sockets disable the Nagle algorithm and data is sent immediately.
462 if (backendDrainOutput(print_fd
, device_fd
))
463 status
= CUPS_SC_STATUS_IO_ERROR
;
465 status
= CUPS_SC_STATUS_OK
;
470 case CUPS_SC_CMD_GET_BIDI
:
475 case CUPS_SC_CMD_GET_DEVICE_ID
:
476 if ((device_id
= getenv("1284DEVICEID")) != NULL
)
478 strlcpy(data
, device_id
, sizeof(data
));
479 datalen
= (int)strlen(data
);
484 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
489 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
494 * 'wait_bc()' - Wait for back-channel data...
497 static int /* O - # bytes read or -1 on error */
498 wait_bc(int device_fd
, /* I - Socket */
499 int secs
) /* I - Seconds to wait */
501 struct timeval timeout
; /* Timeout for select() */
502 fd_set input
; /* Input set for select() */
503 ssize_t bytes
; /* Number of back-channel bytes read */
504 char buffer
[1024]; /* Back-channel buffer */
508 * Wait up to "secs" seconds for backchannel data...
511 timeout
.tv_sec
= secs
;
515 FD_SET(device_fd
, &input
);
517 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
520 * Grab the data coming back and spit it out to stderr...
523 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
525 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
527 cupsBackChannelWrite(buffer
, bytes
, 1.0);
538 * End of "$Id: socket.c 6910 2007-09-04 20:34:29Z mike $".