aboutsummaryrefslogtreecommitdiff
path: root/src/udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/udp.c')
-rw-r--r--src/udp.c98
1 files changed, 62 insertions, 36 deletions
diff --git a/src/udp.c b/src/udp.c
index 1701d1b..bcf515b 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -10,43 +10,49 @@
10 10
11#include "luasocket.h" 11#include "luasocket.h"
12 12
13#include "aux.h" 13#include "auxiliar.h"
14#include "socket.h"
14#include "inet.h" 15#include "inet.h"
16#include "error.h"
15#include "udp.h" 17#include "udp.h"
16 18
17/*=========================================================================*\ 19/*=========================================================================*\
18* Internal function prototypes 20* Internal function prototypes
19\*=========================================================================*/ 21\*=========================================================================*/
20static int udp_global_create(lua_State *L); 22static int global_create(lua_State *L);
21static int udp_meth_send(lua_State *L); 23static int meth_send(lua_State *L);
22static int udp_meth_sendto(lua_State *L); 24static int meth_sendto(lua_State *L);
23static int udp_meth_receive(lua_State *L); 25static int meth_receive(lua_State *L);
24static int udp_meth_receivefrom(lua_State *L); 26static int meth_receivefrom(lua_State *L);
25static int udp_meth_getsockname(lua_State *L); 27static int meth_getsockname(lua_State *L);
26static int udp_meth_getpeername(lua_State *L); 28static int meth_getpeername(lua_State *L);
27static int udp_meth_setsockname(lua_State *L); 29static int meth_setsockname(lua_State *L);
28static int udp_meth_setpeername(lua_State *L); 30static int meth_setpeername(lua_State *L);
29static int udp_meth_close(lua_State *L); 31static int meth_close(lua_State *L);
30static int udp_meth_timeout(lua_State *L); 32static int meth_timeout(lua_State *L);
33static int meth_fd(lua_State *L);
34static int meth_dirty(lua_State *L);
31 35
32/* udp object methods */ 36/* udp object methods */
33static luaL_reg udp[] = { 37static luaL_reg udp[] = {
34 {"setpeername", udp_meth_setpeername}, 38 {"setpeername", meth_setpeername},
35 {"setsockname", udp_meth_setsockname}, 39 {"setsockname", meth_setsockname},
36 {"getsockname", udp_meth_getsockname}, 40 {"getsockname", meth_getsockname},
37 {"getpeername", udp_meth_getpeername}, 41 {"getpeername", meth_getpeername},
38 {"send", udp_meth_send}, 42 {"send", meth_send},
39 {"sendto", udp_meth_sendto}, 43 {"sendto", meth_sendto},
40 {"receive", udp_meth_receive}, 44 {"receive", meth_receive},
41 {"receivefrom", udp_meth_receivefrom}, 45 {"receivefrom", meth_receivefrom},
42 {"timeout", udp_meth_timeout}, 46 {"timeout", meth_timeout},
43 {"close", udp_meth_close}, 47 {"close", meth_close},
48 {"fd", meth_fd},
49 {"dirty", meth_dirty},
44 {NULL, NULL} 50 {NULL, NULL}
45}; 51};
46 52
47/* functions in library namespace */ 53/* functions in library namespace */
48static luaL_reg func[] = { 54static luaL_reg func[] = {
49 {"udp", udp_global_create}, 55 {"udp", global_create},
50 {NULL, NULL} 56 {NULL, NULL}
51}; 57};
52 58
@@ -59,8 +65,10 @@ void udp_open(lua_State *L)
59 aux_newclass(L, "udp{connected}", udp); 65 aux_newclass(L, "udp{connected}", udp);
60 aux_newclass(L, "udp{unconnected}", udp); 66 aux_newclass(L, "udp{unconnected}", udp);
61 /* create class groups */ 67 /* create class groups */
62 aux_add2group(L, "udp{connected}", "udp{any}"); 68 aux_add2group(L, "udp{connected}", "udp{any}");
63 aux_add2group(L, "udp{unconnected}", "udp{any}"); 69 aux_add2group(L, "udp{unconnected}", "udp{any}");
70 aux_add2group(L, "udp{connected}", "select{able}");
71 aux_add2group(L, "udp{unconnected}", "select{able}");
64 /* define library functions */ 72 /* define library functions */
65 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); 73 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
66 lua_pop(L, 1); 74 lua_pop(L, 1);
@@ -72,7 +80,7 @@ void udp_open(lua_State *L)
72/*-------------------------------------------------------------------------*\ 80/*-------------------------------------------------------------------------*\
73* Send data through connected udp socket 81* Send data through connected udp socket
74\*-------------------------------------------------------------------------*/ 82\*-------------------------------------------------------------------------*/
75static int udp_meth_send(lua_State *L) 83static int meth_send(lua_State *L)
76{ 84{
77 p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); 85 p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1);
78 p_tm tm = &udp->tm; 86 p_tm tm = &udp->tm;
@@ -90,7 +98,7 @@ static int udp_meth_send(lua_State *L)
90/*-------------------------------------------------------------------------*\ 98/*-------------------------------------------------------------------------*\
91* Send data through unconnected udp socket 99* Send data through unconnected udp socket
92\*-------------------------------------------------------------------------*/ 100\*-------------------------------------------------------------------------*/
93static int udp_meth_sendto(lua_State *L) 101static int meth_sendto(lua_State *L)
94{ 102{
95 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); 103 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1);
96 size_t count, sent = 0; 104 size_t count, sent = 0;
@@ -117,7 +125,7 @@ static int udp_meth_sendto(lua_State *L)
117/*-------------------------------------------------------------------------*\ 125/*-------------------------------------------------------------------------*\
118* Receives data from a UDP socket 126* Receives data from a UDP socket
119\*-------------------------------------------------------------------------*/ 127\*-------------------------------------------------------------------------*/
120static int udp_meth_receive(lua_State *L) 128static int meth_receive(lua_State *L)
121{ 129{
122 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 130 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
123 char buffer[UDP_DATAGRAMSIZE]; 131 char buffer[UDP_DATAGRAMSIZE];
@@ -136,11 +144,11 @@ static int udp_meth_receive(lua_State *L)
136/*-------------------------------------------------------------------------*\ 144/*-------------------------------------------------------------------------*\
137* Receives data and sender from a UDP socket 145* Receives data and sender from a UDP socket
138\*-------------------------------------------------------------------------*/ 146\*-------------------------------------------------------------------------*/
139static int udp_meth_receivefrom(lua_State *L) 147static int meth_receivefrom(lua_State *L)
140{ 148{
141 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); 149 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1);
142 struct sockaddr_in addr; 150 struct sockaddr_in addr;
143 size_t addr_len = sizeof(addr); 151 socklen_t addr_len = sizeof(addr);
144 char buffer[UDP_DATAGRAMSIZE]; 152 char buffer[UDP_DATAGRAMSIZE];
145 size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); 153 size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
146 int err; 154 int err;
@@ -162,15 +170,33 @@ static int udp_meth_receivefrom(lua_State *L)
162} 170}
163 171
164/*-------------------------------------------------------------------------*\ 172/*-------------------------------------------------------------------------*\
173* Select support methods
174\*-------------------------------------------------------------------------*/
175static int meth_fd(lua_State *L)
176{
177 p_udp udp = (p_udp) aux_checkclass(L, "udp{any}", 1);
178 lua_pushnumber(L, udp->sock);
179 return 1;
180}
181
182static int meth_dirty(lua_State *L)
183{
184 p_udp udp = (p_udp) aux_checkclass(L, "udp{any}", 1);
185 (void) udp;
186 lua_pushboolean(L, 0);
187 return 1;
188}
189
190/*-------------------------------------------------------------------------*\
165* Just call inet methods 191* Just call inet methods
166\*-------------------------------------------------------------------------*/ 192\*-------------------------------------------------------------------------*/
167static int udp_meth_getpeername(lua_State *L) 193static int meth_getpeername(lua_State *L)
168{ 194{
169 p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); 195 p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1);
170 return inet_meth_getpeername(L, &udp->sock); 196 return inet_meth_getpeername(L, &udp->sock);
171} 197}
172 198
173static int udp_meth_getsockname(lua_State *L) 199static int meth_getsockname(lua_State *L)
174{ 200{
175 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 201 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
176 return inet_meth_getsockname(L, &udp->sock); 202 return inet_meth_getsockname(L, &udp->sock);
@@ -179,7 +205,7 @@ static int udp_meth_getsockname(lua_State *L)
179/*-------------------------------------------------------------------------*\ 205/*-------------------------------------------------------------------------*\
180* Just call tm methods 206* Just call tm methods
181\*-------------------------------------------------------------------------*/ 207\*-------------------------------------------------------------------------*/
182static int udp_meth_timeout(lua_State *L) 208static int meth_timeout(lua_State *L)
183{ 209{
184 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 210 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
185 return tm_meth_timeout(L, &udp->tm); 211 return tm_meth_timeout(L, &udp->tm);
@@ -188,7 +214,7 @@ static int udp_meth_timeout(lua_State *L)
188/*-------------------------------------------------------------------------*\ 214/*-------------------------------------------------------------------------*\
189* Turns a master udp object into a client object. 215* Turns a master udp object into a client object.
190\*-------------------------------------------------------------------------*/ 216\*-------------------------------------------------------------------------*/
191static int udp_meth_setpeername(lua_State *L) 217static int meth_setpeername(lua_State *L)
192{ 218{
193 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); 219 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1);
194 const char *address = luaL_checkstring(L, 2); 220 const char *address = luaL_checkstring(L, 2);
@@ -211,7 +237,7 @@ static int udp_meth_setpeername(lua_State *L)
211/*-------------------------------------------------------------------------*\ 237/*-------------------------------------------------------------------------*\
212* Closes socket used by object 238* Closes socket used by object
213\*-------------------------------------------------------------------------*/ 239\*-------------------------------------------------------------------------*/
214static int udp_meth_close(lua_State *L) 240static int meth_close(lua_State *L)
215{ 241{
216 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 242 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1);
217 sock_destroy(&udp->sock); 243 sock_destroy(&udp->sock);
@@ -221,7 +247,7 @@ static int udp_meth_close(lua_State *L)
221/*-------------------------------------------------------------------------*\ 247/*-------------------------------------------------------------------------*\
222* Turns a master object into a server object 248* Turns a master object into a server object
223\*-------------------------------------------------------------------------*/ 249\*-------------------------------------------------------------------------*/
224static int udp_meth_setsockname(lua_State *L) 250static int meth_setsockname(lua_State *L)
225{ 251{
226 p_udp udp = (p_udp) aux_checkclass(L, "udp{master}", 1); 252 p_udp udp = (p_udp) aux_checkclass(L, "udp{master}", 1);
227 const char *address = luaL_checkstring(L, 2); 253 const char *address = luaL_checkstring(L, 2);
@@ -242,7 +268,7 @@ static int udp_meth_setsockname(lua_State *L)
242/*-------------------------------------------------------------------------*\ 268/*-------------------------------------------------------------------------*\
243* Creates a master udp object 269* Creates a master udp object
244\*-------------------------------------------------------------------------*/ 270\*-------------------------------------------------------------------------*/
245int udp_global_create(lua_State *L) 271int global_create(lua_State *L)
246{ 272{
247 /* allocate udp object */ 273 /* allocate udp object */
248 p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); 274 p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));