darcs patch: Implementation of aton and ntoa outside ... (and 1 more)

Simon Marlow simonmarhaskell at gmail.com
Fri Sep 29 08:19:40 EDT 2006


Robert Marlow wrote:
> Mon Sep 25 16:55:55 JST 2006  Robert Marlow <bobstopper at bobturf.org>
>   * Implementation of aton and ntoa outside the IO monad
>   
>   This is a second attempt at the earlier patch
>   
>   inet_ntoa and inet_aton can be implemented purely to avoid the need for
>   the IO monad. Additionally, inet_addr is currently incorrect due to
>   using the inet_addr C function which is considered obsolete due to
>   incorrectly failing while converting 255.255.255.255 as an address.
>   
> 
> Mon Sep 25 17:12:52 JST 2006  Robert Marlow <bobstopper at bobturf.org>
>   * Fixed bugs in names of aton and ntoa
> 
> 
> ------------------------------------------------------------------------
> 
> 
> New patches:
> 
> [Implementation of aton and ntoa outside the IO monad
> Robert Marlow <bobstopper at bobturf.org>**20060925075555
>  
>  This is a second attempt at the earlier patch
>  
>  inet_ntoa and inet_aton can be implemented purely to avoid the need for
>  the IO monad. Additionally, inet_addr is currently incorrect due to
>  using the inet_addr C function which is considered obsolete due to
>  incorrectly failing while converting 255.255.255.255 as an address.
>  
> ] {
> hunk ./Network/Socket.hsc 93
> +    aton,		-- :: String -> HostAddress
> +    ntoa,		-- :: HostAddress -> String
> hunk ./Network/Socket.hsc 1886
> +-- Implementations outside the IO Monad
> +
> +inet_aton :: String -> HostAddress
> +inet_aton ipstr = unsafePerformIO $ do
> +  withCString ipstr $ \str -> do
> +    allocaBytes 4 $ \buf -> do
> +      success <- c_inet_aton str buf
> +      if success == -1
> +        then throw $ ErrorCall $ "inet_addr: Malformed address: " ++ ipstr
> +        else peek buf
> +
> +inet_ntoa :: HostAddress -> String
> +inet_ntoa haddr = unsafePerformIO $ do
> +  pstr <- c_inet_ntoa haddr
> +  peekCString pstr

inet_ntoa() uses a static buffer, so it isn't threadsafe.  So while the current 
IO version is already broken, putting unsafePerformIO around it makes the 
problem more likely to manifest, and harder to avoid.  We really need to do this 
in Haskell code, I think.

Cheers,
	Simon



More information about the Libraries mailing list