diff options
Diffstat (limited to 'src/inet.c')
-rw-r--r-- | src/inet.c | 70 |
1 files changed, 37 insertions, 33 deletions
@@ -50,22 +50,28 @@ int inet_open(lua_State *L) | |||
50 | * Returns all information provided by the resolver given a host name | 50 | * Returns all information provided by the resolver given a host name |
51 | * or ip address | 51 | * or ip address |
52 | \*-------------------------------------------------------------------------*/ | 52 | \*-------------------------------------------------------------------------*/ |
53 | static int inet_global_toip(lua_State *L) | 53 | static int inet_gethost(const char *address, struct hostent **hp) { |
54 | { | ||
55 | const char *address = luaL_checkstring(L, 1); | ||
56 | struct in_addr addr; | 54 | struct in_addr addr; |
57 | struct hostent *hp; | ||
58 | if (inet_aton(address, &addr)) | 55 | if (inet_aton(address, &addr)) |
59 | hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET); | 56 | return sock_gethostbyaddr((char *) &addr, sizeof(addr), hp); |
60 | else | 57 | else |
61 | hp = gethostbyname(address); | 58 | return sock_gethostbyname(address, hp); |
62 | if (!hp) { | 59 | } |
60 | |||
61 | /*-------------------------------------------------------------------------*\ | ||
62 | * Returns all information provided by the resolver given a host name | ||
63 | * or ip address | ||
64 | \*-------------------------------------------------------------------------*/ | ||
65 | static int inet_global_tohostname(lua_State *L) { | ||
66 | const char *address = luaL_checkstring(L, 1); | ||
67 | struct hostent *hp = NULL; | ||
68 | int err = inet_gethost(address, &hp); | ||
69 | if (err != IO_DONE) { | ||
63 | lua_pushnil(L); | 70 | lua_pushnil(L); |
64 | lua_pushstring(L, sock_hoststrerror()); | 71 | lua_pushstring(L, sock_hoststrerror(err)); |
65 | return 2; | 72 | return 2; |
66 | } | 73 | } |
67 | addr = *((struct in_addr *) hp->h_addr); | 74 | lua_pushstring(L, hp->h_name); |
68 | lua_pushstring(L, inet_ntoa(addr)); | ||
69 | inet_pushresolved(L, hp); | 75 | inet_pushresolved(L, hp); |
70 | return 2; | 76 | return 2; |
71 | } | 77 | } |
@@ -74,25 +80,22 @@ static int inet_global_toip(lua_State *L) | |||
74 | * Returns all information provided by the resolver given a host name | 80 | * Returns all information provided by the resolver given a host name |
75 | * or ip address | 81 | * or ip address |
76 | \*-------------------------------------------------------------------------*/ | 82 | \*-------------------------------------------------------------------------*/ |
77 | static int inet_global_tohostname(lua_State *L) | 83 | static int inet_global_toip(lua_State *L) |
78 | { | 84 | { |
79 | const char *address = luaL_checkstring(L, 1); | 85 | const char *address = luaL_checkstring(L, 1); |
80 | struct in_addr addr; | 86 | struct hostent *hp = NULL; |
81 | struct hostent *hp; | 87 | int err = inet_gethost(address, &hp); |
82 | if (inet_aton(address, &addr)) | 88 | if (err != IO_DONE) { |
83 | hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET); | ||
84 | else | ||
85 | hp = gethostbyname(address); | ||
86 | if (!hp) { | ||
87 | lua_pushnil(L); | 89 | lua_pushnil(L); |
88 | lua_pushstring(L, sock_hoststrerror()); | 90 | lua_pushstring(L, sock_hoststrerror(err)); |
89 | return 2; | 91 | return 2; |
90 | } | 92 | } |
91 | lua_pushstring(L, hp->h_name); | 93 | lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr))); |
92 | inet_pushresolved(L, hp); | 94 | inet_pushresolved(L, hp); |
93 | return 2; | 95 | return 2; |
94 | } | 96 | } |
95 | 97 | ||
98 | |||
96 | /*-------------------------------------------------------------------------*\ | 99 | /*-------------------------------------------------------------------------*\ |
97 | * Gets the host name | 100 | * Gets the host name |
98 | \*-------------------------------------------------------------------------*/ | 101 | \*-------------------------------------------------------------------------*/ |
@@ -191,9 +194,8 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp) | |||
191 | /*-------------------------------------------------------------------------*\ | 194 | /*-------------------------------------------------------------------------*\ |
192 | * Tries to create a new inet socket | 195 | * Tries to create a new inet socket |
193 | \*-------------------------------------------------------------------------*/ | 196 | \*-------------------------------------------------------------------------*/ |
194 | const char *inet_trycreate(p_sock ps, int type) | 197 | const char *inet_trycreate(p_sock ps, int type) { |
195 | { | 198 | return sock_strerror(sock_create(ps, AF_INET, type, 0)); |
196 | return sock_create(ps, AF_INET, type, 0); | ||
197 | } | 199 | } |
198 | 200 | ||
199 | /*-------------------------------------------------------------------------*\ | 201 | /*-------------------------------------------------------------------------*\ |
@@ -203,22 +205,23 @@ const char *inet_tryconnect(p_sock ps, const char *address, | |||
203 | unsigned short port, p_tm tm) | 205 | unsigned short port, p_tm tm) |
204 | { | 206 | { |
205 | struct sockaddr_in remote; | 207 | struct sockaddr_in remote; |
206 | const char *err; | 208 | int err; |
207 | memset(&remote, 0, sizeof(remote)); | 209 | memset(&remote, 0, sizeof(remote)); |
208 | remote.sin_family = AF_INET; | 210 | remote.sin_family = AF_INET; |
209 | remote.sin_port = htons(port); | 211 | remote.sin_port = htons(port); |
210 | if (strcmp(address, "*")) { | 212 | if (strcmp(address, "*")) { |
211 | if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) { | 213 | if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) { |
212 | struct hostent *hp = gethostbyname(address); | 214 | struct hostent *hp = NULL; |
213 | struct in_addr **addr; | 215 | struct in_addr **addr; |
214 | if (!hp) return sock_hoststrerror(); | 216 | err = sock_gethostbyname(address, &hp); |
217 | if (err != IO_DONE) return sock_hoststrerror(err); | ||
215 | addr = (struct in_addr **) hp->h_addr_list; | 218 | addr = (struct in_addr **) hp->h_addr_list; |
216 | memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); | 219 | memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); |
217 | } | 220 | } |
218 | } else remote.sin_family = AF_UNSPEC; | 221 | } else remote.sin_family = AF_UNSPEC; |
219 | err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); | 222 | err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); |
220 | if (err) sock_destroy(ps); | 223 | if (err != IO_DONE) sock_destroy(ps); |
221 | return err; | 224 | return sock_strerror(err); |
222 | } | 225 | } |
223 | 226 | ||
224 | /*-------------------------------------------------------------------------*\ | 227 | /*-------------------------------------------------------------------------*\ |
@@ -227,7 +230,7 @@ const char *inet_tryconnect(p_sock ps, const char *address, | |||
227 | const char *inet_trybind(p_sock ps, const char *address, unsigned short port) | 230 | const char *inet_trybind(p_sock ps, const char *address, unsigned short port) |
228 | { | 231 | { |
229 | struct sockaddr_in local; | 232 | struct sockaddr_in local; |
230 | const char *err; | 233 | int err; |
231 | memset(&local, 0, sizeof(local)); | 234 | memset(&local, 0, sizeof(local)); |
232 | /* address is either wildcard or a valid ip address */ | 235 | /* address is either wildcard or a valid ip address */ |
233 | local.sin_addr.s_addr = htonl(INADDR_ANY); | 236 | local.sin_addr.s_addr = htonl(INADDR_ANY); |
@@ -235,15 +238,16 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port) | |||
235 | local.sin_family = AF_INET; | 238 | local.sin_family = AF_INET; |
236 | if (strcmp(address, "*") && | 239 | if (strcmp(address, "*") && |
237 | (!strlen(address) || !inet_aton(address, &local.sin_addr))) { | 240 | (!strlen(address) || !inet_aton(address, &local.sin_addr))) { |
238 | struct hostent *hp = gethostbyname(address); | 241 | struct hostent *hp = NULL; |
239 | struct in_addr **addr; | 242 | struct in_addr **addr; |
240 | if (!hp) return sock_hoststrerror(); | 243 | err = sock_gethostbyname(address, &hp); |
244 | if (err != IO_DONE) return sock_hoststrerror(err); | ||
241 | addr = (struct in_addr **) hp->h_addr_list; | 245 | addr = (struct in_addr **) hp->h_addr_list; |
242 | memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); | 246 | memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); |
243 | } | 247 | } |
244 | err = sock_bind(ps, (SA *) &local, sizeof(local)); | 248 | err = sock_bind(ps, (SA *) &local, sizeof(local)); |
245 | if (err) sock_destroy(ps); | 249 | if (err != IO_DONE) sock_destroy(ps); |
246 | return err; | 250 | return sock_strerror(err); |
247 | } | 251 | } |
248 | 252 | ||
249 | /*-------------------------------------------------------------------------*\ | 253 | /*-------------------------------------------------------------------------*\ |