|
发表于 2006-3-12 10:46:19
|
显示全部楼层
以下的代码是从commoncpp中抽出的, 应该有一点参照价值。
[code:1]
void IPV6Address::setAddress(const char *host)
{
if(hostname)
delString(hostname);
hostname = NULL;
if(!host) // The way this is currently used, this can never happen
host = "::";
#ifdef WIN32
if(!stricmp(host, "localhost"))
host = "::1";
#endif
if(!setIPAddress(host))
{
struct addrinfo hint, *list = NULL, *first;
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_INET6;
struct in6_addr *addr;
struct sockaddr_in6 *ip6addr;
if(getaddrinfo(host, NULL, &hint, &list) || !list)
{
if(ipaddr)
delete[] ipaddr;
ipaddr = new struct in6_addr[1];
memset((void *)&ipaddr[0], 0, sizeof(ipaddr));
return;
}
// Count the number of IP addresses returned
addr_count = 0;
first = list;
while(list)
{
++addr_count;
list = list->ai_next;
}
// Allocate enough memory
if(ipaddr)
delete[] ipaddr; // Cause this was allocated in base
ipaddr = new struct in6_addr[addr_count];
// Now go through the list again assigning to
// the member ipaddr;
list = first;
int i = 0;
while(list)
{
ip6addr = (struct sockaddr_in6 *)list->ai_addr;
addr = &ip6addr->sin6_addr;
if(validator)
(*validator)(*addr);
ipaddr[i++] = *addr;
list = list->ai_next;
}
freeaddrinfo(first);
}
}
[/code:1] |
|