]> 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 6170 2007-01-02 17:26:41Z 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 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 */
99
100
101 /*
102 * Make sure status messages are not buffered...
103 */
104
105 setbuf(stderr, NULL);
106
107 /*
108 * Ignore SIGPIPE signals...
109 */
110
111 #ifdef HAVE_SIGSET
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);
117 #else
118 signal(SIGPIPE, SIG_IGN);
119 #endif /* HAVE_SIGSET */
120
121 /*
122 * Check command-line...
123 */
124
125 if (argc == 1)
126 {
127 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
128 return (CUPS_BACKEND_OK);
129 }
130 else if (argc < 6 || argc > 7)
131 {
132 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
133 argv[0]);
134 return (CUPS_BACKEND_FAILED);
135 }
136
137 /*
138 * If we have 7 arguments, print the file named on the command-line.
139 * Otherwise, send stdin instead...
140 */
141
142 if (argc == 6)
143 {
144 print_fd = 0;
145 copies = 1;
146 }
147 else
148 {
149 /*
150 * Try to open the print file...
151 */
152
153 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
154 {
155 perror("ERROR: unable to open print file");
156 return (CUPS_BACKEND_FAILED);
157 }
158
159 copies = atoi(argv[4]);
160 }
161
162 /*
163 * Extract the hostname and port number from the URI...
164 */
165
166 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
167 method, sizeof(method), username, sizeof(username),
168 hostname, sizeof(hostname), &port,
169 resource, sizeof(resource));
170
171 if (port == 0)
172 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
173
174 /*
175 * Get options, if any...
176 */
177
178 waiteof = 1;
179
180 if ((options = strchr(resource, '?')) != NULL)
181 {
182 /*
183 * Yup, terminate the device name string and move to the first
184 * character of the options...
185 */
186
187 *options++ = '\0';
188
189 /*
190 * Parse options...
191 */
192
193 while (*options)
194 {
195 /*
196 * Get the name...
197 */
198
199 for (ptr = name; *options && *options != '=';)
200 if (ptr < (name + sizeof(name) - 1))
201 *ptr++ = *options++;
202 *ptr = '\0';
203
204 if (*options == '=')
205 {
206 /*
207 * Get the value...
208 */
209
210 options ++;
211
212 for (ptr = value; *options && *options != '+' && *options != '&';)
213 if (ptr < (value + sizeof(value) - 1))
214 *ptr++ = *options++;
215 *ptr = '\0';
216
217 if (*options == '+' || *options == '&')
218 options ++;
219 }
220 else
221 value[0] = '\0';
222
223 /*
224 * Process the option...
225 */
226
227 if (!strcasecmp(name, "waiteof"))
228 {
229 /*
230 * Set the wait-for-eof value...
231 */
232
233 waiteof = !value[0] || !strcasecmp(value, "on") ||
234 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
235 }
236 }
237 }
238
239 /*
240 * Then try to connect to the remote host...
241 */
242
243 sprintf(portname, "%d", port);
244
245 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
246 {
247 fprintf(stderr, "ERROR: Unable to locate printer \'%s\'!\n", hostname);
248 return (CUPS_BACKEND_STOP);
249 }
250
251 fprintf(stderr, "INFO: Attempting to connect to host %s on port %d\n",
252 hostname, port);
253
254 fputs("STATE: +connecting-to-device\n", stderr);
255
256 for (delay = 5;;)
257 {
258 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
259 {
260 error = errno;
261 device_fd = -1;
262
263 if (getenv("CLASS") != NULL)
264 {
265 /*
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.
270 */
271
272 fprintf(stderr, "INFO: Unable to connect to \"%s\", queuing on next printer in class...\n",
273 hostname);
274
275 /*
276 * Sleep 5 seconds to keep the job from requeuing too rapidly...
277 */
278
279 sleep(5);
280
281 return (CUPS_BACKEND_FAILED);
282 }
283
284 if (error == ECONNREFUSED || error == EHOSTDOWN ||
285 error == EHOSTUNREACH)
286 {
287 fprintf(stderr,
288 "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
289 hostname, delay);
290 sleep(delay);
291
292 if (delay < 30)
293 delay += 5;
294 }
295 else
296 {
297 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
298 sleep(30);
299 }
300 }
301 else
302 break;
303 }
304
305 fputs("STATE: -connecting-to-device\n", stderr);
306 fprintf(stderr, "INFO: Connected to %s...\n", hostname);
307
308 #ifdef AF_INET6
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));
313 else
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));
319
320 /*
321 * Print everything...
322 */
323
324 tbytes = 0;
325
326 while (copies > 0 && tbytes >= 0)
327 {
328 copies --;
329
330 if (print_fd != 0)
331 {
332 fputs("PAGE: 1 1\n", stderr);
333 lseek(print_fd, 0, SEEK_SET);
334 }
335
336 tbytes = backendRunLoop(print_fd, device_fd, 1, side_cb);
337
338 if (print_fd != 0 && tbytes >= 0)
339 fprintf(stderr, "INFO: Sent print file, " CUPS_LLFMT " bytes...\n",
340 CUPS_LLCAST tbytes);
341 }
342
343 if (waiteof)
344 {
345 /*
346 * Shutdown the socket and wait for the other end to finish...
347 */
348
349 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr);
350
351 shutdown(device_fd, 1);
352
353 for (;;)
354 {
355 /*
356 * Wait a maximum of 90 seconds for backchannel data or a closed
357 * connection...
358 */
359
360 timeout.tv_sec = 90;
361 timeout.tv_usec = 0;
362
363 FD_ZERO(&input);
364 FD_SET(device_fd, &input);
365
366 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
367 {
368 /*
369 * Grab the data coming back and spit it out to stderr...
370 */
371
372 if ((bc_bytes = read(device_fd, resource, sizeof(resource))) > 0)
373 {
374 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
375 (int)bc_bytes);
376 cupsBackChannelWrite(resource, bc_bytes, 1.0);
377 }
378 else
379 break;
380 }
381 else
382 break;
383 }
384 }
385
386 /*
387 * Close the socket connection...
388 */
389
390 close(device_fd);
391
392 httpAddrFreeList(addrlist);
393
394 /*
395 * Close the input file and return...
396 */
397
398 if (print_fd != 0)
399 close(print_fd);
400
401 if (tbytes >= 0)
402 fputs("INFO: Ready to print.\n", stderr);
403
404 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
405 }
406
407
408 /*
409 * 'side_cb()' - Handle side-channel requests...
410 */
411
412 static void
413 side_cb(int print_fd, /* I - Print file */
414 int device_fd, /* I - Device file */
415 int use_bc) /* I - Using back-channel? */
416 {
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 */
421
422
423 datalen = sizeof(data);
424
425 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
426 {
427 fputs("WARNING: Failed to read side-channel request!\n", stderr);
428 return;
429 }
430
431 switch (command)
432 {
433 case CUPS_SC_CMD_DRAIN_OUTPUT :
434 /*
435 * Our sockets disable the Nagle algorithm and data is sent immediately.
436 */
437
438 status = CUPS_SC_STATUS_OK;
439 datalen = 0;
440 break;
441
442 case CUPS_SC_CMD_GET_BIDI :
443 data[0] = use_bc;
444 datalen = 1;
445 break;
446
447 default :
448 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
449 datalen = 0;
450 break;
451 }
452
453 cupsSideChannelWrite(command, status, data, datalen, 1.0);
454 }
455
456
457 /*
458 * End of "$Id: socket.c 6170 2007-01-02 17:26:41Z mike $".
459 */