How to know data size in UDP?
Kazu Yamamoto ( 山本和彦 )
kazu at iij.ad.jp
Thu Oct 29 04:31:03 EDT 2009
Hello,
I'm trying to implement DNS resolver without any external C libraries
for my daemon program which uses DNS lookup.
I know the "hsdns" library exists. But since it calls GNU asynchronous
DNS resolver library by FFI, all threads of Haskell are blocked on
non-threaded RTS.
If the threaded RTS is used, forkProcess kills the IO thread. With the
threaded RTS of at least GHC 6.10.4, we cannot make our program as a
daemon nor use pre-fork mechanism.
Thus, I decided to implement DNS resolver only with non-blocking
Haskell functions.
I use a *connected* UDP socket like this:
proto <- getProtocolNumber "udp"
let hints = defaultHints { addrFlags = [AI_ADDRCONFIG, AI_NUMERICHOST, AI_PASSIVE]
, addrSocketType = Datagram
, addrProtocol = proto }
a:_ <- getAddrInfo (Just hints) (Just "An IP address of DNS server") (Just "domain")
sock <- socket (addrFamily a) (addrSocketType a) (addrProtocol a)
connect sock (addrAddress a)
h <- socketToHandle sock ReadWriteMode
hSetBuffering h NoBuffering
hSetBinaryMode h True
If my understating is correct, I cannot use recvFrom since it calls
recvfrom() with FFI. So, I use socketToHandle to translate a socket to
a handle.
Since the handle is associated with UDP, the entire data size should
be known when a UDP packet arrived. But I cannot find a way to know
data size. (I want a function like hFileSize.) Note that hGetContents
blocks.
Without data size in advance, a program to handle network protocols on
UDP becomes quite dirty or sometime cannot be implemented. So, please
tell me how to know data size associated with a handler of UDP?
--Kazu
More information about the Glasgow-haskell-users
mailing list