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
6 * http://www.apache.org/licenses/LICENSE-2.0
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.
14 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
15 * Copyright (C) 2008 Steven Barth <steven@midlink.org>
20 #include <linux/sockios.h>
22 #include <sys/types.h>
24 #include <sys/ioctl.h>
33 static int sock_bridge
= 0;
37 sock_bridge
= socket(AF_LOCAL
, SOCK_STREAM
, 0);
41 void bridge_shutdown(void)
49 static inline int _bridge_new(lua_State
*L
, int i
)
52 if(lua_gettop(L
) != 1)
54 lua_pushstring(L
, "invalid arg list");
59 ifname
= (char *)lua_tostring (L
, 1);
60 if(!ioctl(sock_bridge
, i
, ifname
))
61 lua_pushboolean(L
, 1);
63 lua_pushboolean(L
, 0);
67 int bridge_new(lua_State
*L
)
69 return _bridge_new(L
, SIOCBRADDBR
);
72 int bridge_del(lua_State
*L
)
74 return _bridge_new(L
, SIOCBRDELBR
);
77 static inline int _bridge_addif(lua_State
*L
, int i
)
81 if(lua_gettop(L
) != 2)
83 lua_pushstring(L
, "invalid arg list");
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)
93 lua_pushboolean(L
, 0);
96 if(!ioctl(sock_bridge
, i
, &ifr
))
97 lua_pushboolean(L
, 1);
99 lua_pushboolean(L
, 0);
103 int bridge_addif(lua_State
*L
)
105 return _bridge_addif(L
, SIOCBRADDIF
);
108 int bridge_delif(lua_State
*L
)
110 return _bridge_addif(L
, SIOCBRDELIF
);
113 #define SYSFS_PATH_MAX 512
114 #define SYSFS_CLASS_NET "/sys/class/net/"
115 static int isbridge(const struct dirent
*entry
)
117 char path
[SYSFS_PATH_MAX
];
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
);
124 static int isdev(const struct dirent
*entry
)
126 if(*entry
->d_name
== '.')
131 static inline void bridge_getifs(lua_State
*L
, const char *ifname
)
133 struct dirent
**namelist
;
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
);
141 for(i
= 0; i
< count
; i
++)
143 lua_pushinteger(L
, i
+ 1);
144 lua_pushstring(L
, namelist
[i
]->d_name
);
152 int bridge_getall(lua_State
*L
)
154 struct dirent
**namelist
;
156 count
= scandir(SYSFS_CLASS_NET
, &namelist
, isbridge
, alphasort
);
161 for (i
= 0; i
< count
; i
++)
163 lua_pushstring(L
, namelist
[i
]->d_name
);
165 bridge_getifs(L
, namelist
[i
]->d_name
);