author | Mikael Berthe <mikael@lilotux.net> |
Sun, 17 Jul 2005 22:21:51 +0100 | |
changeset 331 | 1510dda4ad71 |
parent 302 | 8ca708a0d550 |
child 400 | e536ab271584 |
permissions | -rw-r--r-- |
25 | 1 |
#include "connwrap.h" |
2 |
||
302
8ca708a0d550
Remove compilation warnings in connwrap library
Mikael Berthe <mikael@lilotux.net>
parents:
235
diff
changeset
|
3 |
#include <stdio.h> |
8ca708a0d550
Remove compilation warnings in connwrap library
Mikael Berthe <mikael@lilotux.net>
parents:
235
diff
changeset
|
4 |
#include <stdlib.h> |
25 | 5 |
#include <netdb.h> |
6 |
#include <string.h> |
|
7 |
#include <netinet/in.h> |
|
8 |
#include <errno.h> |
|
9 |
#include <arpa/inet.h> |
|
10 |
#include <fcntl.h> |
|
11 |
#include <sys/time.h> |
|
112 | 12 |
#include <unistd.h> |
25 | 13 |
|
14 |
#define PROXY_TIMEOUT 10 |
|
15 |
// HTTP proxy timeout in seconds (for the CONNECT method) |
|
16 |
||
17 |
#ifdef HAVE_OPENSSL |
|
18 |
||
19 |
#define OPENSSL_NO_KRB5 1 |
|
20 |
#include <openssl/ssl.h> |
|
21 |
#include <openssl/err.h> |
|
22 |
||
134 | 23 |
#else |
24 |
# ifdef HAVE_GNUTLS |
|
25 |
# include <gnutls/openssl.h> |
|
26 |
# define HAVE_OPENSSL |
|
27 |
# endif |
|
25 | 28 |
#endif |
29 |
||
30 |
static int in_http_connect = 0; |
|
31 |
||
32 |
#ifdef HAVE_OPENSSL |
|
33 |
||
34 |
static SSL_CTX *ctx = 0; |
|
35 |
||
36 |
typedef struct { int fd; SSL *ssl; } sslsock; |
|
37 |
||
38 |
static sslsock *socks = 0; |
|
39 |
static int sockcount = 0; |
|
40 |
||
41 |
static sslsock *getsock(int fd) { |
|
42 |
int i; |
|
43 |
||
44 |
for(i = 0; i < sockcount; i++) |
|
45 |
if(socks[i].fd == fd) |
|
46 |
return &socks[i]; |
|
47 |
||
48 |
return 0; |
|
49 |
} |
|
50 |
||
51 |
static sslsock *addsock(int fd) { |
|
52 |
sslsock *p; |
|
53 |
socks = (sslsock *) realloc(socks, sizeof(sslsock)*++sockcount); |
|
54 |
||
55 |
p = &socks[sockcount-1]; |
|
56 |
||
57 |
if(!ctx) { |
|
58 |
SSL_library_init(); |
|
59 |
SSL_load_error_strings(); |
|
60 |
||
61 |
#ifdef HAVE_SSLEAY |
|
62 |
SSLeay_add_all_algorithms(); |
|
63 |
#else |
|
64 |
OpenSSL_add_all_algorithms(); |
|
65 |
#endif |
|
66 |
||
134 | 67 |
//ctx = SSL_CTX_new(SSLv23_method()); |
68 |
ctx = SSL_CTX_new(SSLv23_client_method()); |
|
25 | 69 |
} |
70 |
||
71 |
p->ssl = SSL_new(ctx); |
|
72 |
SSL_set_fd(p->ssl, p->fd = fd); |
|
73 |
||
74 |
return p; |
|
75 |
} |
|
76 |
||
77 |
static void delsock(int fd) { |
|
78 |
int i, nsockcount; |
|
79 |
sslsock *nsocks; |
|
80 |
||
81 |
nsockcount = 0; |
|
82 |
nsocks = (sslsock *) malloc(sizeof(sslsock)*(sockcount-1)); |
|
83 |
||
84 |
for(i = 0; i < sockcount; i++) { |
|
85 |
if(socks[i].fd != fd) { |
|
86 |
nsocks[nsockcount++] = socks[i]; |
|
87 |
} else { |
|
88 |
SSL_free(socks[i].ssl); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
free(socks); |
|
93 |
||
94 |
socks = nsocks; |
|
95 |
sockcount = nsockcount; |
|
96 |
} |
|
97 |
||
98 |
#endif |
|
99 |
||
100 |
static char *bindaddr = 0, *proxyhost = 0, *proxyuser = 0, *proxypass = 0; |
|
101 |
static int proxyport = 3128; |
|
102 |
static int proxy_ssl = 0; |
|
103 |
||
104 |
#define SOCKOUT(s) write(sockfd, s, strlen(s)) |
|
105 |
||
106 |
int cw_http_connect(int sockfd, const struct sockaddr *serv_addr, int addrlen) { |
|
107 |
int err, pos, fl; |
|
108 |
struct hostent *server; |
|
109 |
struct sockaddr_in paddr; |
|
110 |
char buf[512]; |
|
111 |
fd_set rfds; |
|
112 |
||
113 |
err = 0; |
|
114 |
in_http_connect = 1; |
|
115 |
||
116 |
if(!(server = gethostbyname(proxyhost))) { |
|
117 |
errno = h_errno; |
|
118 |
err = -1; |
|
119 |
} |
|
120 |
||
121 |
if(!err) { |
|
122 |
memset(&paddr, 0, sizeof(paddr)); |
|
123 |
paddr.sin_family = AF_INET; |
|
124 |
memcpy(&paddr.sin_addr.s_addr, *server->h_addr_list, server->h_length); |
|
125 |
paddr.sin_port = htons(proxyport); |
|
126 |
||
127 |
fl = fcntl(sockfd, F_GETFL); |
|
128 |
fcntl(sockfd, F_SETFL, fl & ~O_NONBLOCK); |
|
129 |
||
130 |
buf[0] = 0; |
|
131 |
||
132 |
err = cw_connect(sockfd, (struct sockaddr *) &paddr, sizeof(paddr), proxy_ssl); |
|
133 |
} |
|
134 |
||
135 |
errno = ECONNREFUSED; |
|
136 |
||
137 |
if(!err) { |
|
138 |
struct sockaddr_in *sin = (struct sockaddr_in *) serv_addr; |
|
139 |
char *ip = inet_ntoa(sin->sin_addr), c; |
|
140 |
struct timeval tv; |
|
141 |
||
142 |
sprintf(buf, "%d", ntohs(sin->sin_port)); |
|
143 |
SOCKOUT("CONNECT "); |
|
144 |
SOCKOUT(ip); |
|
145 |
SOCKOUT(":"); |
|
146 |
SOCKOUT(buf); |
|
147 |
SOCKOUT(" HTTP/1.0\r\n"); |
|
148 |
||
149 |
if(proxyuser) { |
|
150 |
char *b; |
|
151 |
SOCKOUT("Proxy-Authorization: Basic "); |
|
152 |
||
153 |
sprintf(buf, "%s:%s", proxyuser, proxypass); |
|
154 |
b = cw_base64_encode(buf); |
|
155 |
SOCKOUT(b); |
|
156 |
free(b); |
|
157 |
||
158 |
SOCKOUT("\r\n"); |
|
159 |
} |
|
160 |
||
161 |
SOCKOUT("\r\n"); |
|
162 |
||
163 |
buf[0] = 0; |
|
164 |
||
165 |
while(err != -1) { |
|
166 |
FD_ZERO(&rfds); |
|
167 |
FD_SET(sockfd, &rfds); |
|
168 |
||
169 |
tv.tv_sec = PROXY_TIMEOUT; |
|
170 |
tv.tv_usec = 0; |
|
171 |
||
172 |
err = select(sockfd+1, &rfds, 0, 0, &tv); |
|
173 |
||
174 |
if(err < 1) err = -1; |
|
175 |
||
176 |
if(err != -1 && FD_ISSET(sockfd, &rfds)) { |
|
177 |
err = read(sockfd, &c, 1); |
|
178 |
if(!err) err = -1; |
|
179 |
||
180 |
if(err != -1) { |
|
181 |
pos = strlen(buf); |
|
182 |
buf[pos] = c; |
|
183 |
buf[pos+1] = 0; |
|
184 |
||
185 |
if(strlen(buf) > 4) |
|
186 |
if(!strcmp(buf+strlen(buf)-4, "\r\n\r\n")) |
|
187 |
break; |
|
188 |
} |
|
189 |
} |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
if(err != -1 && strlen(buf)) { |
|
194 |
char *p = strstr(buf, " "); |
|
195 |
||
196 |
err = -1; |
|
197 |
||
198 |
if(p) |
|
199 |
if(atoi(++p) == 200) |
|
200 |
err = 0; |
|
201 |
||
202 |
fcntl(sockfd, F_SETFL, fl); |
|
203 |
if(fl & O_NONBLOCK) { |
|
204 |
errno = EINPROGRESS; |
|
205 |
err = -1; |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
in_http_connect = 0; |
|
210 |
||
211 |
return err; |
|
212 |
} |
|
213 |
||
214 |
int cw_connect(int sockfd, const struct sockaddr *serv_addr, int addrlen, int ssl) { |
|
215 |
int rc; |
|
216 |
struct sockaddr_in ba; |
|
217 |
||
218 |
if(bindaddr) |
|
219 |
if(strlen(bindaddr)) { |
|
220 |
#ifdef HAVE_INET_ATON |
|
221 |
struct in_addr addr; |
|
222 |
rc = inet_aton(bindaddr, &addr); |
|
223 |
ba.sin_addr.s_addr = addr.s_addr; |
|
224 |
#else |
|
225 |
rc = inet_pton(AF_INET, bindaddr, &ba); |
|
226 |
#endif |
|
227 |
||
228 |
if(rc) { |
|
229 |
ba.sin_port = 0; |
|
230 |
rc = bind(sockfd, (struct sockaddr *) &ba, sizeof(ba)); |
|
231 |
} else { |
|
232 |
rc = -1; |
|
233 |
} |
|
234 |
||
235 |
if(rc) return rc; |
|
236 |
} |
|
237 |
||
238 |
if(proxyhost && !in_http_connect) rc = cw_http_connect(sockfd, serv_addr, addrlen); |
|
239 |
else rc = connect(sockfd, serv_addr, addrlen); |
|
240 |
||
241 |
#ifdef HAVE_OPENSSL |
|
242 |
if(ssl && !rc) { |
|
243 |
sslsock *p = addsock(sockfd); |
|
244 |
if(SSL_connect(p->ssl) != 1) |
|
245 |
return -1; |
|
246 |
} |
|
247 |
#endif |
|
248 |
||
249 |
return rc; |
|
250 |
} |
|
251 |
||
252 |
int cw_nb_connect(int sockfd, const struct sockaddr *serv_addr, int addrlen, int ssl, int *state) { |
|
253 |
int rc = 0; |
|
254 |
struct sockaddr_in ba; |
|
255 |
||
256 |
if(bindaddr) |
|
257 |
if(strlen(bindaddr)) { |
|
258 |
#ifdef HAVE_INET_ATON |
|
259 |
struct in_addr addr; |
|
260 |
rc = inet_aton(bindaddr, &addr); |
|
261 |
ba.sin_addr.s_addr = addr.s_addr; |
|
262 |
#else |
|
263 |
rc = inet_pton(AF_INET, bindaddr, &ba); |
|
264 |
#endif |
|
265 |
||
266 |
if(rc) { |
|
267 |
ba.sin_port = 0; |
|
268 |
rc = bind(sockfd, (struct sockaddr *) &ba, sizeof(ba)); |
|
269 |
} else { |
|
270 |
rc = -1; |
|
271 |
} |
|
272 |
||
273 |
if(rc) return rc; |
|
274 |
} |
|
275 |
||
276 |
#ifdef HAVE_OPENSSL |
|
277 |
if(ssl) { |
|
278 |
if ( !(*state & CW_CONNECT_WANT_SOMETHING)) |
|
279 |
rc = cw_connect(sockfd, serv_addr, addrlen, 0); |
|
280 |
else{ /* check if the socket is connected correctly */ |
|
281 |
int optlen = sizeof(int), optval; |
|
235 | 282 |
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, (socklen_t*)&optlen) || optval) |
25 | 283 |
return -1; |
284 |
} |
|
285 |
||
286 |
if(!rc) { |
|
287 |
sslsock *p; |
|
288 |
if (*state & CW_CONNECT_SSL) |
|
289 |
p = getsock(sockfd); |
|
290 |
else |
|
291 |
p = addsock(sockfd); |
|
292 |
||
293 |
rc = SSL_connect(p->ssl); |
|
294 |
switch(rc){ |
|
295 |
case 1: |
|
296 |
*state = 0; |
|
297 |
return 0; |
|
298 |
case 0: |
|
299 |
return -1; |
|
300 |
default: |
|
301 |
switch (SSL_get_error(p->ssl, rc)){ |
|
302 |
case SSL_ERROR_WANT_READ: |
|
303 |
*state = CW_CONNECT_SSL | CW_CONNECT_WANT_READ; |
|
304 |
return 0; |
|
305 |
case SSL_ERROR_WANT_WRITE: |
|
306 |
*state = CW_CONNECT_SSL | CW_CONNECT_WANT_WRITE; |
|
307 |
return 0; |
|
308 |
default: |
|
309 |
return -1; |
|
310 |
} |
|
311 |
} |
|
312 |
} |
|
313 |
else{ /* catch EINPROGRESS error from the connect call */ |
|
314 |
if (errno == EINPROGRESS){ |
|
315 |
*state = CW_CONNECT_STARTED | CW_CONNECT_WANT_WRITE; |
|
316 |
return 0; |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
return rc; |
|
321 |
} |
|
322 |
#endif |
|
323 |
if ( !(*state & CW_CONNECT_WANT_SOMETHING)) |
|
324 |
rc = connect(sockfd, serv_addr, addrlen); |
|
325 |
else{ /* check if the socket is connected correctly */ |
|
326 |
int optlen = sizeof(int), optval; |
|
235 | 327 |
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, (socklen_t*)&optlen) || optval) |
25 | 328 |
return -1; |
329 |
*state = 0; |
|
330 |
return 0; |
|
331 |
} |
|
332 |
if (rc) |
|
333 |
if (errno == EINPROGRESS){ |
|
334 |
*state = CW_CONNECT_STARTED | CW_CONNECT_WANT_WRITE; |
|
335 |
return 0; |
|
336 |
} |
|
337 |
return rc; |
|
338 |
} |
|
339 |
||
340 |
int cw_accept(int s, struct sockaddr *addr, int *addrlen, int ssl) { |
|
341 |
#ifdef HAVE_OPENSSL |
|
342 |
int rc; |
|
343 |
||
344 |
if(ssl) { |
|
235 | 345 |
rc = accept(s, addr, (socklen_t*)addrlen); |
25 | 346 |
|
347 |
if(!rc) { |
|
348 |
sslsock *p = addsock(s); |
|
349 |
if(SSL_accept(p->ssl) != 1) |
|
350 |
return -1; |
|
351 |
||
352 |
} |
|
353 |
||
354 |
return rc; |
|
355 |
} |
|
356 |
#endif |
|
235 | 357 |
return accept(s, addr, (socklen_t*)addrlen); |
25 | 358 |
} |
359 |
||
360 |
int cw_write(int fd, const void *buf, int count, int ssl) { |
|
361 |
#ifdef HAVE_OPENSSL |
|
362 |
sslsock *p; |
|
363 |
||
364 |
if(ssl) |
|
235 | 365 |
if((p = getsock(fd)) != NULL) |
25 | 366 |
return SSL_write(p->ssl, buf, count); |
367 |
#endif |
|
368 |
return write(fd, buf, count); |
|
369 |
} |
|
370 |
||
371 |
int cw_read(int fd, void *buf, int count, int ssl) { |
|
372 |
#ifdef HAVE_OPENSSL |
|
373 |
sslsock *p; |
|
374 |
||
375 |
if(ssl) |
|
235 | 376 |
if((p = getsock(fd)) != NULL) |
25 | 377 |
return SSL_read(p->ssl, buf, count); |
378 |
#endif |
|
379 |
return read(fd, buf, count); |
|
380 |
} |
|
381 |
||
235 | 382 |
void cw_close(int fd) { |
25 | 383 |
#ifdef HAVE_OPENSSL |
384 |
delsock(fd); |
|
385 |
#endif |
|
386 |
close(fd); |
|
387 |
} |
|
388 |
||
389 |
#define FREEVAR(v) if(v) free(v), v = 0; |
|
390 |
||
391 |
void cw_setbind(const char *abindaddr) { |
|
392 |
FREEVAR(bindaddr); |
|
393 |
bindaddr = strdup(abindaddr); |
|
394 |
} |
|
395 |
||
396 |
void cw_setproxy(const char *aproxyhost, int aproxyport, const char *aproxyuser, const char *aproxypass) { |
|
397 |
FREEVAR(proxyhost); |
|
398 |
FREEVAR(proxyuser); |
|
399 |
FREEVAR(proxypass); |
|
400 |
||
401 |
if(aproxyhost && strlen(aproxyhost)) proxyhost = strdup(aproxyhost); |
|
402 |
if(aproxyuser && strlen(aproxyuser)) proxyuser = strdup(aproxyuser); |
|
403 |
if(aproxypass && strlen(aproxypass)) proxypass = strdup(aproxypass); |
|
404 |
proxyport = aproxyport; |
|
405 |
} |
|
406 |
||
407 |
char *cw_base64_encode(const char *in) { |
|
408 |
static char base64digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._"; |
|
409 |
||
410 |
int j = 0; |
|
411 |
int inlen = strlen(in); |
|
412 |
char *out = (char *) malloc(inlen*4+1), c; |
|
413 |
||
414 |
for(out[0] = 0; inlen >= 3; inlen -= 3) { |
|
415 |
strncat(out, &base64digits[ in[j] >> 2 ], 1); |
|
416 |
strncat(out, &base64digits[ ((in[j] << 4) & 0x30) | (in[j+1] >> 4) ], 1); |
|
417 |
strncat(out, &base64digits[ ((in[j+1] << 2) & 0x3c) | (in[j+2] >> 6) ], 1); |
|
418 |
strncat(out, &base64digits[ in[j+2] & 0x3f ], 1); |
|
419 |
j += 3; |
|
420 |
} |
|
421 |
||
422 |
if(inlen > 0) { |
|
423 |
unsigned char fragment; |
|
424 |
||
425 |
strncat(out, &base64digits[in[j] >> 2], 1); |
|
426 |
fragment = (in[j] << 4) & 0x30; |
|
427 |
||
428 |
if(inlen > 1) |
|
429 |
fragment |= in[j+1] >> 4; |
|
430 |
||
431 |
strncat(out, &base64digits[fragment], 1); |
|
432 |
||
433 |
c = (inlen < 2) ? '-' : base64digits[ (in[j+1] << 2) & 0x3c ]; |
|
434 |
strncat(out, &c, 1); |
|
435 |
c = '-'; |
|
436 |
strncat(out, &c, 1); |
|
437 |
} |
|
438 |
||
439 |
return out; |
|
440 |
} |