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