gcc -I ./ -MMD -c -g -o wpa_ctrl.o wpa_ctrl.c -D CONFIG_CTRL_IFACE -D CONFIG_CTRL_IFACE_UNIX
gcc -I ./ -MMD -c -g -o os_unix.o os_unix.c -D CONFIG_CTRL_IFACE -D CONFIG_CTRL_IFACE_UNIX
gcc -Wall -Wextra -shared -o wpaLua.so wpaLua.c wpa_ctrl.o os_unix.o -llua5.1
wpaLua.c my binding:
/* Attempt at binding wpa_supplicant contral interface to
* lua.
* Psuedo coded first, then trial and error, as I am noob.
* Brian Gifford 2013
*/
#include <lua5.1/lua.h>
#include <lua5.1/lauxlib.h>
#include <lua5.1/lualib.h>
#include "wpa_ctrl.h"
/* Pulled directly from the header the initials */
/* wpa_supplicant/hostapd control interface access */
/**
* wpa_ctrl_open - Open a control interface to wpa_supplicant/hostapd
* @ctrl_path: Path for UNIX domain sockets; ignored if UDP sockets are used.
* Returns: Pointer to abstract control interface data or %NULL on failure
*
* This function is used to open a control interface to wpa_supplicant/hostapd.
* ctrl_path is usually /var/run/wpa_supplicant or /var/run/hostapd. This path
* is configured in wpa_supplicant/hostapd and other programs using the control
* interface need to use matching path configuration.
*/
/* struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path); */
static int ctrl_open(lua_State *L)
{
const char *path = lua_tostring(L, 1);
/* control path find in lua check in lua */
struct wpa_ctrl *l_ctrl = wpa_ctrl_open(path);
/* if l_ctrl == NULL; *DO IN LUA* */
lua_pushlightuserdata(L, l_ctrl);
return 1;
}
/**
* wpa_ctrl_close - Close a control interface to wpa_supplicant/hostapd
* @ctrl: Control interface data from wpa_ctrl_open()
*
* This function is used to close a control interface.
*/
/* void wpa_ctrl_close(struct wpa_ctrl *ctrl); */
static int ctrl_close(lua_State *L)
{
struct wpa_ctrl * l_ctrl = lua_touserdata(L, 1);
wpa_ctrl_close(l_ctrl);
return 0;
}
/**
* wpa_ctrl_request - Send a command to wpa_supplicant/hostapd
* @ctrl: Control interface data from wpa_ctrl_open()
* @cmd: Command; usually, ASCII text, e.g., "PING"
* @cmd_len: Length of the cmd in bytes
* @reply: Buffer for the response
* @reply_len: Reply buffer length
* @msg_cb: Callback function for unsolicited messages or %NULL if not used
* Returns: 0 on success, -1 on error (send or receive failed), -2 on timeout
*
* This function is used to send commands to wpa_supplicant/hostapd. Received
* response will be written to reply and reply_len is set to the actual length
* of the reply. This function will block for up to two seconds while waiting
* for the reply. If unsolicited messages are received, the blocking time may
* be longer.
*
* msg_cb can be used to register a callback function that will be called for
* unsolicited messages received while waiting for the command response. These
* messages may be received if wpa_ctrl_request() is called at the same time as
* wpa_supplicant/hostapd is sending such a message. This can happen only if
* the program has used wpa_ctrl_attach() to register itself as a monitor for
* event messages. Alternatively to msg_cb, programs can register two control
* interface connections and use one of them for commands and the other one for
* receiving event messages, in other words, call wpa_ctrl_attach() only for
* the control interface connection that will be used for event messages.
*/
/* int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
char *reply, size_t *reply_len,
void (*msg_cb)(char *msg, size_t len)); */
static int ctrl_req(lua_State *L)
{
struct wpa_ctrl *l_ctrl = lua_touserdata(L, 1);
const char *cmd = lua_tostring(L, 2);
size_t cmd_len = lua_objlen(L, 2);
char reply[4096];
size_t reply_len = sizeof(reply) - 1;
/* Test if valid command in lua */
lua_Integer err = wpa_ctrl_request(l_ctrl, cmd, cmd_len, reply, &reply_len, NULL);
if (err != 0)
lua_pushinteger(L, err);
else
lua_pushlstring(L, reply, reply_len);
return 1;
}
/**
* wpa_ctrl_attach - Register as an event monitor for the control interface
* @ctrl: Control interface data from wpa_ctrl_open()
* Returns: 0 on success, -1 on failure, -2 on timeout
*
* This function registers the control interface connection as a monitor for
* wpa_supplicant/hostapd events. After a success wpa_ctrl_attach() call, the
* control interface connection starts receiving event messages that can be
* read with wpa_ctrl_recv().
*/
/* int wpa_ctrl_attach(struct wpa_ctrl *ctrl); */
static int ctrl_attach(lua_State *L)
{
struct wpa_ctrl *l_ctrl = lua_touserdata(L, 1);
lua_Integer err = wpa_ctrl_attach(l_ctrl);
lua_pushinteger(L, err);
return 1;
}
/**
* wpa_ctrl_detach - Unregister event monitor from the control interface
* @ctrl: Control interface data from wpa_ctrl_open()
* Returns: 0 on success, -1 on failure, -2 on timeout
*
* This function unregisters the control interface connection as a monitor for
* wpa_supplicant/hostapd events, i.e., cancels the registration done with
* wpa_ctrl_attach().
*/
/* int wpa_ctrl_detach(struct wpa_ctrl *ctrl); */
static int ctrl_detach(lua_State *L)
{
struct wpa_ctrl *l_ctrl = lua_touserdata(L, 1);
lua_Integer err = wpa_ctrl_detach(l_ctrl);
lua_pushinteger(L, err);
return 1;
}
/**
* wpa_ctrl_recv - Receive a pending control interface message
* @ctrl: Control interface data from wpa_ctrl_open()
* @reply: Buffer for the message data
* @reply_len: Length of the reply buffer
* Returns: 0 on success, -1 on failure
*
* This function will receive a pending control interface message. This
* function will block if no messages are available. The received response will
* be written to reply and reply_len is set to the actual length of the reply.
* wpa_ctrl_recv() is only used for event messages, i.e., wpa_ctrl_attach()
* must have been used to register the control interface as an event monitor.
*/
/* int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len); */
static int ctrl_recv(lua_State *L)
{
struct wpa_ctrl *l_ctrl = lua_touserdata(L, 1);
char reply[256];
size_t reply_len = sizeof(reply) - 1;
lua_Integer err = wpa_ctrl_recv(l_ctrl, reply, &reply_len);
if (err != 0)
lua_pushinteger(L, err);
else
lua_pushlstring(L, reply, reply_len);
return 1;
}
/**
* wpa_ctrl_pending - Check whether there are pending event messages
* @ctrl: Control interface data from wpa_ctrl_open()
* Returns: 1 if there are pending messages, 0 if no, or -1 on error
*
* This function will check whether there are any pending control interface
* message available to be received with wpa_ctrl_recv(). wpa_ctrl_pending() is
* only used for event messages, i.e., wpa_ctrl_attach() must have been used to
* register the control interface as an event monitor.
*/
/* int wpa_ctrl_pending(struct wpa_ctrl *ctrl); */
static int ctrl_pending(lua_State *L)
{
struct wpa_ctrl *l_ctrl = lua_touserdata(L, 1);
lua_Integer err = wpa_ctrl_pending(l_ctrl);
lua_pushinteger(L, err);
return 1;
}
/**
* wpa_ctrl_get_fd - Get file descriptor used by the control interface
* @ctrl: Control interface data from wpa_ctrl_open()
* Returns: File descriptor used for the connection
*
* This function can be used to get the file descriptor that is used for the
* control interface connection. The returned value can be used, e.g., with
* select() while waiting for multiple events.
*
* The returned file descriptor must not be used directly for sending or
* receiving packets; instead, the library functions wpa_ctrl_request() and
* wpa_ctrl_recv() must be used for this.
*/
/* int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl); */
static int ctrl_get_fd(lua_State *L)
{
struct wpa_ctrl *l_ctrl = lua_touserdata(L, 1);
lua_Integer fd = wpa_ctrl_get_fd(l_ctrl);
lua_pushinteger(L, fd);
return 1;
}
/* char * wpa_ctrl_get_remote_ifname(struct wpa_ctrl *ctrl); */
/* static int ctrl_get_remote_ifname(lua_State *L)
{
struct wpa_ctrl *l_ctrl = lua_touserdata(L, 1);
char *ifname = wpa_ctrl_get_remote_ifname(l_ctrl);
lua_pushstring(L, ifname);
return 1;
} */
static const luaL_Reg wpaLua [] =
{
{"open", ctrl_open},
{"close", ctrl_close},
{"request", ctrl_req},
{"attach", ctrl_attach},
{"detach", ctrl_detach},
{"receive", ctrl_recv},
{"pending", ctrl_pending},
{"filedesc", ctrl_get_fd},
/* {"ifname", ctrl_get_remote_ifname}, */
{NULL, NULL}
};
int luaopen_wpaLua(lua_State *L)
{
luaL_register(L, "wpaLua", wpaLua);
return 1;
}
--
Brian Gifford
"Deep magic begins here ... " - anonymous
"Professionals do not complain about things, they just consider them a challenge." - Brian D. Gifford