instance Show SockAddr

Graham Klyne GK at
Sat Oct 16 04:37:02 EDT 2004

At 20:43 15/10/04 +0200, Tomasz Zielonka wrote:
>On Fri, Oct 15, 2004 at 04:24:01PM +0200, Peter Simons wrote:
> > Hi,
> >
> > I've found myself needing to show a SockAddr type left and
> > right in my code. Is it possible to add the appropriate
> > definition to Network.Socket? Here is a suggestion:
> >
> > -- |Split up an 32-bit word in network byte order.
> >
> > ha2tpl :: HostAddress -> (Int, Int, Int, Int)
> > ha2tpl n =
> >   let (b1,n1) = (n  .&. 255, n  `shiftR` 8)
> >       (b2,n2) = (n1 .&. 255, n1 `shiftR` 8)
> >       (b3,n3) = (n2 .&. 255, n2 `shiftR` 8)
> >       b4      = n3 .&. 255
> >   in
> >   (fromEnum b1, fromEnum b2, fromEnum b3, fromEnum b4)
>This won't work on big-endian architectures.
>Couldn't GHC's HostAddress be always in host-order, with necessary
>conversions done behind the curtains?

It seems to me that using a single integer/bitstring value for internal 
representation of IP addresses is rather missing a trick.  I'd hope that, 
for the most part, Haskell programs are above this kind of bit-stuffing 
logic, except when it comes to actually interfacing with the real world (in 
this case, a wire protocol).

My hope for a host address representation would be an abstraction whose 
internal details are hidden, with functions to convert to/from textual form 
and wire-form (network byte order), and any other abstract operations that 
may be required (e.g. subnet testing).  This approach should, for example, 
make migration to IPv6 relatively painless.

But I suppose this particular horse has bolted, and we must live with the 
consequences.  Sigh.


>When I was playing with pure Haskell DNS resolver library, the current
>situation forced me to make foreign imports for htonl. Because of this
>the library is 99.9% Haskell + 0.01% C.
>Best regards,
>.signature: Too many levels of symbolic links
>Libraries mailing list
>Libraries at

Graham Klyne
For email:

More information about the Libraries mailing list