I have implemented a forward proxy in linux which listens on few ports. Whenever a connection happens it puts into outer world.
My setup looks as follows:
Outerworld<—–>Proxy(Listens on Port A)<——->Browser(Configured with proxy set toPort A)
However, I am not sure when to close the connection between proxy-outer world and proxy-browser.
I was under impression that if I see a packet with data size 0 (recv()) then it is end of communication from client side. Once I get “recv()=0”, I do “close()” with outer world. Is this the right way to do? Or should I do shutdown()? Also, I think sometimes browser sends a a “FIN” with data packet and I fail to catch it. I sometimes see connection from browser on a socket which was never closed. (Linux reuses the same fd numbers. That is one of the reason, I believe I am missing socket closure.)
Where could I be going wrong.
Thanks
For a generic TCP forwarder, shutdown is the right thing to use. When you read an EOF from one side, stop reading that side and shutdown the writing on the other side. Then you can keep reading and writing the other direction until it also gives you an EOF, and then you close both.