summaryrefslogtreecommitdiff
path: root/accel-pppd/ctrl/ipoe/lua.c
blob: 4fb6380a3fb5b2e42de617bd348ffa2b9fd28a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

/* Include the Lua API header files. */
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include "events.h"
#include "log.h"
#include "utils.h"

#include "ipoe.h"

#include "memdebug.h"

#define IPOE_PACKET4 "ipoe.packet4"

static const char *conf_filename;
static int serial;
static int file_error;

static __thread lua_State *L;
static __thread int __serial;
static pthread_key_t __key;

static int packet4_hdr(lua_State *L);
static int packet4_ifname(lua_State *L);
static int packet4_option(lua_State *L);
static int packet4_options(lua_State *L);
static int packet4_agent_circuit_id(lua_State *L);
static int packet4_agent_remote_id(lua_State *L);

int luaopen_lpack(lua_State *L);

static const struct luaL_reg packet4_lib [] = {
	{"hdr", packet4_hdr},
	{"ifname", packet4_ifname},
	{"option", packet4_option},
	{"options", packet4_options},
	{"agent_circuit_id", packet4_agent_circuit_id},
	{"agent_remote_id", packet4_agent_remote_id},
	{NULL, NULL}
};

static int luaopen_packet4(lua_State *L)
{
  luaL_newmetatable(L, IPOE_PACKET4);

	lua_pushstring(L, "__index");
	lua_pushvalue(L, -2);  /* pushes the metatable */
	lua_settable(L, -3);  /* metatable.__index = metatable */


	luaI_openlib(L, NULL, packet4_lib, 0);

  luaI_openlib(L, "packet4", packet4_lib, 0);

	return 1;
}

static int packet4_hdr(lua_State *L)
{
	struct ipoe_session *ses = luaL_checkudata(L, 1, IPOE_PACKET4);
	const char *name = luaL_checkstring(L, 2);
	char str[20];
	uint8_t *ptr;

	if (!ses || !ses->dhcpv4_request)
		return 0;
	
	if (!strcmp(name, "xid"))
		lua_pushinteger(L, ses->dhcpv4_request->hdr->xid);
	else if (!strcmp(name, "ciaddr")) {
		u_inet_ntoa(ses->dhcpv4_request->hdr->ciaddr, str);
		lua_pushstring(L, str);
	} else if (!strcmp(name, "giaddr")) {
		u_inet_ntoa(ses->dhcpv4_request->hdr->giaddr, str);
		lua_pushstring(L, str);
	} else if (!strcmp(name, "chaddr")) {
		ptr = ses->dhcpv4_request->hdr->chaddr;
		sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
			ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
		lua_pushstring(L, str);
	}

	return 1;
}

static int packet4_ifname(lua_State *L)
{
	struct ipoe_session *ses = luaL_checkudata(L, 1, IPOE_PACKET4);

	if (!ses)
		return 0;
	
	lua_pushstring(L, ses->serv->ifname);
	
	return 1;
}

static int packet4_option(lua_State *L)
{
	struct ipoe_session *ses = luaL_checkudata(L, 1, IPOE_PACKET4);
	int type = luaL_checkinteger(L, 2);
	struct dhcpv4_option *opt;

	if (ses && ses->dhcpv4_request) {
		list_for_each_entry(opt, &ses->dhcpv4_request->options, entry) {
			if (opt->type == type) {
				lua_pushlstring(L, (char *)opt->data, opt->len);
				return 1;
			}
		}
	}

	lua_pushnil(L);

	return 1;
}

static int packet4_options(lua_State *L)
{
	struct ipoe_session *ses = luaL_checkudata(L, 1, IPOE_PACKET4);
	struct dhcpv4_option *opt;
	int i = 1;

	if (!ses || !ses->dhcpv4_request)
		return 0;
	
	lua_newtable(L);

	list_for_each_entry(opt, &ses->dhcpv4_request->options, entry) {
		lua_pushinteger(L, opt->type);
		lua_rawseti(L, -2, i++);
	}

	return 1;
}

static int packet4_agent_circuit_id(lua_State *L)
{
	struct ipoe_session *ses = luaL_checkudata(L, 1, IPOE_PACKET4);

	if (!ses || !ses->dhcpv4_request)
		return 0;
	
	if (ses->agent_circuit_id)
		lua_pushlstring(L, (char *)(ses->agent_circuit_id + 1), *ses->agent_circuit_id);
	else
		lua_pushnil(L);
	
	return 1;
}

static int packet4_agent_remote_id(lua_State *L)
{
	struct ipoe_session *ses = luaL_checkudata(L, 1, IPOE_PACKET4);

	if (!ses || !ses->dhcpv4_request)
		return 0;
	
	if (ses->agent_remote_id)
		lua_pushlstring(L, (char *)(ses->agent_remote_id + 1), *ses->agent_remote_id);
	else
		lua_pushnil(L);
	
	return 1;
}

static void init_lua()
{
	__serial = serial;

	L = lua_open();

	luaL_openlibs(L);

	luaopen_lpack(L);
	luaopen_packet4(L);

	if (luaL_loadfile(L, conf_filename))
		goto out_err;

	if (lua_pcall(L, 0, 0, 0))
		goto out_err;

	lua_pushlightuserdata(L, L);
	luaL_getmetatable(L, IPOE_PACKET4);
	lua_setmetatable(L, -2);

	lua_settop(L, 0);

	file_error = 0;

	pthread_setspecific(__key, L);

	return;

out_err:
	file_error = 1;
	log_ppp_error("ipoe: lua: %s\n", lua_tostring(L, -1));
	lua_close(L);
	L = NULL;
	pthread_setspecific(__key, L);
}

/*static void stackDump (lua_State *L) {
          int i=lua_gettop(L);
          printf(" ----------------  Stack Dump ----------------" );
          while(  i   ) {
            int t = lua_type(L, i);
            switch (t) {
              case LUA_TSTRING:
                printf("%d:`%s'\n", i, lua_tostring(L, i));
              break;
              case LUA_TBOOLEAN:
                printf("%d: %s\n",i,lua_toboolean(L, i) ? "true" : "false");
              break;
              case LUA_TNUMBER:
                printf("%d: %g\n",  i, lua_tonumber(L, i));
             break;
             default: printf("%d: %s\n", i, lua_typename(L, t)); break;
            }
           i--;
          }
         printf("--------------- Stack Dump Finished ---------------" );
    }*/


int ipoe_lua_set_username(struct ipoe_session *ses, const char *func)
{
	if (file_error && serial == __serial)
		return -1;

	if (L && serial != __serial) {
		lua_close(L);
		init_lua();
	} else if (!L)
		init_lua();

	if (!L)
		return -1;

	lua_getglobal(L, func);
	lua_pushlightuserdata(L, ses);

	if (lua_pcall(L, 1, 1, 0)) {
		log_ppp_error("ipoe: lua: %s\n", lua_tostring(L, -1));
		goto out_err;
	}

	if (!lua_isstring(L, -1)) {
		log_ppp_error("ipoe: lua: function '%s' must return a string\n", func);
		goto out_err;
	}

	ses->ses.username = _strdup(lua_tostring(L, -1));

	lua_settop(L, 0);

	return 0;
	
out_err:
	file_error = 1;
	lua_close(L);
	L = NULL;
	pthread_setspecific(__key, L);
	return -1;
}

static void load_config()
{
	conf_filename = conf_get_opt("ipoe", "lua-file");

	serial++;
}

static void init()
{
	load_config();

	pthread_key_create(&__key, (void (*)(void *))lua_close);

	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
}

DEFINE_INIT(100, init);