]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
aff606448f0e8620f95324de6047ef0ce42e3fc8
[thirdparty/cups.git] / backend / socket.c
1 /*
2 * "$Id: socket.c 181 2006-06-22 20:01:18Z jlovell $"
3 *
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 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 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/http-private.h>
36 #include "backend-private.h"
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40
41 #ifdef WIN32
42 # include <winsock.h>
43 #else
44 # include <unistd.h>
45 # include <fcntl.h>
46 # include <sys/socket.h>
47 # include <netinet/in.h>
48 # include <arpa/inet.h>
49 # include <netdb.h>
50 #endif /* WIN32 */
51
52
53 /*
54 * 'main()' - Send a file to the printer or server.
55 *
56 * Usage:
57 *
58 * printer-uri job-id user title copies options [file]
59 */
60
61 int /* O - Exit status */
62 main(int argc, /* I - Number of command-line arguments (6 or 7) */
63 char *argv[]) /* I - Command-line arguments */
64 {
65 char method[255], /* Method in URI */
66 hostname[1024], /* Hostname */
67 username[255], /* Username info (not used) */
68 resource[1024], /* Resource info (not used) */
69 *options, /* Pointer to options */
70 name[255], /* Name of option */
71 value[255], /* Value of option */
72 *ptr; /* Pointer into name or value */
73 int print_fd; /* Print file */
74 int copies; /* Number of copies to print */
75 int waiteof; /* Wait for end-of-file? */
76 int port; /* Port number */
77 char portname[255]; /* Port name */
78 int delay; /* Delay for retries... */
79 int device_fd; /* AppSocket */
80 int error; /* Error code (if any) */
81 http_addrlist_t *addrlist; /* Address list */
82 ssize_t tbytes; /* Total number of bytes written */
83 struct timeval timeout; /* Timeout for select() */
84 fd_set input; /* Input set for select() */
85 ssize_t bc_bytes; /* Number of back-channel bytes read */
86 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
87 struct sigaction action; /* Actions for POSIX signals */
88 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
89
90
91 /*
92 * Make sure status messages are not buffered...
93 */
94
95 setbuf(stderr, NULL);
96
97 /*
98 * Ignore SIGPIPE signals...
99 */
100
101 #ifdef HAVE_SIGSET
102 sigset(SIGPIPE, SIG_IGN);
103 #elif defined(HAVE_SIGACTION)
104 memset(&action, 0, sizeof(action));
105 action.sa_handler = SIG_IGN;
106 sigaction(SIGPIPE, &action, NULL);
107 #else
108 signal(SIGPIPE, SIG_IGN);
109 #endif /* HAVE_SIGSET */
110
111 /*
112 * Check command-line...
113 */
114
115 if (argc == 1)
116 {
117 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
118 return (CUPS_BACKEND_OK);
119 }
120 else if (argc < 6 || argc > 7)
121 {
122 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
123 argv[0]);
124 return (CUPS_BACKEND_FAILED);
125 }
126
127 /*
128 * If we have 7 arguments, print the file named on the command-line.
129 * Otherwise, send stdin instead...
130 */
131
132 if (argc == 6)
133 {
134 print_fd = 0;
135 copies = 1;
136 }
137 else
138 {
139 /*
140 * Try to open the print file...
141 */
142
143 if ((print_fd = open(argv[6], O_RDONLY)) < 0)
144 {
145 perror("ERROR: unable to open print file");
146 return (CUPS_BACKEND_FAILED);
147 }
148
149 copies = atoi(argv[4]);
150 }
151
152 /*
153 * Extract the hostname and port number from the URI...
154 */
155
156 httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
157 method, sizeof(method), username, sizeof(username),
158 hostname, sizeof(hostname), &port,
159 resource, sizeof(resource));
160
161 if (port == 0)
162 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
163
164 /*
165 * Get options, if any...
166 */
167
168 waiteof = 1;
169
170 if ((options = strchr(resource, '?')) != NULL)
171 {
172 /*
173 * Yup, terminate the device name string and move to the first
174 * character of the options...
175 */
176
177 *options++ = '\0';
178
179 /*
180 * Parse options...
181 */
182
183 while (*options)
184 {
185 /*
186 * Get the name...
187 */
188
189 for (ptr = name; *options && *options != '=';)
190 if (ptr < (name + sizeof(name) - 1))
191 *ptr++ = *options++;
192 *ptr = '\0';
193
194 if (*options == '=')
195 {
196 /*
197 * Get the value...
198 */
199
200 options ++;
201
202 for (ptr = value; *options && *options != '+' && *options != '&';)
203 if (ptr < (value + sizeof(value) - 1))
204 *ptr++ = *options++;
205 *ptr = '\0';
206
207 if (*options == '+' || *options == '&')
208 options ++;
209 }
210 else
211 value[0] = '\0';
212
213 /*
214 * Process the option...
215 */
216
217 if (!strcasecmp(name, "waiteof"))
218 {
219 /*
220 * Set the wait-for-eof value...
221 */
222
223 waiteof = !value[0] || !strcasecmp(value, "on") ||
224 !strcasecmp(value, "yes") || !strcasecmp(value, "true");
225 }
226 }
227 }
228
229 /*
230 * Then try to connect to the remote host...
231 */
232
233 sprintf(portname, "%d", port);
234
235 if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
236 {
237 fprintf(stderr, "ERROR: Unable to locate printer \'%s\'!\n", hostname);
238 return (CUPS_BACKEND_STOP);
239 }
240
241 fprintf(stderr, "INFO: Attempting to connect to host %s on port %d\n",
242 hostname, port);
243
244 fputs("STATE: +connecting-to-device\n", stderr);
245
246 for (delay = 5;;)
247 {
248 if (!httpAddrConnect(addrlist, &device_fd))
249 {
250 error = errno;
251 device_fd = -1;
252
253 if (getenv("CLASS") != NULL)
254 {
255 /*
256 * If the CLASS environment variable is set, the job was submitted
257 * to a class and not to a specific queue. In this case, we want
258 * to abort immediately so that the job can be requeued on the next
259 * available printer in the class.
260 */
261
262 fprintf(stderr, "INFO: Unable to connect to \"%s\", queuing on next printer in class...\n",
263 hostname);
264
265 /*
266 * Sleep 5 seconds to keep the job from requeuing too rapidly...
267 */
268
269 sleep(5);
270
271 return (CUPS_BACKEND_FAILED);
272 }
273
274 if (error == ECONNREFUSED || error == EHOSTDOWN ||
275 error == EHOSTUNREACH)
276 {
277 fprintf(stderr,
278 "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
279 hostname, delay);
280 sleep(delay);
281
282 if (delay < 30)
283 delay += 5;
284 }
285 else
286 {
287 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
288 sleep(30);
289 }
290 }
291 else
292 break;
293 }
294
295 fputs("STATE: -connecting-to-device\n", stderr);
296
297 /*
298 * Print everything...
299 */
300
301 tbytes = 0;
302
303 while (copies > 0 && tbytes >= 0)
304 {
305 copies --;
306
307 if (print_fd != 0)
308 {
309 fputs("PAGE: 1 1\n", stderr);
310 lseek(print_fd, 0, SEEK_SET);
311 }
312
313 tbytes = backendRunLoop(print_fd, device_fd, 1);
314
315 if (print_fd != 0 && tbytes >= 0)
316 fprintf(stderr, "INFO: Sent print file, " CUPS_LLFMT " bytes...\n",
317 CUPS_LLCAST tbytes);
318 }
319
320 if (waiteof)
321 {
322 /*
323 * Shutdown the socket and wait for the other end to finish...
324 */
325
326 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr);
327
328 shutdown(device_fd, 1);
329
330 for (;;)
331 {
332 /*
333 * Wait a maximum of 90 seconds for backchannel data or a closed
334 * connection...
335 */
336
337 timeout.tv_sec = 90;
338 timeout.tv_usec = 0;
339
340 FD_ZERO(&input);
341 FD_SET(device_fd, &input);
342
343 #ifdef __hpux
344 if (select(device_fd + 1, (int *)&input, NULL, NULL, &timeout) > 0)
345 #else
346 if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0)
347 #endif /* __hpux */
348 {
349 /*
350 * Grab the data coming back and spit it out to stderr...
351 */
352
353 if ((bc_bytes = read(device_fd, resource, sizeof(resource))) > 0)
354 {
355 fprintf(stderr, "DEBUG: Received %d bytes of back-channel data!\n",
356 (int)bc_bytes);
357 cupsBackChannelWrite(resource, bc_bytes, 1.0);
358 }
359 else
360 break;
361 }
362 else
363 break;
364 }
365 }
366
367 /*
368 * Close the socket connection...
369 */
370
371 close(device_fd);
372
373 httpAddrFreeList(addrlist);
374
375 /*
376 * Close the input file and return...
377 */
378
379 if (print_fd != 0)
380 close(print_fd);
381
382 if (tbytes >= 0)
383 fputs("INFO: Ready to print.\n", stderr);
384
385 return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
386 }
387
388
389 /*
390 * End of "$Id: socket.c 181 2006-06-22 20:01:18Z jlovell $".
391 */