Comparison

util-src/net.c @ 7889:b8d694646597

util-src/*.c: Attach pointer * to name instead of type
author Kim Alvefur <zash@zash.se>
date Sun, 12 Feb 2017 16:42:29 +0100
parent 7818:54669df178c2
child 7931:b619b85e01aa
comparison
equal deleted inserted replaced
7888:74187ee6ed55 7889:b8d694646597
30 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) 30 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
31 #endif 31 #endif
32 32
33 /* Enumerate all locally configured IP addresses */ 33 /* Enumerate all locally configured IP addresses */
34 34
35 const char* const type_strings[] = { 35 const char *const type_strings[] = {
36 "both", 36 "both",
37 "ipv4", 37 "ipv4",
38 "ipv6", 38 "ipv6",
39 NULL 39 NULL
40 }; 40 };
41 41
42 static int lc_local_addresses(lua_State* L) { 42 static int lc_local_addresses(lua_State *L) {
43 #ifndef _WIN32 43 #ifndef _WIN32
44 /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */ 44 /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */
45 const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */ 45 const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */
46 const long ip4_mask = htonl(0xffff0000); 46 const long ip4_mask = htonl(0xffff0000);
47 struct ifaddrs* addr = NULL, *a; 47 struct ifaddrs *addr = NULL, *a;
48 #endif 48 #endif
49 int n = 1; 49 int n = 1;
50 int type = luaL_checkoption(L, 1, "both", type_strings); 50 int type = luaL_checkoption(L, 1, "both", type_strings);
51 const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */ 51 const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */
52 const char ipv4 = (type == 0 || type == 1); 52 const char ipv4 = (type == 0 || type == 1);
67 #ifndef _WIN32 67 #ifndef _WIN32
68 68
69 for(a = addr; a; a = a->ifa_next) { 69 for(a = addr; a; a = a->ifa_next) {
70 int family; 70 int family;
71 char ipaddr[INET6_ADDRSTRLEN]; 71 char ipaddr[INET6_ADDRSTRLEN];
72 const char* tmp = NULL; 72 const char *tmp = NULL;
73 73
74 if(a->ifa_addr == NULL || a->ifa_flags & IFF_LOOPBACK) { 74 if(a->ifa_addr == NULL || a->ifa_flags & IFF_LOOPBACK) {
75 continue; 75 continue;
76 } 76 }
77 77
78 family = a->ifa_addr->sa_family; 78 family = a->ifa_addr->sa_family;
79 79
80 if(ipv4 && family == AF_INET) { 80 if(ipv4 && family == AF_INET) {
81 struct sockaddr_in* sa = (struct sockaddr_in*)a->ifa_addr; 81 struct sockaddr_in *sa = (struct sockaddr_in *)a->ifa_addr;
82 82
83 if(!link_local && ((sa->sin_addr.s_addr & ip4_mask) == ip4_linklocal)) { 83 if(!link_local && ((sa->sin_addr.s_addr & ip4_mask) == ip4_linklocal)) {
84 continue; 84 continue;
85 } 85 }
86 86
87 tmp = inet_ntop(family, &sa->sin_addr, ipaddr, sizeof(ipaddr)); 87 tmp = inet_ntop(family, &sa->sin_addr, ipaddr, sizeof(ipaddr));
88 } else if(ipv6 && family == AF_INET6) { 88 } else if(ipv6 && family == AF_INET6) {
89 struct sockaddr_in6* sa = (struct sockaddr_in6*)a->ifa_addr; 89 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)a->ifa_addr;
90 90
91 if(!link_local && IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { 91 if(!link_local && IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) {
92 continue; 92 continue;
93 } 93 }
94 94
122 122
123 #endif 123 #endif
124 return 1; 124 return 1;
125 } 125 }
126 126
127 int luaopen_util_net(lua_State* L) { 127 int luaopen_util_net(lua_State *L) {
128 #if (LUA_VERSION_NUM > 501) 128 #if (LUA_VERSION_NUM > 501)
129 luaL_checkversion(L); 129 luaL_checkversion(L);
130 #endif 130 #endif
131 luaL_Reg exports[] = { 131 luaL_Reg exports[] = {
132 { "local_addresses", lc_local_addresses }, 132 { "local_addresses", lc_local_addresses },