-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket.c
286 lines (237 loc) · 5.45 KB
/
socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "get-comics.h"
#include <fcntl.h>
#ifndef _WIN32
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
/* Only define this if your OS does not support getaddrinfo.
#define IPV4
*/
/* Non-IPV4 only. We currently get about 78% cache hits! */
#define USE_CACHE
static int tcp_connected(struct connection *conn)
{
#ifdef WANT_SSL
if (is_https(conn->url)) {
if (openssl_connect(conn)) {
fail_connection(conn);
return -1;
}
} else
#endif
conn->connected = 1;
return 0;
}
static int set_non_blocking(int sock)
{
#ifdef _WIN32
u_long optval = 1;
if (ioctlsocket(sock, FIONBIO, &optval)) {
printf("ioctlsocket FIONBIO failed\n");
return -1;
}
#else
int optval = fcntl(sock, F_GETFL, 0);
if (optval == -1 || fcntl(sock, F_SETFL, optval | O_NONBLOCK)) {
my_perror("fcntl O_NONBLOCK");
return -1;
}
#endif
return 0;
}
static inline int inprogress(void)
{
#ifdef _WIN32
return WSAGetLastError() == WSAEWOULDBLOCK;
#else
return errno == EINPROGRESS || errno == EAGAIN;
#endif
}
#ifdef IPV4
int connect_socket(struct connection *conn, char *hostname, char *port_in)
{
struct sockaddr_in sock_name;
int sock;
struct hostent *host;
int port = strtol(port_in, NULL, 10);
host = gethostbyname(hostname);
if (!host) {
printf("Unable to get host %s\n", hostname);
return -1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
my_perror("socket");
return -1;
}
if (set_non_blocking(sock))
goto failed;
if (!set_conn_socket(conn, sock)) {
printf("Problems! Could not set socket\n");
goto failed;
}
memset(&sock_name, 0, sizeof(sock_name));
sock_name.sin_family = AF_INET;
sock_name.sin_addr.s_addr = *(unsigned *)host->h_addr_list[0];
sock_name.sin_port = htons((short)port);
if (connect(sock, (struct sockaddr *)&sock_name, sizeof(sock_name))) {
if (inprogress()) {
if (verbose > 1)
printf("Connection deferred\n");
return 0;
} else {
char errstr[100];
sprintf(errstr, "connect %.80s", hostname);
my_perror(errstr);
}
} else
return tcp_connected(conn);
failed:
closesocket(sock);
return -1;
}
void free_cache(void) {}
#else
#ifdef USE_CACHE
static struct addrinfo *cache;
/* Works for both ipv4 and ipv6 */
struct sockaddr_both {
short sin_family;
unsigned short sin_port;
};
#endif
static struct addrinfo *get_cache(char *hostname, char *port_str)
{
#ifdef USE_CACHE
struct addrinfo *r;
uint16_t port = htons(strtol(port_str, NULL, 10));
for (r = cache; r; r = r->ai_next)
if (strcmp(hostname, r->ai_canonname) == 0) {
struct sockaddr_both *sa = (struct sockaddr_both *)r->ai_addr;
if (sa->sin_port == port)
return r;
}
#endif
return NULL;
}
/* It is not fatal if this fails since it is only for performance. */
static void add_cache(char *hostname, struct addrinfo *r)
{
#ifdef USE_CACHE
struct addrinfo *new = malloc(sizeof(struct addrinfo));
if (!new)
return;
memcpy(new, r, sizeof(struct addrinfo));
new->ai_addr = calloc(1, r->ai_addrlen);
if (!new->ai_addr)
goto failed;
memcpy(new->ai_addr, r->ai_addr, r->ai_addrlen);
new->ai_canonname = strdup(hostname);
if (!new->ai_canonname)
goto failed;
/* Add to head since the default file tends to group hostnames */
new->ai_next = cache;
cache = new;
return;
failed:
free(new->ai_addr);
free(new);
#endif
}
void free_cache(void)
{
#ifdef USE_CACHE
while (cache) {
struct addrinfo *next = cache->ai_next;
free(cache->ai_addr);
free(cache->ai_canonname);
free(cache);
cache = next;
}
#endif
}
static int try_connect(struct addrinfo *r, int *deferred)
{
int sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
if (sock < 0)
return -1;
int flags = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
if (set_non_blocking(sock)) {
closesocket(sock);
return -1;
}
if (connect(sock, r->ai_addr, r->ai_addrlen) == 0) {
/* this almost never happens */
*deferred = 0;
return sock;
}
if (inprogress()) {
if (verbose > 1)
printf("Connection deferred\n");
*deferred = 1;
return sock;
}
closesocket(sock);
return -1;
}
int connect_socket(struct connection *conn, char *hostname, char *port)
{
int sock = -1, deferred;
struct addrinfo hints, *result, *r;
r = get_cache(hostname, port);
if (r)
sock = try_connect(r, &deferred);
if (sock < 0) {
/* We need this or we will get tcp and udp */
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(hostname, port, &hints, &result)) {
printf("Unable to get host %s\n", hostname);
return -1;
}
for (r = result; r; r = r->ai_next) {
sock = try_connect(r, &deferred);
if (sock >= 0)
break;
}
if (r) {
add_cache(hostname, r);
freeaddrinfo(result);
} else {
freeaddrinfo(result);
printf("Unable to get socket for host %s\n", hostname);
return -1;
}
}
if (!set_conn_socket(conn, sock)) {
printf("Problems! Could not set socket\n");
closesocket(sock);
return -1;
}
if (deferred)
return 0;
else
return tcp_connected(conn);
}
#endif
/* This should only be called if conn->connected == 0 */
void check_connect(struct connection *conn)
{
int so_error;
socklen_t optlen = sizeof(so_error);
#ifdef WANT_SSL
if (conn->ssl) {
/* Wait for openssl connection to connect */
if (openssl_check_connect(conn))
fail_connection(conn);
return;
}
#endif
if (getsockopt(conn->poll->fd, SOL_SOCKET, SO_ERROR,
(char *)&so_error, &optlen) == 0 &&
so_error == 0)
tcp_connected(conn);
}