2 * "$Id: socket.c 6403 2007-03-27 16:00:56Z 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...
33 * Include necessary headers.
36 #include <cups/http-private.h>
37 #include "backend-private.h"
39 #include <sys/types.h>
47 # include <sys/socket.h>
48 # include <netinet/in.h>
49 # include <arpa/inet.h>
58 static void side_cb(int print_fd
, int device_fd
, int use_bc
);
62 * 'main()' - Send a file to the printer or server.
66 * printer-uri job-id user title copies options [file]
69 int /* O - Exit status */
70 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
71 char *argv
[]) /* I - Command-line arguments */
73 char method
[255], /* Method in URI */
74 hostname
[1024], /* Hostname */
75 username
[255], /* Username info (not used) */
76 resource
[1024], /* Resource info (not used) */
77 *options
, /* Pointer to options */
78 name
[255], /* Name of option */
79 value
[255], /* Value of option */
80 *ptr
; /* Pointer into name or value */
81 int print_fd
; /* Print file */
82 int copies
; /* Number of copies to print */
83 time_t start_time
; /* Time of first connect */
84 int recoverable
; /* Recoverable error shown? */
85 int contimeout
; /* Connection timeout */
86 int waiteof
; /* Wait for end-of-file? */
87 int port
; /* Port number */
88 char portname
[255]; /* Port name */
89 int delay
; /* Delay for retries... */
90 int device_fd
; /* AppSocket */
91 int error
; /* Error code (if any) */
92 http_addrlist_t
*addrlist
, /* Address list */
93 *addr
; /* Connected address */
94 char addrname
[256]; /* Address name */
95 ssize_t tbytes
; /* Total number of bytes written */
96 struct timeval timeout
; /* Timeout for select() */
97 fd_set input
; /* Input set for select() */
98 ssize_t bc_bytes
; /* Number of back-channel bytes read */
99 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
100 struct sigaction action
; /* Actions for POSIX signals */
101 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
105 * Make sure status messages are not buffered...
108 setbuf(stderr
, NULL
);
111 * Ignore SIGPIPE signals...
115 sigset(SIGPIPE
, SIG_IGN
);
116 #elif defined(HAVE_SIGACTION)
117 memset(&action
, 0, sizeof(action
));
118 action
.sa_handler
= SIG_IGN
;
119 sigaction(SIGPIPE
, &action
, NULL
);
121 signal(SIGPIPE
, SIG_IGN
);
122 #endif /* HAVE_SIGSET */
125 * Check command-line...
130 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
131 return (CUPS_BACKEND_OK
);
133 else if (argc
< 6 || argc
> 7)
135 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
137 return (CUPS_BACKEND_FAILED
);
141 * If we have 7 arguments, print the file named on the command-line.
142 * Otherwise, send stdin instead...
153 * Try to open the print file...
156 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
158 perror("ERROR: unable to open print file");
159 return (CUPS_BACKEND_FAILED
);
162 copies
= atoi(argv
[4]);
166 * Extract the hostname and port number from the URI...
169 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
170 method
, sizeof(method
), username
, sizeof(username
),
171 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...
203 for (ptr
= name
; *options
&& *options
!= '=';)
204 if (ptr
< (name
+ sizeof(name
) - 1))
216 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
217 if (ptr
< (value
+ sizeof(value
) - 1))
221 if (*options
== '+' || *options
== '&')
228 * Process the option...
231 if (!strcasecmp(name
, "waiteof"))
234 * Set the wait-for-eof value...
237 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
238 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
240 else if (!strcasecmp(name
, "contimeout"))
243 * Set the connection timeout...
247 contimeout
= atoi(value
);
253 * Then try to connect to the remote host...
257 start_time
= time(NULL
);
259 sprintf(portname
, "%d", port
);
261 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
263 fprintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"), hostname
);
264 return (CUPS_BACKEND_STOP
);
267 fprintf(stderr
, _("INFO: Attempting to connect to host %s on port %d\n"),
270 fputs("STATE: +connecting-to-device\n", stderr
);
274 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
279 if (getenv("CLASS") != NULL
)
282 * If the CLASS environment variable is set, the job was submitted
283 * to a class and not to a specific queue. In this case, we want
284 * to abort immediately so that the job can be requeued on the next
285 * available printer in the class.
288 fputs(_("INFO: Unable to contact printer, queuing on next "
289 "printer in class...\n"), stderr
);
292 * Sleep 5 seconds to keep the job from requeuing too rapidly...
297 return (CUPS_BACKEND_FAILED
);
300 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
301 error
== EHOSTUNREACH
)
303 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
305 fputs(_("ERROR: Printer not responding!\n"), stderr
);
306 return (CUPS_BACKEND_FAILED
);
312 _("WARNING: recoverable: Network host \'%s\' is busy; will "
313 "retry in %d seconds...\n"),
325 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
326 fputs(_("ERROR: recoverable: Unable to connect to printer; will "
327 "retry in 30 seconds...\n"), stderr
);
338 * If we've shown a recoverable error make sure the printer proxies
339 * have a chance to see the recovered message. Not pretty but
340 * necessary for now...
343 fputs("INFO: recovered: \n", stderr
);
347 fputs("STATE: -connecting-to-device\n", stderr
);
348 fprintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
351 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
352 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
353 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
354 ntohs(addr
->addr
.ipv6
.sin6_port
));
356 #endif /* AF_INET6 */
357 if (addr
->addr
.addr
.sa_family
== AF_INET
)
358 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
359 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
360 ntohs(addr
->addr
.ipv4
.sin_port
));
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
, 1, side_cb
);
380 if (print_fd
!= 0 && tbytes
>= 0)
382 #ifdef HAVE_LONG_LONG
383 _("INFO: Sent print file, %lld bytes...\n"),
385 _("INFO: Sent print file, %ld bytes...\n"),
386 #endif /* HAVE_LONG_LONG */
393 * Shutdown the socket and wait for the other end to finish...
396 fputs(_("INFO: Print file sent, waiting for printer to finish...\n"),
399 shutdown(device_fd
, 1);
404 * Wait a maximum of 90 seconds for backchannel data or a closed
412 FD_SET(device_fd
, &input
);
414 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
417 * Grab the data coming back and spit it out to stderr...
420 if ((bc_bytes
= read(device_fd
, resource
, sizeof(resource
))) > 0)
422 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
424 cupsBackChannelWrite(resource
, bc_bytes
, 1.0);
435 * Close the socket connection...
440 httpAddrFreeList(addrlist
);
443 * Close the input file and return...
450 fputs(_("INFO: Ready to print.\n"), stderr
);
452 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
457 * 'side_cb()' - Handle side-channel requests...
461 side_cb(int print_fd
, /* I - Print file */
462 int device_fd
, /* I - Device file */
463 int use_bc
) /* I - Using back-channel? */
465 cups_sc_command_t command
; /* Request command */
466 cups_sc_status_t status
; /* Request/response status */
467 char data
[2048]; /* Request/response data */
468 int datalen
; /* Request/response data size */
471 datalen
= sizeof(data
);
473 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
475 fputs(_("WARNING: Failed to read side-channel request!\n"), stderr
);
481 case CUPS_SC_CMD_DRAIN_OUTPUT
:
483 * Our sockets disable the Nagle algorithm and data is sent immediately.
486 status
= CUPS_SC_STATUS_OK
;
490 case CUPS_SC_CMD_GET_BIDI
:
496 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
501 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
506 * End of "$Id: socket.c 6403 2007-03-27 16:00:56Z mike $".