-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
215 lines (173 loc) · 4.27 KB
/
utils.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
#include "utils.h"
const char LOWER_XDIGITS[] = "0123456789abcdef";
char *format_hex(const uint8_t *s, size_t len)
{
size_t alloc_size = len * sizeof(char) * 2 + 1;
char *res = (char *)malloc(alloc_size);
memset(res, '\0', alloc_size);
for (size_t i = 0; i < len; i++)
{
uint8_t c = s[i];
res[i * 2] = LOWER_XDIGITS[c >> 4];
res[i * 2 + 1] = LOWER_XDIGITS[c & 0x0f];
}
return res;
}
int resolve_and_connect(const char *host, const char *port,
struct sockaddr *local_addr, size_t *local_addrlen,
struct sockaddr *remote_addr, size_t *remote_addrlen)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int ret, fd;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
ret = getaddrinfo(host, port, &hints, &result);
if (ret != 0)
return -1;
for (rp = result; rp != NULL; rp = rp->ai_next)
{
fd = socket(rp->ai_family, rp->ai_socktype | SOCK_NONBLOCK,
rp->ai_protocol);
if (fd == -1)
continue;
if (connect(fd, rp->ai_addr, rp->ai_addrlen) == 0)
{
*remote_addrlen = rp->ai_addrlen;
memcpy(remote_addr, rp->ai_addr, rp->ai_addrlen);
socklen_t len = (socklen_t)*local_addrlen;
if (getsockname(fd, local_addr, &len) == -1)
return -1;
*local_addrlen = len;
break;
}
close(fd);
}
freeaddrinfo(result);
if (rp == NULL)
return -1;
return fd;
}
int resolve_and_bind(const char *host, const char *port,
struct sockaddr *local_addr, size_t *local_addrlen)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int ret, fd;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
ret = getaddrinfo(host, port, &hints, &result);
if (ret != 0)
return -1;
for (rp = result; rp != NULL; rp = rp->ai_next)
{
fd = socket(rp->ai_family, rp->ai_socktype | SOCK_NONBLOCK, rp->ai_protocol);
if (fd == -1)
continue;
if (bind(fd, rp->ai_addr, rp->ai_addrlen) == 0)
{
*local_addrlen = rp->ai_addrlen;
memcpy(local_addr, rp->ai_addr, rp->ai_addrlen);
break;
}
close(fd);
}
freeaddrinfo(result);
if (rp == NULL)
return -1;
return fd;
}
uint64_t timestamp(void)
{
struct timespec tp;
if (clock_gettime(CLOCK_MONOTONIC, &tp) < 0)
return 0;
return (uint64_t)tp.tv_sec * NGTCP2_SECONDS + (uint64_t)tp.tv_nsec;
}
void log_printf(void *user_data, const char *fmt, ...)
{
va_list ap;
(void)user_data;
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
fprintf(stdout, "\n");
}
ssize_t
recv_packet(int fd, uint8_t *data, size_t data_size,
struct sockaddr *remote_addr, size_t *remote_addrlen)
{
struct iovec iov;
iov.iov_base = data;
iov.iov_len = data_size;
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
msg.msg_name = remote_addr;
msg.msg_namelen = *remote_addrlen;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
ssize_t ret;
do
ret = recvmsg(fd, &msg, MSG_DONTWAIT);
while (ret < 0 && errno == EINTR);
*remote_addrlen = msg.msg_namelen;
return ret;
}
ssize_t
send_packet(int fd, const uint8_t *data, size_t data_size,
struct sockaddr *remote_addr, size_t remote_addrlen)
{
struct iovec iov;
iov.iov_base = (void *)data;
iov.iov_len = data_size;
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
msg.msg_name = remote_addr;
msg.msg_namelen = remote_addrlen;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
ssize_t ret;
do
ret = sendmsg(fd, &msg, MSG_DONTWAIT);
while (ret < 0 && errno == EINTR);
return ret;
}
int gen_random_cid(ngtcp2_cid *cid)
{
int ret;
cid->datalen = NGTCP2_MIN_INITIAL_DCIDLEN;
ret = RAND_bytes(cid->data, (int)cid->datalen);
if (ret != 1)
{
fprintf(stderr, "RAND_bytes failed\n");
return -1;
}
return 0;
}
int get_ip_port(struct sockaddr_storage *addr, char *ip, uint16_t *port)
{
if (ip == NULL && port == NULL)
return 0;
if (addr->ss_family == AF_INET)
{
struct sockaddr_in *addrV4 = (struct sockaddr_in *)addr;
if (port)
*port = ntohs(addrV4->sin_port);
if (ip)
inet_ntop(addrV4->sin_family, &(addrV4->sin_addr), ip, INET_ADDRSTRLEN);
return 0;
}
else if (addr->ss_family == AF_INET6)
{
struct sockaddr_in6 *addrV6 = (struct sockaddr_in6 *)addr;
if (ip)
inet_ntop(addrV6->sin6_family, &(addrV6->sin6_addr), ip, INET6_ADDRSTRLEN);
if (port)
*port = ntohs(addrV6->sin6_port);
return 0;
}
return -1;
}