simple redirect a local port to a remote port



PortRedirect proc pDestIP:DWORD,SrcSock:DWORD,DestPort:DWORD
LOCAL sockAddr:sockaddr_in
LOCAL DestSock:DWORD
LOCAL tempBuffer[4096]:byte

;Establish destination socket
invoke inet_addr,pDestIP
lea edx, sockAddr
mov ecx, DestPort
xchg cl, ch ; convert to network byte order
mov [edx][sockaddr_in.sin_family], AF_INET
mov [edx][sockaddr_in.sin_port], cx
mov [edx][sockaddr_in.sin_addr.S_un.S_addr], eax
invoke socket, AF_INET, SOCK_STREAM, IPPROTO_TCP
mov DestSock,eax
invoke connect, DestSock, addr sockAddr, sizeof sockAddr

;Relay request header
invoke recv,SrcSock,addr tempBuffer,4096,NULL ;Get request header
invoke send,DestSock,addr tempBuffer,eax, 0 ;Relay request header
test eax, eax
jz _connectionClosed ;0 return means connection closed
cmp eax, SOCKET_ERROR
je _error

;Relay data
_recvLoop:
invoke recv, DestSock,addr tempBuffer, 4096, 0
test eax, eax
jz _connectionClosed
cmp eax, SOCKET_ERROR
je _error
invoke send, SrcSock,addr tempBuffer,eax,0 ;Relay data
cmp eax, SOCKET_ERROR
je _error

jmp _recvLoop

_connectionClosed:
invoke closesocket, SrcSock
invoke closesocket, DestPort
ret

_error:
invoke WSAGetLastError
debug
jmp _connectionClosed

PortRedirect endp
Posted on 2003-10-13 01:23:13 by optimus
Hi,
is this like proxy server?
Can this code be used only in http proxy servers or just about any other?
Posted on 2003-10-18 15:29:06 by Mikky
@optimus: Really neat work! :alright:

@Mikky: This is a TCP/IP bouncer, so it works for any protocol that works with TCP. Actually proxies are quite more complicated, as they work like HTTP servers themselves.

Posted on 2003-10-18 15:53:07 by QvasiModo
Hm, so why did they invent httpd-proxy or any other specific proxy (like socks) when usual bouncer like this would do the work?

ps. I think it "proxies" :D
Posted on 2003-10-22 18:19:48 by Mikky
Well, http-proxies do a lot more than simple bouncing. For example, they can alter the http headers, or cache the data downloaded by different users. Besides, a simple TCP/IP relayer always redirects everything to the same IP and port... there's no way to configure it (unless you set up another port to do that).
As for socks proxies, I don't know much about them, but I guess it must be a similar case.
Posted on 2003-10-24 20:46:57 by QvasiModo