]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
ebed1361b476f6c5d220e32be9496f6d00629128
4 * AppSocket backend for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Send a file to the printer or server.
20 * wait_bc() - Wait for back-channel data...
24 * Include necessary headers.
27 #include <cups/http-private.h>
28 #include "backend-private.h"
30 #include <sys/types.h>
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <arpa/inet.h>
49 static int wait_bc(int device_fd
, int secs
);
53 * 'main()' - Send a file to the printer or server.
57 * printer-uri job-id user title copies options [file]
60 int /* O - Exit status */
61 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
62 char *argv
[]) /* I - Command-line arguments */
64 const char *device_uri
; /* Device URI */
65 char scheme
[255], /* Scheme in URI */
66 hostname
[1024], /* Hostname */
67 username
[255], /* Username info (not used) */
68 resource
[1024], /* Resource info (not used) */
69 *options
, /* Pointer to options */
70 *name
, /* Name of option */
71 *value
, /* Value of option */
72 sep
; /* Option separator */
73 int print_fd
; /* Print file */
74 int copies
; /* Number of copies to print */
75 time_t start_time
; /* Time of first connect */
76 int contimeout
; /* Connection timeout */
77 int waiteof
; /* Wait for end-of-file? */
78 int port
; /* Port number */
79 char portname
[255]; /* Port name */
80 int delay
; /* Delay for retries... */
81 int device_fd
; /* AppSocket */
82 int error
; /* Error code (if any) */
83 http_addrlist_t
*addrlist
, /* Address list */
84 *addr
; /* Connected address */
85 char addrname
[256]; /* Address name */
86 int snmp_enabled
= 1; /* Is SNMP enabled? */
87 int snmp_fd
, /* SNMP socket */
88 start_count
, /* Page count via SNMP at start */
89 page_count
, /* Page count via SNMP */
90 have_supplies
; /* Printer supports supply levels? */
91 ssize_t bytes
= 0, /* Initial bytes read */
92 tbytes
; /* Total number of bytes written */
93 char buffer
[1024]; /* Initial print buffer */
94 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
95 struct sigaction action
; /* Actions for POSIX signals */
96 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
100 * Make sure status messages are not buffered...
103 setbuf(stderr
, NULL
);
106 * Ignore SIGPIPE signals...
110 sigset(SIGPIPE
, SIG_IGN
);
111 #elif defined(HAVE_SIGACTION)
112 memset(&action
, 0, sizeof(action
));
113 action
.sa_handler
= SIG_IGN
;
114 sigaction(SIGPIPE
, &action
, NULL
);
116 signal(SIGPIPE
, SIG_IGN
);
117 #endif /* HAVE_SIGSET */
120 * Check command-line...
125 printf("network socket \"Unknown\" \"%s\"\n",
126 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
127 return (CUPS_BACKEND_OK
);
129 else if (argc
< 6 || argc
> 7)
131 _cupsLangPrintf(stderr
,
132 _("Usage: %s job-id user title copies options [file]"),
134 return (CUPS_BACKEND_FAILED
);
138 * If we have 7 arguments, print the file named on the command-line.
139 * Otherwise, send stdin instead...
150 * Try to open the print file...
153 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
155 _cupsLangPrintError("ERROR", _("Unable to open print file"));
156 return (CUPS_BACKEND_FAILED
);
159 copies
= atoi(argv
[4]);
163 * Extract the hostname and port number from the URI...
166 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
168 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
171 if (getenv("CLASS") != NULL
)
172 return (CUPS_BACKEND_FAILED
);
175 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
176 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
177 resource
, sizeof(resource
));
180 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
183 * Get options, if any...
187 contimeout
= 7 * 24 * 60 * 60;
189 if ((options
= strchr(resource
, '?')) != NULL
)
192 * Yup, terminate the device name string and move to the first
193 * character of the options...
210 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
213 if ((sep
= *options
) != '\0')
224 while (*options
&& *options
!= '+' && *options
!= '&')
234 * Process the option...
237 if (!_cups_strcasecmp(name
, "waiteof"))
240 * Set the wait-for-eof value...
243 waiteof
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
244 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
246 else if (!_cups_strcasecmp(name
, "snmp"))
249 * Enable/disable SNMP stuff...
252 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
253 _cups_strcasecmp(value
, "yes") ||
254 _cups_strcasecmp(value
, "true");
256 else if (!_cups_strcasecmp(name
, "contimeout"))
259 * Set the connection timeout...
263 contimeout
= atoi(value
);
269 * Then try finding the remote host...
272 start_time
= time(NULL
);
274 sprintf(portname
, "%d", port
);
276 fputs("STATE: +connecting-to-device\n", stderr
);
277 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
279 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
281 _cupsLangPrintFilter(stderr
, "INFO",
282 _("Unable to locate printer \"%s\"."), hostname
);
285 if (getenv("CLASS") != NULL
)
287 fputs("STATE: -connecting-to-device\n", stderr
);
288 return (CUPS_BACKEND_STOP
);
293 * See if the printer supports SNMP...
297 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
302 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
305 have_supplies
= start_count
= 0;
308 * Wait for data from the filter...
313 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 1, backendNetworkSideCB
))
314 return (CUPS_BACKEND_OK
);
315 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
316 return (CUPS_BACKEND_OK
);
320 * Connect to the printer...
323 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
324 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
328 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
333 if (getenv("CLASS") != NULL
)
336 * If the CLASS environment variable is set, the job was submitted
337 * to a class and not to a specific queue. In this case, we want
338 * to abort immediately so that the job can be requeued on the next
339 * available printer in the class.
342 _cupsLangPrintFilter(stderr
, "INFO",
343 _("Unable to contact printer, queuing on next "
344 "printer in class."));
347 * Sleep 5 seconds to keep the job from requeuing too rapidly...
352 return (CUPS_BACKEND_FAILED
);
355 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
357 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
358 error
== EHOSTUNREACH
)
360 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
362 _cupsLangPrintFilter(stderr
, "ERROR",
363 _("The printer is not responding."));
364 return (CUPS_BACKEND_FAILED
);
370 _cupsLangPrintFilter(stderr
, "WARNING",
371 _("The printer may not exist or "
372 "is unavailable at this time."));
376 _cupsLangPrintFilter(stderr
, "WARNING",
377 _("The printer is unreachable at this "
383 _cupsLangPrintFilter(stderr
, "WARNING",
384 _("The printer is in use."));
395 _cupsLangPrintFilter(stderr
, "ERROR",
396 _("The printer is not responding."));
404 fputs("STATE: -connecting-to-device\n", stderr
);
405 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
407 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
408 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
409 httpAddrPort(&(addr
->addr
)));
412 * Print everything...
418 tbytes
+= write(device_fd
, buffer
, bytes
);
420 while (copies
> 0 && tbytes
>= 0)
426 fputs("PAGE: 1 1\n", stderr
);
427 lseek(print_fd
, 0, SEEK_SET
);
430 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addrlist
->addr
), 1,
431 0, backendNetworkSideCB
);
433 if (print_fd
!= 0 && tbytes
>= 0)
434 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
440 * Shutdown the socket and wait for the other end to finish...
443 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for printer to finish."));
445 shutdown(device_fd
, 1);
447 while (wait_bc(device_fd
, 90) > 0);
451 * Collect the final page count as needed...
455 !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), &page_count
, NULL
) &&
456 page_count
> start_count
)
457 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
460 * Close the socket connection...
465 httpAddrFreeList(addrlist
);
468 * Close the input file and return...
474 return (CUPS_BACKEND_OK
);
479 * 'wait_bc()' - Wait for back-channel data...
482 static int /* O - # bytes read or -1 on error */
483 wait_bc(int device_fd
, /* I - Socket */
484 int secs
) /* I - Seconds to wait */
486 struct timeval timeout
; /* Timeout for select() */
487 fd_set input
; /* Input set for select() */
488 ssize_t bytes
; /* Number of back-channel bytes read */
489 char buffer
[1024]; /* Back-channel buffer */
493 * Wait up to "secs" seconds for backchannel data...
496 timeout
.tv_sec
= secs
;
500 FD_SET(device_fd
, &input
);
502 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
505 * Grab the data coming back and spit it out to stderr...
508 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
510 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data\n",
512 cupsBackChannelWrite(buffer
, bytes
, 1.0);