Networking Forums

Networking Forums > Computer Networking > Linux Networking > Binding an IPv6 address to an AF_INET socket?

Reply
Thread Tools Display Modes

Binding an IPv6 address to an AF_INET socket?

 
 
Roy Smith
Guest
Posts: n/a

 
      05-10-2007, 03:15 AM
The code below attempts to bind an IPv6 address to an AF_INET socket. I
belive this should fail, yet when compiled and run on our RedHat AS-3 and
AS-4 boxes, it prints "bind worked". Does this make sense?

On Solaris-8, it prints "bind failed: Address family not supported by
protocol family", which is about what I would have expected.

Before anybody asks, "Why do you want to do that?", the answer is, "I
don't". It came up during some unit tests, where (at a higher level in
some C++ code), we attempt to do this and expect it to throw an exception.


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>

int main (int argc, char* argv[]) {
* * int s = socket(PF_INET, SOCK_STREAM, 0);
* * printf ("s = %d\n", s);

* * struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
* * struct sockaddr_in6 sa;

* * sa.sin6_family = AF_INET6;
* * sa.sin6_port = 0;
* * sa.sin6_flowinfo = 0;
* * sa.sin6_addr = anyaddr;
* * sa.sin6_scope_id = 0;

* * int retval = bind(s, (struct sockaddr*) &sa, sizeof (sa));
* * if (retval == 0) {
* * * * printf ("bind worked\n");
* * } else {
* * * * perror ("bind failed");
* * }
}
 
Reply With Quote
 
 
 
 
Jim Logajan
Guest
Posts: n/a

 
      05-13-2007, 04:18 AM
Roy Smith <(E-Mail Removed)> wrote:
> The code below attempts to bind an IPv6 address to an AF_INET socket.
> I belive this should fail, yet when compiled and run on our RedHat
> AS-3 and AS-4 boxes, it prints "bind worked". Does this make sense?


It makes sense and if the underlying OS supports IPv6 then the bind should
work. The behavior of bind() under IPv6 is described in RFC 3493.

Why do you believe the bind should fail?

> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <errno.h>
> #include <stdio.h>
>
> int main (int argc, char* argv[]) {
> * * int s = socket(PF_INET, SOCK_STREAM, 0);
> * * printf ("s = %d\n", s);
>
> * * struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
> * * struct sockaddr_in6 sa;
>
> * * sa.sin6_family = AF_INET6;
> * * sa.sin6_port = 0;
> * * sa.sin6_flowinfo = 0;
> * * sa.sin6_addr = anyaddr;
> * * sa.sin6_scope_id = 0;
>
> * * int retval = bind(s, (struct sockaddr*) &sa, sizeof (sa));
> * * if (retval == 0) {
> * * * * printf ("bind worked\n");
> * * } else {
> * * * * perror ("bind failed");
> * * }
> }

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a

 
      05-13-2007, 12:11 PM
In article <Xns992ED8B1141C1JamesLLugojcom@216.168.3.30>,
Jim Logajan <(E-Mail Removed)> wrote:

> Roy Smith <(E-Mail Removed)> wrote:
> > The code below attempts to bind an IPv6 address to an AF_INET socket.
> > I belive this should fail, yet when compiled and run on our RedHat
> > AS-3 and AS-4 boxes, it prints "bind worked". Does this make sense?

>
> It makes sense and if the underlying OS supports IPv6 then the bind should
> work. The behavior of bind() under IPv6 is described in RFC 3493.
>
> Why do you believe the bind should fail?


Because I'm trying to bind an AF_INET6 address to a PF_INET socket. I
don't see anything in 3493 that explicitly says this will fail, but it
seems logical. This isn't something which "3.7 Compatibility with IPv4
Nodes" talks about.





>
> > #include <sys/types.h>
> > #include <sys/socket.h>
> > #include <netinet/in.h>
> > #include <errno.h>
> > #include <stdio.h>
> >
> > int main (int argc, char* argv[]) {
> > * * int s = socket(PF_INET, SOCK_STREAM, 0);
> > * * printf ("s = %d\n", s);
> >
> > * * struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
> > * * struct sockaddr_in6 sa;
> >
> > * * sa.sin6_family = AF_INET6;
> > * * sa.sin6_port = 0;
> > * * sa.sin6_flowinfo = 0;
> > * * sa.sin6_addr = anyaddr;
> > * * sa.sin6_scope_id = 0;
> >
> > * * int retval = bind(s, (struct sockaddr*) &sa, sizeof (sa));
> > * * if (retval == 0) {
> > * * * * printf ("bind worked\n");
> > * * } else {
> > * * * * perror ("bind failed");
> > * * }
> > }

 
Reply With Quote
 
Kari Hurtta
Guest
Posts: n/a

 
      05-13-2007, 03:56 PM
Roy Smith <(E-Mail Removed)> writes:

> In article <Xns992ED8B1141C1JamesLLugojcom@216.168.3.30>,
> Jim Logajan <(E-Mail Removed)> wrote:
>
> > Roy Smith <(E-Mail Removed)> wrote:
> > > The code below attempts to bind an IPv6 address to an AF_INET socket.
> > > I belive this should fail, yet when compiled and run on our RedHat
> > > AS-3 and AS-4 boxes, it prints "bind worked". Does this make sense?

> >
> > It makes sense and if the underlying OS supports IPv6 then the bind should
> > work. The behavior of bind() under IPv6 is described in RFC 3493.
> >
> > Why do you believe the bind should fail?

>
> Because I'm trying to bind an AF_INET6 address to a PF_INET socket. I
> don't see anything in 3493 that explicitly says this will fail, but it
> seems logical. This isn't something which "3.7 Compatibility with IPv4
> Nodes" talks about.


Yes. This is logical, because RFC 3493 defines new protocol family PF_INET6.

| 3.1 IPv6 Address Family and Protocol Family
|
| A new address family name, AF_INET6, is defined in <sys/socket.h>.
| The AF_INET6 definition distinguishes between the original
| sockaddr_in address data structure, and the new sockaddr_in6 data
| structure.
|
| A new protocol family name, PF_INET6, is defined in <sys/socket.h>.
| Like most of the other protocol family names, this will usually be
| defined to have the same value as the corresponding address family
| name:
|
| #define PF_INET6 AF_INET6
|
| The AF_INET6 is used in the first argument to the socket() function
| to indicate that an IPv6 socket is being created.



> > > #include <sys/types.h>
> > > #include <sys/socket.h>
> > > #include <netinet/in.h>
> > > #include <errno.h>
> > > #include <stdio.h>
> > >
> > > int main (int argc, char* argv[]) {
> > > Â* Â* int s = socket(PF_INET, SOCK_STREAM, 0);
> > > Â* Â* printf ("s = %d\n", s);
> > >
> > > Â* Â* struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
> > > Â* Â* struct sockaddr_in6 sa;
> > >
> > > Â* Â* sa.sin6_family = AF_INET6;
> > > Â* Â* sa.sin6_port = 0;
> > > Â* Â* sa.sin6_flowinfo = 0;
> > > Â* Â* sa.sin6_addr = anyaddr;
> > > Â* Â* sa.sin6_scope_id = 0;
> > >
> > > Â* Â* int retval = bind(s, (struct sockaddr*) &sa, sizeof (sa));
> > > Â* Â* if (retval == 0) {
> > > Â* Â* Â* Â* printf ("bind worked\n");
> > > Â* Â* } else {
> > > Â* Â* Â* Â* perror ("bind failed");
> > > Â* Â* }
> > > }

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a

 
      05-13-2007, 06:16 PM
In article <(E-Mail Removed)>,
Kari Hurtta <(E-Mail Removed)> wrote:

> Roy Smith <(E-Mail Removed)> writes:
>
> > In article <Xns992ED8B1141C1JamesLLugojcom@216.168.3.30>,
> > Jim Logajan <(E-Mail Removed)> wrote:
> >
> > > Roy Smith <(E-Mail Removed)> wrote:
> > > > The code below attempts to bind an IPv6 address to an AF_INET socket.
> > > > I belive this should fail, yet when compiled and run on our RedHat
> > > > AS-3 and AS-4 boxes, it prints "bind worked". Does this make sense?
> > >
> > > It makes sense and if the underlying OS supports IPv6 then the bind
> > > should
> > > work. The behavior of bind() under IPv6 is described in RFC 3493.
> > >
> > > Why do you believe the bind should fail?

> >
> > Because I'm trying to bind an AF_INET6 address to a PF_INET socket. I
> > don't see anything in 3493 that explicitly says this will fail, but it
> > seems logical. This isn't something which "3.7 Compatibility with IPv4
> > Nodes" talks about.

>
> Yes. This is logical, because RFC 3493 defines new protocol family PF_INET6.


I'm very confused. I know that it defines PF_INET6. But, when I created
the socket, I created it with PF_INET. I don't understand why or how you
can bind an AF_INET6 address to a PF_INET socket.
 
Reply With Quote
 
Jim Logajan
Guest
Posts: n/a

 
      05-13-2007, 10:03 PM
Roy Smith <(E-Mail Removed)> wrote:
> I'm very confused. I know that it defines PF_INET6. But, when I
> created the socket, I created it with PF_INET. I don't understand why
> or how you can bind an AF_INET6 address to a PF_INET socket.


I can think of two possible explanations (these do not exclude other
possibilities of course!):

(1) The underlying OSs are not doing proper cross-validation of the
socket's address family with the type of address that is actually being
requested.

(2) The underlying OSs are supporting dual-stack mode and allowing IPv4 or
IPv6 addresses to be bound to any socket created under either address
family. On these machines the socket() call can treat the PF_INET and
PF_INET6 as synonymous. So how then does the bind() call know whether the
address being provided is an IPv4 or IPv6 address? It figures it out by
examining the first two bytes of the address being supplied - not by
examining which address family the socket was created for. In this case
when your code did this assignment:

sa.sin6_family = AF_INET6;

it was all the bind() function needed to know that the rest of the contents
at the address pointed to by &sa was an IPv6 address. If you had messed up
and did AF_INET (or PF_INET) instead it would have treated the contents at
&sa as an IPv4 address structure. (ALL the address structure types on dual-
stack machines typically set aside the first 2 bytes of the structures to
hold the address family.)

I believe that explanation (2) is the reason your code is working.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IPv6 address not connecting but IPv4-mapped -IPv6 does. Please help. DanielJohnson Network Routers 0 01-07-2009 12:10 AM
Binding a single IP Address to two different MAC addresses Ramesh Linux Networking 6 02-20-2008 01:35 PM
IPv6 infrastructure for Global and/or Site Specific IPv6 address JackFlash Windows Networking 1 06-25-2007 03:11 PM
connection refused trying to bind AF_INET to AF_NETLINK/NETFILTER socket ssussman@starentnetworks.com Linux Networking 0 05-14-2007 06:52 PM
use IPv6 socket to get file from Apache lgl Linux Networking 0 11-19-2003 08:34 AM



1 2 3 4 5 6 7 8 9 10 11