]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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.
32 * Include necessary headers.
35 #include <cups/http-private.h>
36 #include "backend-private.h"
38 #include <sys/types.h>
46 # include <sys/socket.h>
47 # include <netinet/in.h>
48 # include <arpa/inet.h>
54 * 'main()' - Send a file to the printer or server.
58 * printer-uri job-id user title copies options [file]
61 int /* O - Exit status */
62 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
63 char *argv
[]) /* I - Command-line arguments */
65 char method
[255], /* Method in URI */
66 hostname
[1024], /* Hostname */
67 username
[255], /* Username info (not used) */
68 resource
[1024], /* Resource info (not used) */
69 *options
, /* Pointer to options */
70 name
[255], /* Name of option */
71 value
[255], /* Value of option */
72 *ptr
; /* Pointer into name or value */
73 int print_fd
; /* Print file */
74 int copies
; /* Number of copies to print */
75 int waiteof
; /* Wait for end-of-file? */
76 int port
; /* Port number */
77 char portname
[255]; /* Port name */
78 int delay
; /* Delay for retries... */
79 int device_fd
; /* AppSocket */
80 int error
; /* Error code (if any) */
81 http_addrlist_t
*addrlist
, /* Address list */
82 *addr
; /* Connected address */
83 char addrname
[256]; /* Address name */
84 ssize_t tbytes
; /* Total number of bytes written */
85 struct timeval timeout
; /* Timeout for select() */
86 fd_set input
; /* Input set for select() */
87 ssize_t bc_bytes
; /* Number of back-channel bytes read */
88 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
89 struct sigaction action
; /* Actions for POSIX signals */
90 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
94 * Make sure status messages are not buffered...
100 * Ignore SIGPIPE signals...
104 sigset(SIGPIPE
, SIG_IGN
);
105 #elif defined(HAVE_SIGACTION)
106 memset(&action
, 0, sizeof(action
));
107 action
.sa_handler
= SIG_IGN
;
108 sigaction(SIGPIPE
, &action
, NULL
);
110 signal(SIGPIPE
, SIG_IGN
);
111 #endif /* HAVE_SIGSET */
114 * Check command-line...
119 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
120 return (CUPS_BACKEND_OK
);
122 else if (argc
< 6 || argc
> 7)
124 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
126 return (CUPS_BACKEND_FAILED
);
130 * If we have 7 arguments, print the file named on the command-line.
131 * Otherwise, send stdin instead...
142 * Try to open the print file...
145 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
147 perror("ERROR: unable to open print file");
148 return (CUPS_BACKEND_FAILED
);
151 copies
= atoi(argv
[4]);
155 * Extract the hostname and port number from the URI...
158 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
159 method
, sizeof(method
), username
, sizeof(username
),
160 hostname
, sizeof(hostname
), &port
,
161 resource
, sizeof(resource
));
164 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
167 * Get options, if any...
172 if ((options
= strchr(resource
, '?')) != NULL
)
175 * Yup, terminate the device name string and move to the first
176 * character of the options...
191 for (ptr
= name
; *options
&& *options
!= '=';)
192 if (ptr
< (name
+ sizeof(name
) - 1))
204 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
205 if (ptr
< (value
+ sizeof(value
) - 1))
209 if (*options
== '+' || *options
== '&')
216 * Process the option...
219 if (!strcasecmp(name
, "waiteof"))
222 * Set the wait-for-eof value...
225 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
226 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
232 * Then try to connect to the remote host...
235 sprintf(portname
, "%d", port
);
237 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
239 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\'!\n", hostname
);
240 return (CUPS_BACKEND_STOP
);
243 fprintf(stderr
, "INFO: Attempting to connect to host %s on port %d\n",
246 fputs("STATE: +connecting-to-device\n", stderr
);
250 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
255 if (getenv("CLASS") != NULL
)
258 * If the CLASS environment variable is set, the job was submitted
259 * to a class and not to a specific queue. In this case, we want
260 * to abort immediately so that the job can be requeued on the next
261 * available printer in the class.
264 fprintf(stderr
, "INFO: Unable to connect to \"%s\", queuing on next printer in class...\n",
268 * Sleep 5 seconds to keep the job from requeuing too rapidly...
273 return (CUPS_BACKEND_FAILED
);
276 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
277 error
== EHOSTUNREACH
)
280 "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
289 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
297 fputs("STATE: -connecting-to-device\n", stderr
);
298 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
301 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
302 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
303 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
304 ntohs(addr
->addr
.ipv6
.sin6_port
));
306 #endif /* AF_INET6 */
307 if (addr
->addr
.addr
.sa_family
== AF_INET
)
308 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
309 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
310 ntohs(addr
->addr
.ipv4
.sin_port
));
313 * Print everything...
318 while (copies
> 0 && tbytes
>= 0)
324 fputs("PAGE: 1 1\n", stderr
);
325 lseek(print_fd
, 0, SEEK_SET
);
328 tbytes
= backendRunLoop(print_fd
, device_fd
, 1);
330 if (print_fd
!= 0 && tbytes
>= 0)
331 fprintf(stderr
, "INFO: Sent print file, " CUPS_LLFMT
" bytes...\n",
338 * Shutdown the socket and wait for the other end to finish...
341 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr
);
343 shutdown(device_fd
, 1);
348 * Wait a maximum of 90 seconds for backchannel data or a closed
356 FD_SET(device_fd
, &input
);
358 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
361 * Grab the data coming back and spit it out to stderr...
364 if ((bc_bytes
= read(device_fd
, resource
, sizeof(resource
))) > 0)
366 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
368 cupsBackChannelWrite(resource
, bc_bytes
, 1.0);
379 * Close the socket connection...
384 httpAddrFreeList(addrlist
);
387 * Close the input file and return...
394 fputs("INFO: Ready to print.\n", stderr
);
396 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);