Fix language selection broken after po file merges
[project/luci.git] / libs / luanet / src / bridge.c
1 /*
2  *  Licensed under the Apache License, Version 2.0 (the "License");
3  *  you may not use this file except in compliance with the License.
4  *  You may obtain a copy of the License at
5  *
6  *      http://www.apache.org/licenses/LICENSE-2.0
7  *
8  *  Unless required by applicable law or agreed to in writing, software
9  *  distributed under the License is distributed on an "AS IS" BASIS,
10  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  *  See the License for the specific language governing permissions and
12  *  limitations under the License.
13  *
14  *   Copyright (C) 2008 John Crispin <blogic@openwrt.org> 
15  *   Copyright (C) 2008 Steven Barth <steven@midlink.org>
16  */
17
18 #include <string.h>
19 #include <net/if.h>
20 #include <linux/sockios.h>
21 #include <dirent.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/ioctl.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <lua.h>
30 #include <lualib.h>
31 #include <lauxlib.h>
32
33 static int sock_bridge = 0;
34 int bridge_startup()
35 {
36         if(!sock_bridge)
37                 sock_bridge = socket(AF_LOCAL, SOCK_STREAM, 0);
38         return sock_bridge;
39 }
40
41 void bridge_shutdown(void)
42 {
43         if(!sock_bridge)
44                 return;
45         close(sock_bridge);
46         sock_bridge = 0;
47 }
48
49 static inline int _bridge_new(lua_State *L, int i)
50 {
51         char *ifname;
52         if(lua_gettop(L) != 1)
53         {
54                 lua_pushstring(L, "invalid arg list");
55                 lua_error(L);
56                 return 0;
57         }
58
59         ifname = (char *)lua_tostring (L, 1);
60         if(!ioctl(sock_bridge, i, ifname))
61                 lua_pushboolean(L, 1);
62         else
63                 lua_pushboolean(L, 0);
64         return 1;
65 }
66
67 int bridge_new(lua_State *L)
68 {
69         return _bridge_new(L, SIOCBRADDBR);
70 }
71
72 int bridge_del(lua_State *L)
73 {
74         return _bridge_new(L, SIOCBRDELBR);
75 }
76
77 static inline int _bridge_addif(lua_State *L, int i)
78 {
79         struct ifreq ifr;
80         char *br, *ifname;
81         if(lua_gettop(L) != 2)
82         {
83                 lua_pushstring(L, "invalid arg list");
84                 lua_error(L);
85                 return 0;
86         }
87         br = (char *)lua_tostring (L, 1);
88         ifname = (char *)lua_tostring (L, 2);
89         strncpy(ifr.ifr_name, br, IFNAMSIZ);
90         ifr.ifr_ifindex = if_nametoindex(ifname);
91         if(ifr.ifr_ifindex == 0)
92         {
93                 lua_pushboolean(L, 0);
94                 return 1;
95         }
96         if(!ioctl(sock_bridge, i, &ifr))
97                 lua_pushboolean(L, 1);
98         else
99                 lua_pushboolean(L, 0);
100         return 1;
101 }
102
103 int bridge_addif(lua_State *L)
104 {
105         return _bridge_addif(L, SIOCBRADDIF);
106 }
107
108 int bridge_delif(lua_State *L)
109 {
110         return _bridge_addif(L, SIOCBRDELIF);
111 }
112
113 #define SYSFS_PATH_MAX  512
114 #define SYSFS_CLASS_NET "/sys/class/net/"
115 static int isbridge(const struct dirent *entry)
116 {
117         char path[SYSFS_PATH_MAX];
118         struct stat st;
119
120         snprintf(path, SYSFS_PATH_MAX, SYSFS_CLASS_NET "%s/bridge", entry->d_name);
121         return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
122 }
123
124 static int isdev(const struct dirent *entry)
125 {
126         if(*entry->d_name == '.')
127                 return 0;
128         return 1;
129 }
130
131 static inline void bridge_getifs(lua_State *L, const char *ifname)
132 {
133         struct dirent **namelist;
134         int i, count = 0;
135         char path[SYSFS_PATH_MAX];
136         snprintf(path, SYSFS_PATH_MAX, SYSFS_CLASS_NET "%s/brif", ifname);
137         count = scandir(path, &namelist, isdev, alphasort);
138         if(count < 0)
139                 return;
140
141         for(i = 0; i < count; i++)
142         {
143                 lua_pushinteger(L, i + 1);
144                 lua_pushstring(L, namelist[i]->d_name);
145                 lua_settable(L, -3);
146                 free(namelist[i]);
147         }
148         free(namelist);
149         return;
150 }
151
152 int bridge_getall(lua_State *L)
153 {
154         struct dirent **namelist;
155         int i, count = 0;
156         count = scandir(SYSFS_CLASS_NET, &namelist, isbridge, alphasort);
157         if (count < 0)
158                 return 0;
159
160         lua_newtable(L);
161         for (i = 0; i < count; i++)
162         {
163                 lua_pushstring(L, namelist[i]->d_name);
164                 lua_newtable(L);
165                 bridge_getifs(L, namelist[i]->d_name);
166                 free(namelist[i]);
167                 lua_settable(L, -3);
168         }
169         free(namelist);
170         return 1;
171 }