lpeterse / haskell-socket

A Haskell binding to the POSIX sockets API
MIT License
47 stars 10 forks source link

Non IO way of creating an InetAddress? #19

Closed erantapaa closed 8 years ago

erantapaa commented 8 years ago

It would be nice if there was a way of creating an InetAddress without having to go into the IO-monad. The only way I can see to do it is to use getAddressInfo.

I.e. it would be nice to have functions like:

createInetAddressV4 :: [Word8] -> InetAddress

createInetAddressV6 :: [Word16] -> InetAddress  -- or use Word8

Also see this SO thread: http://stackoverflow.com/q/38770829/866915

lpeterse commented 8 years ago

I agree that it's desirable to have such a functions (especially for unit testing etc).

Here are some questions I had in mind when I decided not to supply them:

inetCustom :: Word8 -> Word8 -> Word8 -> Word8 -> InetAddress

inet6Custom :: Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Inet6Address
erantapaa commented 8 years ago

How about adopting the signatures use by the network package, i.e. see the tupleToHostAddress and tupleToHostAddress6 functions:

https://hackage.haskell.org/package/network-2.6.3.1/docs/Network-Socket.html#v:hostAddressToTuple

There are some other Haskell libraries which deal with IPv4 and IPv6 addresses to compare with - I'll try to dig them up.

erantapaa commented 8 years ago

Some other packages to look at:

network-ip: https://hackage.haskell.org/package/network-ip-0.3/docs/Network-IP-Addr.html

ip-route: https://hackage.haskell.org/package/iproute-1.7.0/docs/Data-IP.html#t:IPv4

lpeterse commented 8 years ago

I personally like the tupleToX and XtoTuple from the network package the most.

ip-route is non-total. network-ip supplies toWords and fromWord, but they are not exact counter parts wrt. to type signature.

I'd like to add the following to the library:

inetAddressToTuple :: InetAddress -> (Word8, Word8, Word8, Word8)
inetAddressFromTuple :: (Word8, Word8, Word8, Word8) -> InetAddress

inet6AddressToTuple :: Inet6Address -> (Word16,Word16,Word16,Word16,Word16,Word16,Word16,Word16)
inet6AddressFromTuple :: (Word16,Word16,Word16,Word16,Word16,Word16,Word16,Word16) -> Inet6Address

Do you agree?

erantapaa commented 8 years ago

That's fine. As long as there is a way to construct a value without resorting to IO.

Other people can then create whatever interface they want.

lpeterse commented 8 years ago

Just pushed a commit with the additions.

lpeterse commented 8 years ago

Changes are in v0.6.2.0 on Hackage.