aboutsummaryrefslogtreecommitdiff
path: root/src/udp.c
blob: 29004fd9882ff9676bcccc6ba907fa5ef9283d35 (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
/*=========================================================================*\
* UDP socket object implementation (inherits from sock and inet)
\*=========================================================================*/
#include <string.h>

#include <lua.h>
#include <lauxlib.h>

#include "lsinet.h"
#include "lsudp.h"
#include "lscompat.h"
#include "lsselect.h"

/*=========================================================================*\
* Internal function prototypes.
\*=========================================================================*/
static int udp_lua_send(lua_State *L);
static int udp_lua_sendto(lua_State *L);
static int udp_lua_receive(lua_State *L);
static int udp_lua_receivefrom(lua_State *L);
static int udp_lua_setpeername(lua_State *L);
static int udp_lua_setsockname(lua_State *L);

static int udp_global_udpsocket(lua_State *L);

static struct luaL_reg funcs[] = {
    {"send", udp_lua_send},
    {"sendto", udp_lua_sendto},
    {"receive", udp_lua_receive},
    {"receivefrom", udp_lua_receivefrom},
    {"setpeername", udp_lua_setpeername},
    {"setsockname", udp_lua_setsockname},
};

/*=========================================================================*\
* Exported functions
\*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
void udp_open(lua_State *L)
{
    unsigned int i;
    priv_newclass(L, UDP_CLASS);
    udp_inherit(L, UDP_CLASS);
    /* declare global functions */
    lua_pushcfunction(L, udp_global_udpsocket);
    priv_newglobal(L, "udp");
    for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++) 
        priv_newglobalmethod(L, funcs[i].name);
    /* make class selectable */
    select_addclass(L, UDP_CLASS);
}

/*-------------------------------------------------------------------------*\
* Hook object methods to methods table.
\*-------------------------------------------------------------------------*/
void udp_inherit(lua_State *L, cchar *lsclass)
{
    unsigned int i;
    inet_inherit(L, lsclass);
    for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++) {
        lua_pushcfunction(L, funcs[i].func);
        priv_setmethod(L, lsclass, funcs[i].name);
    }
}

/*-------------------------------------------------------------------------*\
* Initializes socket structure 
\*-------------------------------------------------------------------------*/
void udp_construct(lua_State *L, p_udp udp)
{
    inet_construct(L, (p_inet) udp);
    udp->udp_connected = 0;
}

/*-------------------------------------------------------------------------*\
* Creates a socket structure and initializes it. A socket object is
* left in the Lua stack.
* Returns
*   pointer to allocated structure
\*-------------------------------------------------------------------------*/
p_udp udp_push(lua_State *L)
{
    p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));
    priv_setclass(L, UDP_CLASS);
    udp_construct(L, udp);
    return udp;
}

/*=========================================================================*\
* Socket table constructors
\*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Creates a udp socket object and returns it to the Lua script. 
* Lua Input: [options]
*   options: socket options table
* Lua Returns
*   On success: udp socket
*   On error: nil, followed by an error message
\*-------------------------------------------------------------------------*/
static int udp_global_udpsocket(lua_State *L)
{
    int oldtop = lua_gettop(L);
    p_udp udp = udp_push(L);
    cchar *err = inet_trysocket((p_inet) udp, SOCK_DGRAM);
    if (err) {
        lua_pushnil(L);
        lua_pushstring(L, err);
        return 2;
    }
    if (oldtop < 1) return 1;
    err = compat_trysetoptions(L, udp->fd);
    if (err) {
        lua_pushnil(L);
        lua_pushstring(L, err);
        return 2;
    }
    return 1;
}

/*=========================================================================*\
* Socket table methods
\*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Receives data from a UDP socket
* Lua Input: sock [, wanted]
*   sock: client socket created by the connect function
*   wanted: the number of bytes expected (default: LUASOCKET_UDPBUFFERSIZE)
* Lua Returns
*   On success: datagram received
*   On error: nil, followed by an error message
\*-------------------------------------------------------------------------*/
static int udp_lua_receive(lua_State *L)
{
    p_udp udp = (p_udp) lua_touserdata(L, 1);
    unsigned char buffer[UDP_DATAGRAMSIZE];
    size_t got, wanted = (size_t) luaL_opt_number(L, 2, sizeof(buffer));
    int err;
    p_tm tm = &udp->base_tm;
    wanted = MIN(wanted, sizeof(buffer));
    tm_markstart(tm);
    err = compat_recv(udp->fd, buffer, wanted, &got, tm_getremaining(tm));
    if (err == PRIV_CLOSED) err = PRIV_REFUSED;
    if (err != PRIV_DONE) lua_pushnil(L);
    else lua_pushlstring(L, buffer, got);
    priv_pusherror(L, err);
    return 2;
}

/*-------------------------------------------------------------------------*\
* Receives a datagram from a UDP socket
* Lua Input: sock [, wanted]
*   sock: client socket created by the connect function
*   wanted: the number of bytes expected (default: LUASOCKET_UDPBUFFERSIZE)
* Lua Returns
*   On success: datagram received, ip and port of sender
*   On error: nil, followed by an error message
\*-------------------------------------------------------------------------*/
static int udp_lua_receivefrom(lua_State *L)
{
    p_udp udp = (p_udp) lua_touserdata(L, 1);
    p_tm tm = &udp->base_tm;
    struct sockaddr_in peer;
    int peer_len = sizeof(peer);
    unsigned char buffer[UDP_DATAGRAMSIZE];
    size_t wanted = (size_t) luaL_opt_number(L, 2, sizeof(buffer));
    size_t got;
    int err;
    if (udp->udp_connected) luaL_error(L, "receivefrom on connected socket");
    tm_markstart(tm);
    wanted = MIN(wanted, sizeof(buffer));
    err = compat_recvfrom(udp->fd, buffer, wanted, &got, tm_getremaining(tm),
            (SA *) &peer, &peer_len);
    if (err == PRIV_CLOSED) err = PRIV_REFUSED;
    if (err == PRIV_DONE) {
        lua_pushlstring(L, buffer, got);
        lua_pushstring(L, inet_ntoa(peer.sin_addr));
        lua_pushnumber(L, ntohs(peer.sin_port));
        return 3;
    } else {
        lua_pushnil(L);
        priv_pusherror(L, err);
        return 2;
    }
}

/*-------------------------------------------------------------------------*\
* Send data through a connected UDP socket
* Lua Input: sock, data
*   sock: udp socket
*   data: data to be sent
* Lua Returns
*   On success: nil, followed by the total number of bytes sent
*   On error: error message
\*-------------------------------------------------------------------------*/
static int udp_lua_send(lua_State *L)
{
    p_udp udp = (p_udp) lua_touserdata(L, 1);
    p_tm tm = &udp->base_tm;
    size_t wanted, sent = 0;
    int err;
    cchar *data = luaL_check_lstr(L, 2, &wanted);
    if (!udp->udp_connected) luaL_error(L, "send on unconnected socket");
    tm_markstart(tm);
    err = compat_send(udp->fd, data, wanted, &sent, tm_getremaining(tm));
    priv_pusherror(L, err == PRIV_CLOSED ? PRIV_REFUSED : err);
    lua_pushnumber(L, sent);
    return 2;
}

/*-------------------------------------------------------------------------*\
* Send data through a unconnected UDP socket
* Lua Input: sock, data, ip, port
*   sock: udp socket
*   data: data to be sent
*   ip: ip address of target
*   port: port in target
* Lua Returns
*   On success: nil, followed by the total number of bytes sent
*   On error: error message
\*-------------------------------------------------------------------------*/
static int udp_lua_sendto(lua_State *L)
{
    p_udp udp = (p_udp) lua_touserdata(L, 1);
    size_t wanted, sent = 0;
    cchar *data = luaL_check_lstr(L, 2, &wanted);
    cchar *ip = luaL_check_string(L, 3);
    ushort port = (ushort) luaL_check_number(L, 4);
    p_tm tm = &udp->base_tm;
    struct sockaddr_in peer;
    int err;
    if (udp->udp_connected) luaL_error(L, "sendto on connected socket");
    memset(&peer, 0, sizeof(peer));
    if (!inet_aton(ip, &peer.sin_addr)) luaL_error(L, "invalid ip address");
    peer.sin_family = AF_INET;
    peer.sin_port = htons(port);
    tm_markstart(tm);
    err = compat_sendto(udp->fd, data, wanted, &sent, tm_getremaining(tm),
            (SA *) &peer, sizeof(peer));
    priv_pusherror(L, err == PRIV_CLOSED ? PRIV_REFUSED : err);
    lua_pushnumber(L, sent);
    return 2;
}

/*-------------------------------------------------------------------------*\
* Associates a local address to an UDP socket
* Lua Input: address, port
*   address: host name or ip address to bind to 
*   port: port to bind to
* Lua Returns
*   On success: nil
*   On error: error message
\*-------------------------------------------------------------------------*/
static int udp_lua_setsockname(lua_State * L)
{
    p_udp udp = (p_udp) lua_touserdata(L, 1);
    cchar *address = luaL_check_string(L, 2);
    ushort port = (ushort) luaL_check_number(L, 3);
    cchar *err = inet_trybind((p_inet) udp, address, port);
    if (err) lua_pushstring(L, err);
    else lua_pushnil(L);
    return 1;
}

/*-------------------------------------------------------------------------*\
* Sets a peer for a UDP socket
* Lua Input: address, port
*   address: remote host name
*   port: remote host port
* Lua Returns
*   On success: nil
*   On error: error message
\*-------------------------------------------------------------------------*/
static int udp_lua_setpeername(lua_State *L)
{
    p_udp udp = (p_udp) lua_touserdata(L, 1);
    cchar *address = luaL_check_string(L, 2);
    ushort port = (ushort) luaL_check_number(L, 3);
    cchar *err = inet_tryconnect((p_inet) udp, address, port);
    if (!err) {
        udp->udp_connected = 1;
        lua_pushnil(L);
    } else lua_pushstring(L, err);
    return 1;
}