[Haskell] Should inet_ntoa Be Pure?

Glynn Clements glynn at gclements.plus.com
Sat May 7 15:34:54 EDT 2005

Axel Simon 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

That doesn't mean that it should be in the IO monad; using Maybe would

> 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.

That shouldn't be an issue so long as the buffer contents are
converted to a Haskell String before the function is called again
within the same thread.

However, I wouldn't rely upon all implementations of inet_ntoa() being

> What you could do is to apply unsafePerformIO to 


Or you could just re-implement the functions in Haskell.

Apart from the re-entrancy issues with inet_ntoa(), many
implementations of inet_addr() have misfeatures, e.g. allowing octets
to be expressed in octal or hex, or allowing numbers outside of the
0-255 range (in which case, the top bits overflow into the next

Glynn Clements <glynn at gclements.plus.com>

More information about the Haskell mailing list