]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / socket.c
1 /*
2 * "$Id: socket.c 6061 2006-10-23 00:26:52Z mike $"
3 *
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * main() - Send a file to the printer or server.
29 */
30
31 /*
32 * Include necessary headers.
33 */
34
35 #include <cups/http-private.h>
36 #include "backend-private.h"
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40
41 #ifdef WIN32
42 # include <winsock.h>
43 #else
44 # include <unistd.h>
45 # include <fcntl.h>
46 # include <sys/socket.h>
47 # include <netinet/in.h>
48 # include <arpa/inet.h>
49 # include <netdb.h>
50 #endif /* WIN32 */
51
52
53 /*
54 * 'main()' - Send a file to the printer or server.
55 *
56 * Usage:
57 *
58 * printer-uri job-id user title copies options [file]
59 */
60
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 */
64 {
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 */
91
92
93 /*
94 * Make sure status messages are not buffered...
95 */
96
97 setbuf(stderr, NULL);
98
99 /*
100 * Ignore SIGPIPE signals...
101 */
102
103 #ifdef HAVE_SIGSET
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);
109 #else
110 signal(SIGPIPE, SIG_IGN);
111 #endif /* HAVE_SIGSET */
112
113 /*
114 * Check command-line...
115 */
116
117 if (argc == 1)
118 {
119 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
120 return (CUPS_BACKEND_OK);
121 }
122 else if (argc < 6 || argc > 7)
123 {
124 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
125 argv[0]);
126 return (CUPS_BACKEND_FAILED);
127 }
128
129 /*
130 * If we have 7 arguments, print the file named on the command-line.
131 * Otherwise, send stdin instead...
132 */
133
134 if (argc == 6)
135 {
136 print_fd = 0;
137 copies = 1;
138 }
139 else
140 {
141 /*
142 * Try to open the print file...
143 */
144
145 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
146 {
147 perror("ERROR: unable to open print file");
148 return (CUPS_BACKEND_FAILED);
149 }
150
151 copies = atoi(argv[4]);
152 }
153
154 /*
155 * Extract the hostname and port number from the URI...
156 */
157
158 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
159 method, sizeof(method), username, sizeof(username),
160 hostname, sizeof(hostname), &port,
161 resource, sizeof(resource));
162
163 if (port == 0)
164 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
165
166 /*
167 * Get options, if any...
168 */
169
170 waiteof = 1;
171
172 if ((options = strchr(resource, '?')) != NULL)
173 {
174 /*
175 * Yup, terminate the device name string and move to the first
176 * character of the options...
177 */
178
179 *options++ = '\0';
180
181 /*
182 * Parse options...
183 */
184
185 while (*options)
186 {
187 /*
188 * Get the name...
189 */
190
191 for (ptr = name; *options && *options != '=';)
192 if (ptr < (name + sizeof(name) - 1))
193 *ptr++ = *options++;
194 *ptr = '\0';
195
196 if (*options == '=')
197 {
198 /*
199 * Get the value...
200 */
201
202 options ++;
203
204 for (ptr = value; *options && *options != '+' && *options != '&';)
205 if (ptr < (value + sizeof(value) - 1))
206 *ptr++ = *options++;
207 *ptr = '\0';
208
209 if (*options == '+' || *options == '&')
210 options ++;
211 }
212 else
213 value[0] = '\0';
214
215 /*
216 * Process the option...
217 */
218
219 if (!strcasecmp(name, "waiteof"))
220 {
221 /*
222 * Set the wait-for-eof value...
223 */
224
225 waiteof = !value[0] || !strcasecmp(value, "on") ||
226 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
227 }
228 }
229 }
230
231 /*
232 * Then try to connect to the remote host...
233 */
234
235 sprintf(portname, "%d", port);
236
237 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
238 {
239 fprintf(stderr, "ERROR: Unable to locate printer \'%s\'!\n", hostname);
240 return (CUPS_BACKEND_STOP);
241 }
242
243 fprintf(stderr, "INFO: Attempting to connect to host %s on port %d\n",
244 hostname, port);
245
246 fputs("STATE: +connecting-to-device\n", stderr);
247
248 for (delay = 5;;)
249 {
250 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
251 {
252 error = errno;
253 device_fd = -1;
254
255 if (getenv("CLASS") != NULL)
256 {
257 /*
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.
262 */
263
264 fprintf(stderr, "INFO: Unable to connect to \"%s\", queuing on next printer in class...\n",
265 hostname);
266
267 /*
268 * Sleep 5 seconds to keep the job from requeuing too rapidly...
269 */
270
271 sleep(5);
272
273 return (CUPS_BACKEND_FAILED);
274 }
275
276 if (error == ECONNREFUSED || error == EHOSTDOWN ||
277 error == EHOSTUNREACH)
278 {
279 fprintf(stderr,
280 "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
281 hostname, delay);
282 sleep(delay);
283
284 if (delay < 30)
285 delay += 5;
286 }
287 else
288 {
289 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
290 sleep(30);
291 }
292 }
293 else
294 break;
295 }
296
297 fputs("STATE: -connecting-to-device\n", stderr);
298 fprintf(stderr, "INFO: Connected to %s...\n", hostname);
299
300 #ifdef AF_INET6
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));
305 else
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));
311
312 /*
313 * Print everything...
314 */
315
316 tbytes = 0;
317
318 while (copies > 0 && tbytes >= 0)
319 {
320 copies --;
321
322 if (print_fd != 0)
323 {
324 fputs("PAGE: 1 1\n", stderr);
325 lseek(print_fd, 0, SEEK_SET);
326 }
327
328 tbytes = backendRunLoop(print_fd, device_fd, 1);
329
330 if (print_fd != 0 && tbytes >= 0)
331 fprintf(stderr, "INFO: Sent print file, " CUPS_LLFMT " bytes...\n",
332 CUPS_LLCAST tbytes);
333 }
334
335 if (waiteof)
336 {
337 /*
338 * Shutdown the socket and wait for the other end to finish...
339 */
340
341 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr);
342
343 shutdown(device_fd, 1);
344
345 for (;;)
346 {
347 /*
348 * Wait a maximum of 90 seconds for backchannel data or a closed
349 * connection...
350 */
351
352 timeout.tv_sec = 90;
353 timeout.tv_usec = 0;
354
355 FD_ZERO(&input);
356 FD_SET(device_fd, &input);
357
358 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
359 {
360 /*
361 * Grab the data coming back and spit it out to stderr...
362 */
363
364 if ((bc_bytes = read(device_fd, resource, sizeof(resource))) > 0)
365 {
366 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
367 (int)bc_bytes);
368 cupsBackChannelWrite(resource, bc_bytes, 1.0);
369 }
370 else
371 break;
372 }
373 else
374 break;
375 }
376 }
377
378 /*
379 * Close the socket connection...
380 */
381
382 close(device_fd);
383
384 httpAddrFreeList(addrlist);
385
386 /*
387 * Close the input file and return...
388 */
389
390 if (print_fd != 0)
391 close(print_fd);
392
393 if (tbytes >= 0)
394 fputs("INFO: Ready to print.\n", stderr);
395
396 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
397 }
398
399
400 /*
401 * End of "$Id: socket.c 6061 2006-10-23 00:26:52Z mike $".
402 */