]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * "$Id: socket.c 7881 2008-08-28 20:21:56Z mike $"
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2011 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 */
77 time_t current_time
, /* Current time */
78 wait_time
; /* Time to wait before shutting down socket */
79 #endif /* __APPLE__ */
80 int contimeout
; /* Connection timeout */
81 int waiteof
; /* Wait for end-of-file? */
82 int port
; /* Port number */
83 char portname
[255]; /* Port name */
84 int delay
; /* Delay for retries... */
85 int device_fd
; /* AppSocket */
86 int error
; /* Error code (if any) */
87 http_addrlist_t
*addrlist
, /* Address list */
88 *addr
; /* Connected address */
89 char addrname
[256]; /* Address name */
90 int snmp_fd
, /* SNMP socket */
91 start_count
, /* Page count via SNMP at start */
92 page_count
, /* Page count via SNMP */
93 have_supplies
; /* Printer supports supply levels? */
94 ssize_t tbytes
; /* Total number of bytes written */
95 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
96 struct sigaction action
; /* Actions for POSIX signals */
97 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
101 * Make sure status messages are not buffered...
104 setbuf(stderr
, NULL
);
107 * Ignore SIGPIPE signals...
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
);
117 signal(SIGPIPE
, SIG_IGN
);
118 #endif /* HAVE_SIGSET */
121 * Check command-line...
126 printf("network socket \"Unknown\" \"%s\"\n",
127 _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect")));
128 return (CUPS_BACKEND_OK
);
130 else if (argc
< 6 || argc
> 7)
132 _cupsLangPrintf(stderr
,
133 _("Usage: %s job-id user title copies options [file]"),
135 return (CUPS_BACKEND_FAILED
);
139 * If we have 7 arguments, print the file named on the command-line.
140 * Otherwise, send stdin instead...
151 * Try to open the print file...
154 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
156 _cupsLangPrintError("ERROR", _("Unable to open print file"));
157 return (CUPS_BACKEND_FAILED
);
160 copies
= atoi(argv
[4]);
164 * Extract the hostname and port number from the URI...
167 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
169 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
172 if (getenv("CLASS") != NULL
)
173 return (CUPS_BACKEND_FAILED
);
176 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
177 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
178 resource
, sizeof(resource
));
181 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
184 * Get options, if any...
188 contimeout
= 7 * 24 * 60 * 60;
190 if ((options
= strchr(resource
, '?')) != NULL
)
193 * Yup, terminate the device name string and move to the first
194 * character of the options...
211 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
214 if ((sep
= *options
) != '\0')
225 while (*options
&& *options
!= '+' && *options
!= '&')
235 * Process the option...
238 if (!strcasecmp(name
, "waiteof"))
241 * Set the wait-for-eof value...
244 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
245 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
247 else if (!strcasecmp(name
, "contimeout"))
250 * Set the connection timeout...
254 contimeout
= atoi(value
);
260 * Then try finding the remote host...
263 start_time
= time(NULL
);
265 sprintf(portname
, "%d", port
);
267 fputs("STATE: +connecting-to-device\n", stderr
);
268 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
270 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
272 _cupsLangPrintFilter(stderr
, "INFO",
273 _("Unable to locate printer \"%s\"."), hostname
);
276 if (getenv("CLASS") != NULL
)
278 fputs("STATE: -connecting-to-device\n", stderr
);
279 return (CUPS_BACKEND_STOP
);
284 * See if the printer supports SNMP...
287 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
289 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &start_count
,
293 have_supplies
= start_count
= 0;
296 * Wait for data from the filter...
300 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), backendNetworkSideCB
))
301 return (CUPS_BACKEND_OK
);
304 * Connect to the printer...
307 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
308 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
312 if ((addr
= httpAddrConnect(addrlist
, &device_fd
)) == NULL
)
317 if (getenv("CLASS") != NULL
)
320 * If the CLASS environment variable is set, the job was submitted
321 * to a class and not to a specific queue. In this case, we want
322 * to abort immediately so that the job can be requeued on the next
323 * available printer in the class.
326 _cupsLangPrintFilter(stderr
, "INFO",
327 _("Unable to contact printer, queuing on next "
328 "printer in class."));
331 * Sleep 5 seconds to keep the job from requeuing too rapidly...
336 return (CUPS_BACKEND_FAILED
);
339 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
341 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
342 error
== EHOSTUNREACH
)
344 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
346 _cupsLangPrintFilter(stderr
, "ERROR",
347 _("The printer is not responding."));
348 return (CUPS_BACKEND_FAILED
);
354 _cupsLangPrintFilter(stderr
, "WARNING",
355 _("Network printer \"%s\" may not exist or "
356 "is unavailable at this time."),
361 _cupsLangPrintFilter(stderr
, "WARNING",
362 _("Network printer \"%s\" is unreachable at "
363 "this time."), hostname
);
368 _cupsLangPrintFilter(stderr
, "WARNING",
369 _("Network printer \"%s\" is busy."),
381 _cupsLangPrintFilter(stderr
, "ERROR",
382 _("Network printer \"%s\" is not responding."),
391 fputs("STATE: -connecting-to-device\n", stderr
);
392 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
395 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
396 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
397 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
398 ntohs(addr
->addr
.ipv6
.sin6_port
));
400 #endif /* AF_INET6 */
401 if (addr
->addr
.addr
.sa_family
== AF_INET
)
402 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4)...\n",
403 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
404 ntohs(addr
->addr
.ipv4
.sin_port
));
407 * Print everything...
412 while (copies
> 0 && tbytes
>= 0)
418 fputs("PAGE: 1 1\n", stderr
);
419 lseek(print_fd
, 0, SEEK_SET
);
422 tbytes
= backendRunLoop(print_fd
, device_fd
, snmp_fd
, &(addr
->addr
), 1, 0,
423 backendNetworkSideCB
);
425 if (print_fd
!= 0 && tbytes
>= 0)
426 _cupsLangPrintFilter(stderr
, "INFO", _("Print file sent."));
431 * Wait up to 5 seconds to get any pending back-channel data...
434 wait_time
= time(NULL
) + 5;
435 while (wait_time
>= time(¤t_time
))
436 if (wait_bc(device_fd
, wait_time
- current_time
) <= 0)
438 #endif /* __APPLE__ */
443 * Shutdown the socket and wait for the other end to finish...
446 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for printer to finish."));
448 shutdown(device_fd
, 1);
450 while (wait_bc(device_fd
, 90) > 0);
454 * Collect the final page count as needed...
458 !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &page_count
, NULL
) &&
459 page_count
> start_count
)
460 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
463 * Close the socket connection...
468 httpAddrFreeList(addrlist
);
471 * Close the input file and return...
477 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
479 return (CUPS_BACKEND_OK
);
484 * 'wait_bc()' - Wait for back-channel data...
487 static int /* O - # bytes read or -1 on error */
488 wait_bc(int device_fd
, /* I - Socket */
489 int secs
) /* I - Seconds to wait */
491 struct timeval timeout
; /* Timeout for select() */
492 fd_set input
; /* Input set for select() */
493 ssize_t bytes
; /* Number of back-channel bytes read */
494 char buffer
[1024]; /* Back-channel buffer */
498 * Wait up to "secs" seconds for backchannel data...
501 timeout
.tv_sec
= secs
;
505 FD_SET(device_fd
, &input
);
507 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
510 * Grab the data coming back and spit it out to stderr...
513 if ((bytes
= read(device_fd
, buffer
, sizeof(buffer
))) > 0)
515 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data\n",
517 cupsBackChannelWrite(buffer
, bytes
, 1.0);
528 * End of "$Id: socket.c 7881 2008-08-28 20:21:56Z mike $".