]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * "$Id: socket.c,v 1.26 2002/01/27 21:16:09 mike Exp $"
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2002 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
26 * main() - Send a file to the printer or server.
30 * Include necessary headers.
33 #include <cups/cups.h>
37 #include <cups/string.h>
39 #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 FILE *fp
; /* Print file */
70 int copies
; /* Number of copies to print */
71 int port
; /* Port number */
72 int delay
; /* Delay for retries... */
73 int fd
; /* AppSocket */
74 int error
; /* Error code (if any) */
75 struct sockaddr_in addr
; /* Socket address */
76 struct hostent
*hostaddr
; /* Host address */
77 int wbytes
; /* Number of bytes written */
78 size_t nbytes
, /* Number of bytes read */
79 tbytes
; /* Total number of bytes written */
80 char buffer
[8192], /* Output buffer */
81 *bufptr
; /* Pointer into buffer */
82 struct timeval timeout
; /* Timeout for select() */
83 fd_set input
; /* Input set for select() */
84 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
85 struct sigaction action
; /* Actions for POSIX signals */
86 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
90 * Make sure status messages are not buffered...
96 * Check command-line...
101 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
104 else if (argc
< 6 || argc
> 7)
106 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
112 * If we have 7 arguments, print the file named on the command-line.
113 * Otherwise, send stdin instead...
124 * Try to open the print file...
127 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
129 perror("ERROR: unable to open print file");
133 copies
= atoi(argv
[4]);
137 * Extract the hostname and port number from the URI...
140 httpSeparate(argv
[0], method
, username
, hostname
, &port
, resource
);
143 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
146 * Then try to connect to the remote host...
149 if ((hostaddr
= httpGetHostByName(hostname
)) == NULL
)
151 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\' - %s\n",
152 hostname
, strerror(errno
));
156 fprintf(stderr
, "INFO: Attempting to connect to host %s on port %d\n",
159 memset(&addr
, 0, sizeof(addr
));
160 memcpy(&(addr
.sin_addr
), hostaddr
->h_addr
, hostaddr
->h_length
);
161 addr
.sin_family
= hostaddr
->h_addrtype
;
162 addr
.sin_port
= htons(port
);
168 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
170 perror("ERROR: Unable to create socket");
174 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
180 if (error
== ECONNREFUSED
)
182 fprintf(stderr
, "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
191 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
200 * Now that we are "connected" to the port, ignore SIGTERM so that we
201 * can finish out any page data the driver sends (e.g. to eject the
202 * current page... Only ignore SIGTERM if we are printing data from
203 * stdin (otherwise you can't cancel raw jobs...)
208 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
209 sigset(SIGTERM
, SIG_IGN
);
210 #elif defined(HAVE_SIGACTION)
211 memset(&action
, 0, sizeof(action
));
213 sigemptyset(&action
.sa_mask
);
214 action
.sa_handler
= SIG_IGN
;
215 sigaction(SIGTERM
, &action
, NULL
);
217 signal(SIGTERM
, SIG_IGN
);
218 #endif /* HAVE_SIGSET */
222 * Finally, send the print file...
229 fputs("PAGE: 1 1\n", stderr
);
233 fputs("INFO: Connected to host, sending print job...\n", stderr
);
236 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0)
239 * Write the print data to the printer...
247 if ((wbytes
= send(fd
, bufptr
, nbytes
, 0)) < 0)
249 perror("ERROR: Unable to send print file to printer");
258 * Check for possible data coming back from the printer...
267 if (select(fd
+ 1, (int *)&input
, NULL
, NULL
, &timeout
) > 0)
269 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
273 * Grab the data coming back and spit it out to stderr...
276 if ((nbytes
= recv(fd
, buffer
, sizeof(buffer
), 0)) > 0)
277 fprintf(stderr
, "INFO: Received %u bytes of back-channel data!\n",
281 fprintf(stderr
, "INFO: Sending print file, %u bytes...\n", tbytes
);
285 * Shutdown the socket and wait for the other end to finish...
288 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr
);
295 * Wait a maximum of 90 seconds for backchannel data or a closed
306 if (select(fd
+ 1, (int *)&input
, NULL
, NULL
, &timeout
) > 0)
308 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
312 * Grab the data coming back and spit it out to stderr...
315 if ((nbytes
= recv(fd
, buffer
, sizeof(buffer
), 0)) > 0)
316 fprintf(stderr
, "INFO: Received %u bytes of back-channel data!\n",
326 * Close the socket connection...
333 * Close the input file and return...
339 fputs("INFO: Ready to print.\n", stderr
);
346 * End of "$Id: socket.c,v 1.26 2002/01/27 21:16:09 mike Exp $".