]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/socket.c
Fix Linux build errors.
[thirdparty/cups.git] / backend / socket.c
CommitLineData
43e2fc22 1/*
b2e10895 2 * "$Id$"
43e2fc22 3 *
e73c6c0a 4 * AppSocket backend for the Common UNIX Printing System (CUPS).
43e2fc22 5 *
a52a4879 6 * Copyright 2007-2010 by Apple Inc.
761a542e 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
43e2fc22 8 *
9 * These coded instructions, statements, and computer programs are the
4e8d321f 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
43e2fc22 12 * "LICENSE" which should have been included with this file. If this
4e8d321f 13 * file is missing or damaged, see the license at "http://www.cups.org/".
43e2fc22 14 *
dab1a4d8 15 * This file is subject to the Apple OS-Developed Software exception.
16 *
43e2fc22 17 * Contents:
18 *
761a542e 19 * main() - Send a file to the printer or server.
313509aa 20 * wait_bc() - Wait for back-channel data...
43e2fc22 21 */
22
23/*
24 * Include necessary headers.
25 */
26
6ca34e4e 27#include <cups/http-private.h>
43b5ad0d 28#include "backend-private.h"
e73c6c0a 29#include <stdarg.h>
e73c6c0a 30#include <sys/types.h>
31#include <sys/stat.h>
32
c3026ddc 33#ifdef WIN32
e73c6c0a 34# include <winsock.h>
35#else
267cf96a 36# include <unistd.h>
37# include <fcntl.h>
e73c6c0a 38# include <sys/socket.h>
39# include <netinet/in.h>
40# include <arpa/inet.h>
41# include <netdb.h>
c3026ddc 42#endif /* WIN32 */
e73c6c0a 43
44
761a542e 45/*
46 * Local functions...
47 */
48
313509aa 49static int wait_bc(int device_fd, int secs);
761a542e 50
51
e73c6c0a 52/*
53 * 'main()' - Send a file to the printer or server.
54 *
55 * Usage:
56 *
57 * printer-uri job-id user title copies options [file]
58 */
59
5baa731f 60int /* O - Exit status */
61main(int argc, /* I - Number of command-line arguments (6 or 7) */
62 char *argv[]) /* I - Command-line arguments */
e73c6c0a 63{
cda2b561 64 const char *device_uri; /* Device URI */
9fa9ba8f 65 char scheme[255], /* Scheme in URI */
5baa731f 66 hostname[1024], /* Hostname */
67 username[255], /* Username info (not used) */
41ed0c21 68 resource[1024], /* Resource info (not used) */
69 *options, /* Pointer to options */
abd1f85f 70 *name, /* Name of option */
71 *value, /* Value of option */
72 sep; /* Option separator */
43b5ad0d 73 int print_fd; /* Print file */
5baa731f 74 int copies; /* Number of copies to print */
e3682167 75 time_t start_time; /* Time of first connect */
76#ifdef __APPLE__
77 time_t current_time, /* Current time */
ef494175 78 wait_time; /* Time to wait before shutting down socket */
e3682167 79#endif /* __APPLE__ */
ff0295f0 80 int contimeout; /* Connection timeout */
41ed0c21 81 int waiteof; /* Wait for end-of-file? */
5baa731f 82 int port; /* Port number */
086c584d 83 char portname[255]; /* Port name */
5baa731f 84 int delay; /* Delay for retries... */
43b5ad0d 85 int device_fd; /* AppSocket */
5baa731f 86 int error; /* Error code (if any) */
a0c0bbe7 87 http_addrlist_t *addrlist, /* Address list */
abd1f85f 88 *addr; /* Connected address */
a0c0bbe7 89 char addrname[256]; /* Address name */
5ba153ab 90 int snmp_fd, /* SNMP socket */
91 start_count, /* Page count via SNMP at start */
6bffe4a6 92 page_count, /* Page count via SNMP */
93 have_supplies; /* Printer supports supply levels? */
43b5ad0d 94 ssize_t tbytes; /* Total number of bytes written */
4ff40357 95#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
5baa731f 96 struct sigaction action; /* Actions for POSIX signals */
4ff40357 97#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
e73c6c0a 98
99
4b23f3b3 100 /*
101 * Make sure status messages are not buffered...
102 */
103
104 setbuf(stderr, NULL);
105
98904cd6 106 /*
107 * Ignore SIGPIPE signals...
108 */
109
110#ifdef HAVE_SIGSET
111 sigset(SIGPIPE, SIG_IGN);
112#elif defined(HAVE_SIGACTION)
113 memset(&action, 0, sizeof(action));
114 action.sa_handler = SIG_IGN;
115 sigaction(SIGPIPE, &action, NULL);
116#else
117 signal(SIGPIPE, SIG_IGN);
118#endif /* HAVE_SIGSET */
119
4b23f3b3 120 /*
121 * Check command-line...
122 */
123
68edc300 124 if (argc == 1)
125 {
3037604c 126 printf("network socket \"Unknown\" \"%s\"\n",
127 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
6248387b 128 return (CUPS_BACKEND_OK);
68edc300 129 }
130 else if (argc < 6 || argc > 7)
e73c6c0a 131 {
472af6f3 132 _cupsLangPrintf(stderr,
133 _("Usage: %s job-id user title copies options [file]\n"),
134 argv[0]);
6248387b 135 return (CUPS_BACKEND_FAILED);
e73c6c0a 136 }
137
138 /*
139 * If we have 7 arguments, print the file named on the command-line.
140 * Otherwise, send stdin instead...
141 */
142
143 if (argc == 6)
3f9cb6c6 144 {
43b5ad0d 145 print_fd = 0;
146 copies = 1;
3f9cb6c6 147 }
e73c6c0a 148 else
149 {
150 /*
151 * Try to open the print file...
152 */
153
43b5ad0d 154 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
e73c6c0a 155 {
537a5f1d 156 _cupsLangPrintf(stderr,
157 _("ERROR: Unable to open print file \"%s\": %s\n"),
158 argv[6], strerror(errno));
6248387b 159 return (CUPS_BACKEND_FAILED);
e73c6c0a 160 }
3f9cb6c6 161
162 copies = atoi(argv[4]);
e73c6c0a 163 }
164
165 /*
166 * Extract the hostname and port number from the URI...
167 */
168
cda2b561 169 if ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
170 return (CUPS_BACKEND_FAILED);
171
9fa9ba8f 172 httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
173 username, sizeof(username), hostname, sizeof(hostname), &port,
0e66904d 174 resource, sizeof(resource));
e73c6c0a 175
176 if (port == 0)
177 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
178
41ed0c21 179 /*
180 * Get options, if any...
181 */
182
ff0295f0 183 waiteof = 1;
184 contimeout = 7 * 24 * 60 * 60;
41ed0c21 185
186 if ((options = strchr(resource, '?')) != NULL)
187 {
188 /*
189 * Yup, terminate the device name string and move to the first
190 * character of the options...
191 */
192
193 *options++ = '\0';
194
195 /*
196 * Parse options...
197 */
198
199 while (*options)
200 {
201 /*
202 * Get the name...
203 */
204
abd1f85f 205 name = options;
41ed0c21 206
abd1f85f 207 while (*options && *options != '=' && *options != '+' && *options != '&')
208 options ++;
209
210 if ((sep = *options) != '\0')
211 *options++ = '\0';
212
213 if (sep == '=')
41ed0c21 214 {
215 /*
216 * Get the value...
217 */
218
abd1f85f 219 value = options;
41ed0c21 220
abd1f85f 221 while (*options && *options != '+' && *options != '&')
41ed0c21 222 options ++;
abd1f85f 223
224 if (*options)
225 *options++ = '\0';
41ed0c21 226 }
227 else
abd1f85f 228 value = (char *)"";
41ed0c21 229
230 /*
231 * Process the option...
232 */
233
234 if (!strcasecmp(name, "waiteof"))
235 {
236 /*
237 * Set the wait-for-eof value...
238 */
239
240 waiteof = !value[0] || !strcasecmp(value, "on") ||
241 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
242 }
ff0295f0 243 else if (!strcasecmp(name, "contimeout"))
244 {
245 /*
246 * Set the connection timeout...
247 */
248
249 if (atoi(value) > 0)
250 contimeout = atoi(value);
251 }
41ed0c21 252 }
253 }
254
e73c6c0a 255 /*
256 * Then try to connect to the remote host...
257 */
258
f831f3d8 259 start_time = time(NULL);
ff0295f0 260
086c584d 261 sprintf(portname, "%d", port);
262
9fa9ba8f 263 fputs("STATE: +connecting-to-device\n", stderr);
bd8ccc10 264 fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
265
086c584d 266 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
e73c6c0a 267 {
157fcea9 268 _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'\n"),
472af6f3 269 hostname);
6248387b 270 return (CUPS_BACKEND_STOP);
e73c6c0a 271 }
272
9fa9ba8f 273 fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
22c808e2 274 _cupsLangPuts(stderr, _("INFO: Connecting to printer...\n"));
e73c6c0a 275
43b5ad0d 276 for (delay = 5;;)
e73c6c0a 277 {
a0c0bbe7 278 if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL)
e73c6c0a 279 {
43b5ad0d 280 error = errno;
281 device_fd = -1;
3f9cb6c6 282
43b5ad0d 283 if (getenv("CLASS") != NULL)
284 {
285 /*
286 * If the CLASS environment variable is set, the job was submitted
287 * to a class and not to a specific queue. In this case, we want
288 * to abort immediately so that the job can be requeued on the next
289 * available printer in the class.
290 */
b7b63780 291
472af6f3 292 _cupsLangPuts(stderr,
293 _("INFO: Unable to contact printer, queuing on next "
294 "printer in class...\n"));
b7b63780 295
43b5ad0d 296 /*
297 * Sleep 5 seconds to keep the job from requeuing too rapidly...
298 */
b7b63780 299
43b5ad0d 300 sleep(5);
b7b63780 301
43b5ad0d 302 return (CUPS_BACKEND_FAILED);
303 }
b7b63780 304
43b5ad0d 305 if (error == ECONNREFUSED || error == EHOSTDOWN ||
306 error == EHOSTUNREACH)
307 {
ff0295f0 308 if (contimeout && (time(NULL) - start_time) > contimeout)
309 {
157fcea9 310 _cupsLangPuts(stderr, _("ERROR: Printer not responding\n"));
ff0295f0 311 return (CUPS_BACKEND_FAILED);
312 }
313
472af6f3 314 _cupsLangPrintf(stderr,
f831f3d8 315 _("WARNING: Network host \'%s\' is busy; will retry in "
316 "%d seconds...\n"), hostname, delay);
ff0295f0 317
43b5ad0d 318 sleep(delay);
10f725af 319
43b5ad0d 320 if (delay < 30)
321 delay += 5;
e73c6c0a 322 }
3f9cb6c6 323 else
43b5ad0d 324 {
472af6f3 325 _cupsLangPrintf(stderr, "DEBUG: Connection error: %s\n",
326 strerror(errno));
327 _cupsLangPuts(stderr,
f831f3d8 328 _("ERROR: Unable to connect to printer; will retry in 30 "
329 "seconds...\n"));
43b5ad0d 330 sleep(30);
331 }
e73c6c0a 332 }
43b5ad0d 333 else
334 break;
335 }
e73c6c0a 336
43b5ad0d 337 fputs("STATE: -connecting-to-device\n", stderr);
22c808e2 338 _cupsLangPuts(stderr, _("INFO: Connected to printer...\n"));
a0c0bbe7 339
340#ifdef AF_INET6
341 if (addr->addr.addr.sa_family == AF_INET6)
ff0295f0 342 fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
343 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
344 ntohs(addr->addr.ipv6.sin6_port));
a0c0bbe7 345 else
346#endif /* AF_INET6 */
347 if (addr->addr.addr.sa_family == AF_INET)
ff0295f0 348 fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4)...\n",
349 httpAddrString(&addr->addr, addrname, sizeof(addrname)),
350 ntohs(addr->addr.ipv4.sin_port));
4ff40357 351
5ba153ab 352 /*
353 * See if the printer supports SNMP...
354 */
355
b9e7ae13 356 if ((snmp_fd = _cupsSNMPOpen(addr->addr.addr.sa_family)) >= 0)
c97b7208 357 {
6bffe4a6 358 have_supplies = !backendSNMPSupplies(snmp_fd, &(addr->addr), &start_count,
359 NULL);
c97b7208 360 }
361 else
6bffe4a6 362 have_supplies = start_count = 0;
5ba153ab 363
43b5ad0d 364 /*
365 * Print everything...
366 */
4ff40357 367
43b5ad0d 368 tbytes = 0;
e73c6c0a 369
43b5ad0d 370 while (copies > 0 && tbytes >= 0)
371 {
3f9cb6c6 372 copies --;
7c88bf41 373
43b5ad0d 374 if (print_fd != 0)
e73c6c0a 375 {
3f9cb6c6 376 fputs("PAGE: 1 1\n", stderr);
43b5ad0d 377 lseek(print_fd, 0, SEEK_SET);
e73c6c0a 378 }
e73c6c0a 379
0592de24 380 tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addr->addr), 1, 0,
5ba153ab 381 backendNetworkSideCB);
5baa731f 382
43b5ad0d 383 if (print_fd != 0 && tbytes >= 0)
472af6f3 384 _cupsLangPrintf(stderr,
ff0295f0 385#ifdef HAVE_LONG_LONG
472af6f3 386 _("INFO: Sent print file, %lld bytes...\n"),
ff0295f0 387#else
472af6f3 388 _("INFO: Sent print file, %ld bytes...\n"),
ff0295f0 389#endif /* HAVE_LONG_LONG */
472af6f3 390 CUPS_LLCAST tbytes);
43b5ad0d 391 }
3f9cb6c6 392
a52a4879 393#ifdef __APPLE__
313509aa 394 /*
ef494175 395 * Wait up to 5 seconds to get any pending back-channel data...
313509aa 396 */
397
ef494175 398 wait_time = time(NULL) + 5;
8953a911 399 while (wait_time >= time(&current_time))
400 if (wait_bc(device_fd, wait_time - current_time) <= 0)
ef494175 401 break;
a52a4879 402#endif /* __APPLE__ */
313509aa 403
43b5ad0d 404 if (waiteof)
405 {
406 /*
407 * Shutdown the socket and wait for the other end to finish...
408 */
3f9cb6c6 409
472af6f3 410 _cupsLangPuts(stderr,
411 _("INFO: Print file sent, waiting for printer to finish...\n"));
0cd4970e 412
43b5ad0d 413 shutdown(device_fd, 1);
3f9cb6c6 414
313509aa 415 while (wait_bc(device_fd, 90) > 0);
43b5ad0d 416 }
b5cb0608 417
5ba153ab 418 /*
419 * Collect the final page count as needed...
420 */
421
6bffe4a6 422 if (have_supplies &&
5ba153ab 423 !backendSNMPSupplies(snmp_fd, &(addr->addr), &page_count, NULL) &&
424 page_count > start_count)
425 fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
426
43b5ad0d 427 /*
428 * Close the socket connection...
429 */
3f9cb6c6 430
43b5ad0d 431 close(device_fd);
e73c6c0a 432
086c584d 433 httpAddrFreeList(addrlist);
434
e73c6c0a 435 /*
3f9cb6c6 436 * Close the input file and return...
e73c6c0a 437 */
438
43b5ad0d 439 if (print_fd != 0)
440 close(print_fd);
e73c6c0a 441
43b5ad0d 442 if (tbytes >= 0)
472af6f3 443 _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
6248387b 444
43b5ad0d 445 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
e73c6c0a 446}
43e2fc22 447
448
313509aa 449/*
450 * 'wait_bc()' - Wait for back-channel data...
451 */
452
453static int /* O - # bytes read or -1 on error */
454wait_bc(int device_fd, /* I - Socket */
455 int secs) /* I - Seconds to wait */
456{
457 struct timeval timeout; /* Timeout for select() */
458 fd_set input; /* Input set for select() */
459 ssize_t bytes; /* Number of back-channel bytes read */
460 char buffer[1024]; /* Back-channel buffer */
461
462
463 /*
464 * Wait up to "secs" seconds for backchannel data...
465 */
466
467 timeout.tv_sec = secs;
468 timeout.tv_usec = 0;
469
470 FD_ZERO(&input);
471 FD_SET(device_fd, &input);
472
473 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
474 {
475 /*
476 * Grab the data coming back and spit it out to stderr...
477 */
478
479 if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0)
480 {
157fcea9 481 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data\n",
313509aa 482 (int)bytes);
483 cupsBackChannelWrite(buffer, bytes, 1.0);
484 }
485
486 return (bytes);
487 }
488 else
489 return (-1);
490}
491
492
b2e10895 493/*
494 * End of "$Id$".
43e2fc22 495 */