2 * "$Id: lpd.c 7740 2008-07-14 23:58:05Z mike $"
4 * Line Printer Daemon backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * lpd_command() - Send an LPR command sequence and wait for a reply.
21 * lpd_queue() - Queue a file using the Line Printer Daemon protocol.
22 * lpd_timeout() - Handle timeout alarms...
23 * lpd_write() - Write a buffer of data to an LPD server.
24 * rresvport_af() - A simple implementation of rresvport_af().
25 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
29 * Include necessary headers.
32 #include <cups/http-private.h>
33 #include "backend-private.h"
35 #include <sys/types.h>
41 # include <sys/socket.h>
42 # include <netinet/in.h>
43 # include <arpa/inet.h>
47 # include <CoreFoundation/CFNumber.h>
48 # include <CoreFoundation/CFPreferences.h>
49 #endif /* __APPLE__ */
56 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
57 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
64 #define MODE_STANDARD 0 /* Queue a copy */
65 #define MODE_STREAM 1 /* Stream a copy */
69 * The order for control and data files in LPD requests...
72 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
73 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
80 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
81 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
82 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
89 static int lpd_command(int lpd_fd
, int timeout
, char *format
, ...);
90 static int lpd_queue(const char *hostname
, int port
, const char *printer
,
91 int print_fd
, int mode
, const char *user
,
92 const char *title
, int copies
, int banner
,
93 int format
, int order
, int reserve
,
94 int manual_copies
, int timeout
, int contimeout
);
95 static void lpd_timeout(int sig
);
96 static int lpd_write(int lpd_fd
, char *buffer
, int length
);
97 #ifndef HAVE_RRESVPORT_AF
98 static int rresvport_af(int *port
, int family
);
99 #endif /* !HAVE_RRESVPORT_AF */
100 static void sigterm_handler(int sig
);
104 * 'main()' - Send a file to the printer or server.
108 * printer-uri job-id user title copies options [file]
111 int /* O - Exit status */
112 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
113 char *argv
[]) /* I - Command-line arguments */
115 const char *device_uri
; /* Device URI */
116 char method
[255], /* Method in URI */
117 hostname
[1024], /* Hostname */
118 username
[255], /* Username info */
119 resource
[1024], /* Resource info (printer name) */
120 *options
, /* Pointer to options */
121 *name
, /* Name of option */
122 *value
, /* Value of option */
123 sep
, /* Separator character */
124 *filename
, /* File to print */
125 title
[256]; /* Title string */
126 int port
; /* Port number */
127 int fd
; /* Print file */
128 int status
; /* Status of LPD job */
129 int mode
; /* Print mode */
130 int banner
; /* Print banner page? */
131 int format
; /* Print format */
132 int order
; /* Order of control/data files */
133 int reserve
; /* Reserve priviledged port? */
134 int sanitize_title
; /* Sanitize title string? */
135 int manual_copies
, /* Do manual copies? */
136 timeout
, /* Timeout */
137 contimeout
, /* Connection timeout */
138 copies
; /* Number of copies */
139 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
140 struct sigaction action
; /* Actions for POSIX signals */
141 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
145 * Make sure status messages are not buffered...
148 setbuf(stderr
, NULL
);
151 * Ignore SIGPIPE and catch SIGTERM signals...
155 sigset(SIGPIPE
, SIG_IGN
);
156 sigset(SIGTERM
, sigterm_handler
);
157 #elif defined(HAVE_SIGACTION)
158 memset(&action
, 0, sizeof(action
));
159 action
.sa_handler
= SIG_IGN
;
160 sigaction(SIGPIPE
, &action
, NULL
);
162 sigemptyset(&action
.sa_mask
);
163 sigaddset(&action
.sa_mask
, SIGTERM
);
164 action
.sa_handler
= sigterm_handler
;
165 sigaction(SIGTERM
, &action
, NULL
);
167 signal(SIGPIPE
, SIG_IGN
);
168 signal(SIGTERM
, sigterm_handler
);
169 #endif /* HAVE_SIGSET */
172 * Check command-line...
177 printf("network lpd \"Unknown\" \"%s\"\n",
178 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
179 return (CUPS_BACKEND_OK
);
181 else if (argc
< 6 || argc
> 7)
183 _cupsLangPrintf(stderr
,
184 _("Usage: %s job-id user title copies options [file]\n"),
186 return (CUPS_BACKEND_FAILED
);
190 * Extract the hostname and printer name from the URI...
193 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
194 return (CUPS_BACKEND_FAILED
);
196 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
,
197 method
, sizeof(method
), username
, sizeof(username
),
198 hostname
, sizeof(hostname
), &port
,
199 resource
, sizeof(resource
));
202 port
= 515; /* Default to port 515 */
207 * If no username is in the device URI, then use the print job user...
210 strlcpy(username
, argv
[2], sizeof(username
));
214 * See if there are any options...
217 mode
= MODE_STANDARD
;
220 order
= ORDER_CONTROL_DATA
;
221 reserve
= RESERVE_ANY
;
224 contimeout
= 7 * 24 * 60 * 60;
228 * We want to pass utf-8 characters, not re-map them (3071945)
234 * Get the default timeout from a system preference...
238 CFPropertyListRef pvalue
; /* Preference value */
239 SInt32 toval
; /* Timeout value */
242 pvalue
= CFPreferencesCopyValue(CFSTR("timeout"),
243 CFSTR("com.apple.print.backends"),
244 kCFPreferencesAnyUser
,
245 kCFPreferencesCurrentHost
);
248 if (CFGetTypeID(pvalue
) == CFNumberGetTypeID())
250 CFNumberGetValue(pvalue
, kCFNumberSInt32Type
, &toval
);
251 contimeout
= (int)toval
;
259 #endif /* __APPLE__ */
261 if ((options
= strchr(resource
, '?')) != NULL
)
264 * Yup, terminate the device name string and move to the first
265 * character of the options...
282 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
285 if ((sep
= *options
) != '\0')
296 while (*options
&& *options
!= '+' && *options
!= '&')
306 * Process the option...
309 if (!strcasecmp(name
, "banner"))
315 banner
= !value
[0] || !strcasecmp(value
, "on") ||
316 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
318 else if (!strcasecmp(name
, "format") && value
[0])
321 * Set output format...
324 if (strchr("cdfglnoprtv", value
[0]))
327 _cupsLangPrintf(stderr
, _("ERROR: Unknown format character \"%c\"\n"),
330 else if (!strcasecmp(name
, "mode") && value
[0])
333 * Set control/data order...
336 if (!strcasecmp(value
, "standard"))
337 mode
= MODE_STANDARD
;
338 else if (!strcasecmp(value
, "stream"))
341 _cupsLangPrintf(stderr
, _("ERROR: Unknown print mode \"%s\"\n"),
344 else if (!strcasecmp(name
, "order") && value
[0])
347 * Set control/data order...
350 if (!strcasecmp(value
, "control,data"))
351 order
= ORDER_CONTROL_DATA
;
352 else if (!strcasecmp(value
, "data,control"))
353 order
= ORDER_DATA_CONTROL
;
355 _cupsLangPrintf(stderr
, _("ERROR: Unknown file order \"%s\"\n"),
358 else if (!strcasecmp(name
, "reserve"))
361 * Set port reservation mode...
364 if (!value
[0] || !strcasecmp(value
, "on") ||
365 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true") ||
366 !strcasecmp(value
, "rfc1179"))
367 reserve
= RESERVE_RFC1179
;
368 else if (!strcasecmp(value
, "any"))
369 reserve
= RESERVE_ANY
;
371 reserve
= RESERVE_NONE
;
373 else if (!strcasecmp(name
, "manual_copies"))
376 * Set manual copies...
379 manual_copies
= !value
[0] || !strcasecmp(value
, "on") ||
380 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
382 else if (!strcasecmp(name
, "sanitize_title"))
385 * Set sanitize title...
388 sanitize_title
= !value
[0] || !strcasecmp(value
, "on") ||
389 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
391 else if (!strcasecmp(name
, "timeout"))
398 timeout
= atoi(value
);
400 else if (!strcasecmp(name
, "contimeout"))
403 * Set the connection timeout...
407 contimeout
= atoi(value
);
412 if (mode
== MODE_STREAM
)
413 order
= ORDER_CONTROL_DATA
;
416 * If we have 7 arguments, print the file named on the command-line.
417 * Otherwise, copy stdin to a temporary file and print the temporary
421 if (argc
== 6 && mode
== MODE_STANDARD
)
424 * Copy stdin to a temporary file...
427 char buffer
[8192]; /* Buffer for copying */
428 int bytes
; /* Number of bytes read */
431 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
433 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
434 return (CUPS_BACKEND_FAILED
);
437 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
438 if (write(fd
, buffer
, bytes
) < bytes
)
440 _cupsLangPrintError(_("ERROR: Unable to write to temporary file"));
443 return (CUPS_BACKEND_FAILED
);
449 * Stream from stdin...
458 fd
= open(filename
, O_RDONLY
);
462 _cupsLangPrintf(stderr
, _("ERROR: Unable to open print file %s: %s\n"),
463 filename
, strerror(errno
));
464 return (CUPS_BACKEND_FAILED
);
469 * Sanitize the document title...
472 strlcpy(title
, argv
[3], sizeof(title
));
477 * Sanitize the title string so that we don't cause problems on
483 for (ptr
= title
; *ptr
; ptr
++)
484 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
496 manual_copies
= atoi(argv
[4]);
502 copies
= atoi(argv
[4]);
505 status
= lpd_queue(hostname
, port
, resource
+ 1, fd
, mode
,
506 username
, title
, copies
,
507 banner
, format
, order
, reserve
, manual_copies
,
508 timeout
, contimeout
);
511 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
514 status
= lpd_queue(hostname
, port
, resource
+ 1, fd
, mode
,
516 banner
, format
, order
, reserve
, 1,
517 timeout
, contimeout
);
520 * Remove the temporary file if necessary...
530 * Return the queue status...
538 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
541 static int /* O - Status of command */
542 lpd_command(int fd
, /* I - Socket connection to LPD host */
543 int timeout
, /* I - Seconds to wait for a response */
544 char *format
, /* I - printf()-style format string */
545 ...) /* I - Additional args as necessary */
547 va_list ap
; /* Argument pointer */
548 char buf
[1024]; /* Output buffer */
549 int bytes
; /* Number of bytes to output */
550 char status
; /* Status from command */
554 * Don't try to send commands if the job has been cancelled...
561 * Format the string...
564 va_start(ap
, format
);
565 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
568 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
571 * Send the command...
574 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
576 if (lpd_write(fd
, buf
, bytes
) < bytes
)
578 _cupsLangPrintError(_("ERROR: Unable to send LPD command"));
583 * Read back the status from the command and return it...
586 fputs("DEBUG: Reading command status...\n", stderr
);
590 if (recv(fd
, &status
, 1, 0) < 1)
592 _cupsLangPrintf(stderr
,
593 _("WARNING: Remote host did not respond with command "
594 "status byte after %d seconds!\n"), timeout
);
600 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
607 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
610 static int /* O - Zero on success, non-zero on failure */
611 lpd_queue(const char *hostname
, /* I - Host to connect to */
612 int port
, /* I - Port to connect on */
613 const char *printer
, /* I - Printer/queue name */
614 int print_fd
, /* I - File to print */
615 int mode
, /* I - Print mode */
616 const char *user
, /* I - Requesting user */
617 const char *title
, /* I - Job title */
618 int copies
, /* I - Number of copies */
619 int banner
, /* I - Print LPD banner? */
620 int format
, /* I - Format specifier */
621 int order
, /* I - Order of data/control files */
622 int reserve
, /* I - Reserve ports? */
623 int manual_copies
, /* I - Do copies by hand... */
624 int timeout
, /* I - Timeout... */
625 int contimeout
) /* I - Connection timeout */
627 char localhost
[255]; /* Local host name */
628 int error
; /* Error number */
629 struct stat filestats
; /* File statistics */
630 int lport
; /* LPD connection local port */
631 int fd
; /* LPD socket */
632 char control
[10240], /* LPD control 'file' */
633 *cptr
; /* Pointer into control file string */
634 char status
; /* Status byte from command */
635 char portname
[255]; /* Port name */
636 int delay
; /* Delay for retries... */
637 char addrname
[256]; /* Address name */
638 http_addrlist_t
*addrlist
, /* Address list */
639 *addr
; /* Socket address */
640 int snmp_fd
, /* SNMP socket */
641 have_supplies
; /* Printer supports supply levels? */
642 int copy
; /* Copies written */
643 time_t start_time
; /* Time of first connect */
644 int recoverable
; /* Recoverable error shown? */
645 size_t nbytes
; /* Number of bytes written */
646 off_t tbytes
; /* Total bytes written */
647 char buffer
[32768]; /* Output buffer */
648 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
649 struct sigaction action
; /* Actions for POSIX signals */
650 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
654 * Setup an alarm handler for timeouts...
657 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
658 sigset(SIGALRM
, lpd_timeout
);
659 #elif defined(HAVE_SIGACTION)
660 memset(&action
, 0, sizeof(action
));
662 sigemptyset(&action
.sa_mask
);
663 action
.sa_handler
= lpd_timeout
;
664 sigaction(SIGALRM
, &action
, NULL
);
666 signal(SIGALRM
, lpd_timeout
);
667 #endif /* HAVE_SIGSET */
670 * Find the printer...
673 sprintf(portname
, "%d", port
);
675 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
677 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
679 return (CUPS_BACKEND_STOP
);
683 * Remember when we started trying to connect to the printer...
687 start_time
= time(NULL
);
690 * Loop forever trying to print the file...
696 * First try to reserve a port for this connection...
699 fputs("STATE: +connecting-to-device\n", stderr
);
700 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n",
701 hostname
, port
, printer
);
702 _cupsLangPuts(stderr
, _("INFO: Connecting to printer...\n"));
704 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
709 * Stop if this job has been cancelled...
714 httpAddrFreeList(addrlist
);
716 return (CUPS_BACKEND_FAILED
);
720 * Choose the next priviledged port...
728 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
734 if (geteuid() || !reserve
)
736 if (getuid() || !reserve
)
737 #endif /* HAVE_GETEUID */
740 * Just create a regular socket...
743 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
745 _cupsLangPrintError(_("ERROR: Unable to create socket"));
756 * We're running as root and want to comply with RFC 1179. Reserve a
757 * priviledged lport between 721 and 731...
760 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
762 _cupsLangPrintError(_("ERROR: Unable to reserve port"));
770 * Connect to the printer or server...
775 httpAddrFreeList(addrlist
);
779 return (CUPS_BACKEND_FAILED
);
782 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
792 if (getenv("CLASS") != NULL
)
795 * If the CLASS environment variable is set, the job was submitted
796 * to a class and not to a specific queue. In this case, we want
797 * to abort immediately so that the job can be requeued on the next
798 * available printer in the class.
801 _cupsLangPuts(stderr
,
802 _("INFO: Unable to contact printer, queuing on next "
803 "printer in class...\n"));
805 httpAddrFreeList(addrlist
);
808 * Sleep 5 seconds to keep the job from requeuing too rapidly...
813 return (CUPS_BACKEND_FAILED
);
816 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
817 error
== EHOSTUNREACH
)
819 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
821 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
822 return (CUPS_BACKEND_FAILED
);
827 _cupsLangPrintf(stderr
,
828 _("WARNING: recoverable: Network host \'%s\' is busy; "
829 "will retry in %d seconds...\n"), hostname
, delay
);
836 else if (error
== EADDRINUSE
)
839 * Try on another port...
848 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
849 _cupsLangPuts(stderr
,
850 _("ERROR: recoverable: Unable to connect to printer; "
851 "will retry in 30 seconds...\n"));
859 * If we've shown a recoverable error make sure the printer proxies
860 * have a chance to see the recovered message. Not pretty but
861 * necessary for now...
864 fputs("INFO: recovered: \n", stderr
);
868 fputs("STATE: -connecting-to-device\n", stderr
);
869 _cupsLangPuts(stderr
, _("INFO: Connected to printer...\n"));
872 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
873 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6) (local port %d)...\n",
874 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
875 ntohs(addr
->addr
.ipv6
.sin6_port
), lport
);
877 #endif /* AF_INET6 */
878 if (addr
->addr
.addr
.sa_family
== AF_INET
)
879 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4) (local port %d)...\n",
880 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
881 ntohs(addr
->addr
.ipv4
.sin_port
), lport
);
884 * See if the printer supports SNMP...
887 if ((snmp_fd
= _cupsSNMPOpen(addr
->addr
.addr
.sa_family
)) >= 0)
888 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
893 * Check for side-channel requests...
896 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
899 * Next, open the print file and figure out its size...
905 * Use the size from the print file...
908 if (fstat(print_fd
, &filestats
))
910 httpAddrFreeList(addrlist
);
913 _cupsLangPrintError(_("ERROR: unable to stat print file"));
914 return (CUPS_BACKEND_FAILED
);
917 filestats
.st_size
*= manual_copies
;
922 * Use a "very large value" for the size so that the printer will
923 * keep printing until we close the connection...
926 #ifdef _LARGEFILE_SOURCE
927 filestats
.st_size
= (size_t)(999999999999.0);
929 filestats
.st_size
= 2147483647;
930 #endif /* _LARGEFILE_SOURCE */
934 * Send a job header to the printer, specifying no banner page and
938 if (lpd_command(fd
, timeout
, "\002%s\n",
939 printer
)) /* Receive print job(s) */
941 httpAddrFreeList(addrlist
);
943 return (CUPS_BACKEND_FAILED
);
946 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
948 snprintf(control
, sizeof(control
),
949 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
950 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
951 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
952 localhost
, user
, title
);
953 cptr
= control
+ strlen(control
);
957 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
958 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
961 cptr
+= strlen(cptr
);
966 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
967 format
, (int)getpid() % 1000, localhost
);
968 cptr
+= strlen(cptr
);
972 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
974 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
975 (int)getpid() % 1000, localhost
, title
);
977 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
979 if (order
== ORDER_CONTROL_DATA
)
982 * Check for side-channel requests...
985 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
988 * Send the control file...
991 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
992 (int)getpid() % 1000, localhost
))
994 httpAddrFreeList(addrlist
);
997 return (CUPS_BACKEND_FAILED
);
1000 _cupsLangPrintf(stderr
, _("INFO: Sending control file (%u bytes)\n"),
1001 (unsigned)strlen(control
));
1003 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1006 _cupsLangPrintError(_("ERROR: Unable to write control file"));
1012 if (read(fd
, &status
, 1) < 1)
1014 _cupsLangPrintf(stderr
,
1015 _("WARNING: Remote host did not respond with control "
1016 "status byte after %d seconds!\n"), timeout
);
1024 _cupsLangPrintf(stderr
,
1025 _("ERROR: Remote host did not accept control file "
1028 _cupsLangPuts(stderr
, _("INFO: Control file sent successfully\n"));
1036 * Check for side-channel requests...
1039 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1042 * Send the print file...
1045 if (lpd_command(fd
, timeout
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1046 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1049 httpAddrFreeList(addrlist
);
1052 return (CUPS_BACKEND_FAILED
);
1055 _cupsLangPrintf(stderr
,
1056 #ifdef HAVE_LONG_LONG
1057 _("INFO: Sending data file (%lld bytes)\n"),
1059 _("INFO: Sending data file (%ld bytes)\n"),
1060 #endif /* HAVE_LONG_LONG */
1061 CUPS_LLCAST filestats
.st_size
);
1064 for (copy
= 0; copy
< manual_copies
; copy
++)
1066 lseek(print_fd
, 0, SEEK_SET
);
1068 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1070 _cupsLangPrintf(stderr
,
1071 _("INFO: Spooling LPR job, %.0f%% complete...\n"),
1072 100.0 * tbytes
/ filestats
.st_size
);
1074 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
1076 _cupsLangPrintError(_("ERROR: Unable to send print file to printer"));
1084 if (mode
== MODE_STANDARD
)
1086 if (tbytes
< filestats
.st_size
)
1088 else if (lpd_write(fd
, "", 1) < 1)
1090 _cupsLangPrintError(_("ERROR: Unable to send trailing nul to printer"));
1096 * Read the status byte from the printer; if we can't read the byte
1097 * back now, we should set status to "errno", however at this point
1098 * we know the printer got the whole file and we don't necessarily
1099 * want to requeue it over and over...
1104 if (recv(fd
, &status
, 1, 0) < 1)
1106 _cupsLangPrintf(stderr
,
1107 _("WARNING: Remote host did not respond with data "
1108 "status byte after %d seconds!\n"), timeout
);
1119 _cupsLangPrintf(stderr
,
1120 _("ERROR: Remote host did not accept data file (%d)\n"),
1123 _cupsLangPuts(stderr
, _("INFO: Data file sent successfully\n"));
1126 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1129 * Check for side-channel requests...
1132 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1135 * Send control file...
1138 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1139 (int)getpid() % 1000, localhost
))
1141 httpAddrFreeList(addrlist
);
1144 return (CUPS_BACKEND_FAILED
);
1147 _cupsLangPrintf(stderr
, _("INFO: Sending control file (%lu bytes)\n"),
1148 (unsigned long)strlen(control
));
1150 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1153 _cupsLangPrintError(_("ERROR: Unable to write control file"));
1159 if (read(fd
, &status
, 1) < 1)
1161 _cupsLangPrintf(stderr
,
1162 _("WARNING: Remote host did not respond with control "
1163 "status byte after %d seconds!\n"), timeout
);
1171 _cupsLangPrintf(stderr
,
1172 _("ERROR: Remote host did not accept control file "
1175 _cupsLangPuts(stderr
, _("INFO: Control file sent successfully\n"));
1179 * Collect the final supply levels as needed...
1183 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1186 * Close the socket connection and input file...
1193 httpAddrFreeList(addrlist
);
1195 return (CUPS_BACKEND_OK
);
1199 * Waiting for a retry...
1205 httpAddrFreeList(addrlist
);
1208 * If we get here, then the job has been cancelled...
1211 return (CUPS_BACKEND_FAILED
);
1216 * 'lpd_timeout()' - Handle timeout alarms...
1220 lpd_timeout(int sig
) /* I - Signal number */
1224 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1225 signal(SIGALRM
, lpd_timeout
);
1226 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1231 * 'lpd_write()' - Write a buffer of data to an LPD server.
1234 static int /* O - Number of bytes written or -1 on error */
1235 lpd_write(int lpd_fd
, /* I - LPD socket */
1236 char *buffer
, /* I - Buffer to write */
1237 int length
) /* I - Number of bytes to write */
1239 int bytes
, /* Number of bytes written */
1240 total
; /* Total number of bytes written */
1247 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1252 if (total
== length
)
1263 #ifndef HAVE_RRESVPORT_AF
1265 * 'rresvport_af()' - A simple implementation of rresvport_af().
1268 static int /* O - Socket or -1 on error */
1269 rresvport_af(int *port
, /* IO - Port number to bind to */
1270 int family
) /* I - Address family */
1272 http_addr_t addr
; /* Socket address */
1273 int fd
; /* Socket file descriptor */
1277 * Try to create an IPv4 socket...
1280 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1284 * Initialize the address buffer...
1287 memset(&addr
, 0, sizeof(addr
));
1288 addr
.addr
.sa_family
= family
;
1291 * Try to bind the socket to a reserved port...
1297 * Set the port number...
1301 if (family
== AF_INET6
)
1302 addr
.ipv6
.sin6_port
= htons(*port
);
1304 # endif /* AF_INET6 */
1305 addr
.ipv4
.sin_port
= htons(*port
);
1308 * Try binding the port to the socket; return if all is OK...
1311 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1315 * Stop if we have any error other than "address already in use"...
1318 if (errno
!= EADDRINUSE
)
1330 * Try the next port...
1337 * Wasn't able to bind to a reserved port, so close the socket and return
1349 #endif /* !HAVE_RRESVPORT_AF */
1353 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1357 sigterm_handler(int sig
) /* I - Signal */
1359 (void)sig
; /* remove compiler warnings... */
1366 * End of "$Id: lpd.c 7740 2008-07-14 23:58:05Z mike $".