Operating System - HP-UX
1819870 Members
2458 Online
109607 Solutions
New Discussion юеВ

sockaddr_in, sockaddr_in6, sockaddr_storage when use which ?

 
test_goraz
Occasional Advisor

sockaddr_in, sockaddr_in6, sockaddr_storage when use which ?

Hi all,

I am playing with some socket programming and one thing confuse me. For ipv4 we have struct sockaddr_in which is able to handle everything related to ipv4 sockets. On other side for ipv6 we have struct sockaddr_in6, which do the same thing for ipv6. It is known that ipv6 created with
socket ( AF_INET, SOCK_STREAM,0) is able to accept connections from ipv4 and ipv6 clients if there is not specificaly written IPV6_V6ONLY in declaration of address type.
For me is confusing the structure
sockaddr_storage ( I read that is used to write address family independent code and that is ok ) but if I have some application for which is necessary to accept connection from ipv4 and ipv6 clients then I can use struct sockaddr_in6, and the application with that structure will work ? Do we need struct sockaddr_storage only on server side when we do not have information will our application be running on ipv4 or ipv6 server.

I am sorry, if this post looks confusing but, it is completealy mess for me to understand this. If someone have knowledge and will to share it thanks in advance

Kind regards,
3 REPLIES 3
rick jones
Honored Contributor

Re: sockaddr_in, sockaddr_in6, sockaddr_storage when use which ?

Generally speaking, you allocate something sized/aligned like sockaddr_storage and then use casts to either sockaddr_in or sockaddr_in6. The sockaddr_in* structures both have the "family" field in the same place and of the same size so even if your sockaddr_storage happends to be holding a sockaddr_in6, you can still cast it to a sockaddr_in for the purposes of checking the sin_family field. If that is then AF_INET6 you would then switch to using a sockaddr_in6 cast of the sockaddr_storage.

You might want to get the latest edition of Stevens, Fenner and Rudoff's Unix Network Programming. It will have lots of examples for you.
there is no rest for the wicked yet the virtuous have no pillows
test_goraz
Occasional Advisor

Re: sockaddr_in, sockaddr_in6, sockaddr_storage when use which ?

Thanks for your answer. I will try to find the book you recommended me. If I have code

struct sockaddr_in saddr;
bzero((char *) &saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons((u_short) service);
saddr.sin_addr.s_addr = INADDR_ANY;

I am just wondering how to addapt it using struct sockaddr_storage to be address independent ?

Thanks for answer
rick jones
Honored Contributor

Re: sockaddr_in, sockaddr_in6, sockaddr_storage when use which ?

make saddr a pointer to sockaddr_in and then point it at a newly added struct sockaddr_storage foo

struct sockaddr_storage foo;

struct sockaddr_in *saddr;

sadder - (struct sockaddr_in *)&foo;

...

converting the rest of the code with the knowledge that saddr is now a pointer to a struct sockaddr_in.
there is no rest for the wicked yet the virtuous have no pillows