]>
Commit | Line | Data |
---|---|---|
43e2fc22 | 1 | /* |
decc1f36 | 2 | * "$Id: socket.c,v 1.6 1999/04/21 15:02:02 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 | * | |
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 */ | |
69 | int port; /* Port number */ | |
70 | int fd; /* AppSocket */ | |
71 | int error; /* Error code (if any) */ | |
72 | struct sockaddr_in addr; /* Socket address */ | |
73 | struct hostent *hostaddr; /* Host address */ | |
7c88bf41 | 74 | int wbytes; /* Number of bytes written */ |
75 | size_t nbytes, /* Number of bytes read */ | |
e73c6c0a | 76 | tbytes; /* Total number of bytes written */ |
7c88bf41 | 77 | char buffer[8192], /* Output buffer */ |
78 | *bufptr; /* Pointer into buffer */ | |
e73c6c0a | 79 | struct timeval timeout; /* Timeout for select() */ |
80 | fd_set input; /* Input set for select() */ | |
81 | ||
82 | ||
83 | if (argc < 6 || argc > 7) | |
84 | { | |
85 | fprintf(stderr, "Usage: %s job-id user title copies options [file]\n", | |
86 | argv[0]); | |
87 | return (1); | |
88 | } | |
89 | ||
90 | /* | |
91 | * If we have 7 arguments, print the file named on the command-line. | |
92 | * Otherwise, send stdin instead... | |
93 | */ | |
94 | ||
95 | if (argc == 6) | |
96 | fp = stdin; | |
97 | else | |
98 | { | |
99 | /* | |
100 | * Try to open the print file... | |
101 | */ | |
102 | ||
103 | if ((fp = fopen(argv[6], "rb")) == NULL) | |
104 | { | |
decc1f36 | 105 | perror("ERROR: unable to open print file"); |
e73c6c0a | 106 | return (1); |
107 | } | |
108 | } | |
109 | ||
110 | /* | |
111 | * Extract the hostname and port number from the URI... | |
112 | */ | |
113 | ||
114 | httpSeparate(argv[0], method, username, hostname, &port, resource); | |
115 | ||
116 | if (port == 0) | |
117 | port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */ | |
118 | ||
119 | /* | |
120 | * Then try to connect to the remote host... | |
121 | */ | |
122 | ||
123 | if ((hostaddr = gethostbyname(hostname)) == NULL) | |
124 | { | |
125 | fprintf(stderr, "ERROR: Unable to locate printer \'%s\' - %s", | |
126 | hostname, strerror(errno)); | |
127 | return (1); | |
128 | } | |
129 | ||
130 | fprintf(stderr, "INFO: Attempting to connect to host %s on port %d\n", | |
131 | hostname, port); | |
132 | ||
133 | memset(&addr, 0, sizeof(addr)); | |
134 | memcpy(&(addr.sin_addr), hostaddr->h_addr, hostaddr->h_length); | |
135 | addr.sin_family = hostaddr->h_addrtype; | |
136 | addr.sin_port = htons(port); | |
137 | ||
138 | for (;;) | |
139 | { | |
140 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) | |
141 | { | |
decc1f36 | 142 | perror("ERROR: Unable to connect to printer"); |
e73c6c0a | 143 | return (1); |
144 | } | |
145 | ||
146 | if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) | |
147 | { | |
148 | error = errno; | |
149 | close(fd); | |
150 | fd = -1; | |
151 | ||
152 | if (error == ECONNREFUSED) | |
153 | { | |
7c88bf41 | 154 | fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...\n", |
e73c6c0a | 155 | hostname); |
156 | sleep(30); | |
157 | } | |
158 | else | |
159 | { | |
7c88bf41 | 160 | perror("ERROR: Unable to connect to printer"); |
e73c6c0a | 161 | return (1); |
162 | } | |
163 | } | |
164 | else | |
165 | break; | |
166 | } | |
167 | ||
168 | /* | |
169 | * Finally, send the print file... | |
170 | */ | |
171 | ||
7c88bf41 | 172 | fputs("INFO: Connected to host, sending print job...\n", stderr); |
173 | ||
e73c6c0a | 174 | tbytes = 0; |
175 | while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) | |
176 | { | |
177 | /* | |
178 | * Write the print data to the printer... | |
179 | */ | |
180 | ||
7c88bf41 | 181 | tbytes += nbytes; |
182 | bufptr = buffer; | |
183 | ||
184 | while (nbytes > 0) | |
e73c6c0a | 185 | { |
7c88bf41 | 186 | if ((wbytes = send(fd, bufptr, nbytes, 0)) < 0) |
187 | { | |
188 | perror("ERROR: Unable to send print file to printer"); | |
189 | break; | |
190 | } | |
191 | ||
192 | nbytes -= wbytes; | |
193 | bufptr += wbytes; | |
e73c6c0a | 194 | } |
e73c6c0a | 195 | |
196 | /* | |
197 | * Check for possible data coming back from the printer... | |
198 | */ | |
199 | ||
200 | timeout.tv_sec = 0; | |
201 | timeout.tv_usec = 0; | |
202 | FD_ZERO(&input); | |
203 | FD_SET(fd, &input); | |
204 | if (select(fd + 1, &input, NULL, NULL, &timeout) > 0) | |
205 | { | |
206 | /* | |
207 | * Grab the data coming back and spit it out to stderr... | |
208 | */ | |
209 | ||
210 | if ((nbytes = recv(fd, buffer, sizeof(buffer), 0)) > 0) | |
211 | fprintf(stderr, "INFO: Received %u bytes of back-channel data!\n", | |
212 | nbytes); | |
213 | } | |
decc1f36 | 214 | else if (argc > 6) |
e73c6c0a | 215 | fprintf(stderr, "INFO: Sending print file, %u bytes...\n", tbytes); |
216 | } | |
217 | ||
218 | /* | |
219 | * Close the socket connection and input file and return... | |
220 | */ | |
221 | ||
222 | close(fd); | |
223 | if (fp != stdin) | |
224 | fclose(fp); | |
225 | ||
226 | return (0); | |
227 | } | |
43e2fc22 | 228 | |
229 | ||
230 | /* | |
decc1f36 | 231 | * End of "$Id: socket.c,v 1.6 1999/04/21 15:02:02 mike Exp $". |
43e2fc22 | 232 | */ |