]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
The IPP backend now supports the following options in the device
[thirdparty/cups.git] / backend / socket.c
1 /*
2 * "$Id: socket.c,v 1.42 2004/05/13 15:13:42 mike Exp $"
3 *
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2004 by Easy Software Products, all rights reserved.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3142 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * main() - Send a file to the printer or server.
29 */
30
31 /*
32 * Include necessary headers.
33 */
34
35 #include <cups/cups.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <cups/http-private.h>
40 #include <cups/string.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <signal.h>
45
46 #ifdef WIN32
47 # include <winsock.h>
48 #else
49 # include <unistd.h>
50 # include <fcntl.h>
51 # include <sys/socket.h>
52 # include <netinet/in.h>
53 # include <arpa/inet.h>
54 # include <netdb.h>
55 #endif /* WIN32 */
56
57
58 /*
59 * Local functions...
60 */
61
62 void print_backchannel(const unsigned char *buffer, int nbytes);
63
64
65 /*
66 * 'main()' - Send a file to the printer or server.
67 *
68 * Usage:
69 *
70 * printer-uri job-id user title copies options [file]
71 */
72
73 int /* O - Exit status */
74 main(int argc, /* I - Number of command-line arguments (6 or 7) */
75 char *argv[]) /* I - Command-line arguments */
76 {
77 char method[255], /* Method in URI */
78 hostname[1024], /* Hostname */
79 username[255], /* Username info (not used) */
80 resource[1024]; /* Resource info (not used) */
81 int fp; /* Print file */
82 int copies; /* Number of copies to print */
83 int port; /* Port number */
84 int delay; /* Delay for retries... */
85 int fd; /* AppSocket */
86 int error; /* Error code (if any) */
87 struct sockaddr_in addr; /* Socket address */
88 struct hostent *hostaddr; /* Host address */
89 int wbytes; /* Number of bytes written */
90 int nbytes; /* Number of bytes read */
91 size_t tbytes; /* Total number of bytes written */
92 char buffer[8192], /* Output buffer */
93 *bufptr; /* Pointer into buffer */
94 struct timeval timeout; /* Timeout for select() */
95 fd_set input; /* Input set for select() */
96 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
97 struct sigaction action; /* Actions for POSIX signals */
98 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
99
100
101 /*
102 * Make sure status messages are not buffered...
103 */
104
105 setbuf(stderr, NULL);
106
107 /*
108 * Ignore SIGPIPE signals...
109 */
110
111 #ifdef HAVE_SIGSET
112 sigset(SIGPIPE, SIG_IGN);
113 #elif defined(HAVE_SIGACTION)
114 memset(&action, 0, sizeof(action));
115 action.sa_handler = SIG_IGN;
116 sigaction(SIGPIPE, &action, NULL);
117 #else
118 signal(SIGPIPE, SIG_IGN);
119 #endif /* HAVE_SIGSET */
120
121 /*
122 * Check command-line...
123 */
124
125 if (argc == 1)
126 {
127 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
128 return (0);
129 }
130 else if (argc < 6 || argc > 7)
131 {
132 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
133 argv[0]);
134 return (1);
135 }
136
137 /*
138 * If we have 7 arguments, print the file named on the command-line.
139 * Otherwise, send stdin instead...
140 */
141
142 if (argc == 6)
143 {
144 fp = 0;
145 copies = 1;
146 }
147 else
148 {
149 /*
150 * Try to open the print file...
151 */
152
153 if ((fp = open(argv[6], O_RDONLY)) < 0)
154 {
155 perror("ERROR: unable to open print file");
156 return (1);
157 }
158
159 copies = atoi(argv[4]);
160 }
161
162 /*
163 * Extract the hostname and port number from the URI...
164 */
165
166 httpSeparate(argv[0], method, username, hostname, &port, resource);
167
168 if (port == 0)
169 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
170
171 /*
172 * Then try to connect to the remote host...
173 */
174
175 if ((hostaddr = httpGetHostByName(hostname)) == NULL)
176 {
177 fprintf(stderr, "ERROR: Unable to locate printer \'%s\' - %s\n",
178 hostname, hstrerror(h_errno));
179 return (1);
180 }
181
182 fprintf(stderr, "INFO: Attempting to connect to host %s on port %d\n",
183 hostname, port);
184
185 memset(&addr, 0, sizeof(addr));
186 memcpy(&(addr.sin_addr), hostaddr->h_addr, hostaddr->h_length);
187 addr.sin_family = hostaddr->h_addrtype;
188 addr.sin_port = htons(port);
189
190 wbytes = 0;
191
192 while (copies > 0)
193 {
194 for (delay = 5;;)
195 {
196 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
197 {
198 perror("ERROR: Unable to create socket");
199 return (1);
200 }
201
202 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
203 {
204 error = errno;
205 close(fd);
206 fd = -1;
207
208 if (error == ECONNREFUSED || error == EHOSTDOWN ||
209 error == EHOSTUNREACH)
210 {
211 fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
212 hostname, delay);
213 sleep(delay);
214
215 if (delay < 30)
216 delay += 5;
217 }
218 else
219 {
220 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
221 sleep(30);
222 }
223 }
224 else
225 break;
226 }
227
228 /*
229 * Now that we are "connected" to the port, ignore SIGTERM so that we
230 * can finish out any page data the driver sends (e.g. to eject the
231 * current page... Only ignore SIGTERM if we are printing data from
232 * stdin (otherwise you can't cancel raw jobs...)
233 */
234
235 if (argc < 7)
236 {
237 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
238 sigset(SIGTERM, SIG_IGN);
239 #elif defined(HAVE_SIGACTION)
240 memset(&action, 0, sizeof(action));
241
242 sigemptyset(&action.sa_mask);
243 action.sa_handler = SIG_IGN;
244 sigaction(SIGTERM, &action, NULL);
245 #else
246 signal(SIGTERM, SIG_IGN);
247 #endif /* HAVE_SIGSET */
248 }
249
250 /*
251 * Finally, send the print file...
252 */
253
254 copies --;
255
256 if (fp != 0)
257 {
258 fputs("PAGE: 1 1\n", stderr);
259 lseek(fp, 0, SEEK_SET);
260 }
261
262 fputs("INFO: Connected to host, sending print job...\n", stderr);
263
264 tbytes = 0;
265 while ((nbytes = read(fp, buffer, sizeof(buffer))) > 0)
266 {
267 /*
268 * Write the print data to the printer...
269 */
270
271 tbytes += nbytes;
272 bufptr = buffer;
273
274 while (nbytes > 0)
275 {
276 if ((wbytes = send(fd, bufptr, nbytes, 0)) < 0)
277 {
278 perror("ERROR: Unable to send print file to printer");
279 break;
280 }
281
282 nbytes -= wbytes;
283 bufptr += wbytes;
284 }
285
286 if (wbytes < 0)
287 break;
288
289 /*
290 * Check for possible data coming back from the printer...
291 */
292
293 timeout.tv_sec = 0;
294 timeout.tv_usec = 0;
295
296 FD_ZERO(&input);
297 FD_SET(fd, &input);
298 if (select(fd + 1, &input, NULL, NULL, &timeout) > 0)
299 {
300 /*
301 * Grab the data coming back and spit it out to stderr...
302 */
303
304 if ((nbytes = recv(fd, buffer, sizeof(buffer), 0)) > 0)
305 {
306 fprintf(stderr, "INFO: Received %d bytes of back-channel data!\n",
307 nbytes);
308 print_backchannel((unsigned char *)buffer, nbytes);
309 }
310 }
311 else if (argc > 6)
312 fprintf(stderr, "INFO: Sending print file, %lu bytes...\n",
313 (unsigned long)tbytes);
314 }
315
316 /*
317 * Shutdown the socket and wait for the other end to finish...
318 */
319
320 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr);
321
322 shutdown(fd, 1);
323
324 for (;;)
325 {
326 /*
327 * Wait a maximum of 90 seconds for backchannel data or a closed
328 * connection...
329 */
330
331 timeout.tv_sec = 90;
332 timeout.tv_usec = 0;
333
334 FD_ZERO(&input);
335 FD_SET(fd, &input);
336
337 #ifdef __hpux
338 if (select(fd + 1, (int *)&input, NULL, NULL, &timeout) > 0)
339 #else
340 if (select(fd + 1, &input, NULL, NULL, &timeout) > 0)
341 #endif /* __hpux */
342 {
343 /*
344 * Grab the data coming back and spit it out to stderr...
345 */
346
347 if ((nbytes = recv(fd, buffer, sizeof(buffer), 0)) > 0)
348 {
349 fprintf(stderr, "INFO: Received %d bytes of back-channel data!\n",
350 nbytes);
351 print_backchannel((unsigned char *)buffer, nbytes);
352 }
353 else
354 break;
355 }
356 else
357 break;
358 }
359
360 /*
361 * Close the socket connection...
362 */
363
364 close(fd);
365 }
366
367 /*
368 * Close the input file and return...
369 */
370
371 if (fp != 0)
372 close(fp);
373
374 return (wbytes < 0);
375 }
376
377
378 /*
379 * 'print_backchannel()' - Print the contents of a back-channel buffer.
380 */
381
382 void
383 print_backchannel(const unsigned char *buffer, /* I - Data buffer */
384 int nbytes) /* I - Number of bytes */
385 {
386 char line[255], /* Formatted line */
387 *lineptr; /* Pointer into line */
388
389
390 for (lineptr = line; nbytes > 0; buffer ++, nbytes --)
391 {
392 if (*buffer < 0x20 || *buffer >= 0x7f)
393 {
394 snprintf(lineptr, sizeof(line) - (lineptr - line), "<%02X>", *buffer);
395 lineptr += strlen(lineptr);
396 }
397 else
398 *lineptr++ = *buffer;
399
400 if ((lineptr - line) > 72)
401 {
402 *lineptr = '\0';
403 fprintf(stderr, "DEBUG: DATA: %s\n", line);
404 lineptr = line;
405 }
406 }
407
408 if (lineptr > line)
409 {
410 *lineptr = '\0';
411 fprintf(stderr, "DEBUG: DATA: %s\n", line);
412 }
413 }
414
415
416 /*
417 * End of "$Id: socket.c,v 1.42 2004/05/13 15:13:42 mike Exp $".
418 */