aboutsummaryrefslogtreecommitdiff
path: root/src/inet.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2003-05-25 01:54:13 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2003-05-25 01:54:13 +0000
commit0f6c8d50a99997ac7829864b1c93362b50f1bbf3 (patch)
treed0cefe3a05484e65b7b7e79d8cae4a1d2e6d19fb /src/inet.c
parentc1ef3e7103cc652d2004ef1ddc9409b946207f33 (diff)
downloadluasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.tar.gz
luasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.tar.bz2
luasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.zip
Porting to LUA 5.0 final
Diffstat (limited to 'src/inet.c')
-rw-r--r--src/inet.c170
1 files changed, 70 insertions, 100 deletions
diff --git a/src/inet.c b/src/inet.c
index 341c60e..f20762f 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -1,12 +1,5 @@
1/*=========================================================================*\ 1/*=========================================================================*\
2* Internet domain class: inherits from the Socket class, and implement 2* Internet domain functions
3* a few methods shared by all internet related objects
4* Lua methods:
5* getpeername: gets socket peer ip address and port
6* getsockname: gets local socket ip address and port
7* Global Lua fuctions:
8* toip: gets resolver info on host name
9* tohostname: gets resolver info on dotted-quad
10* 3*
11* RCS ID: $Id$ 4* RCS ID: $Id$
12\*=========================================================================*/ 5\*=========================================================================*/
@@ -15,23 +8,27 @@
15#include <lua.h> 8#include <lua.h>
16#include <lauxlib.h> 9#include <lauxlib.h>
17 10
18#include "lsinet.h" 11#include "luasocket.h"
19#include "lssock.h" 12#include "inet.h"
20#include "lscompat.h"
21 13
22/*=========================================================================*\ 14/*=========================================================================*\
23* Internal function prototypes. 15* Internal function prototypes.
24\*=========================================================================*/ 16\*=========================================================================*/
25static int inet_lua_toip(lua_State *L); 17static int inet_global_toip(lua_State *L);
26static int inet_lua_tohostname(lua_State *L); 18static int inet_global_tohostname(lua_State *L);
27static int inet_lua_getpeername(lua_State *L); 19
28static int inet_lua_getsockname(lua_State *L);
29static void inet_pushresolved(lua_State *L, struct hostent *hp); 20static void inet_pushresolved(lua_State *L, struct hostent *hp);
30 21
31#ifdef COMPAT_INETATON 22#ifdef INET_ATON
32static int inet_aton(cchar *cp, struct in_addr *inp); 23static int inet_aton(const char *cp, struct in_addr *inp);
33#endif 24#endif
34 25
26static luaL_reg func[] = {
27 { "toip", inet_global_toip },
28 { "tohostname", inet_global_tohostname },
29 { NULL, NULL}
30};
31
35/*=========================================================================*\ 32/*=========================================================================*\
36* Exported functions 33* Exported functions
37\*=========================================================================*/ 34\*=========================================================================*/
@@ -40,39 +37,7 @@ static int inet_aton(cchar *cp, struct in_addr *inp);
40\*-------------------------------------------------------------------------*/ 37\*-------------------------------------------------------------------------*/
41void inet_open(lua_State *L) 38void inet_open(lua_State *L)
42{ 39{
43 lua_pushcfunction(L, inet_lua_toip); 40 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
44 priv_newglobal(L, "toip");
45 lua_pushcfunction(L, inet_lua_tohostname);
46 priv_newglobal(L, "tohostname");
47 priv_newglobalmethod(L, "getsockname");
48 priv_newglobalmethod(L, "getpeername");
49}
50
51/*-------------------------------------------------------------------------*\
52* Hook lua methods to methods table.
53* Input
54* lsclass: class name
55\*-------------------------------------------------------------------------*/
56void inet_inherit(lua_State *L, cchar *lsclass)
57{
58 unsigned int i;
59 static struct luaL_reg funcs[] = {
60 {"getsockname", inet_lua_getsockname},
61 {"getpeername", inet_lua_getpeername},
62 };
63 sock_inherit(L, lsclass);
64 for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++) {
65 lua_pushcfunction(L, funcs[i].func);
66 priv_setmethod(L, lsclass, funcs[i].name);
67 }
68}
69
70/*-------------------------------------------------------------------------*\
71* Constructs the object
72\*-------------------------------------------------------------------------*/
73void inet_construct(lua_State *L, p_inet inet)
74{
75 sock_construct(L, (p_sock) inet);
76} 41}
77 42
78/*=========================================================================*\ 43/*=========================================================================*\
@@ -87,17 +52,18 @@ void inet_construct(lua_State *L, p_inet inet)
87* On success: first IP address followed by a resolved table 52* On success: first IP address followed by a resolved table
88* On error: nil, followed by an error message 53* On error: nil, followed by an error message
89\*-------------------------------------------------------------------------*/ 54\*-------------------------------------------------------------------------*/
90static int inet_lua_toip(lua_State *L) 55static int inet_global_toip(lua_State *L)
91{ 56{
92 cchar *address = luaL_checkstring(L, 1); 57 const char *address = luaL_checkstring(L, 1);
93 struct in_addr addr; 58 struct in_addr addr;
94 struct hostent *hp; 59 struct hostent *hp;
95 if (inet_aton(address, &addr)) 60 if (inet_aton(address, &addr))
96 hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET); 61 hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
97 else hp = gethostbyname(address); 62 else
63 hp = gethostbyname(address);
98 if (!hp) { 64 if (!hp) {
99 lua_pushnil(L); 65 lua_pushnil(L);
100 lua_pushstring(L, compat_hoststrerror()); 66 lua_pushstring(L, sock_hoststrerror());
101 return 2; 67 return 2;
102 } 68 }
103 addr = *((struct in_addr *) hp->h_addr); 69 addr = *((struct in_addr *) hp->h_addr);
@@ -115,17 +81,18 @@ static int inet_lua_toip(lua_State *L)
115* On success: canonic name followed by a resolved table 81* On success: canonic name followed by a resolved table
116* On error: nil, followed by an error message 82* On error: nil, followed by an error message
117\*-------------------------------------------------------------------------*/ 83\*-------------------------------------------------------------------------*/
118static int inet_lua_tohostname(lua_State *L) 84static int inet_global_tohostname(lua_State *L)
119{ 85{
120 cchar *address = luaL_checkstring(L, 1); 86 const char *address = luaL_checkstring(L, 1);
121 struct in_addr addr; 87 struct in_addr addr;
122 struct hostent *hp; 88 struct hostent *hp;
123 if (inet_aton(address, &addr)) 89 if (inet_aton(address, &addr))
124 hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET); 90 hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
125 else hp = gethostbyname(address); 91 else
92 hp = gethostbyname(address);
126 if (!hp) { 93 if (!hp) {
127 lua_pushnil(L); 94 lua_pushnil(L);
128 lua_pushstring(L, compat_hoststrerror()); 95 lua_pushstring(L, sock_hoststrerror());
129 return 2; 96 return 2;
130 } 97 }
131 lua_pushstring(L, hp->h_name); 98 lua_pushstring(L, hp->h_name);
@@ -138,18 +105,17 @@ static int inet_lua_tohostname(lua_State *L)
138\*=========================================================================*/ 105\*=========================================================================*/
139/*-------------------------------------------------------------------------*\ 106/*-------------------------------------------------------------------------*\
140* Retrieves socket peer name 107* Retrieves socket peer name
141* Lua Input: sock 108* Input:
142* sock: socket 109* sock: socket
143* Lua Returns 110* Lua Returns
144* On success: ip address and port of peer 111* On success: ip address and port of peer
145* On error: nil 112* On error: nil
146\*-------------------------------------------------------------------------*/ 113\*-------------------------------------------------------------------------*/
147static int inet_lua_getpeername(lua_State *L) 114int inet_meth_getpeername(lua_State *L, p_sock ps)
148{ 115{
149 p_sock sock = (p_sock) lua_touserdata(L, 1);
150 struct sockaddr_in peer; 116 struct sockaddr_in peer;
151 size_t peer_len = sizeof(peer); 117 size_t peer_len = sizeof(peer);
152 if (getpeername(sock->fd, (SA *) &peer, &peer_len) < 0) { 118 if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) {
153 lua_pushnil(L); 119 lua_pushnil(L);
154 return 1; 120 return 1;
155 } 121 }
@@ -160,18 +126,17 @@ static int inet_lua_getpeername(lua_State *L)
160 126
161/*-------------------------------------------------------------------------*\ 127/*-------------------------------------------------------------------------*\
162* Retrieves socket local name 128* Retrieves socket local name
163* Lua Input: sock 129* Input:
164* sock: socket 130* sock: socket
165* Lua Returns 131* Lua Returns
166* On success: local ip address and port 132* On success: local ip address and port
167* On error: nil 133* On error: nil
168\*-------------------------------------------------------------------------*/ 134\*-------------------------------------------------------------------------*/
169static int inet_lua_getsockname(lua_State *L) 135int inet_meth_getsockname(lua_State *L, p_sock ps)
170{ 136{
171 p_sock sock = (p_sock) lua_touserdata(L, 1);
172 struct sockaddr_in local; 137 struct sockaddr_in local;
173 size_t local_len = sizeof(local); 138 size_t local_len = sizeof(local);
174 if (getsockname(sock->fd, (SA *) &local, &local_len) < 0) { 139 if (getsockname(*ps, (SA *) &local, &local_len) < 0) {
175 lua_pushnil(L); 140 lua_pushnil(L);
176 return 1; 141 return 1;
177 } 142 }
@@ -222,47 +187,53 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
222} 187}
223 188
224/*-------------------------------------------------------------------------*\ 189/*-------------------------------------------------------------------------*\
225* Tries to create a TCP socket and connect to remote address (address, port) 190* Tries to connect to remote address (address, port)
226* Input 191* Input
227* client: socket structure to be used 192* ps: pointer to socket
228* address: host name or ip address 193* address: host name or ip address
229* port: port number to bind to 194* port: port number to bind to
230* Returns 195* Returns
231* NULL in case of success, error message otherwise 196* NULL in case of success, error message otherwise
232\*-------------------------------------------------------------------------*/ 197\*-------------------------------------------------------------------------*/
233cchar *inet_tryconnect(p_inet inet, cchar *address, ushort port) 198const char *inet_tryconnect(p_sock ps, const char *address, ushort port)
234{ 199{
235 struct sockaddr_in remote; 200 struct sockaddr_in remote;
236 memset(&remote, 0, sizeof(remote)); 201 memset(&remote, 0, sizeof(remote));
237 remote.sin_family = AF_INET; 202 remote.sin_family = AF_INET;
238 remote.sin_port = htons(port); 203 remote.sin_port = htons(port);
239 if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) { 204 if (strcmp(address, "*")) {
240 struct hostent *hp = gethostbyname(address); 205 if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) {
241 struct in_addr **addr; 206 struct hostent *hp = gethostbyname(address);
242 if (!hp) return compat_hoststrerror(); 207 struct in_addr **addr;
243 addr = (struct in_addr **) hp->h_addr_list; 208 remote.sin_family = AF_INET;
244 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); 209 if (!hp) return sock_hoststrerror();
245 } 210 addr = (struct in_addr **) hp->h_addr_list;
246 compat_setblocking(inet->fd); 211 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
247 if (compat_connect(inet->fd, (SA *) &remote, sizeof(remote)) < 0) { 212 }
248 const char *err = compat_connectstrerror(); 213 } else remote.sin_family = AF_UNSPEC;
249 compat_close(inet->fd); 214 sock_setblocking(ps);
250 inet->fd = COMPAT_INVALIDFD; 215 const char *err = sock_connect(ps, (SA *) &remote, sizeof(remote));
216 if (err) {
217 sock_destroy(ps);
218 *ps = SOCK_INVALID;
251 return err; 219 return err;
252 } 220 } else {
253 compat_setnonblocking(inet->fd); 221 sock_setnonblocking(ps);
254 return NULL; 222 return NULL;
223 }
255} 224}
256 225
257/*-------------------------------------------------------------------------*\ 226/*-------------------------------------------------------------------------*\
258* Tries to create a TCP socket and bind it to (address, port) 227* Tries to bind socket to (address, port)
259* Input 228* Input
229* sock: pointer to socket
260* address: host name or ip address 230* address: host name or ip address
261* port: port number to bind to 231* port: port number to bind to
262* Returns 232* Returns
263* NULL in case of success, error message otherwise 233* NULL in case of success, error message otherwise
264\*-------------------------------------------------------------------------*/ 234\*-------------------------------------------------------------------------*/
265cchar *inet_trybind(p_inet inet, cchar *address, ushort port) 235const char *inet_trybind(p_sock ps, const char *address, ushort port,
236 int backlog)
266{ 237{
267 struct sockaddr_in local; 238 struct sockaddr_in local;
268 memset(&local, 0, sizeof(local)); 239 memset(&local, 0, sizeof(local));
@@ -274,34 +245,33 @@ cchar *inet_trybind(p_inet inet, cchar *address, ushort port)
274 (!strlen(address) || !inet_aton(address, &local.sin_addr))) { 245 (!strlen(address) || !inet_aton(address, &local.sin_addr))) {
275 struct hostent *hp = gethostbyname(address); 246 struct hostent *hp = gethostbyname(address);
276 struct in_addr **addr; 247 struct in_addr **addr;
277 if (!hp) return compat_hoststrerror(); 248 if (!hp) return sock_hoststrerror();
278 addr = (struct in_addr **) hp->h_addr_list; 249 addr = (struct in_addr **) hp->h_addr_list;
279 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); 250 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
280 } 251 }
281 compat_setblocking(inet->fd); 252 sock_setblocking(ps);
282 if (compat_bind(inet->fd, (SA *) &local, sizeof(local)) < 0) { 253 const char *err = sock_bind(ps, (SA *) &local, sizeof(local));
283 const char *err = compat_bindstrerror(); 254 if (err) {
284 compat_close(inet->fd); 255 sock_destroy(ps);
285 inet->fd = COMPAT_INVALIDFD; 256 *ps = SOCK_INVALID;
286 return err; 257 return err;
258 } else {
259 sock_setnonblocking(ps);
260 if (backlog > 0) sock_listen(ps, backlog);
261 return NULL;
287 } 262 }
288 compat_setnonblocking(inet->fd);
289 return NULL;
290} 263}
291 264
292/*-------------------------------------------------------------------------*\ 265/*-------------------------------------------------------------------------*\
293* Tries to create a new inet socket 266* Tries to create a new inet socket
294* Input 267* Input
295* udp: udp structure 268* sock: pointer to socket
296* Returns 269* Returns
297* NULL if successfull, error message on error 270* NULL if successfull, error message on error
298\*-------------------------------------------------------------------------*/ 271\*-------------------------------------------------------------------------*/
299cchar *inet_trysocket(p_inet inet, int type) 272const char *inet_trycreate(p_sock ps, int type)
300{ 273{
301 if (inet->fd != COMPAT_INVALIDFD) compat_close(inet->fd); 274 return sock_create(ps, AF_INET, type, 0);
302 inet->fd = compat_socket(AF_INET, type, 0);
303 if (inet->fd == COMPAT_INVALIDFD) return compat_socketstrerror();
304 else return NULL;
305} 275}
306 276
307/*-------------------------------------------------------------------------*\ 277/*-------------------------------------------------------------------------*\