#At file:///data0/magnus/mysql/7.0-addrinfo/ based on revid:jonas@stripped
4367 Magnus Blåudd 2011-05-11
Bug#12542120 - rewrite ndb_getinaddr to use getaddrinfo
- change Ndb_getInAddr to use 'getaddrinfo' to convert the address
string into a IPv4 inaddr
- add NdbGetInAddr-t unit test
modified:
storage/ndb/src/common/portlib/CMakeLists.txt
storage/ndb/src/common/portlib/Makefile.am
storage/ndb/src/common/portlib/NdbTCP.cpp
=== modified file 'storage/ndb/src/common/portlib/CMakeLists.txt'
--- a/storage/ndb/src/common/portlib/CMakeLists.txt 2011-02-02 00:40:07 +0000
+++ b/storage/ndb/src/common/portlib/CMakeLists.txt 2011-05-11 09:44:39 +0000
@@ -37,4 +37,8 @@ SET_TARGET_PROPERTIES(NdbDir-t
PROPERTIES COMPILE_FLAGS "-DTEST_NDBDIR")
TARGET_LINK_LIBRARIES(NdbDir-t ndbportlib)
+ADD_EXECUTABLE(NdbGetInAddr-t NdbTCP.cpp)
+SET_TARGET_PROPERTIES(NdbGetInAddr-t
+ PROPERTIES COMPILE_FLAGS "-DTEST_NDBGETINADDR")
+
=== modified file 'storage/ndb/src/common/portlib/Makefile.am'
--- a/storage/ndb/src/common/portlib/Makefile.am 2011-02-01 23:27:25 +0000
+++ b/storage/ndb/src/common/portlib/Makefile.am 2011-05-11 09:44:39 +0000
@@ -33,7 +33,7 @@ EXTRA_PROGRAMS = memtest PortLibTest mun
PortLibTest_SOURCES = NdbPortLibTest.cpp
munmaptest_SOURCES = munmaptest.cpp
-noinst_PROGRAMS = NdbDir-t NdbNuma-t
+noinst_PROGRAMS = NdbDir-t NdbNuma-t NdbGetInAddr-t
NdbDir_t_SOURCES = NdbDir.cpp \
$(top_srcdir)/storage/ndb/src/common/util/basestring_vsnprintf.c
@@ -46,3 +46,5 @@ NdbNuma_t_SOURCES = NdbNuma.cpp
NdbNuma_t_CXXFLAGS = -DTEST_NDBNUMA
NdbNuma_t_LDADD = $(top_builddir)/mysys/libmysyslt.la @LIBDL@
+NdbGetInAddr_t_SOURCES = NdbTCP.cpp
+NdbGetInAddr_t_CXXFLAGS = -DTEST_NDBGETINADDR
=== modified file 'storage/ndb/src/common/portlib/NdbTCP.cpp'
--- a/storage/ndb/src/common/portlib/NdbTCP.cpp 2011-02-01 23:27:25 +0000
+++ b/storage/ndb/src/common/portlib/NdbTCP.cpp 2011-05-11 09:44:39 +0000
@@ -1,5 +1,5 @@
/*
- Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -17,43 +17,121 @@
#include <ndb_global.h>
-#include <my_net.h>
#include <NdbTCP.h>
-
-
extern "C"
-int
+int
Ndb_getInAddr(struct in_addr * dst, const char *address)
{
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET; // Only IPv4 address
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+
+ struct addrinfo* ai_list;
+ if (getaddrinfo(address, NULL, &hints, &ai_list) != 0)
+ {
+ dst->s_addr = INADDR_NONE;
+ return -1;
+ }
+
+ /* Return sin_addr for the first address returned */
+ struct sockaddr_in* sin = (struct sockaddr_in*)ai_list->ai_addr;
+ memcpy(dst, &sin->sin_addr, sizeof(struct in_addr));
+
+ freeaddrinfo(ai_list);
+ return 0;
+}
+
+#ifdef TEST_NDBGETINADDR
+#include <NdbTap.hpp>
+
+static void
+CHECK(const char* address, int expected_res, bool is_numeric= false)
+{
+ struct in_addr addr = { 0 };
+
+ fprintf(stderr, "Checking '%s'\n", address);
+
+ int res= Ndb_getInAddr(&addr, address);
+
+ if (res != expected_res)
+ {
+ fprintf(stderr, "> unexpected result: %d, expected: %d\n",
+ res, expected_res);
+ abort();
+ }
+
+ if (res != 0)
+ {
+ fprintf(stderr, "> returned -1, checking INADDR_NONE\n");
+
+ // Should return INADDR_NONE when when lookup fails
+ struct in_addr none = { INADDR_NONE };
+ if (memcmp(&addr, &none, sizeof(none)) != 0)
+ {
+ fprintf(stderr, "> didn't reurn INADDR_NONE after failure, "
+ "got: '%s', expected; '%s'\n",
+ inet_ntoa(addr), inet_ntoa(none));
+ abort();
+ }
+ fprintf(stderr, "> got INADDR_NONE\n");
+ return;
+ }
+
+ fprintf(stderr, "> '%s' -> '%s'\n", address, inet_ntoa(addr));
+
+ if (is_numeric)
{
- int tmp_errno;
- struct hostent tmp_hostent, *hp;
- char buff[GETHOSTBYNAME_BUFF_SIZE];
- hp = my_gethostbyname_r(address,&tmp_hostent,buff,sizeof(buff),
- &tmp_errno);
- if (hp)
+ // Check that numeric address always map back to itself
+ // ie. compare to value returned by 'inet_aton'
+ fprintf(stderr, "> Checking numeric address against inet_aton\n");
+ struct in_addr addr2;
+ if (inet_aton(address, &addr2) == 0)
{
- memcpy(dst, hp->h_addr, min(sizeof(*dst), (size_t) hp->h_length));
- my_gethostbyname_r_free();
- return 0;
+ fprintf(stderr, "> inet_aton failed to convert the supposedly "
+ "numeric address: '%s'\n", address);
+ abort();
}
- my_gethostbyname_r_free();
+ fprintf(stderr, "> inet_aton(%s) -> '%s'\n", address, inet_ntoa(addr2));
+
+ if (memcmp(&addr, &addr2, sizeof(struct in_addr)) != 0)
+ {
+ fprintf(stderr, "> numeric address '%s' didn't map to same value as "
+ "inet_aton: '%s'", inet_ntoa(addr2));
+ abort();
+ }
+ fprintf(stderr, "> ok\n");
}
- /* Try it as aaa.bbb.ccc.ddd. */
- dst->s_addr = inet_addr(address);
- if (dst->s_addr !=
-#ifdef INADDR_NONE
- INADDR_NONE
-#else
- -1
-#endif
- )
+}
+
+TAPTEST(NdbGetInAddr)
+{
+ CHECK("localhost", 0);
+ CHECK("127.0.0.1", 0, true);
+
+ char hostname_buf[256];
+ if (gethostname(hostname_buf, sizeof(hostname_buf)) == 0)
{
- return 0;
+ // Check this machines hostname
+ CHECK(hostname_buf, 0);
+
+ struct in_addr addr;
+ Ndb_getInAddr(&addr, hostname_buf);
+ // Convert hostname to dotted decimal string ip and check
+ CHECK(inet_ntoa(addr), 0, true);
}
- return -1;
+ CHECK("unknown_?host", -1); // Does not exist
+ CHECK("3ffe:1900:4545:3:200:f8ff:fe21:67cf", -1); // No IPv6
+ CHECK("fe80:0:0:0:200:f8ff:fe21:67cf", -1);
+ CHECK("fe80::200:f8ff:fe21:67cf", -1);
+ CHECK("::1", -1); // the loopback, but still No IPv6
+
+ return 1; // OK
+
}
+#endif
static inline
Attachment: [text/bzr-bundle] bzr/magnus.blaudd@oracle.com-20110511094439-296eztlhfkq27dfh.bundle
| Thread |
|---|
| • bzr commit into mysql-5.1-telco-7.0 branch (magnus.blaudd:4367) Bug#12542120 | Magnus Blåudd | 11 May |