-
Notifications
You must be signed in to change notification settings - Fork 2
/
net.c
196 lines (159 loc) · 4.31 KB
/
net.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
#include "bolo.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <ctype.h>
static int
s_net_parse(char *addr, char **node, char **port)
{
char *p;
CHECK(node != NULL, "s_net_parse() passed a NULL node receiver");
CHECK(port != NULL, "s_net_parse() passed a NULL port receiver");
/*
NETWORK ADDRESS SPECIFICATION FORMAT
[<ipv6>]:<port> - ipv6
<ipv4>:<port> - ipv4
*:<port> - ALL
In the wildcard case(s), *node will be set to NULL
*/
if (!addr || !*addr)
goto invalid;
debugf("parsing %s", addr);
*node = p = addr;
if (!addr)
return -1;
if (*p == '*') {
debugf("wildcard address detected.");
p++; if (*p != ':') goto invalid;
*node = NULL;
p++; goto port;
}
if (*p == '[') {
debugf("IPv6 bracketed address detected.");
*node = ++p;
while (*p && *p != ']') p++;
if (!*p) goto invalid;
*p++ = '\0'; /* NULL-terminate `node` */
if (*p != ':') goto invalid;
p++;
} else {
debugf("IPv4 or FQDN address detected.");
while (*p && *p != ':') p++;
if (!*p) goto invalid;
*p++ = '\0'; /* NULL-terminate `node` */
}
port:
*port = p;
debugf("node is (%s), port is (%s); validating...", *node, *port);
while (isdigit(*p))
p++;
if (*p) /* extra junk after port */
goto invalid;
return 0;
invalid:
errno = EINVAL;
return -1;
}
int
net_bind(const char *_addr, int backlog)
{
struct addrinfo hints, *results, *res;
char *addr, *node, *port;
int v, fd;
if (!_addr)
goto invalid;
if (!(addr = strdup(_addr)))
goto invalid;
if (s_net_parse(addr, &node, &port) != 0)
goto invalid;
debugf("running getaddrinfo to enumerate bindable interfaces.");
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; /* only support tcp */
v = getaddrinfo(node, port, &hints, &results);
if (v != 0) {
errno = EINVAL;
errorf("getaddrinfo errored: %s (error %d)\n", gai_strerror(v), v);
goto failed;
}
errno = ENODEV; /* best guess. */
for (res = results; res; res = res->ai_next) {
debugf("checking interface {AF %d SOCKET %d PROTO %d}",
res->ai_family, res->ai_socktype, res->ai_protocol);
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd < 0) continue;
v = 1;
debugf("socket created; setting SO_REUSEADDR, binding and listening.");
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) != 0
|| bind(fd, res->ai_addr, res->ai_addrlen) != 0
|| listen(fd, backlog) != 0) {
debugf("failed to bind and listen on %s: %s (error %d) -- skipping...",
_addr, strerror(errno), errno);
close(fd);
continue;
}
/* success! */
debugf("fd %d bound and listening on %s", fd, _addr);
freeaddrinfo(results);
return fd;
}
freeaddrinfo(results);
goto failed;
invalid:
errno = EINVAL;
failed:
free(addr);
return -1;
}
int
net_connect(const char *_addr)
{
struct addrinfo hints, *results, *res;
char *addr, *node, *port;
int v, fd;
if (!_addr)
goto invalid;
if (!(addr = strdup(_addr)))
goto invalid;
if (s_net_parse(addr, &node, &port) != 0)
goto invalid;
debugf("running getaddrinfo to enumerate source interfaces.");
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICSERV;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; /* only support tcp */
v = getaddrinfo(node, port, &hints, &results);
if (v != 0) {
errno = EINVAL;
errorf("getaddrinfo errored: %s (error %d)\n", gai_strerror(v), v);
goto failed;
}
errno = ENODEV; /* best guess. */
for (res = results; res; res = res->ai_next) {
debugf("checking interface {AF %d SOCKET %d PROTO %d}",
res->ai_family, res->ai_socktype, res->ai_protocol);
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd < 0) continue;
v = 1;
debugf("socket created; setting SO_REUSEADDR, and connecting.");
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) != 0
|| connect(fd, res->ai_addr, res->ai_addrlen) != 0) {
debugf("failed to connect to %s: %s (error %d) -- skipping...",
_addr, strerror(errno), errno);
close(fd);
continue;
}
/* success! */
debugf("fd %d connected to %s", fd, _addr);
freeaddrinfo(results);
return fd;
}
freeaddrinfo(results);
goto failed;
invalid:
errno = EINVAL;
failed:
return -1;
}