aboutsummaryrefslogtreecommitdiff
path: root/src/udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/udp.c')
-rw-r--r--src/udp.c287
1 files changed, 287 insertions, 0 deletions
diff --git a/src/udp.c b/src/udp.c
new file mode 100644
index 0000000..0dc0df8
--- /dev/null
+++ b/src/udp.c
@@ -0,0 +1,287 @@
1/*=========================================================================*\
2* UDP socket object implementation (inherits from sock and inet)
3\*=========================================================================*/
4#include <string.h>
5
6#include <lua.h>
7#include <lauxlib.h>
8
9#include "lsinet.h"
10#include "lsudp.h"
11#include "lscompat.h"
12#include "lsselect.h"
13
14/*=========================================================================*\
15* Internal function prototypes.
16\*=========================================================================*/
17static int udp_lua_send(lua_State *L);
18static int udp_lua_sendto(lua_State *L);
19static int udp_lua_receive(lua_State *L);
20static int udp_lua_receivefrom(lua_State *L);
21static int udp_lua_setpeername(lua_State *L);
22static int udp_lua_setsockname(lua_State *L);
23
24static int udp_global_udpsocket(lua_State *L);
25
26static struct luaL_reg funcs[] = {
27 {"send", udp_lua_send},
28 {"sendto", udp_lua_sendto},
29 {"receive", udp_lua_receive},
30 {"receivefrom", udp_lua_receivefrom},
31 {"setpeername", udp_lua_setpeername},
32 {"setsockname", udp_lua_setsockname},
33};
34
35/*=========================================================================*\
36* Exported functions
37\*=========================================================================*/
38/*-------------------------------------------------------------------------*\
39* Initializes module
40\*-------------------------------------------------------------------------*/
41void udp_open(lua_State *L)
42{
43 unsigned int i;
44 priv_newclass(L, UDP_CLASS);
45 udp_inherit(L, UDP_CLASS);
46 /* declare global functions */
47 lua_pushcfunction(L, udp_global_udpsocket);
48 lua_setglobal(L, "udpsocket");
49 for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++)
50 priv_newglobalmethod(L, funcs[i].name);
51 /* make class selectable */
52 select_addclass(L, UDP_CLASS);
53}
54
55/*-------------------------------------------------------------------------*\
56* Hook object methods to methods table.
57\*-------------------------------------------------------------------------*/
58void udp_inherit(lua_State *L, cchar *lsclass)
59{
60 unsigned int i;
61 inet_inherit(L, lsclass);
62 for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++) {
63 lua_pushcfunction(L, funcs[i].func);
64 priv_setmethod(L, lsclass, funcs[i].name);
65 }
66}
67
68/*-------------------------------------------------------------------------*\
69* Initializes socket structure
70\*-------------------------------------------------------------------------*/
71void udp_construct(lua_State *L, p_udp udp)
72{
73 inet_construct(L, (p_inet) udp);
74 udp->udp_connected = 0;
75}
76
77/*-------------------------------------------------------------------------*\
78* Creates a socket structure and initializes it. A socket object is
79* left in the Lua stack.
80* Returns
81* pointer to allocated structure
82\*-------------------------------------------------------------------------*/
83p_udp udp_push(lua_State *L)
84{
85 p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));
86 priv_setclass(L, UDP_CLASS);
87 udp_construct(L, udp);
88 return udp;
89}
90
91/*=========================================================================*\
92* Socket table constructors
93\*=========================================================================*/
94/*-------------------------------------------------------------------------*\
95* Creates a udp socket object and returns it to the Lua script.
96* Lua Input: [options]
97* options: socket options table
98* Lua Returns
99* On success: udp socket
100* On error: nil, followed by an error message
101\*-------------------------------------------------------------------------*/
102static int udp_global_udpsocket(lua_State *L)
103{
104 int oldtop = lua_gettop(L);
105 p_udp udp = udp_push(L);
106 cchar *err = inet_trysocket((p_inet) udp, SOCK_DGRAM);
107 if (err) {
108 lua_pushnil(L);
109 lua_pushstring(L, err);
110 return 2;
111 }
112 if (oldtop < 1) return 1;
113 err = compat_trysetoptions(L, udp->fd);
114 if (err) {
115 lua_pushnil(L);
116 lua_pushstring(L, err);
117 return 2;
118 }
119 return 1;
120}
121
122/*=========================================================================*\
123* Socket table methods
124\*=========================================================================*/
125/*-------------------------------------------------------------------------*\
126* Receives data from a UDP socket
127* Lua Input: sock [, wanted]
128* sock: client socket created by the connect function
129* wanted: the number of bytes expected (default: LUASOCKET_UDPBUFFERSIZE)
130* Lua Returns
131* On success: datagram received
132* On error: nil, followed by an error message
133\*-------------------------------------------------------------------------*/
134static int udp_lua_receive(lua_State *L)
135{
136 p_udp udp = (p_udp) lua_touserdata(L, 1);
137 unsigned char buffer[UDP_DATAGRAMSIZE];
138 size_t got, wanted = (size_t) luaL_opt_number(L, 2, sizeof(buffer));
139 int err;
140 p_tm tm = &udp->base_tm;
141 wanted = MIN(wanted, sizeof(buffer));
142 tm_markstart(tm);
143 err = compat_recv(udp->fd, buffer, wanted, &got, tm_getremaining(tm));
144 if (err == PRIV_CLOSED) err = PRIV_REFUSED;
145 if (err != PRIV_DONE) lua_pushnil(L);
146 else lua_pushlstring(L, buffer, got);
147 priv_pusherror(L, err);
148 return 2;
149}
150
151/*-------------------------------------------------------------------------*\
152* Receives a datagram from a UDP socket
153* Lua Input: sock [, wanted]
154* sock: client socket created by the connect function
155* wanted: the number of bytes expected (default: LUASOCKET_UDPBUFFERSIZE)
156* Lua Returns
157* On success: datagram received, ip and port of sender
158* On error: nil, followed by an error message
159\*-------------------------------------------------------------------------*/
160static int udp_lua_receivefrom(lua_State *L)
161{
162 p_udp udp = (p_udp) lua_touserdata(L, 1);
163 p_tm tm = &udp->base_tm;
164 struct sockaddr_in peer;
165 size_t peer_len = sizeof(peer);
166 unsigned char buffer[UDP_DATAGRAMSIZE];
167 size_t wanted = (size_t) luaL_opt_number(L, 2, sizeof(buffer));
168 size_t got;
169 int err;
170 if (udp->udp_connected) lua_error(L, "receivefrom on connected socket");
171 tm_markstart(tm);
172 wanted = MIN(wanted, sizeof(buffer));
173 err = compat_recvfrom(udp->fd, buffer, wanted, &got, tm_getremaining(tm),
174 (SA *) &peer, &peer_len);
175 if (err == PRIV_CLOSED) err = PRIV_REFUSED;
176 if (err == PRIV_DONE) {
177 lua_pushlstring(L, buffer, got);
178 lua_pushstring(L, inet_ntoa(peer.sin_addr));
179 lua_pushnumber(L, ntohs(peer.sin_port));
180 return 3;
181 } else {
182 lua_pushnil(L);
183 priv_pusherror(L, err);
184 return 2;
185 }
186}
187
188/*-------------------------------------------------------------------------*\
189* Send data through a connected UDP socket
190* Lua Input: sock, data
191* sock: udp socket
192* data: data to be sent
193* Lua Returns
194* On success: nil, followed by the total number of bytes sent
195* On error: error message
196\*-------------------------------------------------------------------------*/
197static int udp_lua_send(lua_State *L)
198{
199 p_udp udp = (p_udp) lua_touserdata(L, 1);
200 p_tm tm = &udp->base_tm;
201 size_t wanted, sent = 0;
202 int err;
203 cchar *data = luaL_check_lstr(L, 2, &wanted);
204 if (!udp->udp_connected) lua_error(L, "send on unconnected socket");
205 tm_markstart(tm);
206 err = compat_send(udp->fd, data, wanted, &sent, tm_getremaining(tm));
207 priv_pusherror(L, err == PRIV_CLOSED ? PRIV_REFUSED : err);
208 lua_pushnumber(L, sent);
209 return 2;
210}
211
212/*-------------------------------------------------------------------------*\
213* Send data through a unconnected UDP socket
214* Lua Input: sock, data, ip, port
215* sock: udp socket
216* data: data to be sent
217* ip: ip address of target
218* port: port in target
219* Lua Returns
220* On success: nil, followed by the total number of bytes sent
221* On error: error message
222\*-------------------------------------------------------------------------*/
223static int udp_lua_sendto(lua_State *L)
224{
225 p_udp udp = (p_udp) lua_touserdata(L, 1);
226 size_t wanted, sent = 0;
227 cchar *data = luaL_check_lstr(L, 2, &wanted);
228 cchar *ip = luaL_check_string(L, 3);
229 ushort port = (ushort) luaL_check_number(L, 4);
230 p_tm tm = &udp->base_tm;
231 struct sockaddr_in peer;
232 int err;
233 if (udp->udp_connected) lua_error(L, "sendto on connected socket");
234 memset(&peer, 0, sizeof(peer));
235 if (!inet_aton(ip, &peer.sin_addr)) lua_error(L, "invalid ip address");
236 peer.sin_family = AF_INET;
237 peer.sin_port = htons(port);
238 tm_markstart(tm);
239 err = compat_sendto(udp->fd, data, wanted, &sent, tm_getremaining(tm),
240 (SA *) &peer, sizeof(peer));
241 priv_pusherror(L, err == PRIV_CLOSED ? PRIV_REFUSED : err);
242 lua_pushnumber(L, sent);
243 return 2;
244}
245
246/*-------------------------------------------------------------------------*\
247* Associates a local address to an UDP socket
248* Lua Input: address, port
249* address: host name or ip address to bind to
250* port: port to bind to
251* Lua Returns
252* On success: nil
253* On error: error message
254\*-------------------------------------------------------------------------*/
255static int udp_lua_setsockname(lua_State * L)
256{
257 p_udp udp = (p_udp) lua_touserdata(L, 1);
258 cchar *address = luaL_check_string(L, 2);
259 ushort port = (ushort) luaL_check_number(L, 3);
260 cchar *err = inet_trybind((p_inet) udp, address, port);
261 if (err) lua_pushstring(L, err);
262 else lua_pushnil(L);
263 return 1;
264}
265
266/*-------------------------------------------------------------------------*\
267* Sets a peer for a UDP socket
268* Lua Input: address, port
269* address: remote host name
270* port: remote host port
271* Lua Returns
272* On success: nil
273* On error: error message
274\*-------------------------------------------------------------------------*/
275static int udp_lua_setpeername(lua_State *L)
276{
277 p_udp udp = (p_udp) lua_touserdata(L, 1);
278 cchar *address = luaL_check_string(L, 2);
279 ushort port = (ushort) luaL_check_number(L, 3);
280 cchar *err = inet_tryconnect((p_inet) udp, address, port);
281 if (!err) {
282 udp->udp_connected = 1;
283 lua_pushnil(L);
284 } else lua_pushstring(L, err);
285 return 1;
286}
287