]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
4c779ec7d7ac1329e96b5e6f547b92d848ceee68
[thirdparty/cups.git] / backend / socket.c
1 /*
2 * "$Id: socket.c 6403 2007-03-27 16:00:56Z mike $"
3 *
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2007 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 * side_cb() - Handle side-channel requests...
30 */
31
32 /*
33 * Include necessary headers.
34 */
35
36 #include <cups/http-private.h>
37 #include "backend-private.h"
38 #include <stdarg.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #ifdef WIN32
43 # include <winsock.h>
44 #else
45 # include <unistd.h>
46 # include <fcntl.h>
47 # include <sys/socket.h>
48 # include <netinet/in.h>
49 # include <arpa/inet.h>
50 # include <netdb.h>
51 #endif /* WIN32 */
52
53
54 /*
55 * Local functions...
56 */
57
58 static void side_cb(int print_fd, int device_fd, int use_bc);
59
60
61 /*
62 * 'main()' - Send a file to the printer or server.
63 *
64 * Usage:
65 *
66 * printer-uri job-id user title copies options [file]
67 */
68
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 */
72 {
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 */
102
103
104 /*
105 * Make sure status messages are not buffered...
106 */
107
108 setbuf(stderr, NULL);
109
110 /*
111 * Ignore SIGPIPE signals...
112 */
113
114 #ifdef HAVE_SIGSET
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);
120 #else
121 signal(SIGPIPE, SIG_IGN);
122 #endif /* HAVE_SIGSET */
123
124 /*
125 * Check command-line...
126 */
127
128 if (argc == 1)
129 {
130 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
131 return (CUPS_BACKEND_OK);
132 }
133 else if (argc < 6 || argc > 7)
134 {
135 fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
136 argv[0]);
137 return (CUPS_BACKEND_FAILED);
138 }
139
140 /*
141 * If we have 7 arguments, print the file named on the command-line.
142 * Otherwise, send stdin instead...
143 */
144
145 if (argc == 6)
146 {
147 print_fd = 0;
148 copies = 1;
149 }
150 else
151 {
152 /*
153 * Try to open the print file...
154 */
155
156 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
157 {
158 perror("ERROR: unable to open print file");
159 return (CUPS_BACKEND_FAILED);
160 }
161
162 copies = atoi(argv[4]);
163 }
164
165 /*
166 * Extract the hostname and port number from the URI...
167 */
168
169 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
170 method, sizeof(method), username, sizeof(username),
171 hostname, sizeof(hostname), &port,
172 resource, sizeof(resource));
173
174 if (port == 0)
175 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
176
177 /*
178 * Get options, if any...
179 */
180
181 waiteof = 1;
182 contimeout = 7 * 24 * 60 * 60;
183
184 if ((options = strchr(resource, '?')) != NULL)
185 {
186 /*
187 * Yup, terminate the device name string and move to the first
188 * character of the options...
189 */
190
191 *options++ = '\0';
192
193 /*
194 * Parse options...
195 */
196
197 while (*options)
198 {
199 /*
200 * Get the name...
201 */
202
203 for (ptr = name; *options && *options != '=';)
204 if (ptr < (name + sizeof(name) - 1))
205 *ptr++ = *options++;
206 *ptr = '\0';
207
208 if (*options == '=')
209 {
210 /*
211 * Get the value...
212 */
213
214 options ++;
215
216 for (ptr = value; *options && *options != '+' && *options != '&';)
217 if (ptr < (value + sizeof(value) - 1))
218 *ptr++ = *options++;
219 *ptr = '\0';
220
221 if (*options == '+' || *options == '&')
222 options ++;
223 }
224 else
225 value[0] = '\0';
226
227 /*
228 * Process the option...
229 */
230
231 if (!strcasecmp(name, "waiteof"))
232 {
233 /*
234 * Set the wait-for-eof value...
235 */
236
237 waiteof = !value[0] || !strcasecmp(value, "on") ||
238 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
239 }
240 else if (!strcasecmp(name, "contimeout"))
241 {
242 /*
243 * Set the connection timeout...
244 */
245
246 if (atoi(value) > 0)
247 contimeout = atoi(value);
248 }
249 }
250 }
251
252 /*
253 * Then try to connect to the remote host...
254 */
255
256 recoverable = 0;
257 start_time = time(NULL);
258
259 sprintf(portname, "%d", port);
260
261 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
262 {
263 fprintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"), hostname);
264 return (CUPS_BACKEND_STOP);
265 }
266
267 fprintf(stderr, _("INFO: Attempting to connect to host %s on port %d\n"),
268 hostname, port);
269
270 fputs("STATE: +connecting-to-device\n", stderr);
271
272 for (delay = 5;;)
273 {
274 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
275 {
276 error = errno;
277 device_fd = -1;
278
279 if (getenv("CLASS") != NULL)
280 {
281 /*
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.
286 */
287
288 fputs(_("INFO: Unable to contact printer, queuing on next "
289 "printer in class...\n"), stderr);
290
291 /*
292 * Sleep 5 seconds to keep the job from requeuing too rapidly...
293 */
294
295 sleep(5);
296
297 return (CUPS_BACKEND_FAILED);
298 }
299
300 if (error == ECONNREFUSED || error == EHOSTDOWN ||
301 error == EHOSTUNREACH)
302 {
303 if (contimeout && (time(NULL) - start_time) > contimeout)
304 {
305 fputs(_("ERROR: Printer not responding!\n"), stderr);
306 return (CUPS_BACKEND_FAILED);
307 }
308
309 recoverable = 1;
310
311 fprintf(stderr,
312 _("WARNING: recoverable: Network host \'%s\' is busy; will "
313 "retry in %d seconds...\n"),
314 hostname, delay);
315
316 sleep(delay);
317
318 if (delay < 30)
319 delay += 5;
320 }
321 else
322 {
323 recoverable = 1;
324
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);
328 sleep(30);
329 }
330 }
331 else
332 break;
333 }
334
335 if (recoverable)
336 {
337 /*
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...
341 */
342
343 fputs("INFO: recovered: \n", stderr);
344 sleep(5);
345 }
346
347 fputs("STATE: -connecting-to-device\n", stderr);
348 fprintf(stderr, _("INFO: Connected to %s...\n"), hostname);
349
350 #ifdef AF_INET6
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));
355 else
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));
361
362 /*
363 * Print everything...
364 */
365
366 tbytes = 0;
367
368 while (copies > 0 && tbytes >= 0)
369 {
370 copies --;
371
372 if (print_fd != 0)
373 {
374 fputs("PAGE: 1 1\n", stderr);
375 lseek(print_fd, 0, SEEK_SET);
376 }
377
378 tbytes = backendRunLoop(print_fd, device_fd, 1, side_cb);
379
380 if (print_fd != 0 && tbytes >= 0)
381 fprintf(stderr,
382 #ifdef HAVE_LONG_LONG
383 _("INFO: Sent print file, %lld bytes...\n"),
384 #else
385 _("INFO: Sent print file, %ld bytes...\n"),
386 #endif /* HAVE_LONG_LONG */
387 CUPS_LLCAST tbytes);
388 }
389
390 if (waiteof)
391 {
392 /*
393 * Shutdown the socket and wait for the other end to finish...
394 */
395
396 fputs(_("INFO: Print file sent, waiting for printer to finish...\n"),
397 stderr);
398
399 shutdown(device_fd, 1);
400
401 for (;;)
402 {
403 /*
404 * Wait a maximum of 90 seconds for backchannel data or a closed
405 * connection...
406 */
407
408 timeout.tv_sec = 90;
409 timeout.tv_usec = 0;
410
411 FD_ZERO(&input);
412 FD_SET(device_fd, &input);
413
414 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
415 {
416 /*
417 * Grab the data coming back and spit it out to stderr...
418 */
419
420 if ((bc_bytes = read(device_fd, resource, sizeof(resource))) > 0)
421 {
422 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
423 (int)bc_bytes);
424 cupsBackChannelWrite(resource, bc_bytes, 1.0);
425 }
426 else
427 break;
428 }
429 else
430 break;
431 }
432 }
433
434 /*
435 * Close the socket connection...
436 */
437
438 close(device_fd);
439
440 httpAddrFreeList(addrlist);
441
442 /*
443 * Close the input file and return...
444 */
445
446 if (print_fd != 0)
447 close(print_fd);
448
449 if (tbytes >= 0)
450 fputs(_("INFO: Ready to print.\n"), stderr);
451
452 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
453 }
454
455
456 /*
457 * 'side_cb()' - Handle side-channel requests...
458 */
459
460 static void
461 side_cb(int print_fd, /* I - Print file */
462 int device_fd, /* I - Device file */
463 int use_bc) /* I - Using back-channel? */
464 {
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 */
469
470
471 datalen = sizeof(data);
472
473 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
474 {
475 fputs(_("WARNING: Failed to read side-channel request!\n"), stderr);
476 return;
477 }
478
479 switch (command)
480 {
481 case CUPS_SC_CMD_DRAIN_OUTPUT :
482 /*
483 * Our sockets disable the Nagle algorithm and data is sent immediately.
484 */
485
486 status = CUPS_SC_STATUS_OK;
487 datalen = 0;
488 break;
489
490 case CUPS_SC_CMD_GET_BIDI :
491 data[0] = use_bc;
492 datalen = 1;
493 break;
494
495 default :
496 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
497 datalen = 0;
498 break;
499 }
500
501 cupsSideChannelWrite(command, status, data, datalen, 1.0);
502 }
503
504
505 /*
506 * End of "$Id: socket.c 6403 2007-03-27 16:00:56Z mike $".
507 */