Network.listenOn (PortNumber n) Sometimes Picks IPv6
When I run the following program on a NetBSD system, it listens on *.4444 for IPv6, not IPv4. This could certainly happen on other operating systems as well, though I've not tested others. Whether it happens or not could even vary from release to release.
import IO
import Network
main = do
s <- listenOn (PortNumber 4444)
(h, hostName, portNum) <- Network.accept s
putStrLn $ "Connection from " ++ hostName ++ ":" ++ show portNum
hPutStrLn h "Bye!"
hClose h
I've verified that telnet 127.0.0.1 4444
produces a "connection refused" message, but telnet ::1 4444
connects and displays "Bye!"
I'd go so far to say that this is a bug. At best, one could argue that this behaviour is not entirely intuitive.
I propose that we (re-?)define PortID's PortNumber constructor to refer only to IPv4 protocol ports, and add a new PortNumber6 constructor for IPv6 ports.
Another option would be to do force PortNumber always to bind to IPv4, but not add PortNumber6. I prefer this less because we should be encouraging the use of IPv6, and because having PortNumber6 serves as obvious documentation for what PortNumber and PortNumber6 do.
A third option would be to attempt to listen on both IPv4 and IPv6, when both are available, but this sort of thing is fraught with many problems, the details of which are much more than I'd want to get into in this bug report.
Trac metadata
Trac field | Value |
---|---|
Version | 6.8.3 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | Multiple |
Architecture | Multiple |