d4f13d7642a328b211d43f999e8977c17d4aadfa
[project/luci.git] / libs / nixio / src / address.c
1 /*
2 * nixio - Linux I/O library for lua
3 *
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "nixio.h"
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <errno.h>
23 #include <string.h>
24
25 #ifdef __linux__
26
27 #include <signal.h>
28 #include <setjmp.h>
29 #include <unistd.h>
30
31 /* setjmp() / longjmp() stuff */
32 static jmp_buf nixio__jump_alarm;
33 static void nixio__handle_alarm(int sig) { longjmp(nixio__jump_alarm, 1); }
34
35 #include <linux/netdevice.h>
36
37 /* struct net_device_stats is buggy on amd64, redefine it */
38 struct nixio__nds {
39 uint32_t rx_packets;
40 uint32_t tx_packets;
41 uint32_t rx_bytes;
42 uint32_t tx_bytes;
43 uint32_t rx_errors;
44 uint32_t tx_errors;
45 uint32_t rx_dropped;
46 uint32_t tx_dropped;
47 uint32_t multicast;
48 uint32_t collisions;
49
50 uint32_t rx_length_errors;
51 uint32_t rx_over_errors;
52 uint32_t rx_crc_errors;
53 uint32_t rx_frame_errors;
54 uint32_t rx_fifo_errors;
55 uint32_t rx_missed_errors;
56
57 uint32_t tx_aborted_errors;
58 uint32_t tx_carrier_errors;
59 uint32_t tx_fifo_errors;
60 uint32_t tx_heartbeat_errors;
61 uint32_t tx_window_errors;
62
63 uint32_t rx_compressed;
64 uint32_t tx_compressed;
65 };
66 #endif
67
68 #ifndef NI_MAXHOST
69 #define NI_MAXHOST 1025
70 #endif
71
72 /**
73 * address pushing helper
74 */
75 int nixio__addr_parse(nixio_addr *addr, struct sockaddr *saddr) {
76 void *baddr;
77
78 addr->family = saddr->sa_family;
79 if (saddr->sa_family == AF_INET) {
80 struct sockaddr_in *inetaddr = (struct sockaddr_in*)saddr;
81 addr->port = ntohs(inetaddr->sin_port);
82 baddr = &inetaddr->sin_addr;
83 } else if (saddr->sa_family == AF_INET6) {
84 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)saddr;
85 addr->port = ntohs(inet6addr->sin6_port);
86 baddr = &inet6addr->sin6_addr;
87 #ifdef AF_PACKET
88 } else if (saddr->sa_family == AF_PACKET) {
89 struct sockaddr_ll *etheradddr = (struct sockaddr_ll*)saddr;
90 addr->prefix = etheradddr->sll_hatype;
91 addr->port = etheradddr->sll_ifindex;
92 char *c = addr->host;
93 for (size_t i = 0; i < etheradddr->sll_halen; i++) {
94 *c++ = nixio__bin2hex[(etheradddr->sll_addr[i] & 0xf0) >> 4];
95 *c++ = nixio__bin2hex[(etheradddr->sll_addr[i] & 0x0f)];
96 *c++ = ':';
97 }
98 *(c-1) = 0;
99 return 0;
100 #endif
101 } else {
102 errno = EAFNOSUPPORT;
103 return -1;
104 }
105
106 if (!inet_ntop(saddr->sa_family, baddr, addr->host, sizeof(addr->host))) {
107 return -1;
108 }
109
110 return 0;
111 }
112
113 /**
114 * address pulling helper
115 */
116 int nixio__addr_write(nixio_addr *addr, struct sockaddr *saddr) {
117 if (addr->family == AF_UNSPEC) {
118 if (strchr(addr->host, ':')) {
119 addr->family = AF_INET6;
120 } else {
121 addr->family = AF_INET;
122 }
123 }
124 if (addr->family == AF_INET) {
125 struct sockaddr_in *inetaddr = (struct sockaddr_in *)saddr;
126 memset(inetaddr, 0, sizeof(struct sockaddr_in));
127
128 if (inet_pton(AF_INET, addr->host, &inetaddr->sin_addr) < 1) {
129 return -1;
130 }
131
132 inetaddr->sin_family = AF_INET;
133 inetaddr->sin_port = htons((uint16_t)addr->port);
134 return 0;
135 } else if (addr->family == AF_INET6) {
136 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6 *)saddr;
137 memset(inet6addr, 0, sizeof(struct sockaddr_in6));
138
139 if (inet_pton(AF_INET6, addr->host, &inet6addr->sin6_addr) < 1) {
140 return -1;
141 }
142
143 inet6addr->sin6_family = AF_INET6;
144 inet6addr->sin6_port = htons((uint16_t)addr->port);
145 return 0;
146 } else {
147 errno = EAFNOSUPPORT;
148 return -1;
149 }
150 }
151
152 /**
153 * netmask to prefix helper
154 */
155 int nixio__addr_prefix(struct sockaddr *saddr) {
156 int prefix = 0;
157 size_t len;
158 uint8_t *addr;
159
160 if (saddr->sa_family == AF_INET) {
161 addr = (uint8_t*)(&((struct sockaddr_in*)saddr)->sin_addr);
162 len = 4;
163 } else if (saddr->sa_family == AF_INET6) {
164 addr = (uint8_t*)(&((struct sockaddr_in6*)saddr)->sin6_addr);
165 len = 16;
166 } else {
167 errno = EAFNOSUPPORT;
168 return -1;
169 }
170
171 for (size_t i = 0; i < len; i++) {
172 if (addr[i] == 0xff) {
173 prefix += 8;
174 } else if (addr[i] == 0x00) {
175 break;
176 } else {
177 for (uint8_t c = addr[i]; c; c <<= 1) {
178 prefix++;
179 }
180 }
181 }
182
183 return prefix;
184 }
185
186 /**
187 * getaddrinfo(host, family, port)
188 */
189 static int nixio_getaddrinfo(lua_State *L) {
190 const char *host = NULL;
191 if (!lua_isnoneornil(L, 1)) {
192 host = luaL_checklstring(L, 1, NULL);
193 }
194 const char *family = luaL_optlstring(L, 2, "any", NULL);
195 const char *port = lua_tolstring(L, 3, NULL);
196
197 struct addrinfo hints, *result, *rp;
198 memset(&hints, 0, sizeof(hints));
199
200 if (!strcmp(family, "any")) {
201 hints.ai_family = AF_UNSPEC;
202 } else if (!strcmp(family, "inet")) {
203 hints.ai_family = AF_INET;
204 } else if (!strcmp(family, "inet6")) {
205 hints.ai_family = AF_INET6;
206 } else {
207 return luaL_argerror(L, 2, "supported values: any, inet, inet6");
208 }
209
210 hints.ai_socktype = 0;
211 hints.ai_protocol = 0;
212
213 int aistat = getaddrinfo(host, port, &hints, &result);
214 if (aistat) {
215 lua_pushnil(L);
216 lua_pushinteger(L, aistat);
217 lua_pushstring(L, gai_strerror(aistat));
218 return 3;
219 }
220
221 /* create socket object */
222 lua_newtable(L);
223 int i = 1;
224
225 for (rp = result; rp != NULL; rp = rp->ai_next) {
226 /* avoid duplicate results */
227 #ifndef __WINNT__
228 if (!port && rp->ai_socktype != SOCK_STREAM) {
229 continue;
230 }
231 #endif
232
233 if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) {
234 lua_createtable(L, 0, port ? 4 : 2);
235 if (rp->ai_family == AF_INET) {
236 lua_pushliteral(L, "inet");
237 } else if (rp->ai_family == AF_INET6) {
238 lua_pushliteral(L, "inet6");
239 }
240 lua_setfield(L, -2, "family");
241
242 if (port) {
243 switch (rp->ai_socktype) {
244 case SOCK_STREAM:
245 lua_pushliteral(L, "stream");
246 break;
247 case SOCK_DGRAM:
248 lua_pushliteral(L, "dgram");
249 break;
250 case SOCK_RAW:
251 lua_pushliteral(L, "raw");
252 break;
253 default:
254 lua_pushnil(L);
255 break;
256 }
257 lua_setfield(L, -2, "socktype");
258 }
259
260 nixio_addr addr;
261 if (nixio__addr_parse(&addr, rp->ai_addr)) {
262 freeaddrinfo(result);
263 return nixio__perror_s(L);
264 }
265
266 if (port) {
267 lua_pushinteger(L, addr.port);
268 lua_setfield(L, -2, "port");
269 }
270
271 lua_pushstring(L, addr.host);
272 lua_setfield(L, -2, "address");
273 lua_rawseti(L, -2, i++);
274 }
275 }
276
277 freeaddrinfo(result);
278
279 return 1;
280 }
281
282 /**
283 * getnameinfo(address, family[, timeout])
284 */
285 static int nixio_getnameinfo(lua_State *L) {
286 const char *ip = luaL_checkstring(L, 1);
287 const char *family = luaL_optstring(L, 2, NULL);
288
289 #ifdef __linux__
290 struct sigaction sa_new, sa_old;
291 int timeout = luaL_optnumber(L, 3, 0);
292 if (timeout > 0 && timeout < 1000)
293 {
294 sa_new.sa_handler = nixio__handle_alarm;
295 sa_new.sa_flags = 0;
296 sigemptyset(&sa_new.sa_mask);
297 sigaction(SIGALRM, &sa_new, &sa_old);
298
299 /* user timeout exceeded */
300 if (setjmp(nixio__jump_alarm))
301 {
302 sigaction(SIGALRM, &sa_old, NULL);
303
304 lua_pushnil(L);
305 lua_pushinteger(L, EAI_AGAIN);
306 lua_pushstring(L, gai_strerror(EAI_AGAIN));
307
308 return 3;
309 }
310
311 ualarm(timeout * 1000, 0);
312 }
313 #endif
314
315 char host[NI_MAXHOST];
316
317 struct sockaddr_storage saddr;
318 nixio_addr addr;
319 memset(&addr, 0, sizeof(addr));
320 strncpy(addr.host, ip, sizeof(addr.host) - 1);
321
322 if (!family) {
323 addr.family = AF_UNSPEC;
324 } else if (!strcmp(family, "inet")) {
325 addr.family = AF_INET;
326 } else if (!strcmp(family, "inet6")) {
327 addr.family = AF_INET6;
328 } else {
329 return luaL_argerror(L, 2, "supported values: inet, inet6");
330 }
331
332 nixio__addr_write(&addr, (struct sockaddr *)&saddr);
333
334 int res = getnameinfo((struct sockaddr *)&saddr, sizeof(saddr),
335 host, sizeof(host), NULL, 0, NI_NAMEREQD);
336
337 #ifdef __linux__
338 if (timeout > 0 && timeout < 1000)
339 {
340 ualarm(0);
341 sigaction(SIGALRM, &sa_old, NULL);
342 }
343 #endif
344
345 if (res) {
346 lua_pushnil(L);
347 lua_pushinteger(L, res);
348 lua_pushstring(L, gai_strerror(res));
349 return 3;
350 } else {
351 lua_pushstring(L, host);
352 return 1;
353 }
354 }
355
356 /**
357 * getsockname()
358 */
359 static int nixio_sock_getsockname(lua_State *L) {
360 int sockfd = nixio__checksockfd(L);
361 struct sockaddr_storage saddr;
362 socklen_t addrlen = sizeof(saddr);
363 nixio_addr addr;
364
365 if (getsockname(sockfd, (struct sockaddr*)&saddr, &addrlen) ||
366 nixio__addr_parse(&addr, (struct sockaddr*)&saddr)) {
367 return nixio__perror_s(L);
368 }
369
370 lua_pushstring(L, addr.host);
371 lua_pushinteger(L, addr.port);
372 return 2;
373 }
374
375 /**
376 * getpeername()
377 */
378 static int nixio_sock_getpeername(lua_State *L) {
379 int sockfd = nixio__checksockfd(L);
380 struct sockaddr_storage saddr;
381 socklen_t addrlen = sizeof(saddr);
382 nixio_addr addr;
383
384 if (getpeername(sockfd, (struct sockaddr*)&saddr, &addrlen) ||
385 nixio__addr_parse(&addr, (struct sockaddr*)&saddr)) {
386 return nixio__perror_s(L);
387 }
388
389 lua_pushstring(L, addr.host);
390 lua_pushinteger(L, addr.port);
391 return 2;
392 }
393
394 #if defined(__linux__) || defined(BSD)
395 #ifdef BSD
396 #include <net/if.h>
397 #endif
398 #include <ifaddrs.h>
399
400 static int nixio_getifaddrs(lua_State *L) {
401 nixio_addr addr;
402 struct ifaddrs *ifaddr, *c;
403 if (getifaddrs(&ifaddr) == -1) {
404 return nixio__perror(L);
405 }
406
407 lua_newtable(L);
408 unsigned int i = 1;
409
410 for (c = ifaddr; c; c = c->ifa_next) {
411 lua_newtable(L);
412
413 lua_pushstring(L, c->ifa_name);
414 lua_setfield(L, -2, "name");
415
416 lua_createtable(L, 0, 7);
417 lua_pushboolean(L, c->ifa_flags & IFF_UP);
418 lua_setfield(L, -2, "up");
419
420 lua_pushboolean(L, c->ifa_flags & IFF_BROADCAST);
421 lua_setfield(L, -2, "broadcast");
422
423 lua_pushboolean(L, c->ifa_flags & IFF_LOOPBACK);
424 lua_setfield(L, -2, "loopback");
425
426 lua_pushboolean(L, c->ifa_flags & IFF_POINTOPOINT);
427 lua_setfield(L, -2, "pointtopoint");
428
429 lua_pushboolean(L, c->ifa_flags & IFF_NOARP);
430 lua_setfield(L, -2, "noarp");
431
432 lua_pushboolean(L, c->ifa_flags & IFF_PROMISC);
433 lua_setfield(L, -2, "promisc");
434
435 lua_pushboolean(L, c->ifa_flags & IFF_MULTICAST);
436 lua_setfield(L, -2, "multicast");
437 lua_setfield(L, -2, "flags");
438
439 if (c->ifa_addr) {
440 if (!nixio__addr_parse(&addr, c->ifa_addr)) {
441 lua_pushstring(L, addr.host);
442 lua_setfield(L, -2, "addr");
443 }
444
445 if (c->ifa_addr->sa_family == AF_INET) {
446 lua_pushliteral(L, "inet");
447 } else if (c->ifa_addr->sa_family == AF_INET6) {
448 lua_pushliteral(L, "inet6");
449 #ifdef AF_PACKET
450 } else if (c->ifa_addr->sa_family == AF_PACKET) {
451 lua_pushliteral(L, "packet");
452 #endif
453 } else {
454 lua_pushliteral(L, "unknown");
455 }
456 lua_setfield(L, -2, "family");
457
458 #ifdef __linux__
459 if (c->ifa_addr->sa_family == AF_PACKET) {
460 lua_pushinteger(L, addr.port);
461 lua_setfield(L, -2, "ifindex");
462
463 lua_pushinteger(L, addr.prefix);
464 lua_setfield(L, -2, "hatype");
465 }
466 #endif
467 }
468
469 #ifdef __linux__
470 if (c->ifa_data && (!c->ifa_addr
471 || c->ifa_addr->sa_family == AF_PACKET)) {
472 if (!c->ifa_addr) {
473 lua_pushliteral(L, "packet");
474 lua_setfield(L, -2, "family");
475 }
476
477 lua_createtable(L, 0, 10);
478 struct nixio__nds *stats = c->ifa_data;
479
480 lua_pushnumber(L, stats->rx_packets);
481 lua_setfield(L, -2, "rx_packets");
482
483 lua_pushnumber(L, stats->tx_packets);
484 lua_setfield(L, -2, "tx_packets");
485
486 lua_pushnumber(L, stats->rx_bytes);
487 lua_setfield(L, -2, "rx_bytes");
488
489 lua_pushnumber(L, stats->tx_bytes);
490 lua_setfield(L, -2, "tx_bytes");
491
492 lua_pushnumber(L, stats->rx_errors);
493 lua_setfield(L, -2, "rx_errors");
494
495 lua_pushnumber(L, stats->tx_errors);
496 lua_setfield(L, -2, "tx_errors");
497
498 lua_pushnumber(L, stats->rx_dropped);
499 lua_setfield(L, -2, "rx_dropped");
500
501 lua_pushnumber(L, stats->tx_dropped);
502 lua_setfield(L, -2, "tx_dropped");
503
504 lua_pushnumber(L, stats->multicast);
505 lua_setfield(L, -2, "multicast");
506
507 lua_pushnumber(L, stats->collisions);
508 lua_setfield(L, -2, "collisions");
509 } else {
510 lua_newtable(L);
511 }
512 lua_setfield(L, -2, "data");
513 #endif
514
515 if (c->ifa_netmask && !nixio__addr_parse(&addr, c->ifa_netmask)) {
516 lua_pushstring(L, addr.host);
517 lua_setfield(L, -2, "netmask");
518
519 lua_pushinteger(L, nixio__addr_prefix(c->ifa_netmask));
520 lua_setfield(L, -2, "prefix");
521 }
522
523 if (c->ifa_broadaddr && !nixio__addr_parse(&addr, c->ifa_broadaddr)) {
524 lua_pushstring(L, addr.host);
525 lua_setfield(L, -2, "broadaddr");
526 }
527
528 if (c->ifa_dstaddr && !nixio__addr_parse(&addr, c->ifa_dstaddr)) {
529 lua_pushstring(L, addr.host);
530 lua_setfield(L, -2, "dstaddr");
531 }
532
533 lua_rawseti(L, -2, i++);
534 }
535
536 freeifaddrs(ifaddr);
537 return 1;
538 }
539 #endif
540
541
542 /* module table */
543 static const luaL_reg R[] = {
544 #if defined(__linux__) || defined(BSD)
545 {"getifaddrs", nixio_getifaddrs},
546 #endif
547 {"getaddrinfo", nixio_getaddrinfo},
548 {"getnameinfo", nixio_getnameinfo},
549 {NULL, NULL}
550 };
551
552 /* object table */
553 static const luaL_reg M[] = {
554 {"getsockname", nixio_sock_getsockname},
555 {"getpeername", nixio_sock_getpeername},
556 {NULL, NULL}
557 };
558
559 void nixio_open_address(lua_State *L) {
560 luaL_register(L, NULL, R);
561
562 lua_pushvalue(L, -2);
563 luaL_register(L, NULL, M);
564 lua_pop(L, 1);
565 }