[Haskell-beginners] Implementing a toy network proxy

Michael Snoyman michael at snoyman.com
Thu Sep 23 13:26:06 EDT 2010


It might be easier to use bytestrings instead of Ptr (). I think the
relevant function is hGetNonBlocking[1].

Cheers,
Michael

[1] http://hackage.haskell.org/packages/archive/bytestring/0.9.1.7/doc/html/Data-ByteString.html#v:hGetNonBlocking

On Thu, Sep 23, 2010 at 7:13 PM, Patrick LeBoutillier
<patrick.leboutillier at gmail.com> wrote:
> 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
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list