]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * "$Id: socket.c 181 2006-06-22 20:01:18Z jlovell $"
4 * AppSocket 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.
32 * Include necessary headers.
35 #include <cups/http-private.h>
36 #include "backend-private.h"
38 #include <sys/types.h>
46 # include <sys/socket.h>
47 # include <netinet/in.h>
48 # include <arpa/inet.h>
54 * 'main()' - Send a file to the printer or server.
58 * printer-uri job-id user title copies options [file]
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 */
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 */
92 * Make sure status messages are not buffered...
98 * Ignore SIGPIPE signals...
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
);
108 signal(SIGPIPE
, SIG_IGN
);
109 #endif /* HAVE_SIGSET */
112 * Check command-line...
117 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
118 return (CUPS_BACKEND_OK
);
120 else if (argc
< 6 || argc
> 7)
122 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
124 return (CUPS_BACKEND_FAILED
);
128 * If we have 7 arguments, print the file named on the command-line.
129 * Otherwise, send stdin instead...
140 * Try to open the print file...
143 if ((print_fd
= open(argv
[6], O_RDONLY
)) < 0)
145 perror("ERROR: unable to open print file");
146 return (CUPS_BACKEND_FAILED
);
149 copies
= atoi(argv
[4]);
153 * Extract the hostname and port number from the URI...
156 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
157 method
, sizeof(method
), username
, sizeof(username
),
158 hostname
, sizeof(hostname
), &port
,
159 resource
, sizeof(resource
));
162 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
165 * Get options, if any...
170 if ((options
= strchr(resource
, '?')) != NULL
)
173 * Yup, terminate the device name string and move to the first
174 * character of the options...
189 for (ptr
= name
; *options
&& *options
!= '=';)
190 if (ptr
< (name
+ sizeof(name
) - 1))
202 for (ptr
= value
; *options
&& *options
!= '+' && *options
!= '&';)
203 if (ptr
< (value
+ sizeof(value
) - 1))
207 if (*options
== '+' || *options
== '&')
214 * Process the option...
217 if (!strcasecmp(name
, "waiteof"))
220 * Set the wait-for-eof value...
223 waiteof
= !value
[0] || !strcasecmp(value
, "on") ||
224 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
230 * Then try to connect to the remote host...
233 sprintf(portname
, "%d", port
);
235 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
237 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\'!\n", hostname
);
238 return (CUPS_BACKEND_STOP
);
241 fprintf(stderr
, "INFO: Attempting to connect to host %s on port %d\n",
244 fputs("STATE: +connecting-to-device\n", stderr
);
248 if (!httpAddrConnect(addrlist
, &device_fd
))
253 if (getenv("CLASS") != NULL
)
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.
262 fprintf(stderr
, "INFO: Unable to connect to \"%s\", queuing on next printer in class...\n",
266 * Sleep 5 seconds to keep the job from requeuing too rapidly...
271 return (CUPS_BACKEND_FAILED
);
274 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
275 error
== EHOSTUNREACH
)
278 "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
287 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
295 fputs("STATE: -connecting-to-device\n", stderr
);
298 * Print everything...
303 while (copies
> 0 && tbytes
>= 0)
309 fputs("PAGE: 1 1\n", stderr
);
310 lseek(print_fd
, 0, SEEK_SET
);
313 tbytes
= backendRunLoop(print_fd
, device_fd
, 1);
315 if (print_fd
!= 0 && tbytes
>= 0)
316 fprintf(stderr
, "INFO: Sent print file, " CUPS_LLFMT
" bytes...\n",
323 * Shutdown the socket and wait for the other end to finish...
326 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr
);
328 shutdown(device_fd
, 1);
333 * Wait a maximum of 90 seconds for backchannel data or a closed
341 FD_SET(device_fd
, &input
);
344 if (select(device_fd
+ 1, (int *)&input
, NULL
, NULL
, &timeout
) > 0)
346 if (select(device_fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
350 * Grab the data coming back and spit it out to stderr...
353 if ((bc_bytes
= read(device_fd
, resource
, sizeof(resource
))) > 0)
355 fprintf(stderr
, "DEBUG: Received %d bytes of back-channel data!\n",
357 cupsBackChannelWrite(resource
, bc_bytes
, 1.0);
368 * Close the socket connection...
373 httpAddrFreeList(addrlist
);
376 * Close the input file and return...
383 fputs("INFO: Ready to print.\n", stderr
);
385 return (tbytes
< 0 ? CUPS_BACKEND_FAILED
: CUPS_BACKEND_OK
);
390 * End of "$Id: socket.c 181 2006-06-22 20:01:18Z jlovell $".