-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunix.c
327 lines (291 loc) · 7.49 KB
/
unix.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* The Unix callbacks.
*
* I hate TLS/SSL, it is far to complex, the interfaces suck, and
* it is a pain to use in an embedded system (it requires a million
* different algorithms (is the ChaBlowCurveAES-256 algorithm more secure
* than FishARC4ElipticRSA-128? No one cares you weeny, just pick one)
* if you want to be compatible with any system and requires a ludicrous
* amount of memory per connection (~16-32KiB)), has too many optional
* options and options which are optional, and several different encoding
* and file formats for the certificates (just pick one!).
*
* I hope something simpler replaces it. I swear that the makers of
* cryptographic software deliberately make it difficult to use and
* integrate so as to weaken the security of everything, a conspiracy
* by the C.I.A would make perfect sense as to why it is so shit, hell,
* at least that would make sense!
*
* Luckily I do not have to care that much about it and can just
* wrap everything up and link to a giant blob of fail, cordoning
* everything off in here.
*
* Mind you the socket interface in Unix kind of sucks as well. At
* least that has the excuse of not being made by the original makers
* of Unix. */
#include "httpc.h"
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
/*#include <netinet/tcp.h> // Not portable, needed for keepalive */
#ifndef USE_SSL
#define USE_SSL (1)
#endif
#if USE_SSL != 0
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
#define UNUSED(X) ((void)(X))
typedef struct {
void *ctx;
void *ssl;
int fd;
unsigned use_ssl :1;
} socket_t;
static void *allocate(httpc_options_t *a, size_t sz) {
assert(a);
void *r = a->allocator(a->arena, NULL, 0, sz);
return r ? memset(r, 0, sz) : r;
}
static void deallocate(httpc_options_t *a, void *ptr) {
assert(a);
a->allocator(a->arena, ptr, 0, 0);
}
static int socket_close(socket_t *s, httpc_options_t *a) {
assert(s);
if (!s)
return HTTPC_OK;
int r = HTTPC_OK;
#if USE_SSL != 0
if (s->use_ssl) {
if (s->ssl)
SSL_free(s->ssl);
s->ssl = NULL;
if (s->ctx)
SSL_CTX_free(s->ctx);
s->ctx = NULL;
}
#endif
if (s->fd >= 0)
r = close(s->fd);
s->fd = -1;
deallocate(a, s);
return r;
}
static int ssl_open(socket_t *s, httpc_options_t *a, const char *domain) {
assert(s);
assert(a);
UNUSED(a);
assert(domain);
if (s->fd < 0)
return HTTPC_ERROR;
#if USE_SSL == 0
return HTTPC_ERROR;
#else
const SSL_METHOD *method = TLS_client_method();
s->ctx = SSL_CTX_new(method);
if (!(s->ctx))
goto fail;
s->ssl = SSL_new(s->ctx);
if (!(s->ssl))
goto fail;
SSL_set_fd(s->ssl, s->fd);
if (SSL_set_tlsext_host_name(s->ssl, domain) != 1)
goto fail;
if (SSL_connect(s->ssl) != 1)
goto fail;
return HTTPC_OK;
fail:
return HTTPC_ERROR;
#endif
}
/* NB. read/write do not check if we can retry - too lazy atm to do so */
static int ssl_read(socket_t *s, unsigned char *buf, size_t *length) {
assert(s);
assert(buf);
assert(length);
#if USE_SSL == 0
return HTTPC_ERROR;
#else
const size_t requested = *length;
*length = 0;
const int r = SSL_read(s->ssl, buf, requested);
if (r < 0)
return HTTPC_ERROR;
*length = r;
return HTTPC_OK;
#endif
}
static int ssl_write(socket_t *s, const unsigned char *buf, size_t *length) {
assert(s);
assert(buf);
assert(length);
#if USE_SSL == 0
return HTTPC_ERROR;
#else
assert(*length < INT_MAX);
if (*length == 0)
return HTTPC_OK;
const int r = SSL_write(s->ssl, buf, *length);
*length = 0;
if (r > 0)
*length = r;
return r > 0 ? HTTPC_OK : HTTPC_ERROR;
#endif
}
/* see https://beej.us/guide/bgnet/html/#cb46-22 */
static void *get_in_addr(struct sockaddr *sa) {
assert(sa);
if (sa->sa_family == AF_INET)
return &(((struct sockaddr_in*)sa)->sin_addr);
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int httpc_logger(httpc_options_t *a, void *logger, const char *fmt, va_list ap) {
assert(a);
assert(fmt);
assert(logger);
FILE *f = logger;
va_list copy;
va_copy(copy, ap);
const int r = vfprintf(f, fmt, copy);
va_end(copy);
return r;
}
int httpc_open(httpc_options_t *a, void **sock, void *opts, const char *host_or_ip, unsigned short port, int use_ssl) {
assert(sock);
assert(a);
assert(host_or_ip);
UNUSED(opts);
struct addrinfo *servinfo = NULL, *p = NULL;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
};
if (!USE_SSL && use_ssl)
return HTTPC_ERROR;
socket_t *s = allocate(a, sizeof *s);
if (!s)
return HTTPC_ERROR;
s->use_ssl = use_ssl;
s->fd = -1;
port = port ? port : 80;
char sport[32] = { 0 };
snprintf(sport, sizeof sport, "%u", port);
if (getaddrinfo(host_or_ip, sport, &hints, &servinfo) != 0)
goto fail;
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((s->fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
continue;
}
if (connect(s->fd, p->ai_addr, p->ai_addrlen) == -1) {
close(s->fd);
s->fd = -1;
continue;
}
break;
}
if (p == NULL) {
freeaddrinfo(servinfo);
servinfo = NULL;
goto fail;
}
char ip[INET6_ADDRSTRLEN] = { 0 };
if (NULL == inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), ip, sizeof ip))
goto fail;
freeaddrinfo(servinfo);
servinfo = NULL;
/* If you want to set the keep alive as well:
setsockopt(s->fd, IPPROTO_TCP, TCP_KEEPCNT, &(int){ 5 }, sizeof(int));
setsockopt(s->fd, IPPROTO_TCP, TCP_KEEPIDLE, &(int){ 30 }, sizeof(int));
setsockopt(s->fd, IPPROTO_TCP, TCP_KEEPINTVL, &(int){ 120 }, sizeof(int));
*/
struct timeval tx_tv = { .tv_sec = 30 }, rx_tv = { .tv_sec = 30 };
if (setsockopt(s->fd, SOL_SOCKET, SO_SNDTIMEO, &tx_tv, sizeof tx_tv) < 0)
goto fail;
if (setsockopt(s->fd, SOL_SOCKET, SO_RCVTIMEO, &rx_tv, sizeof rx_tv) < 0)
goto fail;
if (use_ssl) {
if (ssl_open(s, a, host_or_ip) < 0)
goto fail;
}
*sock = s;
return HTTPC_OK;
fail:
*sock = NULL;
socket_close(s, a);
if (servinfo) {
freeaddrinfo(servinfo);
servinfo = NULL;
}
return HTTPC_ERROR;
}
int httpc_close(httpc_options_t *a, void *socket) {
assert(a);
return socket_close(socket, a);
}
int httpc_read(httpc_options_t *a, void *socket, unsigned char *buf, size_t *length) {
assert(a);
assert(socket);
assert(length);
assert(buf);
socket_t *s = socket;
if (s->use_ssl)
return ssl_read(s, buf, length);
const int fd = s->fd;
ssize_t re = 0;
again:
errno = 0;
re = read(fd, buf, *length);
if (re == -1) {
if (errno == EINTR)
goto again;
*length = 0;
return HTTPC_ERROR;
}
*length = re;
return HTTPC_OK;
}
int httpc_write(httpc_options_t *a, void *socket, const unsigned char *buf, size_t *length) {
assert(a);
assert(socket);
assert(buf);
assert(length);
socket_t *s = socket;
if (s->use_ssl)
return ssl_write(s, buf, length);
const int fd = s->fd;
ssize_t wr = 0;
again:
errno = 0;
wr = write(fd, buf, *length);
if ((size_t)wr != *length || wr == -1) {
if (errno == EINTR)
goto again;
return HTTPC_ERROR;
}
*length = wr;
return HTTPC_OK;
}
int httpc_sleep(httpc_options_t *a, unsigned long milliseconds) {
assert(a);
return usleep(milliseconds * 1000ul) < 0 ? HTTPC_ERROR : HTTPC_OK;
}
int httpc_time(httpc_options_t *a, unsigned long *milliseconds) {
assert(a);
assert(milliseconds);
*milliseconds = 0;
struct timespec t;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &t) < 0)
return HTTPC_ERROR;
*milliseconds = (t.tv_sec * 1000ul) + (t.tv_nsec / (1000000ul));
return HTTPC_OK;
}