/* * Return a 'struct hostent *' for an address. */ struct hostent *W32_CALL gethostbyaddr (const char *addr_name, int len, int type) { struct _hostent h; SOCK_DEBUGF (("\ngethostbyaddr: %s", (type == AF_INET && addr_name) ? inet_ntoa(*(struct in_addr*)addr_name) : (type == AF_INET6 && addr_name) ? _inet6_ntoa(addr_name) : "")); #if defined(USE_IPV6) if (type == AF_INET6 && len == sizeof(struct in6_addr)) { struct hostent *he; SOCK_ENTER_SCOPE(); he = gethostbyaddr6 (addr_name); SOCK_LEAVE_SCOPE(); return (he); } #endif if (gethostbyaddr_internal (addr_name, len, type, &h)) { SOCK_DEBUGF ((" `%s'", h.h_name)); if (!did_lookup) SOCK_DEBUGF ((", %s", h.h_timeout ? "cached" : "hosts-file")); return fill_hostent (&h); } SOCK_DEBUGF ((" failed (%s) ", did_lookup ? dom_strerror(dom_errno) : hstrerror(h_errno))); return (NULL); }
int W32_CALL getsockname (int s, struct sockaddr *name, int *namelen) { Socket *socket = _socklist_find (s); int sa_len; SOCK_PROLOGUE (socket, "\ngetsockname:%d", s); sa_len = (socket->so_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); if (!name || !namelen || (*namelen < sa_len)) { SOCK_DEBUGF ((", EINVAL")); SOCK_ERRNO (EINVAL); if (namelen) *namelen = 0; return (-1); } if (!socket->local_addr) { SOCK_DEBUGF ((", EINVAL")); SOCK_ERRNO (EINVAL); /* according to HP/UX manpage */ return (-1); } VERIFY_RW (name, *namelen); *namelen = sa_len; memcpy (name, socket->local_addr, sa_len); #if defined(USE_IPV6) if (socket->so_family == AF_INET6) { const struct sockaddr_in6 *la = (const struct sockaddr_in6*)socket->local_addr; SOCK_DEBUGF ((", %s (%d)", _inet6_ntoa(&la->sin6_addr), ntohs(la->sin6_port))); ARGSUSED (la); } else #endif { const struct sockaddr_in *la = socket->local_addr; SOCK_DEBUGF ((", %s (%d)", inet_ntoa(la->sin_addr), ntohs(la->sin_port))); ARGSUSED (la); } return (0); }
int W32_CALL getpeername (int s, struct sockaddr *name, int *namelen) { Socket *socket = _socklist_find (s); int sa_len; SOCK_PROLOGUE (socket, "\ngetpeername:%d", s); sa_len = (socket->so_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); if (!name || !namelen || (*namelen < sa_len)) { SOCK_DEBUGF ((", EINVAL")); SOCK_ERRNO (EINVAL); if (namelen) *namelen = 0; return (-1); } if (!(socket->so_state & SS_ISCONNECTED)) { SOCK_DEBUGF ((", ENOTCONN")); SOCK_ERRNO (ENOTCONN); return (-1); } VERIFY_RW (name, *namelen); *namelen = sa_len; memcpy (name, socket->remote_addr, *namelen); #if defined(USE_IPV6) if (socket->so_family == AF_INET6) { const struct sockaddr_in6 *ra = (const struct sockaddr_in6*)socket->remote_addr; SOCK_DEBUGF ((", %s (%d)", _inet6_ntoa(&ra->sin6_addr), ntohs(ra->sin6_port))); ARGSUSED (ra); } else #endif { const struct sockaddr_in *ra = socket->remote_addr; SOCK_DEBUGF ((", %s (%d)", inet_ntoa(ra->sin_addr), ntohs(ra->sin_port))); ARGSUSED (ra); } return (0); }
struct hostent * W32_CALL getipnodebyaddr (const void *src, size_t len, int af, int *error) { struct hostent *he1, *he2; const BYTE *cp = (const BYTE*) src; SOCK_DEBUGF (("\ngetipnodebyaddr: ")); if (!src) { *error = NO_RECOVERY; return (NULL); } switch (af) { case AF_INET: if (len < INADDRSZ) { *error = NO_RECOVERY; return (NULL); } break; #if defined(USE_IPV6) case AF_INET6: if (len < IN6ADDRSZ) { *error = NO_RECOVERY; return (NULL); } break; #endif default: *error = NO_RECOVERY; return (NULL); } /* Look up IPv4 and IPv4 mapped/compatible addresses. */ if ((af == AF_INET6 && IN6_IS_ADDR_V4COMPAT(cp)) || (af == AF_INET6 && IN6_IS_ADDR_V4MAPPED(cp)) || (af == AF_INET)) { if (af == AF_INET6) cp += 12; SOCK_ENTER_SCOPE(); he1 = gethostbyaddr ((const char*)cp, 4, AF_INET); SOCK_LEAVE_SCOPE(); if (af == AF_INET) goto ret_copy; /* Convert from AF_INET to AF_INET6. */ he2 = copyandmerge (he1, NULL, af, error); if (he2) { memcpy (he2->h_addr, src, len); /* Restore original address */ SOCK_DEBUGF (("%s", af == AF_INET ? inet_ntoa(*(struct in_addr*)&he2->h_addr) : _inet6_ntoa(he2->h_addr))); } return (he2); } he1 = gethostbyaddr (src, len, AF_INET6); /* Lookup IPv6 address */ ret_copy: if (!he1) { *error = HOST_NOT_FOUND; return (NULL); } return copyandmerge (he1, NULL, af, error); }