]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7deaa1a | 2 | * "$Id: socket.c 6170 2007-01-02 17:26:41Z mike $" |
ef416fc2 | 3 | * |
4 | * AppSocket backend for the Common UNIX Printing System (CUPS). | |
5 | * | |
f7deaa1a | 6 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 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 | * | |
f7deaa1a | 28 | * main() - Send a file to the printer or server. |
29 | * side_cb() - Handle side-channel requests... | |
ef416fc2 | 30 | */ |
31 | ||
32 | /* | |
33 | * Include necessary headers. | |
34 | */ | |
35 | ||
ef416fc2 | 36 | #include <cups/http-private.h> |
ed486911 | 37 | #include "backend-private.h" |
ef416fc2 | 38 | #include <stdarg.h> |
ef416fc2 | 39 | #include <sys/types.h> |
40 | #include <sys/stat.h> | |
ef416fc2 | 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 | ||
f7deaa1a | 54 | /* |
55 | * Local functions... | |
56 | */ | |
57 | ||
58 | static void side_cb(int print_fd, int device_fd, int use_bc); | |
59 | ||
60 | ||
ef416fc2 | 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) */ | |
fa73b229 | 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 */ | |
ed486911 | 81 | int print_fd; /* Print file */ |
ef416fc2 | 82 | int copies; /* Number of copies to print */ |
fa73b229 | 83 | int waiteof; /* Wait for end-of-file? */ |
ef416fc2 | 84 | int port; /* Port number */ |
85 | char portname[255]; /* Port name */ | |
86 | int delay; /* Delay for retries... */ | |
ed486911 | 87 | int device_fd; /* AppSocket */ |
ef416fc2 | 88 | int error; /* Error code (if any) */ |
26d47ec6 | 89 | http_addrlist_t *addrlist, /* Address list */ |
90 | *addr; /* Connected address */ | |
91 | char addrname[256]; /* Address name */ | |
ed486911 | 92 | ssize_t tbytes; /* Total number of bytes written */ |
ef416fc2 | 93 | struct timeval timeout; /* Timeout for select() */ |
ed486911 | 94 | fd_set input; /* Input set for select() */ |
95 | ssize_t bc_bytes; /* Number of back-channel bytes read */ | |
ef416fc2 | 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 | { | |
ed486911 | 144 | print_fd = 0; |
145 | copies = 1; | |
ef416fc2 | 146 | } |
147 | else | |
148 | { | |
149 | /* | |
150 | * Try to open the print file... | |
151 | */ | |
152 | ||
ed486911 | 153 | if ((print_fd = open(argv[6], O_RDONLY)) < 0) |
ef416fc2 | 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 | ||
a4d04587 | 166 | httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv), |
167 | method, sizeof(method), username, sizeof(username), | |
168 | hostname, sizeof(hostname), &port, | |
ef416fc2 | 169 | resource, sizeof(resource)); |
170 | ||
171 | if (port == 0) | |
172 | port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */ | |
173 | ||
fa73b229 | 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 | ||
ef416fc2 | 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 | ||
ed486911 | 254 | fputs("STATE: +connecting-to-device\n", stderr); |
ef416fc2 | 255 | |
ed486911 | 256 | for (delay = 5;;) |
ef416fc2 | 257 | { |
26d47ec6 | 258 | if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL) |
ef416fc2 | 259 | { |
ed486911 | 260 | error = errno; |
261 | device_fd = -1; | |
ef416fc2 | 262 | |
ed486911 | 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 | */ | |
ef416fc2 | 271 | |
ed486911 | 272 | fprintf(stderr, "INFO: Unable to connect to \"%s\", queuing on next printer in class...\n", |
273 | hostname); | |
ef416fc2 | 274 | |
ed486911 | 275 | /* |
276 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
277 | */ | |
ef416fc2 | 278 | |
ed486911 | 279 | sleep(5); |
ef416fc2 | 280 | |
ed486911 | 281 | return (CUPS_BACKEND_FAILED); |
282 | } | |
ef416fc2 | 283 | |
ed486911 | 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); | |
ef416fc2 | 291 | |
ed486911 | 292 | if (delay < 30) |
293 | delay += 5; | |
ef416fc2 | 294 | } |
295 | else | |
ed486911 | 296 | { |
297 | perror("ERROR: Unable to connect to printer (retrying in 30 seconds)"); | |
298 | sleep(30); | |
299 | } | |
ef416fc2 | 300 | } |
ed486911 | 301 | else |
302 | break; | |
303 | } | |
ef416fc2 | 304 | |
ed486911 | 305 | fputs("STATE: -connecting-to-device\n", stderr); |
26d47ec6 | 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)); | |
ef416fc2 | 319 | |
ed486911 | 320 | /* |
321 | * Print everything... | |
322 | */ | |
ef416fc2 | 323 | |
ed486911 | 324 | tbytes = 0; |
ef416fc2 | 325 | |
ed486911 | 326 | while (copies > 0 && tbytes >= 0) |
327 | { | |
ef416fc2 | 328 | copies --; |
329 | ||
ed486911 | 330 | if (print_fd != 0) |
ef416fc2 | 331 | { |
332 | fputs("PAGE: 1 1\n", stderr); | |
ed486911 | 333 | lseek(print_fd, 0, SEEK_SET); |
ef416fc2 | 334 | } |
335 | ||
f7deaa1a | 336 | tbytes = backendRunLoop(print_fd, device_fd, 1, side_cb); |
ef416fc2 | 337 | |
ed486911 | 338 | if (print_fd != 0 && tbytes >= 0) |
339 | fprintf(stderr, "INFO: Sent print file, " CUPS_LLFMT " bytes...\n", | |
340 | CUPS_LLCAST tbytes); | |
341 | } | |
ef416fc2 | 342 | |
ed486911 | 343 | if (waiteof) |
344 | { | |
345 | /* | |
346 | * Shutdown the socket and wait for the other end to finish... | |
347 | */ | |
ef416fc2 | 348 | |
ed486911 | 349 | fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr); |
ef416fc2 | 350 | |
ed486911 | 351 | shutdown(device_fd, 1); |
ef416fc2 | 352 | |
ed486911 | 353 | for (;;) |
ef416fc2 | 354 | { |
355 | /* | |
ed486911 | 356 | * Wait a maximum of 90 seconds for backchannel data or a closed |
357 | * connection... | |
ef416fc2 | 358 | */ |
359 | ||
ed486911 | 360 | timeout.tv_sec = 90; |
361 | timeout.tv_usec = 0; | |
ef416fc2 | 362 | |
ed486911 | 363 | FD_ZERO(&input); |
364 | FD_SET(device_fd, &input); | |
ef416fc2 | 365 | |
ed486911 | 366 | if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0) |
ef416fc2 | 367 | { |
368 | /* | |
ed486911 | 369 | * Grab the data coming back and spit it out to stderr... |
ef416fc2 | 370 | */ |
371 | ||
ed486911 | 372 | if ((bc_bytes = read(device_fd, resource, sizeof(resource))) > 0) |
ef416fc2 | 373 | { |
ed486911 | 374 | fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n", |
375 | (int)bc_bytes); | |
376 | cupsBackChannelWrite(resource, bc_bytes, 1.0); | |
fa73b229 | 377 | } |
ef416fc2 | 378 | else |
379 | break; | |
380 | } | |
ed486911 | 381 | else |
382 | break; | |
ef416fc2 | 383 | } |
ed486911 | 384 | } |
ef416fc2 | 385 | |
ed486911 | 386 | /* |
387 | * Close the socket connection... | |
388 | */ | |
ef416fc2 | 389 | |
ed486911 | 390 | close(device_fd); |
ef416fc2 | 391 | |
392 | httpAddrFreeList(addrlist); | |
393 | ||
394 | /* | |
395 | * Close the input file and return... | |
396 | */ | |
397 | ||
ed486911 | 398 | if (print_fd != 0) |
399 | close(print_fd); | |
ef416fc2 | 400 | |
ed486911 | 401 | if (tbytes >= 0) |
ef416fc2 | 402 | fputs("INFO: Ready to print.\n", stderr); |
403 | ||
ed486911 | 404 | return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK); |
ef416fc2 | 405 | } |
406 | ||
407 | ||
408 | /* | |
f7deaa1a | 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 $". | |
ef416fc2 | 459 | */ |