19 #include <ndb_global.h>
25 #define INADDR_NONE -1
31 Ndb_getInAddr(
struct in_addr * dst,
const char *address)
34 memset(&hints, 0,
sizeof(hints));
35 hints.ai_family = AF_INET;
36 hints.ai_socktype = SOCK_STREAM;
37 hints.ai_protocol = IPPROTO_TCP;
40 if (getaddrinfo(address, NULL, &hints, &ai_list) != 0)
42 dst->s_addr = INADDR_NONE;
47 struct sockaddr_in* sin = (
struct sockaddr_in*)ai_list->ai_addr;
48 memcpy(dst, &sin->sin_addr,
sizeof(
struct in_addr));
50 freeaddrinfo(ai_list);
54 #ifdef TEST_NDBGETINADDR
58 CHECK(
const char* address,
int expected_res,
bool is_numeric=
false)
62 fprintf(stderr,
"Testing '%s'\n", address);
64 int res= Ndb_getInAddr(&addr, address);
66 if (res != expected_res)
68 fprintf(stderr,
"> unexpected result: %d, expected: %d\n",
75 fprintf(stderr,
"> returned -1, checking INADDR_NONE\n");
79 none.s_addr = INADDR_NONE;
80 if (memcmp(&addr, &none,
sizeof(none)) != 0)
82 fprintf(stderr,
"> didn't return INADDR_NONE after failure, "
83 "got: '%s', expected; '%s'\n",
84 inet_ntoa(addr), inet_ntoa(none));
87 fprintf(stderr,
"> ok\n");
91 fprintf(stderr,
"> '%s' -> '%s'\n", address, inet_ntoa(addr));
97 fprintf(stderr,
"> Checking numeric address against inet_addr\n");
99 addr2.s_addr = inet_addr(address);
100 fprintf(stderr,
"> inet_addr(%s) -> '%s'\n", address, inet_ntoa(addr2));
102 if (memcmp(&addr, &addr2,
sizeof(
struct in_addr)) != 0)
104 fprintf(stderr,
"> numeric address '%s' didn't map to same value as "
105 "inet_addr: '%s'", address, inet_ntoa(addr2));
108 fprintf(stderr,
"> ok\n");
120 socket_library_init(
void)
123 WORD requested_version = MAKEWORD( 2, 0 );
125 if (WSAStartup( requested_version, &wsa_data ))
127 fprintf(stderr,
"failed to init Winsock\n");
132 if (wsa_data.wVersion != requested_version)
135 fprintf(stderr,
"Wrong version of Winsock loaded\n");
151 can_resolve_hostname(
const char*
name)
153 fprintf(stderr,
"Checking if '%s' can be used for testing\n", name);
155 memset(&hints, 0,
sizeof(hints));
156 hints.ai_family = AF_INET;
157 hints.ai_socktype = SOCK_STREAM;
158 hints.ai_protocol = IPPROTO_TCP;
161 int err = getaddrinfo(name, NULL, &hints, &ai_list);
165 fprintf(stderr,
"> '%s' -> error: %d '%s'\n",
166 name, err, gai_strerror(err));
168 if (err == EAI_NODATA ||
172 fprintf(stderr,
"> skipping tests with this name...\n");
180 freeaddrinfo(ai_list);
186 TAPTEST(NdbGetInAddr)
188 socket_library_init();
190 if (can_resolve_hostname(
"localhost"))
191 CHECK(
"localhost", 0);
192 CHECK(
"127.0.0.1", 0,
true);
194 char hostname_buf[256];
195 if (gethostname(hostname_buf,
sizeof(hostname_buf)) == 0 &&
196 can_resolve_hostname(hostname_buf))
199 CHECK(hostname_buf, 0);
202 Ndb_getInAddr(&addr, hostname_buf);
204 CHECK(inet_ntoa(addr), 0,
true);
206 CHECK(
"unknown_?host", -1);
207 CHECK(
"3ffe:1900:4545:3:200:f8ff:fe21:67cf", -1);
208 CHECK(
"fe80:0:0:0:200:f8ff:fe21:67cf", -1);
209 CHECK(
"fe80::200:f8ff:fe21:67cf", -1);
212 socket_library_end();
231 #define my_FD_SET(sock,set) FD_SET(ndb_socket_get_native(sock), set)
232 #define my_FD_ISSET(sock,set) FD_ISSET(ndb_socket_get_native(sock), set)
235 int Ndb_check_socket_hup(NDB_SOCKET_TYPE sock)
238 struct pollfd pfd[1];
242 pfd[0].events= POLLHUP | POLLIN | POLLOUT | POLLNVAL;
245 if(pfd[0].revents & (POLLHUP|POLLERR))
250 fd_set readfds, writefds, errorfds;
253 SOCKET_SIZE_TYPE s_err_size=
sizeof(s_err);
259 my_FD_SET(sock, &readfds);
260 my_FD_SET(sock, &writefds);
261 my_FD_SET(sock, &errorfds);
263 if(select(my_socket_nfds(sock,0)+1, &readfds, &writefds, &errorfds, &tv)<0)
266 if(my_FD_ISSET(sock,&errorfds))
270 if (my_getsockopt(sock, SOL_SOCKET, SO_ERROR, &s_err, &s_err_size) != 0)