Unix Programming - static struct addrinfo hints = { 0, AF_INET, SOCK_DGRAM, ... };

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2005 > static struct addrinfo hints = { 0, AF_INET, SOCK_DGRAM, ... };





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author static struct addrinfo hints = { 0, AF_INET, SOCK_DGRAM, ... };
Alexander Farber

2005-09-22, 7:50 am

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

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com