Unix Programming - getaddrinfo error on linux

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2005 > getaddrinfo error on linux





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 getaddrinfo error on linux
jose_luis_fdez_diaz_news@yahoo.es

2005-04-14, 6:03 pm


Hi,

In the next program:

#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>


int main()
{
struct addrinfo hints, *res;

hints.ai_flags=AI_CANONNAME;
hints.ai_family=AF_INET;
if (getaddrinfo("localhost", "domain", &hints, &res ))
{
perror("getaddrinfo: ");
printf("%s\n", gai_strerror(errno));
printf("%s\n", strerror(errno));

exit(1);
}
}



[jdiaz@llamas tmp]$ cc kk.c
[jdiaz@llamas tmp]$ a.out
getaddrinfo: : Success
Unknown error
Illegal seek
[jdiaz@llamas tmp]$




Why does getaddrinfo give an error ?

Thanks,
Jose Luis.

joe@invalid.address

2005-04-14, 6:03 pm

jose_luis_fdez_diaz_news@yahoo.es writes:

> In the next program:
>
> #include <stdio.h>
> #include <errno.h>
> #include <netdb.h>
> #include <sys/socket.h>
>
> int main()
> {
> struct addrinfo hints, *res;
>
> hints.ai_flags=AI_CANONNAME;
> hints.ai_family=AF_INET;
> if (getaddrinfo("localhost", "domain", &hints, &res ))
> {
> perror("getaddrinfo: ");
> printf("%s\n", gai_strerror(errno));
> printf("%s\n", strerror(errno));
>
> exit(1);
> }
> }


> Why does getaddrinfo give an error ?


You're passing the value of errno to gai_strerror(), which doesn't get
set by getaddrinfo(). You should be passing the return value of
getaddrinfo(). If you do that it should give you a more meaningful
message.

Joe
jose_luis_fdez_diaz_news@yahoo.es

2005-04-14, 6:03 pm

>
> You're passing the value of errno to gai_strerror(), which doesn't

get
> set by getaddrinfo(). You should be passing the return value of
> getaddrinfo(). If you do that it should give you a more meaningful
> message.
>
> Joe


[jdiaz@llamas tmp]$ a.out
getaddrinfo: : Success
ai_socktype not supported
Illegal seek
[jdiaz@llamas tmp]$

joe@invalid.address

2005-04-14, 6:03 pm

jose_luis_fdez_diaz_news@yahoo.es writes:

> get
>
> [jdiaz@llamas tmp]$ a.out
> getaddrinfo: : Success
> ai_socktype not supported
> Illegal seek
> [jdiaz@llamas tmp]$


Are you asking what "ai_socktype not supported" means? It's pretty
clear in the man page that ai_family, ai_socktype and ai_protocol
should be set to some supported value in the hints struct. You only
initialzed ai_family, so the other two were left uninitialized, and
had whatever values happened to be on the stack at that point. Try
this

#include <netdb.h>
#include <sys/socket.h>

int main()
{
struct addrinfo hints, *res;
int ret;

hints.ai_flags=AI_CANONNAME;
hints.ai_socktype = SOCK_STREAM;
hints.ai_family=AF_INET;
hints.ai_protocol = 0;
if ((ret=getaddrinfo("localhost", "domain", &hints, &res )))
{
printf("%s\n", gai_strerror(ret));
exit(1);
}
}


Joe
David Schwartz

2005-04-14, 6:03 pm


<jose_luis_fdez_diaz_news@yahoo.es> wrote in message
news:1113497308.928950.196040@g14g2000cwa.googlegroups.com...

> if (getaddrinfo("localhost", "domain", &hints, &res ))
> {
> perror("getaddrinfo: ");
> printf("%s\n", gai_strerror(errno));
> printf("%s\n", strerror(errno));
>
> exit(1);
> }
> }


> [jdiaz@llamas tmp]$ cc kk.c
> [jdiaz@llamas tmp]$ a.out
> getaddrinfo: : Success
> Unknown error
> Illegal seek
> [jdiaz@llamas tmp]$


> Why does getaddrinfo give an error ?


RTFM! You ignored the error code from 'getaddrinfo'!.

DS




David Schwartz

2005-04-14, 6:03 pm


<jose_luis_fdez_diaz_news@yahoo.es> wrote in message
news:1113500088.794563.116290@z14g2000cwz.googlegroups.com...

> [jdiaz@llamas tmp]$ a.out
> getaddrinfo: : Success
> ai_socktype not supported
> Illegal seek
> [jdiaz@llamas tmp]$


Try this:

int main()
{
struct addrinfo hints, *res;

memset(&hints, 0, sizeof(hints));
hints.ai_flags=AI_CANONNAME;
hints.ai_family=AF_INET;
int i=getaddrinfo("localhost", "domain", &hints, &res );
if(i!=0)
{
printf("%s\n", gai_strerror(i));
printf("%s\n", strerror(errno));

exit(1);
}
else printf("success\n");
}


jose_luis_fdez_diaz_news@yahoo.es

2005-04-15, 2:49 am


#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>


int main()
{
struct addrinfo hints, *res;
int r;

memset(&hints, 0, sizeof(hints));
hints.ai_flags=AI_CANONNAME;
hints.ai_family=AF_INET;
if (r=getaddrinfo("localhost", "domain", &hints, &res ))
{
printf("%s\n", gai_strerror(r));

exit(1);
}

printf("%d \"%s\"\n", res->ai_addrlen, res->ai_addr->sa_data);
}

[jdiaz@llamas tmp]$ a.out
16 ""
[jdiaz@llamas tmp]$


Why is "res->ai_addr->sa_data" empty ?

Regards,
Jose Luis.

Floyd L. Davidson

2005-04-15, 7:49 am

jose_luis_fdez_diaz_news@yahoo.es wrote:
>#include <stdio.h>
>#include <errno.h>
>#include <netdb.h>
>#include <sys/socket.h>
>
>int main()
>{
> struct addrinfo hints, *res;
> int r;
>
> memset(&hints, 0, sizeof(hints));
> hints.ai_flags=AI_CANONNAME;
> hints.ai_family=AF_INET;
> if (r=getaddrinfo("localhost", "domain", &hints, &res ))
> {
> printf("%s\n", gai_strerror(r));
>
> exit(1);
> }
>
> printf("%d \"%s\"\n", res->ai_addrlen, res->ai_addr->sa_data);
>}
>
>[jdiaz@llamas tmp]$ a.out
>16 ""
>[jdiaz@llamas tmp]$
>
>Why is "res->ai_addr->sa_data" empty ?


What makes you think it is empty? Here's a more interesting
example, though it is written for Linux hence the include files
are probably different than you need.

Execute this as "foo aol.com", and then try it on google.com and
news.newsguy.com , as three examples that will demonstrate the
linked list that is returned by getaddrinfo().

#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
char *host;
int rtrn;
struct addrinfo hints, *res;

hints.ai_flags=AI_CANONNAME;
memset(&hints, 0, sizeof hints);
hints.ai_family=PF_INET;

if (argc > 1) {
host = argv[1];
} else {
host = "localhost";
}

if ((rtrn = (getaddrinfo(host, "domain", &hints, &res )))) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rtrn));
exit(EXIT_FAILURE);
}

while (res) {
/* print the interface address */
struct sockaddr_in *sa = (struct sockaddr_in *) res->ai_addr;
struct in_addr *inadr = (struct in_addr *) &sa->sin_addr.s_addr;
printf(" %-16s", inet_ntoa(*inadr));
if (res->ai_canonname && *res->ai_canonname) {
printf("canonical name: %s", res->ai_canonname);
}
putchar('\n');
res = res->ai_next;
}
return EXIT_SUCCESS;
}

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
David Schwartz

2005-04-15, 5:57 pm


<jose_luis_fdez_diaz_news@yahoo.es> wrote in message
news:1113544123.450330.191610@g14g2000cwa.googlegroups.com...

> printf("%d \"%s\"\n", res->ai_addrlen, res->ai_addr->sa_data);


> [jdiaz@llamas tmp]$ a.out
> 16 ""
> [jdiaz@llamas tmp]$


> Why is "res->ai_addr->sa_data" empty ?


Garbage in, garbage out. You told 'printf' that res->ai_addr->sa_data
was a C-style string, and it's not.

DS


jose_luis_fdez_diaz_news@yahoo.es

2005-04-18, 2:54 am


David Schwartz wrote:
> <jose_luis_fdez_diaz_news@yahoo.es> wrote in message
> news:1113544123.450330.191610@g14g2000cwa.googlegroups.com...
>
res->ai_addr->sa_data);[vbcol=seagreen]
>
>
>
> Garbage in, garbage out. You told 'printf' that

res->ai_addr->sa_data
> was a C-style string, and it's not.
>
> DS



You are right. I must use inet_ntop.

Regards,
Jose Luis.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com