static int /* O - Zero on success, non-zero on failure */ lpd_queue(const char *hostname, /* I - Host to connect to */ http_addrlist_t *addrlist, /* I - List of host addresses */ const char *printer, /* I - Printer/queue name */ int print_fd, /* I - File to print */ int snmp_fd, /* I - SNMP socket */ int mode, /* I - Print mode */ const char *user, /* I - Requesting user */ const char *title, /* I - Job title */ int copies, /* I - Number of copies */ int banner, /* I - Print LPD banner? */ int format, /* I - Format specifier */ int order, /* I - Order of data/control files */ int reserve, /* I - Reserve ports? */ int manual_copies,/* I - Do copies by hand... */ int timeout, /* I - Timeout... */ int contimeout, /* I - Connection timeout */ const char *orighost) /* I - job-originating-host-name */ { char localhost[255]; /* Local host name */ int error; /* Error number */ struct stat filestats; /* File statistics */ int lport; /* LPD connection local port */ int fd; /* LPD socket */ char control[10240], /* LPD control 'file' */ *cptr; /* Pointer into control file string */ char status; /* Status byte from command */ int delay; /* Delay for retries... */ char addrname[256]; /* Address name */ http_addrlist_t *addr; /* Socket address */ int have_supplies; /* Printer supports supply levels? */ int copy; /* Copies written */ time_t start_time; /* Time of first connect */ size_t nbytes; /* Number of bytes written */ off_t tbytes; /* Total bytes written */ char buffer[32768]; /* Output buffer */ #ifdef WIN32 DWORD tv; /* Timeout in milliseconds */ #else struct timeval tv; /* Timeout in secs and usecs */ #endif /* WIN32 */ /* * Remember when we started trying to connect to the printer... */ start_time = time(NULL); /* * Loop forever trying to print the file... */ while (!abort_job) { /* * First try to reserve a port for this connection... */ fprintf(stderr, "DEBUG: Connecting to %s:%d for printer %s\n", hostname, httpAddrPort(&(addrlist->addr)), printer); _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer.")); for (lport = reserve == RESERVE_RFC1179 ? 732 : 1024, addr = addrlist, delay = 5;; addr = addr->next) { /* * Stop if this job has been canceled... */ if (abort_job) return (CUPS_BACKEND_FAILED); /* * Choose the next priviledged port... */ if (!addr) addr = addrlist; lport --; if (lport < 721 && reserve == RESERVE_RFC1179) lport = 731; else if (lport < 1) lport = 1023; #ifdef HAVE_GETEUID if (geteuid() || !reserve) #else if (getuid() || !reserve) #endif /* HAVE_GETEUID */ { /* * Just create a regular socket... */ if ((fd = socket(addr->addr.addr.sa_family, SOCK_STREAM, 0)) < 0) { perror("DEBUG: Unable to create socket"); sleep(1); continue; } lport = 0; } else { /* * We're running as root and want to comply with RFC 1179. Reserve a * priviledged lport between 721 and 731... */ if ((fd = rresvport_af(&lport, addr->addr.addr.sa_family)) < 0) { perror("DEBUG: Unable to reserve port"); sleep(1); continue; } } /* * Connect to the printer or server... */ if (abort_job) { close(fd); return (CUPS_BACKEND_FAILED); } if (!connect(fd, &(addr->addr.addr), httpAddrLength(&(addr->addr)))) break; error = errno; close(fd); if (addr->next) continue; if (getenv("CLASS") != NULL) { /* * If the CLASS environment variable is set, the job was submitted * to a class and not to a specific queue. In this case, we want * to abort immediately so that the job can be requeued on the next * available printer in the class. */ _cupsLangPrintFilter(stderr, "INFO", _("Unable to contact printer, queuing on next " "printer in class.")); /* * Sleep 5 seconds to keep the job from requeuing too rapidly... */ sleep(5); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error)); if (error == ECONNREFUSED || error == EHOSTDOWN || error == EHOSTUNREACH) { if (contimeout && (time(NULL) - start_time) > contimeout) { _cupsLangPrintFilter(stderr, "ERROR", _("The printer is not responding.")); return (CUPS_BACKEND_FAILED); } switch (error) { case EHOSTDOWN : _cupsLangPrintFilter(stderr, "WARNING", _("The printer may not exist or " "is unavailable at this time.")); break; case EHOSTUNREACH : _cupsLangPrintFilter(stderr, "WARNING", _("The printer is unreachable at " "this time.")); break; case ECONNREFUSED : default : _cupsLangPrintFilter(stderr, "WARNING", _("The printer is in use.")); break; } sleep(delay); if (delay < 30) delay += 5; } else if (error == EADDRINUSE) { /* * Try on another port... */ sleep(1); } else { _cupsLangPrintFilter(stderr, "ERROR", _("The printer is not responding.")); sleep(30); } } /* * Set the timeout... */ #ifdef WIN32 tv = (DWORD)(timeout * 1000); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); #else tv.tv_sec = timeout; tv.tv_usec = 0; setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); #endif /* WIN32 */ fputs("STATE: -connecting-to-device\n", stderr); _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer.")); fprintf(stderr, "DEBUG: Connected to %s:%d (local port %d)...\n", httpAddrString(&(addr->addr), addrname, sizeof(addrname)), httpAddrPort(&(addr->addr)), lport); /* * See if the printer supports SNMP... */ if (snmp_fd >= 0) have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr), NULL, NULL); else have_supplies = 0; /* * Check for side-channel requests... */ backendCheckSideChannel(snmp_fd, &(addrlist->addr)); /* * Next, open the print file and figure out its size... */ if (print_fd) { /* * Use the size from the print file... */ if (fstat(print_fd, &filestats)) { close(fd); perror("DEBUG: unable to stat print file"); return (CUPS_BACKEND_FAILED); } filestats.st_size *= manual_copies; } else { /* * Use a "very large value" for the size so that the printer will * keep printing until we close the connection... */ #ifdef _LARGEFILE_SOURCE filestats.st_size = (size_t)(999999999999.0); #else filestats.st_size = 2147483647; #endif /* _LARGEFILE_SOURCE */ } /* * Send a job header to the printer, specifying no banner page and * literal output... */ if (lpd_command(fd, "\002%s\n", printer)) /* Receive print job(s) */ { close(fd); return (CUPS_BACKEND_FAILED); } if (orighost && _cups_strcasecmp(orighost, "localhost")) strlcpy(localhost, orighost, sizeof(localhost)); else httpGetHostname(NULL, localhost, sizeof(localhost)); snprintf(control, sizeof(control), "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */ "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */ "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */ localhost, user, title); cptr = control + strlen(control); if (banner) { snprintf(cptr, sizeof(control) - (cptr - control), "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */ "L%s\n", localhost, user); cptr += strlen(cptr); } while (copies > 0) { snprintf(cptr, sizeof(control) - (cptr - control), "%cdfA%03d%.15s\n", format, (int)getpid() % 1000, localhost); cptr += strlen(cptr); copies --; } snprintf(cptr, sizeof(control) - (cptr - control), "UdfA%03d%.15s\n" "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */ (int)getpid() % 1000, localhost, title); fprintf(stderr, "DEBUG: Control file is:\n%s", control); if (order == ORDER_CONTROL_DATA) { /* * Check for side-channel requests... */ backendCheckSideChannel(snmp_fd, &(addr->addr)); /* * Send the control file... */ if (lpd_command(fd, "\002%d cfA%03.3d%.15s\n", strlen(control), (int)getpid() % 1000, localhost)) { close(fd); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Sending control file (%u bytes)\n", (unsigned)strlen(control)); if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) { status = errno; perror("DEBUG: Unable to write control file"); } else { if (read(fd, &status, 1) < 1) { _cupsLangPrintFilter(stderr, "WARNING", _("The printer did not respond.")); status = errno; } } if (status != 0) _cupsLangPrintFilter(stderr, "ERROR", _("Remote host did not accept control file (%d)."), status); else _cupsLangPrintFilter(stderr, "INFO", _("Control file sent successfully.")); } else status = 0; if (status == 0) { /* * Check for side-channel requests... */ backendCheckSideChannel(snmp_fd, &(addr->addr)); /* * Send the print file... */ if (lpd_command(fd, "\003" CUPS_LLFMT " dfA%03.3d%.15s\n", CUPS_LLCAST filestats.st_size, (int)getpid() % 1000, localhost)) { close(fd); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Sending data file (" CUPS_LLFMT " bytes)\n", CUPS_LLCAST filestats.st_size); tbytes = 0; for (copy = 0; copy < manual_copies; copy ++) { lseek(print_fd, 0, SEEK_SET); while ((nbytes = read(print_fd, buffer, sizeof(buffer))) > 0) { _cupsLangPrintFilter(stderr, "INFO", _("Spooling job, %.0f%% complete."), 100.0 * tbytes / filestats.st_size); if (lpd_write(fd, buffer, nbytes) < nbytes) { perror("DEBUG: Unable to send print file to printer"); break; } else tbytes += nbytes; } } if (mode == MODE_STANDARD) { if (tbytes < filestats.st_size) status = errno; else if (lpd_write(fd, "", 1) < 1) { perror("DEBUG: Unable to send trailing nul to printer"); status = errno; } else { /* * Read the status byte from the printer; if we can't read the byte * back now, we should set status to "errno", however at this point * we know the printer got the whole file and we don't necessarily * want to requeue it over and over... */ if (recv(fd, &status, 1, 0) < 1) { _cupsLangPrintFilter(stderr, "WARNING", _("The printer did not respond.")); status = 0; } } } else status = 0; if (status != 0) _cupsLangPrintFilter(stderr, "ERROR", _("Remote host did not accept data file (%d)."), status); else _cupsLangPrintFilter(stderr, "INFO", _("Data file sent successfully.")); } if (status == 0 && order == ORDER_DATA_CONTROL) { /* * Check for side-channel requests... */ backendCheckSideChannel(snmp_fd, &(addr->addr)); /* * Send control file... */ if (lpd_command(fd, "\002%d cfA%03.3d%.15s\n", strlen(control), (int)getpid() % 1000, localhost)) { close(fd); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Sending control file (%lu bytes)\n", (unsigned long)strlen(control)); if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) { status = errno; perror("DEBUG: Unable to write control file"); } else { if (read(fd, &status, 1) < 1) { _cupsLangPrintFilter(stderr, "WARNING", _("The printer did not respond.")); status = errno; } } if (status != 0) _cupsLangPrintFilter(stderr, "ERROR", _("Remote host did not accept control file (%d)."), status); else _cupsLangPrintFilter(stderr, "INFO", _("Control file sent successfully.")); } /* * Collect the final supply levels as needed... */ if (have_supplies) backendSNMPSupplies(snmp_fd, &(addr->addr), NULL, NULL); /* * Close the socket connection and input file... */ close(fd); if (status == 0) return (CUPS_BACKEND_OK); /* * Waiting for a retry... */ sleep(30); } /* * If we get here, then the job has been canceled... */ return (CUPS_BACKEND_FAILED); }
static int /* O - Zero on success, non-zero on failure */ lpd_queue(const char *hostname, /* I - Host to connect to */ int port, /* I - Port to connect on */ const char *printer, /* I - Printer/queue name */ const char *filename, /* I - File to print */ const char *user, /* I - Requesting user */ const char *title, /* I - Job title */ int copies, /* I - Number of copies */ int banner, /* I - Print LPD banner? */ int format, /* I - Format specifier */ int order, /* I - Order of data/control files */ int reserve, /* I - Reserve ports? */ int manual_copies, /* I - Do copies by hand... */ int timeout) /* I - Timeout... */ { FILE *fp; /* Job file */ char localhost[255]; /* Local host name */ int error; /* Error number */ struct stat filestats; /* File statistics */ int lport; /* LPD connection local port */ int fd; /* LPD socket */ char control[10240], /* LPD control 'file' */ *cptr; /* Pointer into control file string */ char status; /* Status byte from command */ struct sockaddr_in addr; /* Socket address */ struct hostent *hostaddr; /* Host address */ int copy; /* Copies written */ size_t nbytes, /* Number of bytes written */ tbytes; /* Total bytes written */ char buffer[8192]; /* Output buffer */ #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ /* * Setup an alarm handler for timeouts... */ #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ sigset(SIGALRM, lpd_timeout); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); sigemptyset(&action.sa_mask); action.sa_handler = lpd_timeout; sigaction(SIGALRM, &action, NULL); #else signal(SIGALRM, lpd_timeout); #endif /* HAVE_SIGSET */ /* * Loop forever trying to print the file... */ for (;;) /* FOREVER */ { /* * First try to reserve a port for this connection... */ if ((hostaddr = httpGetHostByName(hostname)) == NULL) { fprintf(stderr, "ERROR: Unable to locate printer \'%s\' - %s\n", hostname, hstrerror(h_errno)); return (1); } fprintf(stderr, "INFO: Attempting to connect to host %s for printer %s\n", hostname, printer); memset(&addr, 0, sizeof(addr)); memcpy(&(addr.sin_addr), hostaddr->h_addr, hostaddr->h_length); addr.sin_family = hostaddr->h_addrtype; addr.sin_port = htons(port); for (lport = reserve == RESERVE_RFC1179 ? 732 : 1024;;) { /* * Choose the next priviledged port... */ lport --; if (lport < 721 && reserve == RESERVE_RFC1179) lport = 731; else if (lport < 1) lport = 1023; #ifdef HAVE_GETEUID if (geteuid() || !reserve) #else if (getuid() || !reserve) #endif /* HAVE_GETEUID */ { /* * Just create a regular socket... */ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("ERROR: Unable to create socket"); return (1); } lport = 0; } else { /* * We're running as root and want to comply with RFC 1179. Reserve a * priviledged lport between 721 and 731... */ if ((fd = rresvport(&lport)) < 0) { perror("ERROR: Unable to reserve port"); sleep(1); continue; } } if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { error = errno; close(fd); fd = -1; if (error == ECONNREFUSED || error == EHOSTDOWN || error == EHOSTUNREACH) { fprintf(stderr, "WARNING: Network host \'%s\' is busy, down, or unreachable; will retry in 30 seconds...\n", hostname); sleep(30); } else if (error == EADDRINUSE) { /* * Try on another port... */ sleep(1); } else { perror("ERROR: Unable to connect to printer; will retry in 30 seconds..."); sleep(30); } } else break; } fprintf(stderr, "INFO: Connected to %s...\n", hostname); fprintf(stderr, "DEBUG: Connected on ports %d (local %d)...\n", port, lport); /* * Next, open the print file and figure out its size... */ if (stat(filename, &filestats)) { perror("ERROR: unable to stat print file"); return (1); } filestats.st_size *= manual_copies; if ((fp = fopen(filename, "rb")) == NULL) { perror("ERROR: unable to open print file for reading"); return (1); } /* * Send a job header to the printer, specifying no banner page and * literal output... */ if (lpd_command(fd, timeout, "\002%s\n", printer)) /* Receive print job(s) */ return (1); gethostname(localhost, sizeof(localhost)); localhost[31] = '\0'; /* RFC 1179, Section 7.2 - host name < 32 chars */ snprintf(control, sizeof(control), "H%s\nP%s\nJ%s\n", localhost, user, title); cptr = control + strlen(control); if (banner) { snprintf(cptr, sizeof(control) - (cptr - control), "C%s\nL%s\n", localhost, user); cptr += strlen(cptr); } while (copies > 0) { snprintf(cptr, sizeof(control) - (cptr - control), "%cdfA%03d%.15s\n", format, getpid() % 1000, localhost); cptr += strlen(cptr); copies --; } snprintf(cptr, sizeof(control) - (cptr - control), "UdfA%03d%.15s\nN%s\n", getpid() % 1000, localhost, title); fprintf(stderr, "DEBUG: Control file is:\n%s", control); if (order == ORDER_CONTROL_DATA) { if (lpd_command(fd, timeout, "\002%d cfA%03.3d%.15s\n", strlen(control), getpid() % 1000, localhost)) return (1); fprintf(stderr, "INFO: Sending control file (%lu bytes)\n", (unsigned long)strlen(control)); if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) { status = errno; perror("ERROR: Unable to write control file"); } else { alarm(timeout); if (read(fd, &status, 1) < 1) { fprintf(stderr, "WARNING: Remote host did not respond with control " "status byte after %d seconds!\n", timeout); status = errno; } alarm(0); } if (status != 0) fprintf(stderr, "ERROR: Remote host did not accept control file (%d)\n", status); else fputs("INFO: Control file sent successfully\n", stderr); } else status = 0; if (status == 0) { /* * Send the print file... */ if (lpd_command(fd, timeout, "\003%u dfA%03.3d%.15s\n", (unsigned)filestats.st_size, getpid() % 1000, localhost)) return (1); fprintf(stderr, "INFO: Sending data file (%u bytes)\n", (unsigned)filestats.st_size); tbytes = 0; for (copy = 0; copy < manual_copies; copy ++) { rewind(fp); while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) { fprintf(stderr, "INFO: Spooling LPR job, %u%% complete...\n", (unsigned)(100.0f * tbytes / filestats.st_size)); if (lpd_write(fd, buffer, nbytes) < nbytes) { perror("ERROR: Unable to send print file to printer"); break; } else tbytes += nbytes; } } if (tbytes < filestats.st_size) status = errno; else if (lpd_write(fd, "", 1) < 1) { perror("ERROR: Unable to send trailing nul to printer"); status = errno; } else { /* * Read the status byte from the printer; if we can't read the byte * back now, we should set status to "errno", however at this point * we know the printer got the whole file and we don't necessarily * want to requeue it over and over... */ alarm(timeout); if (recv(fd, &status, 1, 0) < 1) { fprintf(stderr, "WARNING: Remote host did not respond with data " "status byte after %d seconds!\n", timeout); status = 0; } alarm(0); } if (status != 0) fprintf(stderr, "ERROR: Remote host did not accept data file (%d)\n", status); else fputs("INFO: Data file sent successfully\n", stderr); } if (status == 0 && order == ORDER_DATA_CONTROL) { if (lpd_command(fd, timeout, "\002%d cfA%03.3d%.15s\n", strlen(control), getpid() % 1000, localhost)) return (1); fprintf(stderr, "INFO: Sending control file (%lu bytes)\n", (unsigned long)strlen(control)); if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) { status = errno; perror("ERROR: Unable to write control file"); } else { alarm(timeout); if (read(fd, &status, 1) < 1) { fprintf(stderr, "WARNING: Remote host did not respond with control " "status byte after %d seconds!\n", timeout); status = errno; } alarm(0); } if (status != 0) fprintf(stderr, "ERROR: Remote host did not accept control file (%d)\n", status); else fputs("INFO: Control file sent successfully\n", stderr); } /* * Close the socket connection and input file... */ close(fd); fclose(fp); if (status == 0) return (0); /* * Waiting for a retry... */ sleep(30); } }