09-22-05 12:50 PM
Hello,
is it ok to statically initialize a struct addrinfo this way:
static struct addrinfo hints =
{ 0, AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, NULL, 0, NULL};
? I have the following function connecting UDP sockets for a longer
list of hosts and would like to speed it up by getting rid of its
first 4 lines:
int
udp_connect(const char *hname)
{
struct addrinfo hints, *paihead, *pai;
int result, udpfd = -1;
bzero(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
/* resolve the host name and service port */
if ((result = getaddrinfo(hname, "echo", &hints, &paihead)) !=
0) {
die("Can't resolve '%s': %s", hname,
gai_strerror(result));
}
/* create a UDP socket and connect it to the host */
for (pai = paihead; pai != NULL; pai = pai->ai_next) {
if((udpfd = socket(pai->ai_family, pai->ai_socktype,
pai->ai_protocol)) == -1)
continue;
/* connect succeded -> print IP:port and proceed */
if(connect(udpfd, pai->ai_addr, pai->ai_addrlen) == 0)
{
if (debug)
print_address(hname, pai->ai_addr);
break;
}
close(udpfd);
udpfd = -1;
}
freeaddrinfo(paihead);
if (-1 == udpfd)
die("Can't connect a UDP socket to '%s'", hname);
return udpfd;
}
Or is addrinfo an "opaque" kind of structure?
I've looked up its definition in /usr/include/netdb.h
on Linux, HP-UX and OpenBSD and they seem to match...
Regards
Alex
[ Post a follow-up to this message ]
|