diff options
| author | moteus <mimir@newmail.ru> | 2013-05-28 14:06:18 +0400 |
|---|---|---|
| committer | moteus <mimir@newmail.ru> | 2013-05-28 14:06:18 +0400 |
| commit | 5167ddaf499cf198b10208a2f76c27629e99ae1b (patch) | |
| tree | 193b3a7199ad5b0cfbe8a0d02e5fa469394608b6 /src/inet.c | |
| parent | 45ff0e17753201f4c42c714fdc28898c73e59eb9 (diff) | |
| parent | 2d51d6168874cdb2b72ee4f56f414d9a9a9d92e5 (diff) | |
| download | luasocket-5167ddaf499cf198b10208a2f76c27629e99ae1b.tar.gz luasocket-5167ddaf499cf198b10208a2f76c27629e99ae1b.tar.bz2 luasocket-5167ddaf499cf198b10208a2f76c27629e99ae1b.zip | |
Merge branch 'unstable' of git://github.com/diegonehab/luasocket into moteus-lua52
Diffstat (limited to 'src/inet.c')
| -rw-r--r-- | src/inet.c | 78 |
1 files changed, 34 insertions, 44 deletions
| @@ -79,24 +79,22 @@ static int inet_global_tohostname(lua_State *L) { | |||
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | static int inet_global_getnameinfo(lua_State *L) { | 81 | static int inet_global_getnameinfo(lua_State *L) { |
| 82 | char hbuf[NI_MAXHOST]; | ||
| 83 | char sbuf[NI_MAXSERV]; | ||
| 82 | int i, ret; | 84 | int i, ret; |
| 83 | char host[1024]; | ||
| 84 | char serv[32]; | ||
| 85 | struct addrinfo hints; | 85 | struct addrinfo hints; |
| 86 | struct addrinfo *resolved, *iter; | 86 | struct addrinfo *resolved, *iter; |
| 87 | const char *node = luaL_optstring(L, 1, NULL); | 87 | const char *host = luaL_optstring(L, 1, NULL); |
| 88 | const char *service = luaL_optstring(L, 2, NULL); | 88 | const char *serv = luaL_optstring(L, 2, NULL); |
| 89 | 89 | ||
| 90 | if (!(node || service)) | 90 | if (!(host || serv)) |
| 91 | luaL_error(L, "You have to specify a hostname, a service, or both"); | 91 | luaL_error(L, "host and serv cannot be both nil"); |
| 92 | 92 | ||
| 93 | memset(&hints, 0, sizeof(hints)); | 93 | memset(&hints, 0, sizeof(hints)); |
| 94 | hints.ai_socktype = SOCK_STREAM; | 94 | hints.ai_socktype = SOCK_STREAM; |
| 95 | hints.ai_family = PF_UNSPEC; | 95 | hints.ai_family = PF_UNSPEC; |
| 96 | 96 | ||
| 97 | /* getaddrinfo must get a node and a service argument */ | 97 | ret = getaddrinfo(host, serv, &hints, &resolved); |
| 98 | ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7", | ||
| 99 | &hints, &resolved); | ||
| 100 | if (ret != 0) { | 98 | if (ret != 0) { |
| 101 | lua_pushnil(L); | 99 | lua_pushnil(L); |
| 102 | lua_pushstring(L, socket_gaistrerror(ret)); | 100 | lua_pushstring(L, socket_gaistrerror(ret)); |
| @@ -105,19 +103,19 @@ static int inet_global_getnameinfo(lua_State *L) { | |||
| 105 | 103 | ||
| 106 | lua_newtable(L); | 104 | lua_newtable(L); |
| 107 | for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { | 105 | for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { |
| 108 | getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, host, | 106 | getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, |
| 109 | node ? (socklen_t) sizeof(host) : 0, serv, service ? (socklen_t) sizeof(serv) : 0, 0); | 107 | hbuf, host? (socklen_t) sizeof(hbuf): 0, |
| 110 | 108 | sbuf, serv? (socklen_t) sizeof(sbuf): 0, 0); | |
| 111 | if (node) { | 109 | if (host) { |
| 112 | lua_pushnumber(L, i); | 110 | lua_pushnumber(L, i); |
| 113 | lua_pushstring(L, host); | 111 | lua_pushstring(L, hbuf); |
| 114 | lua_settable(L, -3); | 112 | lua_settable(L, -3); |
| 115 | } | 113 | } |
| 116 | } | 114 | } |
| 117 | freeaddrinfo(resolved); | 115 | freeaddrinfo(resolved); |
| 118 | 116 | ||
| 119 | if (service) { | 117 | if (serv) { |
| 120 | lua_pushstring(L, serv); | 118 | lua_pushstring(L, sbuf); |
| 121 | return 2; | 119 | return 2; |
| 122 | } else { | 120 | } else { |
| 123 | return 1; | 121 | return 1; |
| @@ -176,20 +174,10 @@ static int inet_global_getaddrinfo(lua_State *L) | |||
| 176 | } | 174 | } |
| 177 | lua_newtable(L); | 175 | lua_newtable(L); |
| 178 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { | 176 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { |
| 179 | char hbuf[NI_MAXHOST] | 177 | char hbuf[NI_MAXHOST]; |
| 180 | #ifndef _WINDOWS | 178 | ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, |
| 181 | ,sbuf[NI_MAXSERV] | 179 | hbuf, (socklen_t) sizeof(hbuf), NULL, 0, NI_NUMERICHOST); |
| 182 | #endif | 180 | if (ret){ |
| 183 | ; | ||
| 184 | ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf, | ||
| 185 | (socklen_t) sizeof(hbuf), | ||
| 186 | #ifdef _WINDOWS | ||
| 187 | NULL, 0, | ||
| 188 | #else | ||
| 189 | sbuf, 0, | ||
| 190 | #endif | ||
| 191 | NI_NUMERICHOST); | ||
| 192 | if(ret){ | ||
| 193 | lua_pushnil(L); | 181 | lua_pushnil(L); |
| 194 | lua_pushstring(L, socket_gaistrerror(ret)); | 182 | lua_pushstring(L, socket_gaistrerror(ret)); |
| 195 | return 2; | 183 | return 2; |
| @@ -218,7 +206,6 @@ static int inet_global_getaddrinfo(lua_State *L) | |||
| 218 | return 1; | 206 | return 1; |
| 219 | } | 207 | } |
| 220 | 208 | ||
| 221 | |||
| 222 | /*-------------------------------------------------------------------------*\ | 209 | /*-------------------------------------------------------------------------*\ |
| 223 | * Gets the host name | 210 | * Gets the host name |
| 224 | \*-------------------------------------------------------------------------*/ | 211 | \*-------------------------------------------------------------------------*/ |
| @@ -237,7 +224,6 @@ static int inet_global_gethostname(lua_State *L) | |||
| 237 | } | 224 | } |
| 238 | 225 | ||
| 239 | 226 | ||
| 240 | |||
| 241 | /*=========================================================================*\ | 227 | /*=========================================================================*\ |
| 242 | * Lua methods | 228 | * Lua methods |
| 243 | \*=========================================================================*/ | 229 | \*=========================================================================*/ |
| @@ -520,7 +506,7 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv, | |||
| 520 | * Some systems do not provide this so that we provide our own. It's not | 506 | * Some systems do not provide this so that we provide our own. It's not |
| 521 | * marvelously fast, but it works just fine. | 507 | * marvelously fast, but it works just fine. |
| 522 | \*-------------------------------------------------------------------------*/ | 508 | \*-------------------------------------------------------------------------*/ |
| 523 | #ifdef INET_ATON | 509 | #ifdef LUASOCKET_INET_ATON |
| 524 | int inet_aton(const char *cp, struct in_addr *inp) | 510 | int inet_aton(const char *cp, struct in_addr *inp) |
| 525 | { | 511 | { |
| 526 | unsigned int a = 0, b = 0, c = 0, d = 0; | 512 | unsigned int a = 0, b = 0, c = 0, d = 0; |
| @@ -547,7 +533,7 @@ int inet_aton(const char *cp, struct in_addr *inp) | |||
| 547 | * http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html | 533 | * http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html |
| 548 | \*-------------------------------------------------------------------------*/ | 534 | \*-------------------------------------------------------------------------*/ |
| 549 | 535 | ||
| 550 | #ifdef INET_PTON | 536 | #ifdef LUASOCKET_INET_PTON |
| 551 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) | 537 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) |
| 552 | { | 538 | { |
| 553 | if (af == AF_INET) { | 539 | if (af == AF_INET) { |
| @@ -572,19 +558,23 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) | |||
| 572 | 558 | ||
| 573 | int inet_pton(int af, const char *src, void *dst) | 559 | int inet_pton(int af, const char *src, void *dst) |
| 574 | { | 560 | { |
| 575 | struct addrinfo hints, *res, *ressave; | 561 | struct addrinfo hints, *res; |
| 562 | int ret = 1; | ||
| 576 | memset(&hints, 0, sizeof(struct addrinfo)); | 563 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 577 | hints.ai_family = af; | 564 | hints.ai_family = af; |
| 578 | if (getaddrinfo(src, NULL, &hints, &res) != 0) { | 565 | hints.ai_flags = AI_NUMERICHOST; |
| 579 | return -1; | 566 | if (getaddrinfo(src, NULL, &hints, &res) != 0) return -1; |
| 580 | } | 567 | if (af == AF_INET) { |
| 581 | ressave = res; | 568 | struct sockaddr_in *in = (struct sockaddr_in *) res->ai_addr; |
| 582 | while (res) { | 569 | memcpy(dst, &in->sin_addr, sizeof(in->sin_addr)); |
| 583 | memcpy(dst, res->ai_addr, res->ai_addrlen); | 570 | } else if (af == AF_INET6) { |
| 584 | res = res->ai_next; | 571 | struct sockaddr_in6 *in = (struct sockaddr_in6 *) res->ai_addr; |
| 572 | memcpy(dst, &in->sin6_addr, sizeof(in->sin6_addr)); | ||
| 573 | } else { | ||
| 574 | ret = -1; | ||
| 585 | } | 575 | } |
| 586 | freeaddrinfo(ressave); | 576 | freeaddrinfo(res); |
| 587 | return 0; | 577 | return ret; |
| 588 | } | 578 | } |
| 589 | 579 | ||
| 590 | #endif | 580 | #endif |
