
On 05.11 18:29, Ashley Yakeley wrote:
It's quite low-level, isn't it? I wonder if it would be worth doing something along these lines:
newtype IPv4Address = IPv4Address Word32 newtype IPv6Address = IPv6Address Word128
This brings the question on whether they would be native or big endian - and the user would still need to use resolver API to get at these. Also the resolver API might return addresses in different address families so we would need: data Either3 a b c = First a | Second b | Third c getAddrInfo :: HostName -> PortName -> Flags -> IO [Either3 IPv4Address IPv6Address OtherAddress] while it is currently getAddrInfo :: HostName -> PortName -> Flags -> IO [(SocketType, SocketAddress)] which means applications can use addresses in different families in an uniform way.
type PortNumber = Word16
data InternetService addr = MkInternetService { localAddress :: addr, sendUDP :: UDPOptions -> addr -> PortNumber -> [Word8] -> IO (), -- etc. }
getIPv4Services :: IO [InternetService IPv4Address] getIPv6Services :: IO [InternetService IPv6Address]
Does it make sense to have the address type in there? Most of the time high level applications should not care whether the socket is IPv4 or IPv6 (or something entirely else). One of my main goals has been to make the protocols transparent - changing from IPv4 to being protocol agnostic should be as easy as possible. The API you are suggesting would be certainly quite easy to implement on top of the current one. If you can specify the interface you would like I think I can include something like that in the next release... Currently the code would look like: sendTCPTo hostname port data = do s <- connectTCP hostname port sendString s data sendUDPTo hostname addressType port data = do let st = SocketType addressType sockDgram 0 s <- socket st ((cst,csa):_) <- getAddrInfo hostname port 0 st connect s csa sendString s data sendUDPv4 host port data = sendUDPTo host afInet port data sendUDPv6 host port data = sendUDPTo host afInet6 port data sendUDPAny host port data= sendUDPTo host afUnspec port data Adding a function like connectTCP for UDP would be very easy, but in practise most of the UDP code I have worked with was interested in the low level details. - Einar Karttunen