[Haskell-beginners] Implementing a toy network proxy

Patrick LeBoutillier patrick.leboutillier at gmail.com
Thu Sep 23 13:13:53 EDT 2010


Hi ,

I'm trying to write a toy generic network proxy that will accept
connections on a port, and for each connection connect
to a remote server and forward the traffic.


Here's what I have do far:


import Network
import System.IO
import Control.Exception
import Control.Concurrent


copy :: Handle -> Handle -> IO ()
copy a b = undefined


redir :: Handle -> Handle -> IO ()
redir h1 h2 = forkIO (copy h1 h2) >> forkIO (copy h2 h1) >> return ()


acceptLoop :: Socket -> HostName -> PortID -> IO ()
acceptLoop sock rhost rport = loop
  where loop = do
         (local, host, port) <- accept sock
         remote <- connectTo rhost rport
         redir local remote
         loop >> return ()


main = do
  let local_port = 8000
  let remote_host = "whatever"
  let remote_port = 80
  server <- listenOn (PortNumber local_port)
  acceptLoop server remote_host (PortNumber remote_port)



Basically I'm stuck at how to implement the copy function. I want it
to block until some data is available on a,
read it and then write it to b.

I think I need to use hGetBufNonBlocking, but I don't know how to get a "Ptr a".

I am on the right track with this?


Patrick
-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


More information about the Beginners mailing list