-
Notifications
You must be signed in to change notification settings - Fork 1
/
addr.c
306 lines (264 loc) · 8.8 KB
/
addr.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include "debugp.h"
// will error if already unsigned
#define US(X) (_Generic((X), \
char: (unsigned char)(X), \
short: (unsigned short)(X), \
int: (unsigned int)(X), \
long: (unsigned long)(X), \
long long: (unsigned long long)(X) \
))
#pragma GCC visibility push(internal)
// https://blog.habets.se/2022/10/No-way-to-parse-integers-in-C.html
static int parse_port(const char *str) {
char *end;
errno = 0;
long x = strtol(str, &end, 10);
if (errno == 0 && x >= 0 && x < 65536 && str != end && end[0] == '\0') {
return US(x);
}
return -1;
}
static int getsockinfo(int fd, int *domain, int *type, int *protocol) {
int ret;
socklen_t optlen;
*domain = -1; optlen = sizeof(int);
ret = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, domain, &optlen);
if (ret < 0) { return ret; } else if (optlen > sizeof(int)) { return ENOBUFS; }
*type = -1; optlen = sizeof(int);
ret = getsockopt(fd, SOL_SOCKET, SO_TYPE, type, &optlen);
if (ret < 0) { return ret; } else if (optlen > sizeof(int)) { return ENOBUFS; }
*protocol = -1; optlen = sizeof(int);
ret = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, protocol, &optlen);
if (ret < 0) { return ret; } else if (optlen > sizeof(int)) { return ENOBUFS; }
return 0;
}
// allocate a new addrinfo struct
static struct addrinfo * newaddrinfo() {
struct addrinfo *ai = malloc(sizeof(struct addrinfo));
if (ai == NULL) return NULL;
ai->ai_canonname = NULL;
ai->ai_next = NULL;
// allocate sockaddr
ai->ai_addrlen = sizeof(struct sockaddr_storage);
if ((ai->ai_addr = malloc(ai->ai_addrlen)) == NULL) {
free(ai);
return NULL;
}
return ai;
}
int deladdrinfo(struct addrinfo *curr) {
int ret = 0;
while (curr != NULL) {
struct addrinfo *next = curr->ai_next;
free(curr->ai_canonname);
free(curr->ai_addr);
free(curr);
curr = next;
++ret;
}
return ret;
}
// get addrinfo struct based on file descriptor
int fdaddrinfo(int fd, const struct sockaddr *sa, struct addrinfo **res) {
int err;
struct addrinfo *ai = newaddrinfo();
if ((*res = ai) == NULL) { errno = ENOMEM; return -1; }
if (sa != NULL) {
// copy the supplied sockaddr
memcpy(ai->ai_addr, sa, ai->ai_addrlen);
} else {
// populate sockaddr from fd
if ((err = getsockname(fd, ai->ai_addr, &(ai->ai_addrlen))) != 0) {
deladdrinfo(ai);
*res = NULL;
return err;
}
}
// populate other fields
if ((err = getsockinfo(fd, &(ai->ai_family), &(ai->ai_socktype), &(ai->ai_protocol))) != 0) {
deladdrinfo(ai);
*res = NULL;
return err;
}
return 0;
}
// like inet_pton, but with protocol and port
struct addrinfo * ai_pton(const char *str) {
struct addrinfo *ai = newaddrinfo();
if (ai == NULL) { errno = ENOMEM; return NULL; }
struct sockaddr *sa = ai->ai_addr;
const char *sep = strstr(str, "://");
if (sep == NULL) return NULL;
const char *proto = str;
size_t proto_sz = sep - str;
const char *port, *addr = sep + 3;
char ip[64];
if (proto_sz == 4 && memcmp(proto, "unix", 4) == 0) {
ai->ai_family = sa->sa_family = AF_UNIX;
ai->ai_socktype = SOCK_STREAM;
ai->ai_protocol = 0;
struct sockaddr_un *sun = (struct sockaddr_un *)sa;
const char *path = realpath(addr, NULL);
if (path == NULL) path = addr;
size_t n = strnlen(path, sizeof(sun->sun_path));
// check whether path is too long
if (n >= sizeof(sun->sun_path)) { errno = ENOBUFS; goto ai_pton_fail; }
// we just checked buffer size
strcpy(sun->sun_path, path);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
if (path != addr) free(path);
#pragma GCC diagnostic pop
} else if (proto_sz == 3 && (memcmp(proto, "udp", 3) == 0 || memcmp(proto, "tcp", 3) == 0)) {
int p;
if (memcmp(proto, "tcp", 3) == 0) {
ai->ai_socktype = SOCK_STREAM;
ai->ai_protocol = IPPROTO_TCP;
} else if (memcmp(proto, "udp", 3) == 0) {
ai->ai_socktype = SOCK_DGRAM;
ai->ai_protocol = IPPROTO_UDP;
}
if (addr[0] == '[') {
sep = strstr(addr, "]:");
if (sep == NULL) { errno = EINVAL; goto ai_pton_fail; }
// check whether ip string is too long
size_t n = (sep - addr) - 1;
if (n > (sizeof(ip)-1)) { errno = EINVAL; goto ai_pton_fail; }
// we just checked the size
memcpy(ip, addr+1, n); ip[n] = '\0';
port = addr + n + 3;
ai->ai_family = sa->sa_family = AF_INET6;
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
if (inet_pton(AF_INET6, ip, (void *)&(sin6->sin6_addr)) != 1) {
errno = EINVAL;
goto ai_pton_fail;
}
if ((p = parse_port(port)) < 0) { errno = EINVAL; goto ai_pton_fail; }
sin6->sin6_port = htons(p);
} else {
sep = strchr(addr, ':');
if (sep == NULL) { errno = EINVAL; goto ai_pton_fail; }
// check whether ip string is too long
size_t n = sep - addr;
if (n > (sizeof(ip)-1)) { errno = EINVAL; goto ai_pton_fail; }
// we just checked the size
memcpy(ip, addr, n); ip[n] = '\0';
port = addr + n + 1;
ai->ai_family = sa->sa_family = AF_INET;
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
if (inet_pton(AF_INET, ip, (void *)&(sin->sin_addr)) != 1) {
errno = EINVAL;
goto ai_pton_fail;
}
if ((p = parse_port(port)) < 0) { errno = EINVAL; goto ai_pton_fail; }
sin->sin_port = htons(p);
}
} else {
errno = EINVAL;
goto ai_pton_fail;
}
return ai;
ai_pton_fail:
deladdrinfo(ai);
return NULL;
}
// like inet_ntop, but with protocol and port
char * ai_ntop(char *dst, socklen_t size, const struct addrinfo *ai) {
// set up output
size_t n;
char *path, *str = dst;
dst[size-1] = '\0';
const struct sockaddr *sa = ai->ai_addr;
if (sa == NULL) return NULL;
int family = ai->ai_family;
int socktype = ai->ai_socktype;
int protocol = ai->ai_protocol;
if (family != sa->sa_family) {
snprintf(dst, size, "Address family mismatch: ai %d != sa %d", family, sa->sa_family);
return NULL;
}
if (family == AF_INET || family == AF_INET6) {
// get the protcol info
if (socktype == SOCK_DGRAM && (protocol == 0 || protocol == IPPROTO_UDP)) {
protocol = IPPROTO_UDP;
n = snprintf(str, size, "udp://");
str += n; size -= n;
} else if (socktype == SOCK_STREAM && (protocol == 0 || protocol == IPPROTO_TCP)) {
protocol = IPPROTO_TCP;
n = snprintf(str, size, "tcp://");
str += n; size -= n;
} else {
n = snprintf(str, size, "unknown(%d,%d,%d)://", family, socktype, protocol);
str += n; size -= n;
}
// get the ip address and port
uint16_t port;
if (family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
inet_ntop(AF_INET, &(sin->sin_addr), str, size);
n = strnlen(str, size);
str += n; size -= n;
port = ntohs(sin->sin_port);
} else if (family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
if (size > 1) { str[0] = '['; ++str; --size; }
inet_ntop(AF_INET6, &(sin6->sin6_addr), str, size);
n = strnlen(str, size);
str += n; size -= n;
if (size > 1) { str[0] = ']'; ++str; --size; }
port = ntohs(sin6->sin6_port);
}
if (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP) {
snprintf(str, size, ":%u", port);
}
} else if (family == AF_UNIX) {
if (socktype == SOCK_STREAM) {
struct sockaddr_un *sun = (struct sockaddr_un *)sa;
n = snprintf(str, size, "unix://");
str += n; size -= n;
path = realpath((const char *)&(sun->sun_path), NULL);
if (path == NULL) path = (char *)&(sun->sun_path);
strncpy(str, path, size);
if (path != (char *)&(sun->sun_path)) free(path);
} else {
snprintf(dst, size, "Unknown unix socket type %d (0x%4x)", socktype, US(socktype));
return NULL;
}
} else {
snprintf(dst, size, "Unknown address family %d (0x%4x)", family, US(family));
return NULL;
}
// detect buffer out of space
if (str[size-1] != '\0') { str[size-1] = '\0'; return NULL; }
return dst;
}
// like inet_ntop, but with protocol and port
char * fd_ntop(char *dst, socklen_t size, int sockfd, const struct sockaddr *sa) {
struct addrinfo ai;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
ai.ai_addr = (const struct sockaddr *)sa;
#pragma GCC diagnostic pop
if (getsockinfo(sockfd, &ai.ai_family, &ai.ai_socktype, &ai.ai_protocol) != 0) {
return NULL;
}
return ai_ntop(dst, size, &ai);
}
#pragma GCC visibility pop