aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorunknown <diego@csz852.win2k.cs.ust.hk>2013-05-24 18:33:43 +0800
committerunknown <diego@csz852.win2k.cs.ust.hk>2013-05-24 18:33:43 +0800
commitcbc77440c8d84487ed652ee65551df0f8592a53f (patch)
tree4a6fc94d2ea9149990ff0fa86fa34f132d99ad13 /src
parentca48baf495d57a592030db82d5e4407596f0afde (diff)
parent571308a94e30f15669c1c5e2df4d7713885e8859 (diff)
downloadluasocket-cbc77440c8d84487ed652ee65551df0f8592a53f.tar.gz
luasocket-cbc77440c8d84487ed652ee65551df0f8592a53f.tar.bz2
luasocket-cbc77440c8d84487ed652ee65551df0f8592a53f.zip
Merge branch 'lua52-mingw' of https://github.com/pkulchenko/luasocket into pkulchenko
Diffstat (limited to 'src')
-rw-r--r--src/inet.c50
-rw-r--r--src/inet.h6
2 files changed, 56 insertions, 0 deletions
diff --git a/src/inet.c b/src/inet.c
index 69d32e6..1017022 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -507,4 +507,54 @@ int inet_aton(const char *cp, struct in_addr *inp)
507} 507}
508#endif 508#endif
509 509
510// inet_ntop/inet_pton for MinGW from http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html
510 511
512#ifdef INET_PTON
513const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
514{
515 if (af == AF_INET)
516 {
517 struct sockaddr_in in;
518 memset(&in, 0, sizeof(in));
519 in.sin_family = AF_INET;
520 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
521 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
522 return dst;
523 }
524 else if (af == AF_INET6)
525 {
526 struct sockaddr_in6 in;
527 memset(&in, 0, sizeof(in));
528 in.sin6_family = AF_INET6;
529 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
530 getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
531 return dst;
532 }
533 return NULL;
534}
535
536int inet_pton(int af, const char *src, void *dst)
537{
538 struct addrinfo hints, *res, *ressave;
539
540 memset(&hints, 0, sizeof(struct addrinfo));
541 hints.ai_family = af;
542
543 if (getaddrinfo(src, NULL, &hints, &res) != 0)
544 {
545 return -1;
546 }
547
548 ressave = res;
549
550 while (res)
551 {
552 memcpy(dst, res->ai_addr, res->ai_addrlen);
553 res = res->ai_next;
554 }
555
556 freeaddrinfo(ressave);
557 return 0;
558}
559
560#endif
diff --git a/src/inet.h b/src/inet.h
index 4678ba6..2f72533 100644
--- a/src/inet.h
+++ b/src/inet.h
@@ -20,6 +20,7 @@
20 20
21#ifdef _WIN32 21#ifdef _WIN32
22#define INET_ATON 22#define INET_ATON
23#define INET_PTON
23#endif 24#endif
24 25
25int inet_open(lua_State *L); 26int inet_open(lua_State *L);
@@ -42,4 +43,9 @@ int inet_optsocktype(lua_State* L, int narg, const char* def);
42int inet_aton(const char *cp, struct in_addr *inp); 43int inet_aton(const char *cp, struct in_addr *inp);
43#endif 44#endif
44 45
46#ifdef INET_PTON
47const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
48int inet_pton(int af, const char *src, void *dst);
49#endif
50
45#endif /* INET_H */ 51#endif /* INET_H */