Is it possible to have a single process that would have two sockets, one using the AF_INET and one using the AF_INET6 families, while both being bound to the same port number, e.g. 20000?

If the IPv6 socket setsockopts to be IPv6-only, would the kernel correctly direct packets to the correct socket based on the IP protocol version?


Yes, it is possible (there may be complications depending on the higher level language and how well it lets you select such details).

#!/usr/bin/env expect
set port 1234
proc v4 {fh caddr cport} { puts $fh v4; close $fh }
proc v6 {fh caddr cport} { puts $fh v6; close $fh }
socket -myaddr -server [list v4] $port
socket -myaddr ::1 -server [list v6] $port
vwait godot

With that running:

% lsof -i -nP | grep 1234
expect 96688 jhqdoe 10u IPv4 0xf7... 0t0 TCP (LISTEN)
expect 96688 jhqdoe 11u IPv6 0xf7... 0t0 TCP [::1]:1234 (LISTEN)
% nc 1234
% nc ::1 1234
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.