aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Kulchenko <paul@kulchenko.com>2013-04-07 12:39:56 -0700
committerPaul Kulchenko <paul@kulchenko.com>2013-04-07 12:39:56 -0700
commit5a58786a39bbef7ed4805821cc921f1d40f12068 (patch)
treed5d412ec3a54e904e82b68945a153e60f656e4ed
parentd548a78e5516bcc85d44f1d419cf53c71d6dcd79 (diff)
downloadluasocket-5a58786a39bbef7ed4805821cc921f1d40f12068.tar.gz
luasocket-5a58786a39bbef7ed4805821cc921f1d40f12068.tar.bz2
luasocket-5a58786a39bbef7ed4805821cc921f1d40f12068.zip
Added inet_pton/inet_ntop for MinGW on Windows; compiles with Lua52.
-rw-r--r--build-mingw.sh9
-rw-r--r--src/inet.c50
-rw-r--r--src/inet.h6
3 files changed, 65 insertions, 0 deletions
diff --git a/build-mingw.sh b/build-mingw.sh
new file mode 100644
index 0000000..2387ae6
--- /dev/null
+++ b/build-mingw.sh
@@ -0,0 +1,9 @@
1LUA=../lua-5.2.1/src/
2BUILD_FLAGS="-Wl,-s -O2 -shared -D LUA_COMPAT_MODULE -D IPV6_V6ONLY=1 -D WINVER=0x0501 -s -I src -I $LUA -L $LUA"
3
4mkdir -p lib/mime lib/socket
5gcc $BUILD_FLAGS -o "lib/mime/core.dll" src/mime.c -llua \
6 || { echo "Error: failed to build LuaSocket/mime"; exit 1; }
7gcc $BUILD_FLAGS -o "lib/socket/core.dll" \
8 src/{luasocket.c,auxiliar.c,buffer.c,except.c,inet.c,io.c,options.c,select.c,tcp.c,timeout.c,udp.c,wsocket.c} -lwsock32 -lws2_32 -llua \
9 || { echo "Error: failed to build LuaSocket/socket"; exit 1; }
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 */