How to have two different sockets for IPv4 and IPv6

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

How to have two different sockets for IPv4 and IPv6

Mohsin
Hi,

I am trying to write a web server that listens on both IPv4 and IPv6 addresses. Although, only one implementation at a time works fine. However, the code that I wrote for both simulateniously doesn't work. I mean I'm creating two diff webserver tasks for IPv4 and IPv6. The socket for both AF_INET6 and AF_INET and binding it with port=80 which is as follows:

//webserver_task_IPv4
iStatus = socket( AF_INET, SOCK_STREAM, 0 );
sin.sin_family = AF_INET;
sin.sin_len = sizeof(sin);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = HTTP_PORT_NUMBER;               //port = 80
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));

//webserver_task_IPv6
iStatus = socket( AF_INET6, SOCK_STREAM, 0 );
sin.sin6_family = AF_INET6;
sin.sin6_len = sizeof(sin);
memset(&(sin.sin6_addr), 0, sizeof(sin.sin6_addr));
sin.sin6_port = HTTP_PORT_NUMBER;               //port = 80


Actually I'm looking for "in6addr_any" for filling IPv6 ANY Address into IPv6 socket case but can't find anywhere in the stack and hence used memset() to set sin6_addr to 0. So is there any replacement variable defined for it in the stack?

Both above tasks works fine when implemented one at a time.

Can anyone please suggest proper socket binding that can listen for both IPv4 and IPv6 at the same time.

Any help would be highly appreciated.

Thanks & regards,
Mohsin Kesarani


 
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Ivan Delamer-2
I don't use the socket API but as far as I can remember, all you need
is one socket listening on IPv6 ANY address. This will accept IPv4
connections as well by default. It can also be disabled.

If there is no predefined sin6 address for ANY, your procedure of
setting it to 0 is correct.

Somebody about a year or two ago had made sure this was working. Is it
not working for you?

Cheers
Ivan



> Date: Wed, 8 Apr 2015 06:15:08 -0700 (MST)
> From: Mohsin <[hidden email]>
> To: [hidden email]
> Subject: [lwip-users] How to have two different sockets for IPv4 and
> IPv6
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=us-ascii
>
> Hi,
>
> I am trying to write a web server that listens on both IPv4 and IPv6
> addresses. Although, only one implementation at a time works fine.
> However,
> the code that I wrote for both simulateniously doesn't work. I mean
> I'm
> creating two diff webserver tasks for IPv4 and IPv6. The socket for
> both
> AF_INET6 and AF_INET and binding it with port=80 which is as follows:
>
> //webserver_task_IPv4
> iStatus = socket( AF_INET, SOCK_STREAM, 0 );
> sin.sin_family = AF_INET;
> sin.sin_len = sizeof(sin);
> sin.sin_addr.s_addr = htonl(INADDR_ANY);
> sin.sin_port = HTTP_PORT_NUMBER;               //port = 80
> memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
>
> //webserver_task_IPv6
> iStatus = socket( AF_INET6, SOCK_STREAM, 0 );
> sin.sin6_family = AF_INET6;
> sin.sin6_len = sizeof(sin);
> memset(&(sin.sin6_addr), 0, sizeof(sin.sin6_addr));
> sin.sin6_port = HTTP_PORT_NUMBER;               //port = 80
>
>
> Actually I'm looking for "in6addr_any" for filling IPv6 ANY Address
> into
> IPv6 socket case but can't find anywhere in the stack and hence used
> memset() to set sin6_addr to 0. So is there any replacement variable
> defined
> for it in the stack?
>
> Both above tasks works fine when implemented one at a time.
>
> Can anyone please suggest proper socket binding that can listen for
> both
> IPv4 and IPv6 at the same time.
>
> Any help would be highly appreciated.
>
> Thanks & regards,
> Mohsin Kesarani
>
>

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Mohsin
Ivan Delamer-2 wrote
If there is no predefined sin6 address for ANY, your procedure of
setting it to 0 is correct.

Somebody about a year or two ago had made sure this was working. Is it
not working for you?
No, it's not working for me.
Only IPv6 works when I set it to 0. But at the same time, IPv4 doesn't work.

I've also heard that setting sin6_addr to in6addr_any will work for both IPv4 and IPv6, that's why only I was looking for in6addr_any in the stack.

So, my ultimate question is whether in6addr_any is equivalent to setting sin6_addr=0 or there is something unique with in6addr_any variable which allows both IPv4,v6 to work?


Regards,
Mohsin Kesarani

 ______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

goldsimon@gmx.de
Mohsin wrote:
I've also heard that setting sin6_addr to in6addr_any will work for both
IPv4 and IPv6, that's why only I was looking for in6addr_any in the stack.

So, my ultimate question is whether in6addr_any is equivalent to setting
sin6_addr=0

Yes, it is. Jus like using IN6ADDR_ANY_INIT macro. We just haven't yet implemented in6addr_any, that's all.

The whole point here seems to be that simultaneous listening on the same port but different protocols doesn't work. That would be worth a bug.

Simon

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

goldsimon@gmx.de
In reply to this post by Ivan Delamer-2
Ivan Delamer wrote:
> I don't use the socket API but as far as I can remember, all you need
> is one socket listening on IPv6 ANY address.

Is that so? What's the behaviour then if you first bind an IPv4-ANY
socket and then the IPv6-ANY socket? Does it run IPv6-only or fail?

I must admit I'm not too familiar with dual-version socket handling...

Simon

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Mohsin
goldsimon@gmx.de wrote
Ivan Delamer wrote:
> I don't use the socket API but as far as I can remember, all you need
> is one socket listening on IPv6 ANY address.

Is that so? What's the behaviour then if you first bind an IPv4-ANY
socket and then the IPv6-ANY socket? Does it run IPv6-only or fail?
Yes, it totally fails and none of the IP version works when I bind IPv4-ANY socket and IPv6-ANY socket at the same time.
So, I also suspect that there is some issue(may be bug) in handling different protocols simultaneously with same listening port.

Otherwise, binding only IPv4-ANY socket or only IPv6-ANY socket works fine.

Regards,
Mohsin Kesarani
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Philip Gladstone
In reply to this post by goldsimon@gmx.de
On 4/8/15 14:25, [hidden email] wrote:
> Ivan Delamer wrote:
>> I don't use the socket API but as far as I can remember, all you need
>> is one socket listening on IPv6 ANY address.
>
> Is that so? What's the behaviour then if you first bind an IPv4-ANY
> socket and then the IPv6-ANY socket? Does it run IPv6-only or fail?
>
> I must admit I'm not too familiar with dual-version socket handling...
>
I have it working for UDP -- I listen on the same port on both IPv4 and
IPv6 and it works. I know that Simon merged my patch to make this type
of thing work in the current development version. I have not (yet)
upgraded to that version to verify.

Philip

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Mohsin
Philip Gladstone wrote
I have it working for UDP -- I listen on the same port on both IPv4 and
IPv6 and it works. I know that Simon merged my patch to make this type
of thing work in the current development version.
I would be very grateful to you if you can mention that patch which worked for you.
So, that atleast I can verify or implement it in my development.

Regards,
Mohsin Kesarani
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Philip Gladstone
On 4/8/15 23:37, Mohsin wrote:
> Philip Gladstone wrote
>> I have it working for UDP -- I listen on the same port on both IPv4 and
>> IPv6 and it works. I know that Simon merged my patch to make this type
>> of thing work in the current development version.
> I would be very grateful to you if you can mention that patch which worked
> for you.
> So, that atleast I can verify or implement it in my development.
>
https://savannah.nongnu.org/patch/?8358

The other possibility is that you need to set SO_REUSEADDR or SO_REUSEPORT

Philip


_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

goldsimon@gmx.de
In reply to this post by goldsimon@gmx.de
[hidden email] wrote:
> Ivan Delamer wrote:
>> I don't use the socket API but as far as I can remember, all you need
>> is one socket listening on IPv6 ANY address.
> Is that so?

After a bit of reading, I'm back in the issue: yes, it's that way and it
should work in lwIP, too. If not, that's a (regression) bug.

Simon

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Mohsin
goldsimon@gmx.de wrote
[hidden email] wrote:
> Ivan Delamer wrote:
>> I don't use the socket API but as far as I can remember, all you need
>> is one socket listening on IPv6 ANY address.
> Is that so?

After a bit of reading, I'm back in the issue: yes, it's that way and it
should work in lwIP, too. If not, that's a (regression) bug.
Hi Simon,

I tried binding a socket to IPv6 ANY address, but it just works for IPv6 address only and not for IPv4.

I even tried setting #define SO_REUSE  1 as per Philips suggestion, but still same problem.

So, maybe there is some functionality lacking in pcb which can support  reuse of same listening port for different protocol versions simultaneously.


Regards,
Mohsin Kesarani

Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Rahul Gundecha-2
Hi Mohsin,

I have http server working simultaneously on both IPv4 and IPv6 sockets. Here are my suggestions in your code:
1. Why you are using same "iStatus" for both socket descriptors?
2. Same question for "sin". For IPv4, you need to use struct sockaddr_in, whereas for IPv6 struct sockaddr_in6 variable, so two different variables.
3. iStatus = socket( AF_INET, SOCK_STREAM, 0 ); => 0 should be replaced by IPPROTO_TCP which is 6

Thanks,
Rahul

sin.sin_family = AF_INET;
sin.sin_len = sizeof(sin);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = HTTP_PORT_NUMBER;               //port = 80
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));

//webserver_task_IPv6
iStatus = socket( AF_INET6, SOCK_STREAM, 0 );
sin.sin6_family = AF_INET6;
sin.sin6_len = sizeof(sin);
memset(&(sin.sin6_addr), 0, sizeof(sin.sin6_addr));
sin.sin6_port = HTTP_PORT_NUMBER;               //port = 80


On Fri, Apr 10, 2015 at 12:17 AM, Mohsin <[hidden email]> wrote:
[hidden email] wrote
> goldsimon@

>  wrote:
>> Ivan Delamer wrote:
>>> I don't use the socket API but as far as I can remember, all you need
>>> is one socket listening on IPv6 ANY address.
>> Is that so?
>
> After a bit of reading, I'm back in the issue: yes, it's that way and it
> should work in lwIP, too. If not, that's a (regression) bug.

Hi Simon,

I tried binding a socket to IPv6 ANY address, but it just works for IPv6
address only and not for IPv4.

I even tried setting #define SO_REUSE  1 as per Philips suggestion, but
still same problem.

So, maybe there is some functionality lacking in pcb which can support
reuse of same listening port for different protocol versions simultaneously.


Regards,
Mohsin Kesarani





--
View this message in context: http://lwip.100.n7.nabble.com/How-to-have-two-different-sockets-for-IPv4-and-IPv6-tp24291p24302.html
Sent from the lwip-users mailing list archive at Nabble.com.

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users


_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Mohsin
Rahul Gundecha-2 wrote
Hi Mohsin,

I have http server working simultaneously on both IPv4 and IPv6 sockets.
Here are my suggestions in your code:
1. Why you are using same "iStatus" for both socket descriptors?
2. Same question for "sin". For IPv4, you need to use struct sockaddr_in,
whereas for IPv6 struct sockaddr_in6 variable, so two different variables.
3. iStatus = socket( AF_INET, SOCK_STREAM, 0 ); => 0 should be replaced by
IPPROTO_TCP which is 6
Thank you Rahul for your suggestions.
I've already tried changing socket descriptor variables as well as sock struct variables for IPv4 and IPv6 as you suggested above, but none works.

Actually, I first bind IPv4 ANY address and then IPv6 ANY address to same port=80 in two different tasks. So in this case, IPv4 is atleast sending SYN+ACK for first TCP SYN request from PC and then RST,ACK whereas IPv6 immediately sends RST,ACK for every TCP req.

If I reverse the sequence of bind. i.e. IPv6 ANY address first and then IPv4 ANY address, then IPv6 is able to send SYN+ACK reply to first TCP SYN request whereas IPv4 immediately sends RST,ACK for every SYN req.

Hence, I think there happens some conflict in binding of two diff protocols to same port so that whichever is binded first is able to send atleast one SYN+ACK instead of directly RST.


Regards,
Mohsin
Reply | Threaded
Open this post in threaded view
|

Re: How to have two different sockets for IPv4 and IPv6

Rahul Gundecha-2
Hi Mohsin,

It will be good if you post your latest code here which includes all of the earlier suggestions.

Thanks,
Rahul


On Fri, Apr 17, 2015 at 2:31 PM, Mohsin <[hidden email]> wrote:
Rahul Gundecha-2 wrote
> Hi Mohsin,
>
> I have http server working simultaneously on both IPv4 and IPv6 sockets.
> Here are my suggestions in your code:
> 1. Why you are using same "iStatus" for both socket descriptors?
> 2. Same question for "sin". For IPv4, you need to use struct sockaddr_in,
> whereas for IPv6 struct sockaddr_in6 variable, so two different variables.
> 3. iStatus = socket( AF_INET, SOCK_STREAM, 0 ); => 0 should be replaced by
> IPPROTO_TCP which is 6

Thank you Rahul for your suggestions.
I've already tried changing socket descriptor variables as well as sock
struct variables for IPv4 and IPv6 as you suggested above, but none works.

Actually, I first bind IPv4 ANY address and then IPv6 ANY address to same
port=80 in two different tasks. So in this case, IPv4 is atleast sending
SYN+ACK for first TCP SYN request from PC and then RST,ACK whereas IPv6
immediately sends RST,ACK for every TCP req.

If I reverse the sequence of bind. i.e. IPv6 ANY address first and then IPv4
ANY address, then IPv6 is able to send SYN+ACK reply to first TCP SYN
request whereas IPv4 immediately sends RST,ACK for every SYN req.

Hence, I think there happens some conflict in binding of two diff protocols
to same port so that whichever is binded first is able to send atleast one
SYN+ACK instead of directly RST.


Regards,
Mohsin



--
View this message in context: http://lwip.100.n7.nabble.com/How-to-have-two-different-sockets-for-IPv4-and-IPv6-tp24291p24320.html
Sent from the lwip-users mailing list archive at Nabble.com.

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users


_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users