Bug#503912: test program
Hi,
I didn't come up with a patch, the code is quite overwhelming... however,
please find in attachment a simple C++ program that shows the problem.
Thanks, Joost Damad
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string>
#include <iostream>
#include <cassert>
#include <cstring>
#include <sstream>
static std::string toString(const struct in6_addr &addr) {
char buf[INET6_ADDRSTRLEN + 1];
if (inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN) == 0) {
throw "fail";
}
std::string s(buf);
return s;
}
static bool test_resolve(const std::string &addr, int flags, struct in6_addr &addr2) {
struct addrinfo hints;
struct addrinfo *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
hints.ai_flags = flags;
int s = getaddrinfo(addr.c_str(), 0, &hints, &result);
if (s != 0) {
std::stringstream ss;
ss << addr << " : " << gai_strerror(s);
std::string str = ss.str();
return false;
}
if (result == 0) {
return false;
}
assert(result->ai_family == AF_INET6);
assert(result->ai_addrlen == sizeof(struct sockaddr_in6));
addr2 =
((struct sockaddr_in6 *) result->ai_addr)->sin6_addr;
freeaddrinfo(result);
return true;
}
int main(int argc, char **argv) {
struct in6_addr addr;
if (test_resolve("localhost", AI_V4MAPPED, addr)) {
std::cout << "resolved: " << toString(addr) << std::endl;
} else {
std::cout << "failed!" << std::endl;
}
if (test_resolve("localhost", AI_V4MAPPED | AI_ALL, addr)) {
std::cout << "resolved: " << toString(addr) << std::endl;
} else {
std::cout << "failed!" << std::endl;
}
if (test_resolve("www.damad.be", AI_V4MAPPED, addr)) {
std::cout << "resolved: " << toString(addr) << std::endl;
} else {
std::cout << "failed!" << std::endl;
}
if (test_resolve("www.damad.be", AI_V4MAPPED | AI_ALL, addr)) {
std::cout << "resolved: " << toString(addr) << std::endl;
} else {
std::cout << "failed!" << std::endl;
}
}
Reply to: