]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/socket.c
Needed to check for "DuplexNoTumble" and not "NoTumble" for duplex option.
[thirdparty/cups.git] / backend / socket.c
CommitLineData
43e2fc22 1/*
58ec2a95 2 * "$Id: socket.c,v 1.4 1999/03/21 02:09:58 mike Exp $"
43e2fc22 3 *
e73c6c0a 4 * AppSocket backend for the Common UNIX Printing System (CUPS).
43e2fc22 5 *
3a193f5e 6 * Copyright 1997-1999 by Easy Software Products, all rights reserved.
43e2fc22 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
58ec2a95 17 * 44141 Airport View Drive, Suite 204
43e2fc22 18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
43e2fc22 26 */
27
28/*
29 * Include necessary headers.
30 */
31
e73c6c0a 32#include <cups/cups.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <stdarg.h>
36#include <cups/string.h>
37#include <errno.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40
41#if defined(WIN32) || defined(__EMX__)
42# include <winsock.h>
43#else
44# include <sys/socket.h>
45# include <netinet/in.h>
46# include <arpa/inet.h>
47# include <netdb.h>
48#endif /* WIN32 || __EMX__ */
49
50
51/*
52 * 'main()' - Send a file to the printer or server.
53 *
54 * Usage:
55 *
56 * printer-uri job-id user title copies options [file]
57 */
58
59int /* O - Exit status */
60main(int argc, /* I - Number of command-line arguments (6 or 7) */
61 char *argv[]) /* I - Command-line arguments */
62{
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() */
78
79
80 if (argc < 6 || argc > 7)
81 {
82 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
83 argv[0]);
84 return (1);
85 }
86
87 /*
88 * If we have 7 arguments, print the file named on the command-line.
89 * Otherwise, send stdin instead...
90 */
91
92 if (argc == 6)
93 fp = stdin;
94 else
95 {
96 /*
97 * Try to open the print file...
98 */
99
100 if ((fp = fopen(argv[6], "rb")) == NULL)
101 {
102 perror("ERROR: unable to open print file - ");
103 return (1);
104 }
105 }
106
107 /*
108 * Extract the hostname and port number from the URI...
109 */
110
111 httpSeparate(argv[0], method, username, hostname, &port, resource);
112
113 if (port == 0)
114 port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */
115
116 /*
117 * Then try to connect to the remote host...
118 */
119
120 if ((hostaddr = gethostbyname(hostname)) == NULL)
121 {
122 fprintf(stderr, "ERROR: Unable to locate printer \'%s\' - %s",
123 hostname, strerror(errno));
124 return (1);
125 }
126
127 fprintf(stderr, "INFO: Attempting to connect to host %s on port %d\n",
128 hostname, port);
129
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);
134
135 for (;;)
136 {
137 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
138 {
139 perror("ERROR: Unable to connect to printer - ");
140 return (1);
141 }
142
143 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
144 {
145 error = errno;
146 close(fd);
147 fd = -1;
148
149 if (error == ECONNREFUSED)
150 {
151 fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...",
152 hostname);
153 sleep(30);
154 }
155 else
156 {
157 perror("ERROR: Unable to connect to printer - ");
158 return (1);
159 }
160 }
161 else
162 break;
163 }
164
165 /*
166 * Finally, send the print file...
167 */
168
169 tbytes = 0;
170 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
171 {
172 /*
173 * Write the print data to the printer...
174 */
175
176 if (send(fd, buffer, nbytes, 0) < nbytes)
177 {
178 perror("ERROR: Unable to send print file to printer - ");
179 break;
180 }
181 else
182 tbytes += nbytes;
183
184 /*
185 * Check for possible data coming back from the printer...
186 */
187
188 timeout.tv_sec = 0;
189 timeout.tv_usec = 0;
190 FD_ZERO(&input);
191 FD_SET(fd, &input);
192 if (select(fd + 1, &input, NULL, NULL, &timeout) > 0)
193 {
194 /*
195 * Grab the data coming back and spit it out to stderr...
196 */
197
198 if ((nbytes = recv(fd, buffer, sizeof(buffer), 0)) > 0)
199 fprintf(stderr, "INFO: Received %u bytes of back-channel data!\n",
200 nbytes);
201 }
202 else
203 fprintf(stderr, "INFO: Sending print file, %u bytes...\n", tbytes);
204 }
205
206 /*
207 * Close the socket connection and input file and return...
208 */
209
210 close(fd);
211 if (fp != stdin)
212 fclose(fp);
213
214 return (0);
215}
43e2fc22 216
217
218/*
58ec2a95 219 * End of "$Id: socket.c,v 1.4 1999/03/21 02:09:58 mike Exp $".
43e2fc22 220 */