diff options
Diffstat (limited to 'src/udp.c')
-rw-r--r-- | src/udp.c | 88 |
1 files changed, 44 insertions, 44 deletions
@@ -45,7 +45,7 @@ static int meth_dirty(lua_State *L); | |||
45 | /* udp object methods */ | 45 | /* udp object methods */ |
46 | static luaL_reg udp[] = { | 46 | static luaL_reg udp[] = { |
47 | {"__gc", meth_close}, | 47 | {"__gc", meth_close}, |
48 | {"__tostring", aux_tostring}, | 48 | {"__tostring", auxiliar_tostring}, |
49 | {"close", meth_close}, | 49 | {"close", meth_close}, |
50 | {"dirty", meth_dirty}, | 50 | {"dirty", meth_dirty}, |
51 | {"getfd", meth_getfd}, | 51 | {"getfd", meth_getfd}, |
@@ -87,13 +87,13 @@ static luaL_reg func[] = { | |||
87 | int udp_open(lua_State *L) | 87 | int udp_open(lua_State *L) |
88 | { | 88 | { |
89 | /* create classes */ | 89 | /* create classes */ |
90 | aux_newclass(L, "udp{connected}", udp); | 90 | auxiliar_newclass(L, "udp{connected}", udp); |
91 | aux_newclass(L, "udp{unconnected}", udp); | 91 | auxiliar_newclass(L, "udp{unconnected}", udp); |
92 | /* create class groups */ | 92 | /* create class groups */ |
93 | aux_add2group(L, "udp{connected}", "udp{any}"); | 93 | auxiliar_add2group(L, "udp{connected}", "udp{any}"); |
94 | aux_add2group(L, "udp{unconnected}", "udp{any}"); | 94 | auxiliar_add2group(L, "udp{unconnected}", "udp{any}"); |
95 | aux_add2group(L, "udp{connected}", "select{able}"); | 95 | auxiliar_add2group(L, "udp{connected}", "select{able}"); |
96 | aux_add2group(L, "udp{unconnected}", "select{able}"); | 96 | auxiliar_add2group(L, "udp{unconnected}", "select{able}"); |
97 | /* define library functions */ | 97 | /* define library functions */ |
98 | luaL_openlib(L, NULL, func, 0); | 98 | luaL_openlib(L, NULL, func, 0); |
99 | return 0; | 99 | return 0; |
@@ -106,20 +106,20 @@ const char *udp_strerror(int err) { | |||
106 | /* a 'closed' error on an unconnected means the target address was not | 106 | /* a 'closed' error on an unconnected means the target address was not |
107 | * accepted by the transport layer */ | 107 | * accepted by the transport layer */ |
108 | if (err == IO_CLOSED) return "refused"; | 108 | if (err == IO_CLOSED) return "refused"; |
109 | else return sock_strerror(err); | 109 | else return socket_strerror(err); |
110 | } | 110 | } |
111 | 111 | ||
112 | /*-------------------------------------------------------------------------*\ | 112 | /*-------------------------------------------------------------------------*\ |
113 | * Send data through connected udp socket | 113 | * Send data through connected udp socket |
114 | \*-------------------------------------------------------------------------*/ | 114 | \*-------------------------------------------------------------------------*/ |
115 | static int meth_send(lua_State *L) { | 115 | static int meth_send(lua_State *L) { |
116 | p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); | 116 | p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1); |
117 | p_tm tm = &udp->tm; | 117 | p_timeout tm = &udp->tm; |
118 | size_t count, sent = 0; | 118 | size_t count, sent = 0; |
119 | int err; | 119 | int err; |
120 | const char *data = luaL_checklstring(L, 2, &count); | 120 | const char *data = luaL_checklstring(L, 2, &count); |
121 | tm_markstart(tm); | 121 | timeout_markstart(tm); |
122 | err = sock_send(&udp->sock, data, count, &sent, tm); | 122 | err = socket_send(&udp->sock, data, count, &sent, tm); |
123 | if (err != IO_DONE) { | 123 | if (err != IO_DONE) { |
124 | lua_pushnil(L); | 124 | lua_pushnil(L); |
125 | lua_pushstring(L, udp_strerror(err)); | 125 | lua_pushstring(L, udp_strerror(err)); |
@@ -133,12 +133,12 @@ static int meth_send(lua_State *L) { | |||
133 | * Send data through unconnected udp socket | 133 | * Send data through unconnected udp socket |
134 | \*-------------------------------------------------------------------------*/ | 134 | \*-------------------------------------------------------------------------*/ |
135 | static int meth_sendto(lua_State *L) { | 135 | static int meth_sendto(lua_State *L) { |
136 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); | 136 | p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); |
137 | size_t count, sent = 0; | 137 | size_t count, sent = 0; |
138 | const char *data = luaL_checklstring(L, 2, &count); | 138 | const char *data = luaL_checklstring(L, 2, &count); |
139 | const char *ip = luaL_checkstring(L, 3); | 139 | const char *ip = luaL_checkstring(L, 3); |
140 | unsigned short port = (unsigned short) luaL_checknumber(L, 4); | 140 | unsigned short port = (unsigned short) luaL_checknumber(L, 4); |
141 | p_tm tm = &udp->tm; | 141 | p_timeout tm = &udp->tm; |
142 | struct sockaddr_in addr; | 142 | struct sockaddr_in addr; |
143 | int err; | 143 | int err; |
144 | memset(&addr, 0, sizeof(addr)); | 144 | memset(&addr, 0, sizeof(addr)); |
@@ -146,8 +146,8 @@ static int meth_sendto(lua_State *L) { | |||
146 | luaL_argerror(L, 3, "invalid ip address"); | 146 | luaL_argerror(L, 3, "invalid ip address"); |
147 | addr.sin_family = AF_INET; | 147 | addr.sin_family = AF_INET; |
148 | addr.sin_port = htons(port); | 148 | addr.sin_port = htons(port); |
149 | tm_markstart(tm); | 149 | timeout_markstart(tm); |
150 | err = sock_sendto(&udp->sock, data, count, &sent, | 150 | err = socket_sendto(&udp->sock, data, count, &sent, |
151 | (SA *) &addr, sizeof(addr), tm); | 151 | (SA *) &addr, sizeof(addr), tm); |
152 | if (err != IO_DONE) { | 152 | if (err != IO_DONE) { |
153 | lua_pushnil(L); | 153 | lua_pushnil(L); |
@@ -162,14 +162,14 @@ static int meth_sendto(lua_State *L) { | |||
162 | * Receives data from a UDP socket | 162 | * Receives data from a UDP socket |
163 | \*-------------------------------------------------------------------------*/ | 163 | \*-------------------------------------------------------------------------*/ |
164 | static int meth_receive(lua_State *L) { | 164 | static int meth_receive(lua_State *L) { |
165 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 165 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
166 | char buffer[UDP_DATAGRAMSIZE]; | 166 | char buffer[UDP_DATAGRAMSIZE]; |
167 | size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); | 167 | size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); |
168 | int err; | 168 | int err; |
169 | p_tm tm = &udp->tm; | 169 | p_timeout tm = &udp->tm; |
170 | count = MIN(count, sizeof(buffer)); | 170 | count = MIN(count, sizeof(buffer)); |
171 | tm_markstart(tm); | 171 | timeout_markstart(tm); |
172 | err = sock_recv(&udp->sock, buffer, count, &got, tm); | 172 | err = socket_recv(&udp->sock, buffer, count, &got, tm); |
173 | if (err != IO_DONE) { | 173 | if (err != IO_DONE) { |
174 | lua_pushnil(L); | 174 | lua_pushnil(L); |
175 | lua_pushstring(L, udp_strerror(err)); | 175 | lua_pushstring(L, udp_strerror(err)); |
@@ -183,16 +183,16 @@ static int meth_receive(lua_State *L) { | |||
183 | * Receives data and sender from a UDP socket | 183 | * Receives data and sender from a UDP socket |
184 | \*-------------------------------------------------------------------------*/ | 184 | \*-------------------------------------------------------------------------*/ |
185 | static int meth_receivefrom(lua_State *L) { | 185 | static int meth_receivefrom(lua_State *L) { |
186 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); | 186 | p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); |
187 | struct sockaddr_in addr; | 187 | struct sockaddr_in addr; |
188 | socklen_t addr_len = sizeof(addr); | 188 | socklen_t addr_len = sizeof(addr); |
189 | char buffer[UDP_DATAGRAMSIZE]; | 189 | char buffer[UDP_DATAGRAMSIZE]; |
190 | size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); | 190 | size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); |
191 | int err; | 191 | int err; |
192 | p_tm tm = &udp->tm; | 192 | p_timeout tm = &udp->tm; |
193 | tm_markstart(tm); | 193 | timeout_markstart(tm); |
194 | count = MIN(count, sizeof(buffer)); | 194 | count = MIN(count, sizeof(buffer)); |
195 | err = sock_recvfrom(&udp->sock, buffer, count, &got, | 195 | err = socket_recvfrom(&udp->sock, buffer, count, &got, |
196 | (SA *) &addr, &addr_len, tm); | 196 | (SA *) &addr, &addr_len, tm); |
197 | if (err == IO_DONE) { | 197 | if (err == IO_DONE) { |
198 | lua_pushlstring(L, buffer, got); | 198 | lua_pushlstring(L, buffer, got); |
@@ -210,20 +210,20 @@ static int meth_receivefrom(lua_State *L) { | |||
210 | * Select support methods | 210 | * Select support methods |
211 | \*-------------------------------------------------------------------------*/ | 211 | \*-------------------------------------------------------------------------*/ |
212 | static int meth_getfd(lua_State *L) { | 212 | static int meth_getfd(lua_State *L) { |
213 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 213 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
214 | lua_pushnumber(L, (int) udp->sock); | 214 | lua_pushnumber(L, (int) udp->sock); |
215 | return 1; | 215 | return 1; |
216 | } | 216 | } |
217 | 217 | ||
218 | /* this is very dangerous, but can be handy for those that are brave enough */ | 218 | /* this is very dangerous, but can be handy for those that are brave enough */ |
219 | static int meth_setfd(lua_State *L) { | 219 | static int meth_setfd(lua_State *L) { |
220 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 220 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
221 | udp->sock = (t_sock) luaL_checknumber(L, 2); | 221 | udp->sock = (t_socket) luaL_checknumber(L, 2); |
222 | return 0; | 222 | return 0; |
223 | } | 223 | } |
224 | 224 | ||
225 | static int meth_dirty(lua_State *L) { | 225 | static int meth_dirty(lua_State *L) { |
226 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 226 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
227 | (void) udp; | 227 | (void) udp; |
228 | lua_pushboolean(L, 0); | 228 | lua_pushboolean(L, 0); |
229 | return 1; | 229 | return 1; |
@@ -233,12 +233,12 @@ static int meth_dirty(lua_State *L) { | |||
233 | * Just call inet methods | 233 | * Just call inet methods |
234 | \*-------------------------------------------------------------------------*/ | 234 | \*-------------------------------------------------------------------------*/ |
235 | static int meth_getpeername(lua_State *L) { | 235 | static int meth_getpeername(lua_State *L) { |
236 | p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); | 236 | p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1); |
237 | return inet_meth_getpeername(L, &udp->sock); | 237 | return inet_meth_getpeername(L, &udp->sock); |
238 | } | 238 | } |
239 | 239 | ||
240 | static int meth_getsockname(lua_State *L) { | 240 | static int meth_getsockname(lua_State *L) { |
241 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 241 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
242 | return inet_meth_getsockname(L, &udp->sock); | 242 | return inet_meth_getsockname(L, &udp->sock); |
243 | } | 243 | } |
244 | 244 | ||
@@ -246,7 +246,7 @@ static int meth_getsockname(lua_State *L) { | |||
246 | * Just call option handler | 246 | * Just call option handler |
247 | \*-------------------------------------------------------------------------*/ | 247 | \*-------------------------------------------------------------------------*/ |
248 | static int meth_setoption(lua_State *L) { | 248 | static int meth_setoption(lua_State *L) { |
249 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 249 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
250 | return opt_meth_setoption(L, opt, &udp->sock); | 250 | return opt_meth_setoption(L, opt, &udp->sock); |
251 | } | 251 | } |
252 | 252 | ||
@@ -254,16 +254,16 @@ static int meth_setoption(lua_State *L) { | |||
254 | * Just call tm methods | 254 | * Just call tm methods |
255 | \*-------------------------------------------------------------------------*/ | 255 | \*-------------------------------------------------------------------------*/ |
256 | static int meth_settimeout(lua_State *L) { | 256 | static int meth_settimeout(lua_State *L) { |
257 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 257 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
258 | return tm_meth_settimeout(L, &udp->tm); | 258 | return timeout_meth_settimeout(L, &udp->tm); |
259 | } | 259 | } |
260 | 260 | ||
261 | /*-------------------------------------------------------------------------*\ | 261 | /*-------------------------------------------------------------------------*\ |
262 | * Turns a master udp object into a client object. | 262 | * Turns a master udp object into a client object. |
263 | \*-------------------------------------------------------------------------*/ | 263 | \*-------------------------------------------------------------------------*/ |
264 | static int meth_setpeername(lua_State *L) { | 264 | static int meth_setpeername(lua_State *L) { |
265 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 265 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
266 | p_tm tm = &udp->tm; | 266 | p_timeout tm = &udp->tm; |
267 | const char *address = luaL_checkstring(L, 2); | 267 | const char *address = luaL_checkstring(L, 2); |
268 | int connecting = strcmp(address, "*"); | 268 | int connecting = strcmp(address, "*"); |
269 | unsigned short port = connecting ? | 269 | unsigned short port = connecting ? |
@@ -276,8 +276,8 @@ static int meth_setpeername(lua_State *L) { | |||
276 | return 2; | 276 | return 2; |
277 | } | 277 | } |
278 | /* change class to connected or unconnected depending on address */ | 278 | /* change class to connected or unconnected depending on address */ |
279 | if (connecting) aux_setclass(L, "udp{connected}", 1); | 279 | if (connecting) auxiliar_setclass(L, "udp{connected}", 1); |
280 | else aux_setclass(L, "udp{unconnected}", 1); | 280 | else auxiliar_setclass(L, "udp{unconnected}", 1); |
281 | lua_pushnumber(L, 1); | 281 | lua_pushnumber(L, 1); |
282 | return 1; | 282 | return 1; |
283 | } | 283 | } |
@@ -286,8 +286,8 @@ static int meth_setpeername(lua_State *L) { | |||
286 | * Closes socket used by object | 286 | * Closes socket used by object |
287 | \*-------------------------------------------------------------------------*/ | 287 | \*-------------------------------------------------------------------------*/ |
288 | static int meth_close(lua_State *L) { | 288 | static int meth_close(lua_State *L) { |
289 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 289 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
290 | sock_destroy(&udp->sock); | 290 | socket_destroy(&udp->sock); |
291 | lua_pushnumber(L, 1); | 291 | lua_pushnumber(L, 1); |
292 | return 1; | 292 | return 1; |
293 | } | 293 | } |
@@ -296,7 +296,7 @@ static int meth_close(lua_State *L) { | |||
296 | * Turns a master object into a server object | 296 | * Turns a master object into a server object |
297 | \*-------------------------------------------------------------------------*/ | 297 | \*-------------------------------------------------------------------------*/ |
298 | static int meth_setsockname(lua_State *L) { | 298 | static int meth_setsockname(lua_State *L) { |
299 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); | 299 | p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); |
300 | const char *address = luaL_checkstring(L, 2); | 300 | const char *address = luaL_checkstring(L, 2); |
301 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 301 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
302 | const char *err = inet_trybind(&udp->sock, address, port); | 302 | const char *err = inet_trybind(&udp->sock, address, port); |
@@ -316,17 +316,17 @@ static int meth_setsockname(lua_State *L) { | |||
316 | * Creates a master udp object | 316 | * Creates a master udp object |
317 | \*-------------------------------------------------------------------------*/ | 317 | \*-------------------------------------------------------------------------*/ |
318 | static int global_create(lua_State *L) { | 318 | static int global_create(lua_State *L) { |
319 | t_sock sock; | 319 | t_socket sock; |
320 | const char *err = inet_trycreate(&sock, SOCK_DGRAM); | 320 | const char *err = inet_trycreate(&sock, SOCK_DGRAM); |
321 | /* try to allocate a system socket */ | 321 | /* try to allocate a system socket */ |
322 | if (!err) { | 322 | if (!err) { |
323 | /* allocate tcp object */ | 323 | /* allocate tcp object */ |
324 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); | 324 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); |
325 | aux_setclass(L, "udp{unconnected}", -1); | 325 | auxiliar_setclass(L, "udp{unconnected}", -1); |
326 | /* initialize remaining structure fields */ | 326 | /* initialize remaining structure fields */ |
327 | sock_setnonblocking(&sock); | 327 | socket_setnonblocking(&sock); |
328 | udp->sock = sock; | 328 | udp->sock = sock; |
329 | tm_init(&udp->tm, -1, -1); | 329 | timeout_init(&udp->tm, -1, -1); |
330 | return 1; | 330 | return 1; |
331 | } else { | 331 | } else { |
332 | lua_pushnil(L); | 332 | lua_pushnil(L); |