mclellan, dave wrote: > Hi: I can't seem to find inet_ntop and _pton implemented under WIN32 (and > 64-bit). WSAAddressToString does the job, except its answer doesn't comply > with inet_ntop. WSAAddressToString returnes the IP address enclosed in > square brackets, followed by the port. I need just the address, not the > other decorations. > > Have I missed something? is inet_ntop really there somewhere, but I can't > find it? It's not mentioned in MSDN that I can find.
It is not there, in general I would use something to wrap it like the two functions defined below. Greets, Jeroen -- #ifdef _WIN32 const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) { if (af == AF_INET) { struct sockaddr_in in; memset(&in, 0, sizeof(in)); in.sin_family = AF_INET; memcpy(&in.sin_addr, src, sizeof(struct in_addr)); getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST); return dst; } else if (af == AF_INET6) { struct sockaddr_in6 in; memset(&in, 0, sizeof(in)); in.sin6_family = AF_INET6; memcpy(&in.sin6_addr, src, sizeof(struct in_addr6)); getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST); return dst; } return NULL; } int inet_pton(int af, const char *src, void *dst) { struct addrinfo hints, *res, *ressave; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = af; if (getaddrinfo(src, NULL, &hints, &res) != 0) { dolog(LOG_ERR, "Couldn't resolve host %s\n", src); return -1; } ressave = res; while (res) { memcpy(dst, res->ai_addr, res->ai_addrlen); res = res->ai_next; } freeaddrinfo(ressave); return 0; } #endif
signature.asc
Description: OpenPGP digital signature