]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * "$Id: socket.c,v 1.42 2004/05/13 15:13:42 mike Exp $"
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2004 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-3142 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/cups.h>
39 #include <cups/http-private.h>
40 #include <cups/string.h>
42 #include <sys/types.h>
51 # include <sys/socket.h>
52 # include <netinet/in.h>
53 # include <arpa/inet.h>
62 void print_backchannel(const unsigned char *buffer
, int nbytes
);
66 * 'main()' - Send a file to the printer or server.
70 * printer-uri job-id user title copies options [file]
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 */
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 */
102 * Make sure status messages are not buffered...
105 setbuf(stderr
, NULL
);
108 * Ignore SIGPIPE signals...
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
);
118 signal(SIGPIPE
, SIG_IGN
);
119 #endif /* HAVE_SIGSET */
122 * Check command-line...
127 puts("network socket \"Unknown\" \"AppSocket/HP JetDirect\"");
130 else if (argc
< 6 || argc
> 7)
132 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
138 * If we have 7 arguments, print the file named on the command-line.
139 * Otherwise, send stdin instead...
150 * Try to open the print file...
153 if ((fp
= open(argv
[6], O_RDONLY
)) < 0)
155 perror("ERROR: unable to open print file");
159 copies
= atoi(argv
[4]);
163 * Extract the hostname and port number from the URI...
166 httpSeparate(argv
[0], method
, username
, hostname
, &port
, resource
);
169 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
172 * Then try to connect to the remote host...
175 if ((hostaddr
= httpGetHostByName(hostname
)) == NULL
)
177 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\' - %s\n",
178 hostname
, hstrerror(h_errno
));
182 fprintf(stderr
, "INFO: Attempting to connect to host %s on port %d\n",
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
);
196 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
198 perror("ERROR: Unable to create socket");
202 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
208 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
209 error
== EHOSTUNREACH
)
211 fprintf(stderr
, "INFO: Network host \'%s\' is busy; will retry in %d seconds...\n",
220 perror("ERROR: Unable to connect to printer (retrying in 30 seconds)");
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...)
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
));
242 sigemptyset(&action
.sa_mask
);
243 action
.sa_handler
= SIG_IGN
;
244 sigaction(SIGTERM
, &action
, NULL
);
246 signal(SIGTERM
, SIG_IGN
);
247 #endif /* HAVE_SIGSET */
251 * Finally, send the print file...
258 fputs("PAGE: 1 1\n", stderr
);
259 lseek(fp
, 0, SEEK_SET
);
262 fputs("INFO: Connected to host, sending print job...\n", stderr
);
265 while ((nbytes
= read(fp
, buffer
, sizeof(buffer
))) > 0)
268 * Write the print data to the printer...
276 if ((wbytes
= send(fd
, bufptr
, nbytes
, 0)) < 0)
278 perror("ERROR: Unable to send print file to printer");
290 * Check for possible data coming back from the printer...
298 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
301 * Grab the data coming back and spit it out to stderr...
304 if ((nbytes
= recv(fd
, buffer
, sizeof(buffer
), 0)) > 0)
306 fprintf(stderr
, "INFO: Received %d bytes of back-channel data!\n",
308 print_backchannel((unsigned char *)buffer
, nbytes
);
312 fprintf(stderr
, "INFO: Sending print file, %lu bytes...\n",
313 (unsigned long)tbytes
);
317 * Shutdown the socket and wait for the other end to finish...
320 fputs("INFO: Print file sent, waiting for printer to finish...\n", stderr
);
327 * Wait a maximum of 90 seconds for backchannel data or a closed
338 if (select(fd
+ 1, (int *)&input
, NULL
, NULL
, &timeout
) > 0)
340 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
344 * Grab the data coming back and spit it out to stderr...
347 if ((nbytes
= recv(fd
, buffer
, sizeof(buffer
), 0)) > 0)
349 fprintf(stderr
, "INFO: Received %d bytes of back-channel data!\n",
351 print_backchannel((unsigned char *)buffer
, nbytes
);
361 * Close the socket connection...
368 * Close the input file and return...
379 * 'print_backchannel()' - Print the contents of a back-channel buffer.
383 print_backchannel(const unsigned char *buffer
, /* I - Data buffer */
384 int nbytes
) /* I - Number of bytes */
386 char line
[255], /* Formatted line */
387 *lineptr
; /* Pointer into line */
390 for (lineptr
= line
; nbytes
> 0; buffer
++, nbytes
--)
392 if (*buffer
< 0x20 || *buffer
>= 0x7f)
394 snprintf(lineptr
, sizeof(line
) - (lineptr
- line
), "<%02X>", *buffer
);
395 lineptr
+= strlen(lineptr
);
398 *lineptr
++ = *buffer
;
400 if ((lineptr
- line
) > 72)
403 fprintf(stderr
, "DEBUG: DATA: %s\n", line
);
411 fprintf(stderr
, "DEBUG: DATA: %s\n", line
);
417 * End of "$Id: socket.c,v 1.42 2004/05/13 15:13:42 mike Exp $".