]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/socket.c
2 * "$Id: socket.c,v 1.3 1999/03/06 20:26:07 mike Exp $"
4 * AppSocket backend for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-1999 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 * 44145 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
29 * Include necessary headers.
32 #include <cups/cups.h>
36 #include <cups/string.h>
38 #include <sys/types.h>
41 #if defined(WIN32) || defined(__EMX__)
44 # include <sys/socket.h>
45 # include <netinet/in.h>
46 # include <arpa/inet.h>
48 #endif /* WIN32 || __EMX__ */
52 * 'main()' - Send a file to the printer or server.
56 * printer-uri job-id user title copies options [file]
59 int /* O - Exit status */
60 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
61 char *argv
[]) /* I - Command-line arguments */
63 char method
[255], /* Method in URI */
64 hostname
[1024], /* Hostname */
65 username
[255], /* Username info (not used) */
66 resource
[1024]; /* Resource info (not used) */
67 FILE *fp
; /* Print file */
68 int port
; /* Port number */
69 int fd
; /* AppSocket */
70 int error
; /* Error code (if any) */
71 struct sockaddr_in addr
; /* Socket address */
72 struct hostent
*hostaddr
; /* Host address */
73 size_t nbytes
, /* Number of bytes written */
74 tbytes
; /* Total number of bytes written */
75 char buffer
[8192]; /* Output buffer */
76 struct timeval timeout
; /* Timeout for select() */
77 fd_set input
; /* Input set for select() */
80 if (argc
< 6 || argc
> 7)
82 fprintf(stderr
, "Usage: %s job-id user title copies options [file]\n",
88 * If we have 7 arguments, print the file named on the command-line.
89 * Otherwise, send stdin instead...
97 * Try to open the print file...
100 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
102 perror("ERROR: unable to open print file - ");
108 * Extract the hostname and port number from the URI...
111 httpSeparate(argv
[0], method
, username
, hostname
, &port
, resource
);
114 port
= 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
117 * Then try to connect to the remote host...
120 if ((hostaddr
= gethostbyname(hostname
)) == NULL
)
122 fprintf(stderr
, "ERROR: Unable to locate printer \'%s\' - %s",
123 hostname
, strerror(errno
));
127 fprintf(stderr
, "INFO: Attempting to connect to host %s on port %d\n",
130 memset(&addr
, 0, sizeof(addr
));
131 memcpy(&(addr
.sin_addr
), hostaddr
->h_addr
, hostaddr
->h_length
);
132 addr
.sin_family
= hostaddr
->h_addrtype
;
133 addr
.sin_port
= htons(port
);
137 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
139 perror("ERROR: Unable to connect to printer - ");
143 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
149 if (error
== ECONNREFUSED
)
151 fprintf(stderr
, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...",
157 perror("ERROR: Unable to connect to printer - ");
166 * Finally, send the print file...
170 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0)
173 * Write the print data to the printer...
176 if (send(fd
, buffer
, nbytes
, 0) < nbytes
)
178 perror("ERROR: Unable to send print file to printer - ");
185 * Check for possible data coming back from the printer...
192 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
195 * Grab the data coming back and spit it out to stderr...
198 if ((nbytes
= recv(fd
, buffer
, sizeof(buffer
), 0)) > 0)
199 fprintf(stderr
, "INFO: Received %u bytes of back-channel data!\n",
203 fprintf(stderr
, "INFO: Sending print file, %u bytes...\n", tbytes
);
207 * Close the socket connection and input file and return...
219 * End of "$Id: socket.c,v 1.3 1999/03/06 20:26:07 mike Exp $".