-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.c
345 lines (301 loc) · 8.5 KB
/
socket.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (C) 2018 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup lua
* @{
*
* @file
* @brief Basic UDP sockets.
*
* Lua bindings for UDP sock.
*
* @author Juan Carrano <j.carrano@fu-berlin.de>
*
* @}
*/
#define LUA_LIB
#include "lprefix.h"
#include "net/sock/udp.h"
#include "net/sock/util.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
/* MetaTable names */
#define SOCK_UDP_TNAME "sock_udp"
enum EP_PARSE_RESULT { EP_NULL, EP_PARSED, EP_ERROR};
/**
* Push a nil and a string to the lua stack.
*/
static void _nil_and_str(lua_State *L, const char *s)
{
lua_pushnil(L);
lua_pushstring(L, s);
}
/**
* Get a 16 bit number from a table.
*
* If the table entry is nil, it does nothing and returns EP_NULL.
*
* @return EP_NULL, EP_PARSED on success. EP_ERROR on error.
*/
static enum EP_PARSE_RESULT _field2int(lua_State *L, int index, const char *key,
uint16_t *result)
{
if (lua_getfield (L, index, key) != LUA_TNIL) {
int isnum;
lua_Number pn = lua_tonumberx(L, -1, &isnum);
if (!isnum) {
_nil_and_str(L, "Cannot convert object to number");
return EP_ERROR;
} else if (pn >=0 && pn <= UINT16_MAX){
*result = pn;
lua_pop(L, 1);
return EP_PARSED;
} else {
_nil_and_str(L, "Number off-range (must be 16 bit)");
return EP_ERROR;
}
}
return EP_NULL;
}
/**
* Convert a string or a table into a sock_udp_ep_t.
*
* @param index Index of the string or table in the stack.
*
* @return EP_NULL If the endpoint should be null
* @return EP_PARSED If the endpoint was parsed and the result is in *ep
* @return EP_ERROR If there was an error parsing the endpoint. A nil and
* a message will be pushed to the stack.
*/
static enum EP_PARSE_RESULT _parse_udp_endpoint(lua_State *L, int index, sock_udp_ep_t *ep)
{
switch (lua_type(L, index)) {
case LUA_TNONE: /* falls through */
case LUA_TNIL:
return EP_NULL;
case LUA_TSTRING:
{
const char *s = lua_tolstring(L, index, NULL);
if (sock_udp_str2ep(ep, s) == 0) {
return EP_PARSED;
} else {
_nil_and_str(L, "Address/port badly formatted");
return EP_ERROR;
}
}
break;
default: /* table-like */
ep->port = 0;
ep->netif = SOCK_ADDR_ANY_NETIF;
ep->family = AF_UNSPEC;
if (_field2int(L, index, "port", &ep->port) == EP_ERROR) {
return EP_ERROR;
}
if (_field2int(L, index, "netif", &ep->netif) == EP_ERROR) {
return EP_ERROR;
}
if (lua_getfield (L, index, "address") != LUA_TNIL) {
size_t slen;
const char *addr = lua_tolstring (L, -1, &slen);
ep->family = AF_INET6;
if (!slen
|| ipv6_addr_from_str((ipv6_addr_t*)&ep->addr.ipv6, addr) == NULL) {
_nil_and_str(L, "Address badly formatted");
return EP_ERROR;
}
lua_pop(L, 1);
}
return EP_PARSED;
}
}
/**
* Create a new UDP socket.
*
* @param local Local endpoint (as table or string, can be nil).
* @param remote Remote endpoint (as table or string, can be nil).
* @param flags Additional parameters after the remote endpoint will be
* interpreted as flags. Not implemented yet.
* @return UDP socket object (full userdata with custom metatable)
*/
static int udp_new(lua_State *L)
{
uint16_t flags = 0;
int retval;
sock_udp_ep_t local, remote;
sock_udp_ep_t *plocal, *premote;
switch (_parse_udp_endpoint(L, 1, &local)) {
default:
case EP_NULL:
plocal = NULL;
break;
case EP_PARSED:
plocal = &local;
break;
case EP_ERROR:
return 2; /* 2 return values, a nil and a message */
}
switch (_parse_udp_endpoint(L, 2, &remote)) {
default:
case EP_NULL:
premote = NULL;
break;
case EP_PARSED:
premote = &remote;
break;
case EP_ERROR:
return 2; /* 2 return values, a nil and a message */
}
sock_udp_t *s = lua_newuserdata(L, sizeof(*s));
retval = sock_udp_create(s, plocal, premote, flags);
if (retval != 0) {
lua_pushnil(L);
}
switch (retval) {
case -EINVAL:
lua_pushliteral(L, "Invalid endpoints");
break;
case -EAFNOSUPPORT:
lua_pushliteral(L, "Socket type not supported");
break;
case -EADDRINUSE:
lua_pushliteral(L, "Address in use");
break;
default:
lua_pushliteral(L, "Unknown error");
break;
case 0:
break;
}
if (retval != 0) {
return 2;
}
luaL_setmetatable(L, SOCK_UDP_TNAME);
return 1;
}
/**
* Receive data from a UDP socket.
*
* @param sock
* @param n Receive up to n bytes.
* @param timeout_ms Use 0 to return immediately, -1 for no timeout.
* @param remote (optional) remote end point.
*
* @return Received data as string, or nil+error message.
*/
static int udp_recv(lua_State *L)
{
/* s cannot be NULL */
sock_udp_t *s = luaL_checkudata(L, 1, SOCK_UDP_TNAME);
int n = luaL_checkinteger(L, 2);
int timeout = luaL_checkinteger(L, 3);
sock_udp_ep_t remote, *premote;
switch (_parse_udp_endpoint(L, 4, &remote)) {
default:
case EP_NULL:
premote = NULL;
break;
case EP_PARSED:
premote = &remote;
break;
case EP_ERROR:
return 2; /* 2 return values, a nil and a message */
}
/* This is wasteful: we are allocating an array and then copying the contents
* to a smaller array instead of resizing, but AFAIK it is not possible to
* preallocate and then shrink a string in lua.
*/
void *buf = lua_newuserdata(L, n);
ssize_t nrecv = sock_udp_recv(s, buf, n, timeout, premote);
if (nrecv < 0) {
lua_pop(L, 1);
lua_pushnil(L);
lua_pushliteral(L, "error, i'm to lazy to report properly");
return 2;
} else {
lua_pushlstring(L, buf, nrecv);
lua_replace(L, -2); /*this is to get rid of the userdata */
return 1;
}
}
/**
* Send data through a udp socket.
*
* @param sock
* @param data String containing the data to be sent.
* @param remote (optional) remote end point.
*
* @return number of bytes sent.
*/
static int udp_send(lua_State *L)
{
sock_udp_t *s = luaL_checkudata(L, 1, SOCK_UDP_TNAME);
size_t len;
ssize_t sent;
const char *data = luaL_checklstring(L, 2, &len);
sock_udp_ep_t remote, *premote;
switch (_parse_udp_endpoint(L, 3, &remote)) {
default:
case EP_NULL:
premote = NULL;
break;
case EP_PARSED:
premote = &remote;
break;
case EP_ERROR:
return 2; /* 2 return values, a nil and a message */
}
sent = sock_udp_send(s, data, len, premote);
if (sent < 0) {
lua_pop(L, 1);
lua_pushnil(L);
lua_pushliteral(L, "error, i'm to lazy to report properly");
return 2;
} else {
lua_pushinteger(L, sent);
return 1;
}
}
/**
* Close a UDP socket.
*/
static int udp_close(lua_State *L)
{
/* s cannot be NULL */
sock_udp_t *s = luaL_checkudata(L, 1, SOCK_UDP_TNAME);
sock_udp_close(s);
return 0;
}
static const luaL_Reg udp_methods[] = {
{"close", udp_close},
{"recv", udp_recv},
{"send", udp_send},
{NULL, NULL}
};
static const luaL_Reg funcs[] = {
{"udp", udp_new},
/* placeholders */
{"REUSE_EP", NULL},
{NULL, NULL}
};
/**
* Load the library.
*
* @return Lua table.
*/
int luaopen_socket(lua_State *L)
{
if (luaL_newmetatable(L, SOCK_UDP_TNAME)) {
luaL_newlib(L, udp_methods);
lua_setfield(L, -2, "__index");
}
luaL_newlib(L, funcs);
lua_pushinteger(L, SOCK_FLAGS_REUSE_EP);
lua_setfield(L, -2, "REUSE_EP");
return 1;
}