} sockaddr_in* addr = new sockaddr_in; memset(addr, 0, sizeof(*addr)); addr->sin_family = AF_INET; addr->sin_port = htons(port); hostent* hp = SocketBase::GetHostByName(host.c_str()); if (hp == 0) { DEBUG_SOCKET("failed to get host by name in connect") return false; } char* ip = InetNtoA(*(struct in_addr*)*hp->h_addr_list); addr->sin_addr.s_addr = InetAddr(ip); int err = 0; bool success = SocketBase::Connect(sock_,reinterpret_cast<struct sockaddr*>(addr), sizeof(*addr),timeout,err); if (!success) { DEBUG_SOCKET("failed to connect to " << host << ":" << port) eMessage_ = SocketBase::GetErrorMessage(err); } return success; }
char * AddrToAddrStr(char *const dst, size_t dsize, struct sockaddr_in * const saddrp, int dns, const char *fmt) { char addrName[128]; char *addrNamePtr; struct hostent *hp; char str[128]; char s_name[64]; char *dlim, *dp; const char *cp; if (dsize == 0) return NULL; memset(dst, 0, dsize); addrNamePtr = NULL; if (dns == 0) { InetNtoA(addrName, &saddrp->sin_addr, sizeof(addrName)); addrNamePtr = addrName; } else { #ifdef DNSSEC_LOCAL_VALIDATION val_status_t val_status; hp = val_gethostbyaddr(NULL, (const char*)&saddrp->sin_addr, sizeof(struct in_addr), AF_INET, &val_status); if ((hp != NULL) && (!val_istrusted(val_status))) hp = NULL; #else hp = gethostbyaddr((gethost_addrptr_t) &saddrp->sin_addr, sizeof(struct in_addr), AF_INET); #endif if ((hp != NULL) && (hp->h_name != NULL) && (hp->h_name[0] != '\0')) { addrNamePtr = hp->h_name; } else { InetNtoA(addrName, &saddrp->sin_addr, sizeof(addrName)); addrNamePtr = addrName; } } if (fmt == NULL) fmt = "%h:%p"; for (dp = dst, dlim = dp + dsize - 1; ; fmt++) { if (*fmt == '\0') { break; /* done */ } else if (*fmt == '%') { fmt++; if (*fmt == '%') { if (dp < dlim) *dp++ = '%'; } else if (*fmt == 'p') { sprintf(str, "%u", (unsigned int) ntohs(saddrp->sin_port)); for (cp = str; *cp != '\0'; cp++) if (dp < dlim) *dp++ = *cp; *dp = '\0'; } else if (*fmt == 'h') { if (addrNamePtr != NULL) { cp = addrNamePtr; } else { cp = "unknown"; } for ( ; *cp != '\0'; cp++) if (dp < dlim) *dp++ = *cp; *dp = '\0'; } else if (*fmt == 's') { cp = s_name; (void) ServicePortNumberToName(ntohs(saddrp->sin_port), s_name, sizeof(s_name), 0); for ( ; *cp != '\0'; cp++) if (dp < dlim) *dp++ = *cp; /* endservent(); */ *dp = '\0'; } else if ((*fmt == 't') || (*fmt == 'u')) { cp = s_name; (void) ServicePortNumberToName(ntohs(saddrp->sin_port), s_name, sizeof(s_name), (int) *fmt); for ( ; *cp != '\0'; cp++) if (dp < dlim) *dp++ = *cp; /* endservent(); */ *dp = '\0'; } else if (*fmt == '\0') { break; } else { if (dp < dlim) *dp++ = *fmt; } } else if (dp < dlim) { *dp++ = *fmt; } } *dp = '\0'; return (dst); } /* AddrToAddrStr */