forked from blechschmidt/massdns
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
net.h
204 lines (184 loc) · 5.05 KB
/
net.h
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
#ifndef MASSRESOLVER_NET_H
#define MASSRESOLVER_NET_H
#include <stdbool.h>
#include <fcntl.h>
#include <net/if.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <inttypes.h>
#define loop_sockets(sockets) \
for (socket_info_t *socket = (sockets)->data; socket < ((socket_info_t*)(sockets)->data) + (sockets)->len; socket++)
typedef enum
{
PROTO_IPV4 = 1 << 0,
PROTO_IPV6 = 1 << 1
} ip_support_t;
typedef enum
{
SOCKET_TYPE_INTERFACE,
SOCKET_TYPE_QUERY,
SOCKET_TYPE_CONTROL
} socket_type_t;
typedef enum
{
NETMODE_EPOLL,
NETMODE_BUSYPOLL
} netmode_t;
typedef struct
{
ip_support_t protocol;
int descriptor;
socket_type_t type;
void *data;
} socket_info_t;
void socket_noblock(socket_info_t* socket)
{
int sd = socket->descriptor;
int flags = fcntl(sd, F_GETFL, 0);
fcntl(sd, F_SETFL, flags | O_NONBLOCK);
}
void add_sockets(int epollfd, uint32_t events, int op, buffer_t *sockets)
{
socket_info_t *interface_sockets = sockets->data;
for (size_t i = 0; i < sockets->len; i++)
{
struct epoll_event ev;
bzero(&ev, sizeof(ev));
ev.data.ptr = &interface_sockets[i];
ev.events = events;
if (epoll_ctl(epollfd, op, interface_sockets[i].descriptor, &ev) != 0)
{
perror("Failed to add epoll event");
exit(EXIT_FAILURE);
}
}
}
bool str_to_addr(char *str, uint16_t default_port, struct sockaddr_storage *addr)
{
if(str == NULL || str[0] == 0)
{
return false;
}
while(*str == ' ' || *str == '\t') // Skip whitespaces ("trim left")
{
str++;
}
unsigned long int port = default_port;
if(str[0] == '[')
{
str++;
char *closing_bracket = strstr(str, "]");
if(!closing_bracket)
{
return false;
}
if(closing_bracket[1] == ':') // Is there a port separator?
{
*closing_bracket = 0;
char *invalid_char;
port = strtoul(closing_bracket + 2, &invalid_char, 10);
if (*invalid_char != 0 || port >= UINT16_MAX)
{
return false;
}
}
}
else // We either have an IPv6 address without square brackets or an IPv4 address
{
bool v4 = false;
char *colon = NULL;
for(char *c = str; *c != 0; c++)
{
if(*c == '.' && colon == NULL) // dot before colon
{
v4 = true;
}
if(*c == ':')
{
colon = c;
}
}
if(v4 && colon) // We found the port separator
{
*colon = 0;
char *invalid_char;
port = strtoul(colon + 1, &invalid_char, 10);
if (*invalid_char != 0 || port >= UINT16_MAX)
{
return false;
}
}
}
if (inet_pton(AF_INET, str, &((struct sockaddr_in*)addr)->sin_addr) == 1)
{
((struct sockaddr_in*)addr)->sin_port = htons((uint16_t )port);
((struct sockaddr_in*)addr)->sin_family = AF_INET;
return true;
}
else if (inet_pton(AF_INET6, str, &((struct sockaddr_in6*)addr)->sin6_addr) == 1)
{
((struct sockaddr_in6*)addr)->sin6_port = htons((uint16_t )port);
((struct sockaddr_in6*)addr)->sin6_family = AF_INET6;
return true;
}
return false;
}
int get_iface_hw_addr(char *iface, uint8_t *hw_mac)
{
int s;
struct ifreq buffer;
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
return EXIT_FAILURE;
}
bzero(&buffer, sizeof(buffer));
strncpy(buffer.ifr_name, iface, IFNAMSIZ);
ioctl(s, SIOCGIFHWADDR, &buffer);
close(s);
memcpy(hw_mac, buffer.ifr_hwaddr.sa_data, 6);
return EXIT_SUCCESS;
}
#define MAC_READABLE_BUFLEN 18
int get_iface_hw_addr_readable(char *iface, char *hw_mac)
{
uint8_t buffer[6];
int result = get_iface_hw_addr(iface, buffer);
for(uint8_t *b = buffer; b < buffer + 6; b++)
{
sprintf(hw_mac, "%02x:", *b);
hw_mac += 3;
if(b == buffer + 5)
{
*(hw_mac - 1) = 0;
}
}
return result;
}
char *sockaddr2str(struct sockaddr_storage *addr)
{
static char str[INET6_ADDRSTRLEN + sizeof(":65535") + 2]; // + 2 for [ and ]
static uint16_t port;
size_t len;
if(addr->ss_family == AF_INET)
{
port = ntohs(((struct sockaddr_in*)addr)->sin_port);
inet_ntop(addr->ss_family, &((struct sockaddr_in*)addr)->sin_addr, str, sizeof(str));
len = strlen(str);
// inet_ntop does not allow us to determine, how long the printed string was.
// Thus, we have to use strlen.
}
else
{
str[0] = '[';
port = ntohs(((struct sockaddr_in6*)addr)->sin6_port);
inet_ntop(addr->ss_family, &((struct sockaddr_in6*)addr)->sin6_addr, str + 1, sizeof(str) - 1);
len = strlen(str);
str[len++] = ']';
str[len] = 0;
}
snprintf(str + len, sizeof(str) - len, ":%" PRIu16, port);
return str;
}
#endif //MASSRESOLVER_NET_H