]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * AppSocket backend for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * "LICENSE" which should have been included with this file. If this
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers.
20 #include <cups/http-private.h>
21 #include "backend-private.h"
23 #include <sys/types.h>
31 # include <sys/socket.h>
32 # include <netinet/in.h>
33 # include <arpa/inet.h>
42 static ssize_t
wait_bc(int device_fd
, int secs
);
46 * 'main()' - Send a file to the printer or server.
50 * printer-uri job-id user title copies options [file]
53 int /* O - Exit status */
54 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
55 char *argv
[]) /* I - Command-line arguments */
57 const char *device_uri
; /* Device URI */
58 char scheme
[255], /* Scheme in URI */
59 hostname
[1024], /* Hostname */
60 username
[255], /* Username info (not used) */
61 resource
[1024], /* Resource info (not used) */
62 *options
, /* Pointer to options */
63 *name
, /* Name of option */
64 *value
, /* Value of option */
65 sep
; /* Option separator */
66 int print_fd
; /* Print file */
67 int copies
; /* Number of copies to print */
68 time_t start_time
; /* Time of first connect */
69 int contimeout
; /* Connection timeout */
70 int waiteof
; /* Wait for end-of-file? */
71 int port
; /* Port number */
72 char portname
[255]; /* Port name */
73 int delay
; /* Delay for retries... */
74 int device_fd
; /* AppSocket */
75 int error
; /* Error code (if any) */
76 http_addrlist_t
*addrlist
, /* Address list */
77 *addr
; /* Connected address */
78 char addrname
[256]; /* Address name */
79 int snmp_enabled
= 1; /* Is SNMP enabled? */
80 int snmp_fd
, /* SNMP socket */
81 start_count
, /* Page count via SNMP at start */
82 page_count
, /* Page count via SNMP */
83 have_supplies
; /* Printer supports supply levels? */
84 ssize_t bytes
= 0, /* Initial bytes read */
85 tbytes
; /* Total number of bytes written */
86 char buffer
[1024]; /* Initial print buffer */
87 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
88 struct sigaction action
; /* Actions for POSIX signals */
89 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
93 * Make sure status messages are not buffered...
99 * Ignore SIGPIPE signals...
103 sigset(SIGPIPE
, SIG_IGN
);
104 #elif defined(HAVE_SIGACTION)
105 memset(&action
, 0, sizeof(action
));
106 action
.sa_handler
= SIG_IGN
;
107 sigaction(SIGPIPE
, &action
, NULL
);
109 signal(SIGPIPE
, SIG_IGN
);
110 #endif /* HAVE_SIGSET */
113 * Check command-line...
118 printf("network socket \"Unknown\" \"%s\"\n",
119 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
120 return (CUPS_BACKEND_OK
);
122 else if (argc
< 6 || argc
> 7)
124 _cupsLangPrintf(stderr
,
125 _("Usage: %s job-id user title copies options [file]"),
127 return (CUPS_BACKEND_FAILED
);
131 * If we have 7 arguments, print the file named on the command-line.
132 * Otherwise, send stdin instead...
143 * Try to open the print file...
146 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
148 _cupsLangPrintError("ERROR", _("Unable to open print file"));
149 return (CUPS_BACKEND_FAILED
);
152 copies
= atoi(argv
[4]);
156 * Extract the hostname and port number from the URI...
159 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
161 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
164 if (getenv("CLASS") != NULL
)
165 return (CUPS_BACKEND_FAILED
);
168 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
169 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
170 resource
, sizeof(resource
));
173 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
176 * Get options, if any...
180 contimeout
= 7 * 24 * 60 * 60;
182 if ((options
= strchr(resource
, '?')) != NULL
)
185 * Yup, terminate the device name string and move to the first
186 * character of the options...
203 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
206 if ((sep
= *options
) != '\0')
217 while (*options
&& *options
!= '+' && *options
!= '&')
227 * Process the option...
230 if (!_cups_strcasecmp(name
, "waiteof"))
233 * Set the wait-for-eof value...
236 waiteof
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
237 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
239 else if (!_cups_strcasecmp(name
, "snmp"))
242 * Enable/disable SNMP stuff...
245 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
246 !_cups_strcasecmp(value
, "yes") ||
247 !_cups_strcasecmp(value
, "true");
249 else if (!_cups_strcasecmp(name
, "contimeout"))
252 * Set the connection timeout...
256 contimeout
= atoi(value
);
262 * Then try finding the remote host...
265 start_time
= time(NULL
);
267 sprintf(portname
, "%d", port
);
269 fputs("STATE: +connecting-to-device\n", stderr
);
270 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
272 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
274 _cupsLangPrintFilter(stderr
, "INFO",
275 _("Unable to locate printer \"%s\"."), hostname
);
278 if (getenv("CLASS") != NULL
)
280 fputs("STATE: -connecting-to-device\n", stderr
);
281 return (CUPS_BACKEND_STOP
);
286 * See if the printer supports SNMP...
290 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
295 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
298 have_supplies
= start_count
= 0;
301 * Wait for data from the filter...
306 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 1, backendNetworkSideCB
))
307 return (CUPS_BACKEND_OK
);
308 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
309 return (CUPS_BACKEND_OK
);
313 * Connect to the printer...
316 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
317 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
321 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
326 if (getenv("CLASS") != NULL
)
329 * If the CLASS environment variable is set, the job was submitted
330 * to a class and not to a specific queue. In this case, we want
331 * to abort immediately so that the job can be requeued on the next
332 * available printer in the class.
335 _cupsLangPrintFilter(stderr
, "INFO",
336 _("Unable to contact printer, queuing on next "
337 "printer in class."));
340 * Sleep 5 seconds to keep the job from requeuing too rapidly...
345 return (CUPS_BACKEND_FAILED
);
348 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
350 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
351 error
== EHOSTUNREACH
)
353 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
355 _cupsLangPrintFilter(stderr
, "ERROR",
356 _("The printer is not responding."));
357 return (CUPS_BACKEND_FAILED
);
363 _cupsLangPrintFilter(stderr
, "WARNING",
364 _("The printer may not exist or "
365 "is unavailable at this time."));
369 _cupsLangPrintFilter(stderr
, "WARNING",
370 _("The printer is unreachable at this "
376 _cupsLangPrintFilter(stderr
, "WARNING",
377 _("The printer is in use."));
381 sleep((unsigned)delay
);
388 _cupsLangPrintFilter(stderr
, "ERROR",
389 _("The printer is not responding."));
397 fputs("STATE: -connecting-to-device\n", stderr
);
398 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
400 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
401 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
402 httpAddrPort(&(addr
->addr
)));
405 * Print everything...
411 tbytes
+= write(device_fd
, buffer
, (size_t)bytes
);
413 while (copies
> 0 && tbytes
>= 0)
419 fputs("PAGE: 1 1\n", stderr
);
420 lseek(print_fd
, 0, SEEK_SET
);
423 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addrlist
->addr
), 1,
424 0, backendNetworkSideCB
);
426 if (print_fd
!= 0 && tbytes
>= 0)
427 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
430 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
435 * Shutdown the socket and wait for the other end to finish...
438 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for printer to finish."));
440 shutdown(device_fd
, 1);
442 while (wait_bc(device_fd
, 90) > 0);
446 * Collect the final page count as needed...
450 !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), &page_count
, NULL
) &&
451 page_count
> start_count
)
452 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
455 * Close the socket connection...
460 httpAddrFreeList(addrlist
);
463 * Close the input file and return...
469 return (CUPS_BACKEND_OK
);
474 * 'wait_bc()' - Wait for back-channel data...
477 static ssize_t
/* O - # bytes read or -1 on error */
478 wait_bc(int device_fd
, /* I - Socket */
479 int secs
) /* I - Seconds to wait */
481 struct timeval timeout
; /* Timeout for select() */
482 fd_set input
; /* Input set for select() */
483 ssize_t bytes
; /* Number of back-channel bytes read */
484 char buffer
[1024]; /* Back-channel buffer */
488 * Wait up to "secs" seconds for backchannel data...
491 timeout
.tv_sec
= secs
;
495 FD_SET(device_fd
, &input
);
497 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
500 * Grab the data coming back and spit it out to stderr...
503 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
505 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data\n",
507 cupsBackChannelWrite(buffer
, (size_t)bytes
, 1.0);