getaddrinfo help

Hello,

I am trying to write a simple C program for ip lookup.
Result now points to a struct wich holds the results of my lookup, but struct addrinfo itself contains a pointer to a struct of type sockaddr_in. So i have a pointer to a pointer to a struct ... How would you get the address from that?



Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>


int status = 0;

struct addrinfo hints *p,*result;

memset(&hints,0,sizeof hints);

hints.ai_family = AF_INET;
hints.ai_socktype = SOCKSTREAM;

status = getaddrinfo("www.freebsd.org",NULL,&hints,&result);
}
 
Because the query might return more than one address. Always write your programs in a way that you don't assume anything about the results. That means you'll have to loop trough the list and figure out what is the result or results you need. There isn't much of pointer algebra if you want just the first result, the pointer result is already pointing to the first element of the list so result->ai_addr should be the address and so on.
 
Hm,

I found this and didn't quite understand it. What is the difference between
Code:
return &(((struct sockaddr_in*)sa)->sin_addr);

and
Code:
return &(struct sockaddr_in *)(sa->sin_addr);

?
 
Business_Woman said:
Hm,

I found this and didn't quite understand it. What is the difference between
Code:
return &(((struct sockaddr_in*)sa)->sin_addr);

and
Code:
return &(struct sockaddr_in *)(sa->sin_addr);

?

In first return you assign type "struct sockaddr_in*" to "sa". In second you give that type to "sa->sin_addr". See where I am going? That kinda changes a lot how your code works.
 
expl said:
In first return you assign type "struct sockaddr_in*" to "sa". In second you give that type to "sa->sin_addr". See where I am going? That kinda changes a lot how your code works.


Yes, i understand that. But sa->sin_addr is just a shorter way of writing *sa.sin_addr = ..
So is this the same as writing &((struct sockaddr *)*sa).sin_addr ?

And in the second case &((struct sockaddr *)*sa.sin_addr)
?
 
Back
Top