I'm trying to write a proxy that tunnels TCP connections over a sequence of HTTP POST requests. (Yes, this is evil.) I'm using Lua and Copas for this as being the easiest way to do this kind of server. It's mostly working well except for... well, this. What I'm finding is that I can establish the tunnel, and pass data back and forth, and then eventually something will go wrong. This can manifest in various ways: either the proxy will block on a Copas read or write and stop responding to any other connections, or else one of the sockets will mysteriously close itself. I managed to persuade it to fail in the second mode often enough to do some debugging. Looking at the system call trace, I can see it do this: send(5, "0円0円0円214円6円 0円0円0円200円37円U22円I21円k\"!260円302円317円17円307円352円&220円33円327円2円Eu213円"..., 144, 0) = 144 ...as it sends some stuff to the upstream server; then I can see this: send(4, "HTTP/1.1 200 OK\r\nX-SessionId: 84"..., 40, 0) = 40 fcntl64(4, F_GETFL) = 0x802 (flags O_RDWR|O_NONBLOCK) fcntl64(4, F_SETFL, O_RDWR) = 0 close(4) ...as it tells the downstream server it's done it. This happens over and over again as data goes through. Then, eventually, this happens: send(5, "0円0円0円\f\n25円0円0円0円0円0円0円0円0円0円0円", 16, 0) = 16 send(4, "HTTP/1.1 200 OK\r\nX-SessionId: 84"..., 40, 0) = 40 fcntl64(5, F_GETFL) = 0x802 (flags O_RDWR|O_NONBLOCK) fcntl64(5, F_SETFL, O_RDWR) = 0 close(5) = 0 It seems to have just closed the wrong socket! My program, such as it is, is here: http://pastebin.com/yTXGh6xB (183 lines, too antisocial to post). Am I going insane, or am I just making a stupid mistake somewhere? I'm on Debian, using stock Lua 5.1.4-5, copas 1.1.5-2, coxpcall 1.13.0-2, luasocket 2.0.2-4 respectively. -- ┌─── dg@cowlark.com ───── http://www.cowlark.com ───── │ │ "I have a mind like a steel trap. It's rusty and full of dead mice." │ --- Anonymous, on rasfc
Attachment:
signature.asc
Description: OpenPGP digital signature