41c6b8ae849c2a0c2204869e7f772ee7eaadb096
2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/types.h>
21 #include <sys/param.h>
26 #include <linux/netdevice.h>
28 /* struct net_device_stats is buggy on amd64, redefine it */
41 uint32_t rx_length_errors
;
42 uint32_t rx_over_errors
;
43 uint32_t rx_crc_errors
;
44 uint32_t rx_frame_errors
;
45 uint32_t rx_fifo_errors
;
46 uint32_t rx_missed_errors
;
48 uint32_t tx_aborted_errors
;
49 uint32_t tx_carrier_errors
;
50 uint32_t tx_fifo_errors
;
51 uint32_t tx_heartbeat_errors
;
52 uint32_t tx_window_errors
;
54 uint32_t rx_compressed
;
55 uint32_t tx_compressed
;
60 #define NI_MAXHOST 1025
64 * address pushing helper
66 int nixio__addr_parse(nixio_addr
*addr
, struct sockaddr
*saddr
) {
69 addr
->family
= saddr
->sa_family
;
70 if (saddr
->sa_family
== AF_INET
) {
71 struct sockaddr_in
*inetaddr
= (struct sockaddr_in
*)saddr
;
72 addr
->port
= ntohs(inetaddr
->sin_port
);
73 baddr
= &inetaddr
->sin_addr
;
74 } else if (saddr
->sa_family
== AF_INET6
) {
75 struct sockaddr_in6
*inet6addr
= (struct sockaddr_in6
*)saddr
;
76 addr
->port
= ntohs(inet6addr
->sin6_port
);
77 baddr
= &inet6addr
->sin6_addr
;
79 } else if (saddr
->sa_family
== AF_PACKET
) {
80 struct sockaddr_ll
*etheradddr
= (struct sockaddr_ll
*)saddr
;
81 addr
->prefix
= etheradddr
->sll_hatype
;
82 addr
->port
= etheradddr
->sll_ifindex
;
84 for (size_t i
= 0; i
< etheradddr
->sll_halen
; i
++) {
85 *c
++ = nixio__bin2hex
[(etheradddr
->sll_addr
[i
] & 0xf0) >> 4];
86 *c
++ = nixio__bin2hex
[(etheradddr
->sll_addr
[i
] & 0x0f)];
97 if (!inet_ntop(saddr
->sa_family
, baddr
, addr
->host
, sizeof(addr
->host
))) {
105 * address pulling helper
107 int nixio__addr_write(nixio_addr
*addr
, struct sockaddr
*saddr
) {
108 if (addr
->family
== AF_UNSPEC
) {
109 if (strchr(addr
->host
, ':')) {
110 addr
->family
= AF_INET6
;
112 addr
->family
= AF_INET
;
115 if (addr
->family
== AF_INET
) {
116 struct sockaddr_in
*inetaddr
= (struct sockaddr_in
*)saddr
;
117 memset(inetaddr
, 0, sizeof(struct sockaddr_in
));
119 if (inet_pton(AF_INET
, addr
->host
, &inetaddr
->sin_addr
) < 1) {
123 inetaddr
->sin_family
= AF_INET
;
124 inetaddr
->sin_port
= htons((uint16_t)addr
->port
);
126 } else if (addr
->family
== AF_INET6
) {
127 struct sockaddr_in6
*inet6addr
= (struct sockaddr_in6
*)saddr
;
128 memset(inet6addr
, 0, sizeof(struct sockaddr_in6
));
130 if (inet_pton(AF_INET6
, addr
->host
, &inet6addr
->sin6_addr
) < 1) {
134 inet6addr
->sin6_family
= AF_INET6
;
135 inet6addr
->sin6_port
= htons((uint16_t)addr
->port
);
138 errno
= EAFNOSUPPORT
;
144 * netmask to prefix helper
146 int nixio__addr_prefix(struct sockaddr
*saddr
) {
151 if (saddr
->sa_family
== AF_INET
) {
152 addr
= (uint8_t*)(&((struct sockaddr_in
*)saddr
)->sin_addr
);
154 } else if (saddr
->sa_family
== AF_INET6
) {
155 addr
= (uint8_t*)(&((struct sockaddr_in6
*)saddr
)->sin6_addr
);
158 errno
= EAFNOSUPPORT
;
162 for (size_t i
= 0; i
< len
; i
++) {
163 if (addr
[i
] == 0xff) {
165 } else if (addr
[i
] == 0x00) {
168 for (uint8_t c
= addr
[i
]; c
; c
<<= 1) {
178 * getaddrinfo(host, family, port)
180 static int nixio_getaddrinfo(lua_State
*L
) {
181 const char *host
= NULL
;
182 if (!lua_isnoneornil(L
, 1)) {
183 host
= luaL_checklstring(L
, 1, NULL
);
185 const char *family
= luaL_optlstring(L
, 2, "any", NULL
);
186 const char *port
= lua_tolstring(L
, 3, NULL
);
188 struct addrinfo hints
, *result
, *rp
;
189 memset(&hints
, 0, sizeof(hints
));
191 if (!strcmp(family
, "any")) {
192 hints
.ai_family
= AF_UNSPEC
;
193 } else if (!strcmp(family
, "inet")) {
194 hints
.ai_family
= AF_INET
;
195 } else if (!strcmp(family
, "inet6")) {
196 hints
.ai_family
= AF_INET6
;
198 return luaL_argerror(L
, 2, "supported values: any, inet, inet6");
201 hints
.ai_socktype
= 0;
202 hints
.ai_protocol
= 0;
204 int aistat
= getaddrinfo(host
, port
, &hints
, &result
);
207 lua_pushinteger(L
, aistat
);
208 lua_pushstring(L
, gai_strerror(aistat
));
212 /* create socket object */
216 for (rp
= result
; rp
!= NULL
; rp
= rp
->ai_next
) {
217 /* avoid duplicate results */
219 if (!port
&& rp
->ai_socktype
!= SOCK_STREAM
) {
224 if (rp
->ai_family
== AF_INET
|| rp
->ai_family
== AF_INET6
) {
225 lua_createtable(L
, 0, port
? 4 : 2);
226 if (rp
->ai_family
== AF_INET
) {
227 lua_pushliteral(L
, "inet");
228 } else if (rp
->ai_family
== AF_INET6
) {
229 lua_pushliteral(L
, "inet6");
231 lua_setfield(L
, -2, "family");
234 switch (rp
->ai_socktype
) {
236 lua_pushliteral(L
, "stream");
239 lua_pushliteral(L
, "dgram");
242 lua_pushliteral(L
, "raw");
248 lua_setfield(L
, -2, "socktype");
252 if (nixio__addr_parse(&addr
, rp
->ai_addr
)) {
253 freeaddrinfo(result
);
254 return nixio__perror_s(L
);
258 lua_pushinteger(L
, addr
.port
);
259 lua_setfield(L
, -2, "port");
262 lua_pushstring(L
, addr
.host
);
263 lua_setfield(L
, -2, "address");
264 lua_rawseti(L
, -2, i
++);
268 freeaddrinfo(result
);
274 * getnameinfo(address, family)
276 static int nixio_getnameinfo(lua_State
*L
) {
277 const char *ip
= luaL_checkstring(L
, 1);
278 const char *family
= luaL_optstring(L
, 2, NULL
);
279 char host
[NI_MAXHOST
];
281 struct sockaddr_storage saddr
;
283 memset(&addr
, 0, sizeof(addr
));
284 strncpy(addr
.host
, ip
, sizeof(addr
.host
) - 1);
287 addr
.family
= AF_UNSPEC
;
288 } else if (!strcmp(family
, "inet")) {
289 addr
.family
= AF_INET
;
290 } else if (!strcmp(family
, "inet6")) {
291 addr
.family
= AF_INET6
;
293 return luaL_argerror(L
, 2, "supported values: inet, inet6");
296 nixio__addr_write(&addr
, (struct sockaddr
*)&saddr
);
298 int res
= getnameinfo((struct sockaddr
*)&saddr
, sizeof(saddr
),
299 host
, sizeof(host
), NULL
, 0, NI_NAMEREQD
);
302 lua_pushinteger(L
, res
);
303 lua_pushstring(L
, gai_strerror(res
));
306 lua_pushstring(L
, host
);
314 static int nixio_sock_getsockname(lua_State
*L
) {
315 int sockfd
= nixio__checksockfd(L
);
316 struct sockaddr_storage saddr
;
317 socklen_t addrlen
= sizeof(saddr
);
320 if (getsockname(sockfd
, (struct sockaddr
*)&saddr
, &addrlen
) ||
321 nixio__addr_parse(&addr
, (struct sockaddr
*)&saddr
)) {
322 return nixio__perror_s(L
);
325 lua_pushstring(L
, addr
.host
);
326 lua_pushinteger(L
, addr
.port
);
333 static int nixio_sock_getpeername(lua_State
*L
) {
334 int sockfd
= nixio__checksockfd(L
);
335 struct sockaddr_storage saddr
;
336 socklen_t addrlen
= sizeof(saddr
);
339 if (getpeername(sockfd
, (struct sockaddr
*)&saddr
, &addrlen
) ||
340 nixio__addr_parse(&addr
, (struct sockaddr
*)&saddr
)) {
341 return nixio__perror_s(L
);
344 lua_pushstring(L
, addr
.host
);
345 lua_pushinteger(L
, addr
.port
);
349 #if defined(__linux__) || defined(BSD)
355 static int nixio_getifaddrs(lua_State
*L
) {
357 struct ifaddrs
*ifaddr
, *c
;
358 if (getifaddrs(&ifaddr
) == -1) {
359 return nixio__perror(L
);
365 for (c
= ifaddr
; c
; c
= c
->ifa_next
) {
368 lua_pushstring(L
, c
->ifa_name
);
369 lua_setfield(L
, -2, "name");
371 lua_createtable(L
, 0, 7);
372 lua_pushboolean(L
, c
->ifa_flags
& IFF_UP
);
373 lua_setfield(L
, -2, "up");
375 lua_pushboolean(L
, c
->ifa_flags
& IFF_BROADCAST
);
376 lua_setfield(L
, -2, "broadcast");
378 lua_pushboolean(L
, c
->ifa_flags
& IFF_LOOPBACK
);
379 lua_setfield(L
, -2, "loopback");
381 lua_pushboolean(L
, c
->ifa_flags
& IFF_POINTOPOINT
);
382 lua_setfield(L
, -2, "pointtopoint");
384 lua_pushboolean(L
, c
->ifa_flags
& IFF_NOARP
);
385 lua_setfield(L
, -2, "noarp");
387 lua_pushboolean(L
, c
->ifa_flags
& IFF_PROMISC
);
388 lua_setfield(L
, -2, "promisc");
390 lua_pushboolean(L
, c
->ifa_flags
& IFF_MULTICAST
);
391 lua_setfield(L
, -2, "multicast");
392 lua_setfield(L
, -2, "flags");
394 if (c
->ifa_addr
&& !nixio__addr_parse(&addr
, c
->ifa_addr
)) {
395 lua_pushstring(L
, addr
.host
);
396 lua_setfield(L
, -2, "addr");
398 if (c
->ifa_addr
->sa_family
== AF_INET
) {
399 lua_pushliteral(L
, "inet");
400 } else if (c
->ifa_addr
->sa_family
== AF_INET6
) {
401 lua_pushliteral(L
, "inet6");
403 } else if (c
->ifa_addr
->sa_family
== AF_PACKET
) {
404 lua_pushliteral(L
, "packet");
407 lua_pushliteral(L
, "unknown");
409 lua_setfield(L
, -2, "family");
412 if (c
->ifa_addr
->sa_family
== AF_PACKET
) {
413 lua_pushinteger(L
, addr
.port
);
414 lua_setfield(L
, -2, "ifindex");
416 lua_pushinteger(L
, addr
.prefix
);
417 lua_setfield(L
, -2, "hatype");
420 lua_createtable(L
, 0, 10);
421 struct nixio__nds
*stats
= c
->ifa_data
;
423 lua_pushnumber(L
, stats
->rx_packets
);
424 lua_setfield(L
, -2, "rx_packets");
426 lua_pushnumber(L
, stats
->tx_packets
);
427 lua_setfield(L
, -2, "tx_packets");
429 lua_pushnumber(L
, stats
->rx_bytes
);
430 lua_setfield(L
, -2, "rx_bytes");
432 lua_pushnumber(L
, stats
->tx_bytes
);
433 lua_setfield(L
, -2, "tx_bytes");
435 lua_pushnumber(L
, stats
->rx_errors
);
436 lua_setfield(L
, -2, "rx_errors");
438 lua_pushnumber(L
, stats
->tx_errors
);
439 lua_setfield(L
, -2, "tx_errors");
441 lua_pushnumber(L
, stats
->rx_dropped
);
442 lua_setfield(L
, -2, "rx_dropped");
444 lua_pushnumber(L
, stats
->tx_dropped
);
445 lua_setfield(L
, -2, "tx_dropped");
447 lua_pushnumber(L
, stats
->multicast
);
448 lua_setfield(L
, -2, "multicast");
450 lua_pushnumber(L
, stats
->collisions
);
451 lua_setfield(L
, -2, "collisions");
455 lua_setfield(L
, -2, "data");
460 if (c
->ifa_netmask
&& !nixio__addr_parse(&addr
, c
->ifa_netmask
)) {
461 lua_pushstring(L
, addr
.host
);
462 lua_setfield(L
, -2, "netmask");
464 lua_pushinteger(L
, nixio__addr_prefix(c
->ifa_netmask
));
465 lua_setfield(L
, -2, "prefix");
468 if (c
->ifa_broadaddr
&& !nixio__addr_parse(&addr
, c
->ifa_broadaddr
)) {
469 lua_pushstring(L
, addr
.host
);
470 lua_setfield(L
, -2, "broadaddr");
473 if (c
->ifa_dstaddr
&& !nixio__addr_parse(&addr
, c
->ifa_dstaddr
)) {
474 lua_pushstring(L
, addr
.host
);
475 lua_setfield(L
, -2, "dstaddr");
478 lua_rawseti(L
, -2, i
++);
488 static const luaL_reg R
[] = {
489 #if defined(__linux__) || defined(BSD)
490 {"getifaddrs", nixio_getifaddrs
},
492 {"getaddrinfo", nixio_getaddrinfo
},
493 {"getnameinfo", nixio_getnameinfo
},
498 static const luaL_reg M
[] = {
499 {"getsockname", nixio_sock_getsockname
},
500 {"getpeername", nixio_sock_getpeername
},
504 void nixio_open_address(lua_State
*L
) {
505 luaL_register(L
, NULL
, R
);
507 lua_pushvalue(L
, -2);
508 luaL_register(L
, NULL
, M
);