annotate mcabber/connwrap/connwrap.c @ 1266:3bd496b9a9f7

Fix proxy usage when SSL is disabled Thanks to "Dr. Guildo" for the report.
author Mikael Berthe <mikael@lilotux.net>
date Sun, 12 Aug 2007 16:19:52 +0200
parents eb38963e082f
children 3067c096cfc4
rev   line source
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
1 #include "connwrap.h"
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
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
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
5 #include <netdb.h>
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
6 #include <string.h>
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
7 #include <netinet/in.h>
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
8 #include <errno.h>
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
9 #include <arpa/inet.h>
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
10 #include <fcntl.h>
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
11 #include <sys/time.h>
112
edb5591e2e64 [/trunk] Changeset 126 by mikael
mikael
parents: 25
diff changeset
12 #include <unistd.h>
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
13
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
14 #define PROXY_TIMEOUT 10
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
15 // HTTP proxy timeout in seconds (for the CONNECT method)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
16
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
17 #ifdef HAVE_OPENSSL
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
18 # define OPENSSL_NO_KRB5 1
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
19 # include <openssl/ssl.h>
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
20 # include <openssl/err.h>
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
21 # define HAVE_SSL
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
22 # undef HAVE_GNUTLS // Can't use both...
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
23 #elif defined HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
24 # include <gnutls/gnutls.h>
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
25 # define HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
26 #endif
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
27
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
28 static int in_http_connect = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
29
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
30 #ifdef HAVE_OPENSSL
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
31 static SSL_CTX *ctx = NULL;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
32 typedef struct { int fd; SSL *ssl; } sslsock;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
33 #elif defined HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
34 typedef struct { int fd; gnutls_session_t session; } sslsock;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
35 #endif
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
36
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
37
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
38 #ifdef HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
39
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
40 /* verify > 0 indicates verify depth as well */
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
41 static int verify = -1;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
42 static const char *cafile = NULL;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
43 static const char *capath = NULL;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
44 static const char *cipherlist = NULL;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
45 static const char *peer = NULL;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
46 static const char *sslerror = NULL;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
47
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
48 #ifdef HAVE_OPENSSL
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
49 static int verify_cb(int preverify_ok, X509_STORE_CTX *cx)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
50 {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
51 X509 *cert;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
52 X509_NAME *nm;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
53 int lastpos;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
54
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
55 if(!preverify_ok) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
56 long err = X509_STORE_CTX_get_error(cx);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
57
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
58 sslerror = X509_verify_cert_error_string(err);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
59 return 0;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
60 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
61
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
62 if (peer == NULL)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
63 return 1;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
64
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
65 if ((cert = X509_STORE_CTX_get_current_cert(cx)) == NULL) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
66 sslerror = "internal SSL error";
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
67 return 0;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
68 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
69
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
70 /* We only want to look at the peername if we're working on the peer
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
71 * certificate. */
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
72 if (cert != cx->cert)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
73 return 1;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
74
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
75 if ((nm = X509_get_subject_name (cert)) == NULL) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
76 sslerror = "internal SSL error";
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
77 return 0;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
78 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
79
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
80 for(lastpos = -1; ; ) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
81 X509_NAME_ENTRY *e;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
82 ASN1_STRING *a;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
83 ASN1_STRING *p;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
84 int match;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
85
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
86 lastpos = X509_NAME_get_index_by_NID(nm, NID_commonName, lastpos);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
87 if (lastpos == -1)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
88 break;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
89 if ((e = X509_NAME_get_entry(nm, lastpos)) == NULL) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
90 sslerror = "internal SSL error";
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
91 return 0;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
92 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
93 if ((a = X509_NAME_ENTRY_get_data(e)) == NULL) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
94 sslerror = "internal SSL error";
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
95 return 0;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
96 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
97 if ((p = ASN1_STRING_type_new(ASN1_STRING_type(a))) == NULL) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
98 sslerror = "internal SSL error";
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
99 return 0;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
100 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
101 (void) ASN1_STRING_set(p, peer, -1);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
102 match = !ASN1_STRING_cmp(a, p);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
103 ASN1_STRING_free(p);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
104 if(match)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
105 return 1;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
106 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
107
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
108 sslerror = "server certificate cn mismatch";
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
109 return 0;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
110 }
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
111 #endif
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
112
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
113 static void init(int fd, sslsock *p) {
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
114 #ifdef HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
115 gnutls_certificate_credentials_t xcred;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
116 #endif
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
117
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
118 #ifdef HAVE_OPENSSL
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
119 if(ctx)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
120 return;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
121 SSL_library_init();
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
122 SSL_load_error_strings();
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
123
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
124 #ifdef HAVE_SSLEAY
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
125 SSLeay_add_all_algorithms();
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
126 #else
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
127 OpenSSL_add_all_algorithms();
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
128 #endif
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
129
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
130 /* May need to use distinct SSLEAY bindings below... */
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
131
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
132 ctx = SSL_CTX_new(SSLv23_client_method());
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
133 if(cipherlist)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
134 (void)SSL_CTX_set_cipher_list(ctx, cipherlist);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
135 if(cafile || capath)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
136 (void)SSL_CTX_load_verify_locations(ctx, cafile, capath);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
137 if(verify) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
138 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_cb);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
139 if(verify > 0)
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
140 SSL_CTX_set_verify_depth(ctx, verify);
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
141 } else
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
142 SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
143
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
144 p->ssl = SSL_new(ctx);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
145 SSL_set_fd(p->ssl, p->fd = fd);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
146
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
147 #elif defined HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
148 gnutls_global_init();
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
149 gnutls_certificate_allocate_credentials(&xcred);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
150 gnutls_init(&(p->session), GNUTLS_CLIENT);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
151 gnutls_set_default_priority(p->session);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
152 gnutls_credentials_set(p->session, GNUTLS_CRD_CERTIFICATE, xcred);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
153 p->fd = fd;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
154 gnutls_transport_set_ptr(p->session,(gnutls_transport_ptr_t)fd);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
155 #endif
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
156 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
157
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
158 static sslsock *socks = NULL;
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
159 static int sockcount = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
160
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
161 static sslsock *getsock(int fd) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
162 int i;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
163
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
164 for(i = 0; i < sockcount; i++)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
165 if(socks[i].fd == fd)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
166 return &socks[i];
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
167
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
168 return NULL;
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
169 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
170
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
171 static sslsock *addsock(int fd) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
172 sslsock *p;
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
173
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
174 sockcount++;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
175
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
176 if (socks)
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
177 socks = (sslsock *) realloc(socks, sizeof(sslsock)*sockcount);
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
178 else
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
179 socks = (sslsock *) malloc(sizeof(sslsock)*sockcount);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
180
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
181 p = &socks[sockcount-1];
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
182
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
183 init(fd, p);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
184
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
185 sslerror = NULL;
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
186
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
187 return p;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
188 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
189
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
190 static void delsock(int fd) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
191 int i, nsockcount;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
192 sslsock *nsocks;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
193
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
194 nsockcount = 0;
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
195
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
196 if (sockcount > 1) {
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
197 nsocks = (sslsock *) malloc(sizeof(sslsock)*(sockcount-1));
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
198
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
199 for(i = 0; i < sockcount; i++) {
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
200 if(socks[i].fd != fd) {
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
201 nsocks[nsockcount++] = socks[i];
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
202 } else {
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
203 #ifdef HAVE_OPENSSL
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
204 SSL_free(socks[i].ssl);
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
205 #elif defined HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
206 gnutls_bye(socks[i].session, GNUTLS_SHUT_WR);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
207 gnutls_deinit(socks[i].session);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
208 #endif
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
209 }
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
210 }
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
211
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
212 } else {
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
213 #ifdef HAVE_OPENSSL
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
214 if (ctx)
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
215 SSL_CTX_free(ctx);
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
216 ctx = 0;
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
217 #endif
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
218 nsocks = NULL;
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
219 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
220
984
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
221 if (socks)
3225a1ba050d Fix a potential libconnwrap issue
Mikael Berthe <mikael@lilotux.net>
parents: 955
diff changeset
222 free(socks);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
223 socks = nsocks;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
224 sockcount = nsockcount;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
225 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
226
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
227 void cw_set_ssl_options(int sslverify,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
228 const char *sslcafile, const char *sslcapath,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
229 const char *sslciphers, const char *sslpeer) {
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
230 verify = sslverify;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
231 cafile = sslcafile;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
232 capath = sslcapath;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
233 cipherlist = sslciphers;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
234 peer = sslpeer;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
235 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
236
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
237 const char *cw_get_ssl_error(void) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
238 return sslerror;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
239 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
240
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
241 #else // HAVE_SSL
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
242
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
243 void cw_set_ssl_options(int sslverify,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
244 const char *sslcafile, const char *sslcapath,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
245 const char *sslciphers, const char *sslpeer) { }
938
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
246
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
247 const char *cw_get_ssl_error(void) {
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
248 return NULL;
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
249 }
40175f3dcef7 SSL server certificate verification
Jefferson Ogata <ogata@antibozo.net>
parents: 431
diff changeset
250
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
251 #endif // HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
252
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
253 static char *bindaddr = 0, *proxyhost = 0, *proxyuser = 0, *proxypass = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
254 static int proxyport = 3128;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
255 static int proxy_ssl = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
256
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
257 #define SOCKOUT(s) write(sockfd, s, strlen(s))
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
258
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
259 int cw_http_connect(int sockfd, const struct sockaddr *serv_addr, int addrlen) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
260 int err, pos, fl;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
261 struct hostent *server;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
262 struct sockaddr_in paddr;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
263 char buf[512];
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
264 fd_set rfds;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
265
400
e536ab271584 Kill a warning in the connwrap library
Mikael Berthe <mikael@lilotux.net>
parents: 302
diff changeset
266 fl = 0;
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
267 err = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
268 in_http_connect = 1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
269
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
270 if(!(server = gethostbyname(proxyhost))) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
271 errno = h_errno;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
272 err = -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
273 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
274
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
275 if(!err) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
276 memset(&paddr, 0, sizeof(paddr));
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
277 paddr.sin_family = AF_INET;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
278 memcpy(&paddr.sin_addr.s_addr, *server->h_addr_list, server->h_length);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
279 paddr.sin_port = htons(proxyport);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
280
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
281 fl = fcntl(sockfd, F_GETFL);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
282 fcntl(sockfd, F_SETFL, fl & ~O_NONBLOCK);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
283
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
284 buf[0] = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
285
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
286 err = cw_connect(sockfd, (struct sockaddr *) &paddr, sizeof(paddr),
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
287 proxy_ssl);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
288 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
289
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
290 errno = ECONNREFUSED;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
291
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
292 if(!err) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
293 struct sockaddr_in *sin = (struct sockaddr_in *) serv_addr;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
294 char *ip = inet_ntoa(sin->sin_addr), c;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
295 struct timeval tv;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
296
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
297 sprintf(buf, "%d", ntohs(sin->sin_port));
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
298 SOCKOUT("CONNECT ");
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
299 SOCKOUT(ip);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
300 SOCKOUT(":");
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
301 SOCKOUT(buf);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
302 SOCKOUT(" HTTP/1.0\r\n");
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
303
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
304 if(proxyuser) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
305 char *b;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
306 SOCKOUT("Proxy-Authorization: Basic ");
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
307
427
ac85ce87f539 Fix buffer overflow in cw_setproxy()
Mikael Berthe <mikael@lilotux.net>
parents: 414
diff changeset
308 snprintf(buf, sizeof(buf), "%s:%s", proxyuser, proxypass);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
309 b = cw_base64_encode(buf);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
310 SOCKOUT(b);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
311 free(b);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
312
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
313 SOCKOUT("\r\n");
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
314 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
315
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
316 SOCKOUT("\r\n");
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
317
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
318 buf[0] = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
319
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
320 while(err != -1) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
321 FD_ZERO(&rfds);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
322 FD_SET(sockfd, &rfds);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
323
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
324 tv.tv_sec = PROXY_TIMEOUT;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
325 tv.tv_usec = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
326
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
327 err = select(sockfd+1, &rfds, 0, 0, &tv);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
328
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
329 if(err < 1) err = -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
330
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
331 if(err != -1 && FD_ISSET(sockfd, &rfds)) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
332 err = read(sockfd, &c, 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
333 if(!err) err = -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
334
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
335 if(err != -1) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
336 pos = strlen(buf);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
337 buf[pos] = c;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
338 buf[pos+1] = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
339
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
340 if(strlen(buf) > 4)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
341 if(!strcmp(buf+strlen(buf)-4, "\r\n\r\n"))
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
342 break;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
343 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
344 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
345 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
346 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
347
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
348 if(err != -1 && strlen(buf)) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
349 char *p = strstr(buf, " ");
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
350
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
351 err = -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
352
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
353 if(p)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
354 if(atoi(++p) == 200)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
355 err = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
356
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
357 fcntl(sockfd, F_SETFL, fl);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
358 if(fl & O_NONBLOCK) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
359 errno = EINPROGRESS;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
360 err = -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
361 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
362 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
363
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
364 in_http_connect = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
365
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
366 return err;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
367 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
368
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
369 int cw_connect(int sockfd, const struct sockaddr *serv_addr, int addrlen,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
370 int ssl) {
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
371 int rc;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
372 struct sockaddr_in ba;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
373
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
374 if(bindaddr)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
375 if(strlen(bindaddr)) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
376 #ifdef HAVE_INET_ATON
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
377 struct in_addr addr;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
378 rc = inet_aton(bindaddr, &addr);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
379 ba.sin_addr.s_addr = addr.s_addr;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
380 #else
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
381 rc = inet_pton(AF_INET, bindaddr, &ba);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
382 #endif
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
383
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
384 if(rc) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
385 ba.sin_port = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
386 rc = bind(sockfd, (struct sockaddr *) &ba, sizeof(ba));
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
387 } else {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
388 rc = -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
389 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
390
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
391 if(rc) return rc;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
392 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
393
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
394 if(proxyhost && !in_http_connect)
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
395 rc = cw_http_connect(sockfd, serv_addr, addrlen);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
396 else
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
397 rc = connect(sockfd, serv_addr, addrlen);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
398
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
399 #ifdef HAVE_OPENSSL
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
400 if(ssl && !rc) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
401 sslsock *p = addsock(sockfd);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
402 if(SSL_connect(p->ssl) != 1)
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
403 return -1; // XXX "Can't connect to SSL"
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
404 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
405 #endif
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
406
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
407 return rc;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
408 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
409
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
410 int cw_nb_connect(int sockfd, const struct sockaddr *serv_addr, int addrlen,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
411 int ssl, int *state) {
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
412 int rc = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
413 struct sockaddr_in ba;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
414
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
415 if(bindaddr)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
416 if(strlen(bindaddr)) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
417 #ifdef HAVE_INET_ATON
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
418 struct in_addr addr;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
419 rc = inet_aton(bindaddr, &addr);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
420 ba.sin_addr.s_addr = addr.s_addr;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
421 #else
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
422 rc = inet_pton(AF_INET, bindaddr, &ba);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
423 #endif
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
424
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
425 if(rc) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
426 ba.sin_port = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
427 rc = bind(sockfd, (struct sockaddr *) &ba, sizeof(ba));
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
428 } else {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
429 rc = -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
430 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
431
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
432 if(rc) return rc;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
433 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
434
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
435 #ifdef HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
436 if(ssl) {
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
437 if ( !(*state & CW_CONNECT_WANT_SOMETHING)) {
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
438 rc = cw_connect(sockfd, serv_addr, addrlen, 0);
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
439 } else { /* check if the socket is connected correctly */
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
440 int optlen = sizeof(int), optval;
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
441 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
442 (socklen_t*)&optlen) || optval)
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
443 return -1;
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
444 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
445
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
446 if(!rc) {
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
447 #ifdef HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
448 int ret;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
449 #endif
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
450 sslsock *p;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
451 if (*state & CW_CONNECT_SSL)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
452 p = getsock(sockfd);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
453 else
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
454 p = addsock(sockfd);
414
ec86d759ed54 Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 409
diff changeset
455
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
456 #ifdef HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
457 do {
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
458 ret = gnutls_handshake(p->session);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
459 } while ((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED));
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
460 if (ret < 0) {
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
461 gnutls_deinit(p->session);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
462 gnutls_perror(ret);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
463 return -1;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
464 }
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
465 else{
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
466 *state = 1;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
467 return 0;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
468 }
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
469 #elif defined HAVE_OPENSSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
470 rc = SSL_connect(p->ssl);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
471 switch(rc){
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
472 case 1:
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
473 *state = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
474 return 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
475 case 0:
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
476 return -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
477 default:
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
478 switch (SSL_get_error(p->ssl, rc)){
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
479 case SSL_ERROR_WANT_READ:
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
480 *state = CW_CONNECT_SSL | CW_CONNECT_WANT_READ;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
481 return 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
482 case SSL_ERROR_WANT_WRITE:
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
483 *state = CW_CONNECT_SSL | CW_CONNECT_WANT_WRITE;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
484 return 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
485 default:
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
486 return -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
487 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
488 }
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
489 #endif
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
490 } else { /* catch EINPROGRESS error from the connect call */
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
491 if (errno == EINPROGRESS){
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
492 *state = CW_CONNECT_STARTED | CW_CONNECT_WANT_WRITE;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
493 return 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
494 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
495 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
496
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
497 return rc;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
498 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
499 #endif
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
500 if ( !(*state & CW_CONNECT_WANT_SOMETHING)) {
1266
3bd496b9a9f7 Fix proxy usage when SSL is disabled
Mikael Berthe <mikael@lilotux.net>
parents: 1253
diff changeset
501 rc = cw_connect(sockfd, serv_addr, addrlen, 0);
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
502 } else { /* check if the socket is connected correctly */
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
503 int optlen = sizeof(int), optval;
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
504 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
505 (socklen_t*)&optlen) || optval)
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
506 return -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
507 *state = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
508 return 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
509 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
510 if (rc)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
511 if (errno == EINPROGRESS){
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
512 *state = CW_CONNECT_STARTED | CW_CONNECT_WANT_WRITE;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
513 return 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
514 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
515 return rc;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
516 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
517
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
518 int cw_accept(int s, struct sockaddr *addr, int *addrlen, int ssl) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
519 #ifdef HAVE_OPENSSL
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
520 int rc;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
521
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
522 if(ssl) {
235
f7f07794d2df [/trunk] Changeset 248 by mikael
mikael
parents: 134
diff changeset
523 rc = accept(s, addr, (socklen_t*)addrlen);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
524
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
525 if(!rc) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
526 sslsock *p = addsock(s);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
527 if(SSL_accept(p->ssl) != 1)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
528 return -1;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
529 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
530 return rc;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
531 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
532 #endif
235
f7f07794d2df [/trunk] Changeset 248 by mikael
mikael
parents: 134
diff changeset
533 return accept(s, addr, (socklen_t*)addrlen);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
534 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
535
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
536 int cw_write(int fd, const void *buf, int count, int ssl) {
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
537 #ifdef HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
538 sslsock *p;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
539
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
540 if(ssl) {
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
541 #ifdef HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
542 p = getsock(fd);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
543 if(p) {
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
544 int ret;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
545 if((ret = gnutls_record_send( p->session, buf, count) < 0))
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
546 fprintf(stderr, "Can't write to server");
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
547 return ret;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
548 }
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
549 #elif defined HAVE_OPENSSL
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
550 if((p = getsock(fd)) != NULL)
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
551 return SSL_write(p->ssl, buf, count);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
552 #endif
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
553 }
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
554 #endif // HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
555 return write(fd, buf, count);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
556 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
557
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
558 int cw_read(int fd, void *buf, int count, int ssl) {
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
559 #ifdef HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
560 sslsock *p;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
561
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
562 if(ssl) {
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
563 #ifdef HAVE_GNUTLS
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
564 p = getsock(fd);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
565 if(p) {
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
566 int ret;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
567 do {
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
568 ret = gnutls_record_recv(p->session, buf, count);
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
569 } while (ret < 0 &&
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
570 (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN));
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
571 return ret;
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
572 }
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
573 #elif defined HAVE_OPENSSL
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
574 if((p = getsock(fd)) != NULL)
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
575 return SSL_read(p->ssl, buf, count);
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
576 #endif
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
577 }
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
578 #endif // HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
579 return read(fd, buf, count);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
580 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
581
235
f7f07794d2df [/trunk] Changeset 248 by mikael
mikael
parents: 134
diff changeset
582 void cw_close(int fd) {
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
583 #ifdef HAVE_SSL
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
584 delsock(fd);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
585 #endif
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
586 close(fd);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
587 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
588
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
589 #define FREEVAR(v) if(v) free(v), v = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
590
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
591 void cw_setbind(const char *abindaddr) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
592 FREEVAR(bindaddr);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
593 bindaddr = strdup(abindaddr);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
594 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
595
1253
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
596 void cw_setproxy(const char *aproxyhost, int aproxyport,
eb38963e082f Add gnutls suppport
Mikael Berthe <mikael@lilotux.net>
parents: 984
diff changeset
597 const char *aproxyuser, const char *aproxypass) {
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
598 FREEVAR(proxyhost);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
599 FREEVAR(proxyuser);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
600 FREEVAR(proxypass);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
601
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
602 if(aproxyhost && strlen(aproxyhost)) proxyhost = strdup(aproxyhost);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
603 if(aproxyuser && strlen(aproxyuser)) proxyuser = strdup(aproxyuser);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
604 if(aproxypass && strlen(aproxypass)) proxypass = strdup(aproxypass);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
605 proxyport = aproxyport;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
606 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
607
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
608 char *cw_base64_encode(const char *in) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
609 static char base64digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
610
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
611 int j = 0;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
612 int inlen = strlen(in);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
613 char *out = (char *) malloc(inlen*4+1), c;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
614
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
615 for(out[0] = 0; inlen >= 3; inlen -= 3) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
616 strncat(out, &base64digits[ in[j] >> 2 ], 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
617 strncat(out, &base64digits[ ((in[j] << 4) & 0x30) | (in[j+1] >> 4) ], 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
618 strncat(out, &base64digits[ ((in[j+1] << 2) & 0x3c) | (in[j+2] >> 6) ], 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
619 strncat(out, &base64digits[ in[j+2] & 0x3f ], 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
620 j += 3;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
621 }
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
622
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
623 if(inlen > 0) {
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
624 unsigned char fragment;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
625
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
626 strncat(out, &base64digits[in[j] >> 2], 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
627 fragment = (in[j] << 4) & 0x30;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
628
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
629 if(inlen > 1)
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
630 fragment |= in[j+1] >> 4;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
631
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
632 strncat(out, &base64digits[fragment], 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
633
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
634 c = (inlen < 2) ? '-' : base64digits[ (in[j+1] << 2) & 0x3c ];
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
635 strncat(out, &c, 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
636 c = '-';
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
637 strncat(out, &c, 1);
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
638 }
414
ec86d759ed54 Trailing whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 409
diff changeset
639
25
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
640 return out;
bf3d6e241714 [/trunk] Changeset 41 by mikael
mikael
parents:
diff changeset
641 }