| 44 | const jalib::JSockAddr jalib::JSockAddr::ANY(NULL); |
| 45 | |
| 46 | jalib::JSockAddr::JSockAddr(const char *hostname /* == NULL*/, |
| 47 | int port /* == -1*/) |
| 48 | { |
| 49 | // Initialization for any case |
| 50 | memset((void *)&_addr, 0, sizeof(_addr)); |
| 51 | for (unsigned int i = 0; i < (max_count + 1); i++) { |
| 52 | _addr[i].sin_family = AF_INET; |
| 53 | } |
| 54 | _count = 0; |
| 55 | |
| 56 | if (hostname == NULL) { |
| 57 | _count = 1; |
| 58 | _addr[0].sin_addr.s_addr = INADDR_ANY; |
| 59 | if (port != -1) { |
| 60 | _addr[0].sin_port = htons(port); |
| 61 | } |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | #if 0 |
| 66 | struct hostent ret, *result; |
| 67 | char buf[1024]; |
| 68 | int h_errnop; |
| 69 | |
| 70 | int res = |
| 71 | gethostbyname_r(hostname, &ret, buf, sizeof buf, &result, &h_errnop); |
| 72 | |
| 73 | // Fall back to gethostbyname on error |
| 74 | if (res != 0) { |
| 75 | JWARNING(false) (hostname) (hstrerror(h_errno)) |
| 76 | .Text("gethostbyname_r failed, calling gethostbyname"); |
| 77 | result = gethostbyname(hostname); |
| 78 | } |
| 79 | |
| 80 | JWARNING(result != NULL) (hostname).Text("No such host"); |
| 81 | |
| 82 | if (result != NULL) { |
| 83 | JASSERT((int)sizeof(_addr.sin_addr.s_addr) <= result->h_length) |
| 84 | (sizeof(_addr.sin_addr.s_addr)) (result->h_length); |
| 85 | |
| 86 | memcpy(&_addr.sin_addr.s_addr, result->h_addr, result->h_length); |
| 87 | if (port != -1) { |
| 88 | _addr.sin_port = htons(port); |
| 89 | } |
| 90 | } else { // else (hostname, port) not valid; poison the port number |
| 91 | JASSERT(typeid(_addr.sin_port) == typeid(in_port_t)); |
| 92 | _addr.sin_port = (in_port_t)-2; |
| 93 | } |
| 94 | #else // if 0 |
| 95 | struct addrinfo hints; |
| 96 | struct addrinfo *res; |
| 97 | memset(&hints, '\0', sizeof hints); |
| 98 | hints.ai_family = AF_INET; |
| 99 | hints.ai_socktype = SOCK_STREAM; |
| 100 | hints.ai_flags = AI_ADDRCONFIG; |
| 101 | |
| 102 | /* FIXME: Ulrich Drepper states the following about getaddrinfo(): |
| 103 | * The most important thing when using getaddrinfo is to make sure that all |
nothing calls this directly
no test coverage detected