2 * "$Id: socket.c 6170 2007-01-02 17:26: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...
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 int waiteof
; /* Wait for end-of-file? */
84 int port
; /* Port number */
85 char portname
[255]; /* Port name */
86 int delay
; /* Delay for retries... */
87 int device_fd
; /* AppSocket */
88 int error
; /* Error code (if any) */
89 http_addrlist_t
*addrlist
, /* Address list */
90 *addr
; /* Connected address */
91 char addrname
[256]; /* Address name */
92 ssize_t tbytes
; /* Total number of bytes written */
93 struct timeval timeout
; /* Timeout for select() */
94 fd_set input
; /* Input set for select() */
95 ssize_t bc_bytes
; /* Number of back-channel bytes read */
96 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
97 struct sigaction action
; /* Actions for POSIX signals */
98 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
102 * Make sure status messages are not buffered...
105 setbuf(stderr
, NULL
);
108 * Ignore SIGPIPE signals...
112 sigset(SIGPIPE
, SIG_IGN
);
113 #elif defined(HAVE_SIGACTION)
114 memset(&action
, 0, sizeof(action
));
115 action
.sa_handler
= SIG_IGN
;
116 sigaction(SIGPIPE
, &action
, NULL
);
118 signal(SIGPIPE
, SIG_IGN
);
119 #endif /* HAVE_SIGSET */
122 * Check command-line...
127 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
128 return (CUPS_BACKEND_OK
);
130 else if (argc
< 6 || argc
> 7)
132 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
134 return (CUPS_BACKEND_FAILED
);
138 * If we have 7 arguments, print the file named on the command-line.
139 * Otherwise, send stdin instead...
150 * Try to open the print file...
153 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
155 perror("ERROR: unable to open print file");
156 return (CUPS_BACKEND_FAILED
);
159 copies
= atoi(argv
[4]);
163 * Extract the hostname and port number from the URI...
166 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
167 method
, sizeof(method
), username
, sizeof(username
),
168 hostname
, sizeof(hostname
), &port
,
169 resource
, sizeof(resource
));
172 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
175 * Get options, if any...
180 if ((options
= strchr(resource
, '?')) != NULL
)
183 * Yup, terminate the device name string and move to the first
184 * character of the options...
199 for (ptr
= name
; *options
&& *options
!= '=';)
200 if (ptr
< (name
+ sizeof(name
) - 1))
212 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
213 if (ptr
< (value
+ sizeof(value
) - 1))
217 if (*options
== '+' || *options
== '&')
224 * Process the option...
227 if (!strcasecmp(name
, "waiteof"))
230 * Set the wait-for-eof value...
233 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
234 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
240 * Then try to connect to the remote host...
243 sprintf(portname
, "%d", port
);
245 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
247 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\'!\n", hostname
);
248 return (CUPS_BACKEND_STOP
);
251 fprintf(stderr
, "INFO: Attempting to connect to host %s on port %d\n",
254 fputs("STATE: +connecting-to-device\n", stderr
);
258 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
263 if (getenv("CLASS") != NULL
)
266 * If the CLASS environment variable is set, the job was submitted
267 * to a class and not to a specific queue. In this case, we want
268 * to abort immediately so that the job can be requeued on the next
269 * available printer in the class.
272 fprintf(stderr
, "INFO: Unable to connect to \"%s\", queuing on next printer in class...\n",
276 * Sleep 5 seconds to keep the job from requeuing too rapidly...
281 return (CUPS_BACKEND_FAILED
);
284 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
285 error
== EHOSTUNREACH
)
288 "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
297 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
305 fputs("STATE: -connecting-to-device\n", stderr
);
306 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
309 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
310 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
311 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
312 ntohs(addr
->addr
.ipv6
.sin6_port
));
314 #endif /* AF_INET6 */
315 if (addr
->addr
.addr
.sa_family
== AF_INET
)
316 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
317 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
318 ntohs(addr
->addr
.ipv4
.sin_port
));
321 * Print everything...
326 while (copies
> 0 && tbytes
>= 0)
332 fputs("PAGE: 1 1\n", stderr
);
333 lseek(print_fd
, 0, SEEK_SET
);
336 tbytes
= backendRunLoop(print_fd
, device_fd
, 1, side_cb
);
338 if (print_fd
!= 0 && tbytes
>= 0)
339 fprintf(stderr
, "INFO: Sent print file, " CUPS_LLFMT
" bytes...\n",
346 * Shutdown the socket and wait for the other end to finish...
349 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr
);
351 shutdown(device_fd
, 1);
356 * Wait a maximum of 90 seconds for backchannel data or a closed
364 FD_SET(device_fd
, &input
);
366 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
369 * Grab the data coming back and spit it out to stderr...
372 if ((bc_bytes
= read(device_fd
, resource
, sizeof(resource
))) > 0)
374 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
376 cupsBackChannelWrite(resource
, bc_bytes
, 1.0);
387 * Close the socket connection...
392 httpAddrFreeList(addrlist
);
395 * Close the input file and return...
402 fputs("INFO: Ready to print.\n", stderr
);
404 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
409 * 'side_cb()' - Handle side-channel requests...
413 side_cb(int print_fd
, /* I - Print file */
414 int device_fd
, /* I - Device file */
415 int use_bc
) /* I - Using back-channel? */
417 cups_sc_command_t command
; /* Request command */
418 cups_sc_status_t status
; /* Request/response status */
419 char data
[2048]; /* Request/response data */
420 int datalen
; /* Request/response data size */
423 datalen
= sizeof(data
);
425 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
427 fputs("WARNING: Failed to read side-channel request!\n", stderr
);
433 case CUPS_SC_CMD_DRAIN_OUTPUT
:
435 * Our sockets disable the Nagle algorithm and data is sent immediately.
438 status
= CUPS_SC_STATUS_OK
;
442 case CUPS_SC_CMD_GET_BIDI
:
448 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
453 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);
458 * End of "$Id: socket.c 6170 2007-01-02 17:26:41Z mike $".