2 * "$Id: lpd.c 6061 2006-10-23 00:26:52Z mike $"
4 * Line Printer Daemon backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Send a file to the printer or server.
29 * lpd_command() - Send an LPR command sequence and wait for a reply.
30 * lpd_queue() - Queue a file using the Line Printer Daemon protocol.
31 * lpd_timeout() - Handle timeout alarms...
32 * lpd_write() - Write a buffer of data to an LPD server.
33 * rresvport_af() - A simple implementation of rresvport_af().
34 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
38 * Include necessary headers.
41 #include <cups/backend.h>
42 #include <cups/http-private.h>
43 #include <cups/cups.h>
48 #include <cups/string.h>
50 #include <sys/types.h>
57 # include <sys/socket.h>
58 # include <netinet/in.h>
59 # include <arpa/inet.h>
63 # include <CoreFoundation/CFNumber.h>
64 # include <CoreFoundation/CFPreferences.h>
65 #endif /* __APPLE__ */
72 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
73 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
77 * The order for control and data files in LPD requests...
80 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
81 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
88 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
89 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
90 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
97 static int lpd_command(int lpd_fd
, int timeout
, char *format
, ...);
98 static int lpd_queue(const char *hostname
, int port
, const char *printer
,
100 const char *user
, const char *title
, int copies
,
101 int banner
, int format
, int order
, int reserve
,
102 int manual_copies
, int timeout
, int contimeout
);
103 static void lpd_timeout(int sig
);
104 static int lpd_write(int lpd_fd
, char *buffer
, int length
);
105 #ifndef HAVE_RRESVPORT_AF
106 static int rresvport_af(int *port
, int family
);
107 #endif /* !HAVE_RRESVPORT_AF */
108 static void sigterm_handler(int sig
);
112 * 'main()' - Send a file to the printer or server.
116 * printer-uri job-id user title copies options [file]
119 int /* O - Exit status */
120 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
121 char *argv
[]) /* I - Command-line arguments */
123 char method
[255], /* Method in URI */
124 hostname
[1024], /* Hostname */
125 username
[255], /* Username info */
126 resource
[1024], /* Resource info (printer name) */
127 *options
, /* Pointer to options */
128 name
[255], /* Name of option */
129 value
[255], /* Value of option */
130 *ptr
, /* Pointer into name or value */
131 *filename
, /* File to print */
132 title
[256]; /* Title string */
133 int port
; /* Port number */
134 int status
; /* Status of LPD job */
135 int banner
; /* Print banner page? */
136 int format
; /* Print format */
137 int order
; /* Order of control/data files */
138 int reserve
; /* Reserve priviledged port? */
139 int sanitize_title
; /* Sanitize title string? */
140 int manual_copies
, /* Do manual copies? */
141 timeout
, /* Timeout */
142 contimeout
, /* Connection timeout */
143 copies
; /* Number of copies */
144 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
145 struct sigaction action
; /* Actions for POSIX signals */
146 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
150 * Make sure status messages are not buffered...
153 setbuf(stderr
, NULL
);
156 * Ignore SIGPIPE and catch SIGTERM signals...
160 sigset(SIGPIPE
, SIG_IGN
);
161 sigset(SIGTERM
, sigterm_handler
);
162 #elif defined(HAVE_SIGACTION)
163 memset(&action
, 0, sizeof(action
));
164 action
.sa_handler
= SIG_IGN
;
165 sigaction(SIGPIPE
, &action
, NULL
);
167 sigemptyset(&action
.sa_mask
);
168 sigaddset(&action
.sa_mask
, SIGTERM
);
169 action
.sa_handler
= sigterm_handler
;
170 sigaction(SIGTERM
, &action
, NULL
);
172 signal(SIGPIPE
, SIG_IGN
);
173 signal(SIGTERM
, sigterm_handler
);
174 #endif /* HAVE_SIGSET */
177 * Check command-line...
182 puts("network lpd \"Unknown\" \"LPD/LPR Host or Printer\"");
183 return (CUPS_BACKEND_OK
);
185 else if (argc
< 6 || argc
> 7)
187 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
189 return (CUPS_BACKEND_FAILED
);
193 * If we have 7 arguments, print the file named on the command-line.
194 * Otherwise, copy stdin to a temporary file and print the temporary
201 * Copy stdin to a temporary file...
204 int fd
; /* Temporary file */
205 char buffer
[8192]; /* Buffer for copying */
206 int bytes
; /* Number of bytes read */
209 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
211 perror("ERROR: unable to create temporary file");
212 return (CUPS_BACKEND_FAILED
);
215 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
216 if (write(fd
, buffer
, bytes
) < bytes
)
218 perror("ERROR: unable to write to temporary file");
221 return (CUPS_BACKEND_FAILED
);
225 filename
= tmpfilename
;
231 * Extract the hostname and printer name from the URI...
234 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
235 method
, sizeof(method
), username
, sizeof(username
),
236 hostname
, sizeof(hostname
), &port
,
237 resource
, sizeof(resource
));
242 * If no username is in the device URI, then use the print job user...
245 strlcpy(username
, argv
[2], sizeof(username
));
249 * See if there are any options...
254 order
= ORDER_CONTROL_DATA
;
255 reserve
= RESERVE_ANY
;
258 contimeout
= 7 * 24 * 60 * 60;
261 /* We want to pass utf-8 characters, not re-map them (3071945) */
264 /* Get the default timeout from a system preference... */
266 CFPropertyListRef pvalue
; /* Preference value */
267 SInt32 toval
; /* Timeout value */
270 pvalue
= CFPreferencesCopyValue(CFSTR("timeout"),
271 CFSTR("com.apple.print.backends"),
272 kCFPreferencesAnyUser
,
273 kCFPreferencesCurrentHost
);
276 if (CFGetTypeID(pvalue
) == CFNumberGetTypeID())
278 CFNumberGetValue(pvalue
, kCFNumberSInt32Type
, &toval
);
279 contimeout
= (int)toval
;
287 #endif /* __APPLE__ */
289 if ((options
= strchr(resource
, '?')) != NULL
)
292 * Yup, terminate the device name string and move to the first
293 * character of the options...
308 for (ptr
= name
; *options
&& *options
!= '=';)
309 if (ptr
< (name
+ sizeof(name
) - 1))
321 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
322 if (ptr
< (value
+ sizeof(value
) - 1))
326 if (*options
== '+' || *options
== '&')
333 * Process the option...
336 if (!strcasecmp(name
, "banner"))
342 banner
= !value
[0] || !strcasecmp(value
, "on") ||
343 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
345 else if (!strcasecmp(name
, "format") && value
[0])
348 * Set output format...
351 if (strchr("cdfglnoprtv", value
[0]))
354 fprintf(stderr
, "ERROR: Unknown format character \"%c\"\n", value
[0]);
356 else if (!strcasecmp(name
, "order") && value
[0])
359 * Set control/data order...
362 if (!strcasecmp(value
, "control,data"))
363 order
= ORDER_CONTROL_DATA
;
364 else if (!strcasecmp(value
, "data,control"))
365 order
= ORDER_DATA_CONTROL
;
367 fprintf(stderr
, "ERROR: Unknown file order \"%s\"\n", value
);
369 else if (!strcasecmp(name
, "reserve"))
372 * Set port reservation mode...
375 if (!value
[0] || !strcasecmp(value
, "on") ||
376 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true") ||
377 !strcasecmp(value
, "rfc1179"))
378 reserve
= RESERVE_RFC1179
;
379 else if (!strcasecmp(value
, "any"))
380 reserve
= RESERVE_ANY
;
382 reserve
= RESERVE_NONE
;
384 else if (!strcasecmp(name
, "manual_copies"))
387 * Set manual copies...
390 manual_copies
= !value
[0] || !strcasecmp(value
, "on") ||
391 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
393 else if (!strcasecmp(name
, "sanitize_title"))
396 * Set sanitize title...
399 sanitize_title
= !value
[0] || !strcasecmp(value
, "on") ||
400 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
402 else if (!strcasecmp(name
, "timeout"))
409 timeout
= atoi(value
);
411 else if (!strcasecmp(name
, "contimeout"))
418 contimeout
= atoi(value
);
424 * Sanitize the document title...
427 strlcpy(title
, argv
[3], sizeof(title
));
432 * Sanitize the title string so that we don't cause problems on
436 for (ptr
= title
; *ptr
; ptr
++)
437 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
449 manual_copies
= atoi(argv
[4]);
455 copies
= atoi(argv
[4]);
458 status
= lpd_queue(hostname
, port
, resource
+ 1, filename
,
459 username
, title
, copies
,
460 banner
, format
, order
, reserve
, manual_copies
,
461 timeout
, contimeout
);
464 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
467 status
= lpd_queue(hostname
, port
, resource
+ 1, filename
,
469 banner
, format
, order
, reserve
, 1,
470 timeout
, contimeout
);
473 * Remove the temporary file if necessary...
480 * Return the queue status...
488 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
491 static int /* O - Status of command */
492 lpd_command(int fd
, /* I - Socket connection to LPD host */
493 int timeout
, /* I - Seconds to wait for a response */
494 char *format
, /* I - printf()-style format string */
495 ...) /* I - Additional args as necessary */
497 va_list ap
; /* Argument pointer */
498 char buf
[1024]; /* Output buffer */
499 int bytes
; /* Number of bytes to output */
500 char status
; /* Status from command */
504 * Don't try to send commands if the job has been cancelled...
511 * Format the string...
514 va_start(ap
, format
);
515 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
518 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
521 * Send the command...
524 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
526 if (lpd_write(fd
, buf
, bytes
) < bytes
)
528 perror("ERROR: Unable to send LPD command");
533 * Read back the status from the command and return it...
536 fprintf(stderr
, "DEBUG: Reading command status...\n");
540 if (recv(fd
, &status
, 1, 0) < 1)
542 fprintf(stderr
, "WARNING: Remote host did not respond with command "
543 "status byte after %d seconds!\n", timeout
);
549 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
556 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
559 static int /* O - Zero on success, non-zero on failure */
560 lpd_queue(const char *hostname
, /* I - Host to connect to */
561 int port
, /* I - Port to connect on */
562 const char *printer
, /* I - Printer/queue name */
563 const char *filename
, /* I - File to print */
564 const char *user
, /* I - Requesting user */
565 const char *title
, /* I - Job title */
566 int copies
, /* I - Number of copies */
567 int banner
, /* I - Print LPD banner? */
568 int format
, /* I - Format specifier */
569 int order
, /* I - Order of data/control files */
570 int reserve
, /* I - Reserve ports? */
571 int manual_copies
, /* I - Do copies by hand... */
572 int timeout
, /* I - Timeout... */
573 int contimeout
) /* I - Connection timeout */
575 FILE *fp
; /* Job file */
576 char localhost
[255]; /* Local host name */
577 int error
; /* Error number */
578 struct stat filestats
; /* File statistics */
579 int lport
; /* LPD connection local port */
580 int fd
; /* LPD socket */
581 char control
[10240], /* LPD control 'file' */
582 *cptr
; /* Pointer into control file string */
583 char status
; /* Status byte from command */
584 char portname
[255]; /* Port name */
585 char addrname
[256]; /* Address name */
586 http_addrlist_t
*addrlist
, /* Address list */
587 *addr
; /* Socket address */
588 int copy
; /* Copies written */
589 time_t start_time
; /* Time of first connect */
591 int recoverable
; /* Recoverable error shown? */
592 #endif /* __APPLE__ */
593 size_t nbytes
; /* Number of bytes written */
594 off_t tbytes
; /* Total bytes written */
595 char buffer
[32768]; /* Output buffer */
596 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
597 struct sigaction action
; /* Actions for POSIX signals */
598 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
602 * Setup an alarm handler for timeouts...
605 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
606 sigset(SIGALRM
, lpd_timeout
);
607 #elif defined(HAVE_SIGACTION)
608 memset(&action
, 0, sizeof(action
));
610 sigemptyset(&action
.sa_mask
);
611 action
.sa_handler
= lpd_timeout
;
612 sigaction(SIGALRM
, &action
, NULL
);
614 signal(SIGALRM
, lpd_timeout
);
615 #endif /* HAVE_SIGSET */
618 * Find the printer...
621 sprintf(portname
, "%d", port
);
623 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
625 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\'!\n",
627 return (CUPS_BACKEND_STOP
);
631 * Remember when we starting trying to connect to the printer...
636 #endif /* __APPLE__ */
637 start_time
= time(NULL
);
640 * Loop forever trying to print the file...
646 * First try to reserve a port for this connection...
649 fputs("STATE: +connecting-to-device\n", stderr
);
650 fprintf(stderr
, "INFO: Attempting to connect to host %s for printer %s\n",
653 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
;;
657 * Stop if this job has been cancelled...
662 httpAddrFreeList(addrlist
);
664 return (CUPS_BACKEND_FAILED
);
668 * Choose the next priviledged port...
676 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
682 if (geteuid() || !reserve
)
684 if (getuid() || !reserve
)
685 #endif /* HAVE_GETEUID */
688 * Just create a regular socket...
691 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
693 perror("ERROR: Unable to create socket");
704 * We're running as root and want to comply with RFC 1179. Reserve a
705 * priviledged lport between 721 and 731...
708 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
710 perror("ERROR: Unable to reserve port");
718 * Connect to the printer or server...
723 httpAddrFreeList(addrlist
);
727 return (CUPS_BACKEND_FAILED
);
730 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
740 if (getenv("CLASS") != NULL
)
743 * If the CLASS environment variable is set, the job was submitted
744 * to a class and not to a specific queue. In this case, we want
745 * to abort immediately so that the job can be requeued on the next
746 * available printer in the class.
749 fprintf(stderr
, "INFO: Unable to connect to %s, queuing on next printer in class...\n",
752 httpAddrFreeList(addrlist
);
755 * Sleep 5 seconds to keep the job from requeuing too rapidly...
760 return (CUPS_BACKEND_FAILED
);
763 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
764 error
== EHOSTUNREACH
)
766 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
768 fputs("ERROR: Printer not responding!\n", stderr
);
769 return (CUPS_BACKEND_FAILED
);
774 fprintf(stderr
, "WARNING: recoverable: "
775 "Network host \'%s\' is busy, down, or "
776 "unreachable; will retry in 30 seconds...\n",
779 fprintf(stderr
, "WARNING: "
780 "Network host \'%s\' is busy, down, or "
781 "unreachable; will retry in 30 seconds...\n",
783 #endif /* __APPLE__ */
786 else if (error
== EADDRINUSE
)
789 * Try on another port...
798 perror("ERROR: recoverable: "
799 "Unable to connect to printer; will retry in 30 seconds...");
802 "Unable to connect to printer; will retry in 30 seconds...");
803 #endif /* __APPLE__ */
812 * If we've shown a recoverable error make sure the printer proxies
813 * have a chance to see the recovered message. Not pretty but
814 * necessary for now...
817 fputs("INFO: recovered: \n", stderr
);
820 #endif /* __APPLE__ */
822 fputs("STATE: -connecting-to-device\n", stderr
);
823 fprintf(stderr
, "INFO: Connected to %s...\n", hostname
);
826 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
827 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6) (local port %d)...\n",
828 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
829 ntohs(addr
->addr
.ipv6
.sin6_port
), lport
);
831 #endif /* AF_INET6 */
832 if (addr
->addr
.addr
.sa_family
== AF_INET
)
833 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4) (local port %d)...\n",
834 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
835 ntohs(addr
->addr
.ipv4
.sin_port
), lport
);
838 * Next, open the print file and figure out its size...
841 if (stat(filename
, &filestats
))
843 httpAddrFreeList(addrlist
);
846 perror("ERROR: unable to stat print file");
847 return (CUPS_BACKEND_FAILED
);
850 filestats
.st_size
*= manual_copies
;
852 if ((fp
= fopen(filename
, "rb")) == NULL
)
854 httpAddrFreeList(addrlist
);
857 perror("ERROR: unable to open print file for reading");
858 return (CUPS_BACKEND_FAILED
);
862 * Send a job header to the printer, specifying no banner page and
866 if (lpd_command(fd
, timeout
, "\002%s\n",
867 printer
)) /* Receive print job(s) */
869 httpAddrFreeList(addrlist
);
871 return (CUPS_BACKEND_FAILED
);
874 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
876 snprintf(control
, sizeof(control
),
877 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
878 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
879 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
880 localhost
, user
, title
);
881 cptr
= control
+ strlen(control
);
885 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
886 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
889 cptr
+= strlen(cptr
);
894 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
895 format
, (int)getpid() % 1000, localhost
);
896 cptr
+= strlen(cptr
);
900 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
902 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
903 (int)getpid() % 1000, localhost
, title
);
905 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
907 if (order
== ORDER_CONTROL_DATA
)
909 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
910 (int)getpid() % 1000, localhost
))
912 httpAddrFreeList(addrlist
);
915 return (CUPS_BACKEND_FAILED
);
918 fprintf(stderr
, "INFO: Sending control file (%u bytes)\n",
919 (unsigned)strlen(control
));
921 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
924 perror("ERROR: Unable to write control file");
930 if (read(fd
, &status
, 1) < 1)
932 fprintf(stderr
, "WARNING: Remote host did not respond with control "
933 "status byte after %d seconds!\n", timeout
);
941 fprintf(stderr
, "ERROR: Remote host did not accept control file (%d)\n",
944 fputs("INFO: Control file sent successfully\n", stderr
);
952 * Send the print file...
955 if (lpd_command(fd
, timeout
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
956 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
959 httpAddrFreeList(addrlist
);
962 return (CUPS_BACKEND_FAILED
);
965 fprintf(stderr
, "INFO: Sending data file (" CUPS_LLFMT
" bytes)\n",
966 CUPS_LLCAST filestats
.st_size
);
969 for (copy
= 0; copy
< manual_copies
; copy
++)
973 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0)
975 fprintf(stderr
, "INFO: Spooling LPR job, %.0f%% complete...\n",
976 100.0 * tbytes
/ filestats
.st_size
);
978 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
980 perror("ERROR: Unable to send print file to printer");
988 if (tbytes
< filestats
.st_size
)
990 else if (lpd_write(fd
, "", 1) < 1)
992 perror("ERROR: Unable to send trailing nul to printer");
998 * Read the status byte from the printer; if we can't read the byte
999 * back now, we should set status to "errno", however at this point
1000 * we know the printer got the whole file and we don't necessarily
1001 * want to requeue it over and over...
1006 if (recv(fd
, &status
, 1, 0) < 1)
1008 fprintf(stderr
, "WARNING: Remote host did not respond with data "
1009 "status byte after %d seconds!\n", timeout
);
1017 fprintf(stderr
, "ERROR: Remote host did not accept data file (%d)\n",
1020 fputs("INFO: Data file sent successfully\n", stderr
);
1023 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1025 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1026 (int)getpid() % 1000, localhost
))
1028 httpAddrFreeList(addrlist
);
1031 return (CUPS_BACKEND_FAILED
);
1034 fprintf(stderr
, "INFO: Sending control file (%lu bytes)\n",
1035 (unsigned long)strlen(control
));
1037 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1040 perror("ERROR: Unable to write control file");
1046 if (read(fd
, &status
, 1) < 1)
1048 fprintf(stderr
, "WARNING: Remote host did not respond with control "
1049 "status byte after %d seconds!\n", timeout
);
1057 fprintf(stderr
, "ERROR: Remote host did not accept control file (%d)\n",
1060 fputs("INFO: Control file sent successfully\n", stderr
);
1064 * Close the socket connection and input file...
1072 httpAddrFreeList(addrlist
);
1074 return (CUPS_BACKEND_OK
);
1078 * Waiting for a retry...
1084 httpAddrFreeList(addrlist
);
1087 * If we get here, then the job has been cancelled...
1090 return (CUPS_BACKEND_FAILED
);
1095 * 'lpd_timeout()' - Handle timeout alarms...
1099 lpd_timeout(int sig
) /* I - Signal number */
1103 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1104 signal(SIGALRM
, lpd_timeout
);
1105 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1110 * 'lpd_write()' - Write a buffer of data to an LPD server.
1113 static int /* O - Number of bytes written or -1 on error */
1114 lpd_write(int lpd_fd
, /* I - LPD socket */
1115 char *buffer
, /* I - Buffer to write */
1116 int length
) /* I - Number of bytes to write */
1118 int bytes
, /* Number of bytes written */
1119 total
; /* Total number of bytes written */
1126 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1131 if (total
== length
)
1142 #ifndef HAVE_RRESVPORT_AF
1144 * 'rresvport_af()' - A simple implementation of rresvport_af().
1147 static int /* O - Socket or -1 on error */
1148 rresvport_af(int *port
, /* IO - Port number to bind to */
1149 int family
) /* I - Address family */
1151 http_addr_t addr
; /* Socket address */
1152 int fd
; /* Socket file descriptor */
1156 * Try to create an IPv4 socket...
1159 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1163 * Initialize the address buffer...
1166 memset(&addr
, 0, sizeof(addr
));
1167 addr
.addr
.sa_family
= family
;
1170 * Try to bind the socket to a reserved port...
1176 * Set the port number...
1180 if (family
== AF_INET6
)
1181 addr
.ipv6
.sin6_port
= htons(*port
);
1183 # endif /* AF_INET6 */
1184 addr
.ipv4
.sin_port
= htons(*port
);
1187 * Try binding the port to the socket; return if all is OK...
1190 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1194 * Stop if we have any error other than "address already in use"...
1197 if (errno
!= EADDRINUSE
)
1209 * Try the next port...
1216 * Wasn't able to bind to a reserved port, so close the socket and return
1228 #endif /* !HAVE_RRESVPORT_AF */
1232 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1236 sigterm_handler(int sig
) /* I - Signal */
1238 (void)sig
; /* remove compiler warnings... */
1245 * End of "$Id: lpd.c 6061 2006-10-23 00:26:52Z mike $".