]>
Commit | Line | Data |
---|---|---|
43e2fc22 | 1 | /* |
71fe22b7 | 2 | * "$Id: socket.c,v 1.9 2000/01/04 13:45:33 mike Exp $" |
43e2fc22 | 3 | * |
e73c6c0a | 4 | * AppSocket backend for the Common UNIX Printing System (CUPS). |
43e2fc22 | 5 | * |
71fe22b7 | 6 | * Copyright 1997-2000 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 | * | |
decc1f36 | 26 | * main() - Send a file to the printer or server. |
43e2fc22 | 27 | */ |
28 | ||
29 | /* | |
30 | * Include necessary headers. | |
31 | */ | |
32 | ||
e73c6c0a | 33 | #include <cups/cups.h> |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <stdarg.h> | |
37 | #include <cups/string.h> | |
38 | #include <errno.h> | |
39 | #include <sys/types.h> | |
40 | #include <sys/stat.h> | |
41 | ||
42 | #if defined(WIN32) || defined(__EMX__) | |
43 | # include <winsock.h> | |
44 | #else | |
45 | # include <sys/socket.h> | |
46 | # include <netinet/in.h> | |
47 | # include <arpa/inet.h> | |
48 | # include <netdb.h> | |
49 | #endif /* WIN32 || __EMX__ */ | |
50 | ||
51 | ||
52 | /* | |
53 | * 'main()' - Send a file to the printer or server. | |
54 | * | |
55 | * Usage: | |
56 | * | |
57 | * printer-uri job-id user title copies options [file] | |
58 | */ | |
59 | ||
60 | int /* O - Exit status */ | |
61 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
62 | char *argv[]) /* I - Command-line arguments */ | |
63 | { | |
64 | char method[255], /* Method in URI */ | |
65 | hostname[1024], /* Hostname */ | |
66 | username[255], /* Username info (not used) */ | |
67 | resource[1024]; /* Resource info (not used) */ | |
68 | FILE *fp; /* Print file */ | |
3f9cb6c6 | 69 | int copies; /* Number of copies to print */ |
e73c6c0a | 70 | int port; /* Port number */ |
71 | int fd; /* AppSocket */ | |
72 | int error; /* Error code (if any) */ | |
73 | struct sockaddr_in addr; /* Socket address */ | |
74 | struct hostent *hostaddr; /* Host address */ | |
7c88bf41 | 75 | int wbytes; /* Number of bytes written */ |
76 | size_t nbytes, /* Number of bytes read */ | |
e73c6c0a | 77 | tbytes; /* Total number of bytes written */ |
7c88bf41 | 78 | char buffer[8192], /* Output buffer */ |
79 | *bufptr; /* Pointer into buffer */ | |
e73c6c0a | 80 | struct timeval timeout; /* Timeout for select() */ |
81 | fd_set input; /* Input set for select() */ | |
82 | ||
83 | ||
84 | if (argc < 6 || argc > 7) | |
85 | { | |
86 | fprintf(stderr, "Usage: %s job-id user title copies options [file]\n", | |
87 | argv[0]); | |
88 | return (1); | |
89 | } | |
90 | ||
91 | /* | |
92 | * If we have 7 arguments, print the file named on the command-line. | |
93 | * Otherwise, send stdin instead... | |
94 | */ | |
95 | ||
96 | if (argc == 6) | |
3f9cb6c6 | 97 | { |
98 | fp = stdin; | |
99 | copies = 1; | |
100 | } | |
e73c6c0a | 101 | else |
102 | { | |
103 | /* | |
104 | * Try to open the print file... | |
105 | */ | |
106 | ||
107 | if ((fp = fopen(argv[6], "rb")) == NULL) | |
108 | { | |
decc1f36 | 109 | perror("ERROR: unable to open print file"); |
e73c6c0a | 110 | return (1); |
111 | } | |
3f9cb6c6 | 112 | |
113 | copies = atoi(argv[4]); | |
e73c6c0a | 114 | } |
115 | ||
116 | /* | |
117 | * Extract the hostname and port number from the URI... | |
118 | */ | |
119 | ||
120 | httpSeparate(argv[0], method, username, hostname, &port, resource); | |
121 | ||
122 | if (port == 0) | |
123 | port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */ | |
124 | ||
125 | /* | |
126 | * Then try to connect to the remote host... | |
127 | */ | |
128 | ||
129 | if ((hostaddr = gethostbyname(hostname)) == NULL) | |
130 | { | |
131 | fprintf(stderr, "ERROR: Unable to locate printer \'%s\' - %s", | |
132 | hostname, strerror(errno)); | |
133 | return (1); | |
134 | } | |
135 | ||
136 | fprintf(stderr, "INFO: Attempting to connect to host %s on port %d\n", | |
137 | hostname, port); | |
138 | ||
139 | memset(&addr, 0, sizeof(addr)); | |
140 | memcpy(&(addr.sin_addr), hostaddr->h_addr, hostaddr->h_length); | |
141 | addr.sin_family = hostaddr->h_addrtype; | |
142 | addr.sin_port = htons(port); | |
143 | ||
3f9cb6c6 | 144 | while (copies > 0) |
e73c6c0a | 145 | { |
3f9cb6c6 | 146 | for (;;) |
e73c6c0a | 147 | { |
3f9cb6c6 | 148 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
e73c6c0a | 149 | { |
2cc18dd2 | 150 | perror("ERROR: Unable to create socket"); |
3f9cb6c6 | 151 | return (1); |
e73c6c0a | 152 | } |
3f9cb6c6 | 153 | |
154 | if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) | |
e73c6c0a | 155 | { |
3f9cb6c6 | 156 | error = errno; |
157 | close(fd); | |
158 | fd = -1; | |
159 | ||
160 | if (error == ECONNREFUSED) | |
161 | { | |
162 | fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...\n", | |
163 | hostname); | |
164 | sleep(30); | |
165 | } | |
166 | else | |
167 | { | |
168 | perror("ERROR: Unable to connect to printer"); | |
2cc18dd2 | 169 | sleep(30); |
3f9cb6c6 | 170 | } |
e73c6c0a | 171 | } |
3f9cb6c6 | 172 | else |
173 | break; | |
e73c6c0a | 174 | } |
e73c6c0a | 175 | |
e73c6c0a | 176 | /* |
3f9cb6c6 | 177 | * Finally, send the print file... |
e73c6c0a | 178 | */ |
179 | ||
3f9cb6c6 | 180 | copies --; |
7c88bf41 | 181 | |
3f9cb6c6 | 182 | if (fp != stdin) |
e73c6c0a | 183 | { |
3f9cb6c6 | 184 | fputs("PAGE: 1 1\n", stderr); |
185 | rewind(fp); | |
e73c6c0a | 186 | } |
e73c6c0a | 187 | |
3f9cb6c6 | 188 | fputs("INFO: Connected to host, sending print job...\n", stderr); |
e73c6c0a | 189 | |
3f9cb6c6 | 190 | tbytes = 0; |
191 | while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) | |
e73c6c0a | 192 | { |
193 | /* | |
3f9cb6c6 | 194 | * Write the print data to the printer... |
195 | */ | |
196 | ||
197 | tbytes += nbytes; | |
198 | bufptr = buffer; | |
199 | ||
200 | while (nbytes > 0) | |
201 | { | |
202 | if ((wbytes = send(fd, bufptr, nbytes, 0)) < 0) | |
203 | { | |
204 | perror("ERROR: Unable to send print file to printer"); | |
205 | break; | |
206 | } | |
207 | ||
208 | nbytes -= wbytes; | |
209 | bufptr += wbytes; | |
210 | } | |
211 | ||
212 | /* | |
213 | * Check for possible data coming back from the printer... | |
e73c6c0a | 214 | */ |
215 | ||
3f9cb6c6 | 216 | timeout.tv_sec = 0; |
217 | timeout.tv_usec = 0; | |
218 | FD_ZERO(&input); | |
219 | FD_SET(fd, &input); | |
220 | if (select(fd + 1, &input, NULL, NULL, &timeout) > 0) | |
221 | { | |
222 | /* | |
223 | * Grab the data coming back and spit it out to stderr... | |
224 | */ | |
225 | ||
226 | if ((nbytes = recv(fd, buffer, sizeof(buffer), 0)) > 0) | |
227 | fprintf(stderr, "INFO: Received %u bytes of back-channel data!\n", | |
228 | nbytes); | |
229 | } | |
230 | else if (argc > 6) | |
231 | fprintf(stderr, "INFO: Sending print file, %u bytes...\n", tbytes); | |
e73c6c0a | 232 | } |
3f9cb6c6 | 233 | |
234 | /* | |
235 | * Close the socket connection... | |
236 | */ | |
237 | ||
238 | close(fd); | |
e73c6c0a | 239 | } |
240 | ||
241 | /* | |
3f9cb6c6 | 242 | * Close the input file and return... |
e73c6c0a | 243 | */ |
244 | ||
e73c6c0a | 245 | if (fp != stdin) |
246 | fclose(fp); | |
247 | ||
248 | return (0); | |
249 | } | |
43e2fc22 | 250 | |
251 | ||
252 | /* | |
71fe22b7 | 253 | * End of "$Id: socket.c,v 1.9 2000/01/04 13:45:33 mike Exp $". |
43e2fc22 | 254 | */ |