[Haskell] Should inet_ntoa Be Pure?

Axel Simon A.Simon at kent.ac.uk
Sat May 7 07:55:11 EDT 2005


On 5/7/05, Dominic Steinitz <dominic.steinitz at blueyonder.co.uk> wrote:
> > Does anyone know why these are in the IO monad? Aren't they pure functions
> > converting between dotted-decimal strings and a 32-bit network byte ordered
> > binary value?

I guess the answer is no for both: The first one can fail and the second
one overwrites a fixed string buffer (yuck!). From the man page:

     The return value from inet_ntoa() points to a  buffer  which
     is  overwritten on each call.  This buffer is implemented as
     thread-specific data in multithreaded applications.

Hence ntoa needs to be an IO action so that the value is read
immediately before the next ntoa call is executed. What you could do is
to apply unsafePerformIO to 

> On Sat, 2005-05-07 at 13:40 +0200, David Sankel wrote:
> Below is the relevant source code.
[..]
> inet_ntoa :: HostAddress -> IO String
> inet_ntoa haddr = do
>   pstr <- c_inet_ntoa haddr
>   peekCString pstr

function which is safe.

Both function should probably be deprecated in favour of ntop and pton
since only these can also deal with IPv6 addresses. Furthermore ntop can
be a pure function as it takes a 46-byte string buffer as argument.

Axel.




More information about the Haskell mailing list