]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * "$Id: socket.c,v 1.38 2003/08/30 23:12:10 mike Exp $"
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 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-3111 USA
20 * Voice: (301) 373-9603
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.
32 * Include necessary headers.
35 #include <cups/cups.h>
39 #include <cups/string.h>
41 #include <sys/types.h>
50 # include <sys/socket.h>
51 # include <netinet/in.h>
52 # include <arpa/inet.h>
58 * Some OS's don't have hstrerror(), most notably Solaris...
61 #ifndef HAVE_HSTRERROR
62 # define hstrerror cups_hstrerror
64 const char * /* O - Error string */
65 cups_hstrerror(int error
) /* I - Error number */
67 static const char * const errors
[] =
72 "Unrecoverable lookup error.",
73 "No data associated with name."
77 if (error
< 0 || error
> 4)
78 return ("Unknown hostname lookup error.");
80 return (errors
[error
]);
84 * AIX doesn't provide a prototype but does provide the function...
86 extern const char *hstrerror(int);
87 #endif /* !HAVE_HSTRERROR */
94 void print_backchannel(const unsigned char *buffer
, int nbytes
);
98 * 'main()' - Send a file to the printer or server.
102 * printer-uri job-id user title copies options [file]
105 int /* O - Exit status */
106 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
107 char *argv
[]) /* I - Command-line arguments */
109 char method
[255], /* Method in URI */
110 hostname
[1024], /* Hostname */
111 username
[255], /* Username info (not used) */
112 resource
[1024]; /* Resource info (not used) */
113 int fp
; /* Print file */
114 int copies
; /* Number of copies to print */
115 int port
; /* Port number */
116 int delay
; /* Delay for retries... */
117 int fd
; /* AppSocket */
118 int error
; /* Error code (if any) */
119 struct sockaddr_in addr
; /* Socket address */
120 struct hostent
*hostaddr
; /* Host address */
121 int wbytes
; /* Number of bytes written */
122 int nbytes
; /* Number of bytes read */
123 size_t tbytes
; /* Total number of bytes written */
124 char buffer
[8192], /* Output buffer */
125 *bufptr
; /* Pointer into buffer */
126 struct timeval timeout
; /* Timeout for select() */
127 fd_set input
; /* Input set for select() */
128 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
129 struct sigaction action
; /* Actions for POSIX signals */
130 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
134 * Make sure status messages are not buffered...
137 setbuf(stderr
, NULL
);
140 * Ignore SIGPIPE signals...
144 sigset(SIGPIPE
, SIG_IGN
);
145 #elif defined(HAVE_SIGACTION)
146 memset(&action
, 0, sizeof(action
));
147 action
.sa_handler
= SIG_IGN
;
148 sigaction(SIGPIPE
, &action
, NULL
);
150 signal(SIGPIPE
, SIG_IGN
);
151 #endif /* HAVE_SIGSET */
154 * Check command-line...
159 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
162 else if (argc
< 6 || argc
> 7)
164 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
170 * If we have 7 arguments, print the file named on the command-line.
171 * Otherwise, send stdin instead...
182 * Try to open the print file...
185 if ((fp
= open(argv
[6], O_RDONLY
)) < 0)
187 perror("ERROR: unable to open print file");
191 copies
= atoi(argv
[4]);
195 * Extract the hostname and port number from the URI...
198 httpSeparate(argv
[0], method
, username
, hostname
, &port
, resource
);
201 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
204 * Then try to connect to the remote host...
207 if ((hostaddr
= httpGetHostByName(hostname
)) == NULL
)
209 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\' - %s\n",
210 hostname
, hstrerror(h_errno
));
214 fprintf(stderr
, "INFO: Attempting to connect to host %s on port %d\n",
217 memset(&addr
, 0, sizeof(addr
));
218 memcpy(&(addr
.sin_addr
), hostaddr
->h_addr
, hostaddr
->h_length
);
219 addr
.sin_family
= hostaddr
->h_addrtype
;
220 addr
.sin_port
= htons(port
);
226 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
228 perror("ERROR: Unable to create socket");
232 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
238 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
239 error
== EHOSTUNREACH
)
241 fprintf(stderr
, "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
250 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
259 * Now that we are "connected" to the port, ignore SIGTERM so that we
260 * can finish out any page data the driver sends (e.g. to eject the
261 * current page... Only ignore SIGTERM if we are printing data from
262 * stdin (otherwise you can't cancel raw jobs...)
267 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
268 sigset(SIGTERM
, SIG_IGN
);
269 #elif defined(HAVE_SIGACTION)
270 memset(&action
, 0, sizeof(action
));
272 sigemptyset(&action
.sa_mask
);
273 action
.sa_handler
= SIG_IGN
;
274 sigaction(SIGTERM
, &action
, NULL
);
276 signal(SIGTERM
, SIG_IGN
);
277 #endif /* HAVE_SIGSET */
281 * Finally, send the print file...
288 fputs("PAGE: 1 1\n", stderr
);
289 lseek(fp
, 0, SEEK_SET
);
292 fputs("INFO: Connected to host, sending print job...\n", stderr
);
295 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
298 * Write the print data to the printer...
306 if ((wbytes
= send(fd
, bufptr
, nbytes
, 0)) < 0)
308 perror("ERROR: Unable to send print file to printer");
320 * Check for possible data coming back from the printer...
328 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
331 * Grab the data coming back and spit it out to stderr...
334 if ((nbytes
= recv(fd
, buffer
, sizeof(buffer
), 0)) > 0)
336 fprintf(stderr
, "INFO: Received %d bytes of back-channel data!\n",
338 print_backchannel((unsigned char *)buffer
, nbytes
);
342 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
343 (unsigned long)tbytes
);
347 * Shutdown the socket and wait for the other end to finish...
350 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr
);
357 * Wait a maximum of 90 seconds for backchannel data or a closed
368 if (select(fd
+ 1, (int *)&input
, NULL
, NULL
, &timeout
) > 0)
370 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
374 * Grab the data coming back and spit it out to stderr...
377 if ((nbytes
= recv(fd
, buffer
, sizeof(buffer
), 0)) > 0)
379 fprintf(stderr
, "INFO: Received %d bytes of back-channel data!\n",
381 print_backchannel((unsigned char *)buffer
, nbytes
);
391 * Close the socket connection...
398 * Close the input file and return...
404 fputs("INFO: Ready to print.\n", stderr
);
411 * 'print_backchannel()' - Print the contents of a back-channel buffer.
415 print_backchannel(const unsigned char *buffer
, /* I - Data buffer */
416 int nbytes
) /* I - Number of bytes */
418 char line
[255], /* Formatted line */
419 *lineptr
; /* Pointer into line */
422 for (lineptr
= line
; nbytes
> 0; buffer
++, nbytes
--)
424 if (*buffer
< 0x20 || *buffer
>= 0x7f)
426 snprintf(lineptr
, sizeof(line
) - (lineptr
- line
), "<%02X>", *buffer
);
427 lineptr
+= strlen(lineptr
);
430 *lineptr
++ = *buffer
;
432 if ((lineptr
- line
) > 72)
435 fprintf(stderr
, "DEBUG: DATA: %s\n", line
);
443 fprintf(stderr
, "DEBUG: DATA: %s\n", line
);
449 * End of "$Id: socket.c,v 1.38 2003/08/30 23:12:10 mike Exp $".