/**********************************************************************
Module Name:AgentGateWay.c
Date:2001/4/15
CopyRight(c) eyas
說明:端口重定向工具,在網關上運作,把端口重定向到内網的IP、PORT,
就可以進入内網了
sock[0]==>sClient sock[1]==>sTarget
**********************************************************************/
#include
#include
#include "TCPDataRedird.c"
#define TargetIP TEXT("192.168.1.3")
#define TargetPort (int)3389
#define ListenPort (int)3389//監聽端口
#pragma comment(lib,"ws2_32.lib")
int main()
{
WSADATA wsd;
SOCKET sListen=INVALID_SOCKET,//本機監聽的socket
sock[2];
struct sockaddr_in Local,Client,Target;
int iAddrSize;
HANDLE hThreadC2T=NULL,//C2T=ClientToTarget
hThreadT2C=NULL;//T2C=TargetToClient
DWORD dwThreadID;
__try
{
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
printf("/nWSAStartup() failed:%d",GetLastError());
__leave;
}
sListen=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sListen==INVALID_SOCKET)
{
printf("/nsocket() failed:%d",GetLastError());
__leave;
}
Local.sin_addr.s_addr=htonl(INADDR_ANY);
Local.sin_family=AF_INET;
Local.sin_port=htons(ListenPort);
Target.sin_family=AF_INET;
Target.sin_addr.s_addr=inet_addr(TargetIP);
Target.sin_port=htons(TargetPort);
if(bind(sListen,(struct sockaddr *)&Local,sizeof(Local))==SOCKET_ERROR)
{
printf("/nbind() failed:%d",GetLastError());
__leave;
}
if(listen(sListen,1)==SOCKET_ERROR)
{
printf("/nlisten() failed:%d",GetLastError());
__leave;
}
//scoket循環
while(1)
{
printf("/n/n*************Waiting Client Connect to**************/n/n");
iAddrSize=sizeof(Client);
//get socket sClient
sock[0]=accept(sListen,(struct sockaddr *)&Client,&iAddrSize);
if(sock[0]==INVALID_SOCKET)
{
printf("/naccept() failed:%d",GetLastError());
break;
}
printf("/nAccept client==>%s:%d",inet_ntoa(Client.sin_addr), ntohs(Client.sin_port));
//create socket sTarget
sock[1]=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sock[1]==INVALID_SOCKET)
{
printf("/nsocket() failed:%d",GetLastError());
__leave;
}
//connect to target port
if(connect(sock[1],(struct sockaddr *)&Target,sizeof(Target))==SOCKET_ERROR)
{
printf("/nconnect() failed:%d",GetLastError());
__leave;
}
printf("/nconnect to target 3389 success!");
//建立兩個線程進行資料轉發
hThreadC2T=CreateThread(NULL,0,TCPDataC2T,(LPVOID)sock,0,&dwThreadID);
hThreadT2C=CreateThread(NULL,0,TCPDataT2C,(LPVOID)sock,0,&dwThreadID);
//等待兩個線程結束
WaitForSingleObject(hThreadC2T,INFINITE);
WaitForSingleObject(hThreadT2C,INFINITE);
CloseHandle(hThreadC2T);
CloseHandle(hThreadT2C);
closesocket(sock[1]);
closesocket(sock[0]);
printf("/n/n*****************Connection Close*******************/n/n");
}//end of sock外循環
}//end of try
__finally
{
if(sListen!=INVALID_SOCKET) closesocket(sListen);
if(sock[0]!=INVALID_SOCKET) closesocket(sock[0]);
if(sock[1]!=INVALID_SOCKET) closesocket(sock[1]);
if(hThreadC2T!=NULL) CloseHandle(hThreadC2T);
if(hThreadT2C!=NULL) CloseHandle(hThreadT2C);
WSACleanup();
}
return 0;
}
/*************************************************************************
Module:TCPDataRedird.c
Date:2001/4/16
CopyRight(c) eyas
HomePage:www.patching.net
Thanks to shotgun
說明:TCP socket資料轉發,sock[0]==>sClient sock[1]==>sTarget
*************************************************************************/
#define BuffSize 20*1024 //緩沖區大小20k
//此函數負責從Client讀取資料,然後轉發給Target
DWORD WINAPI TCPDataC2T(SOCKET* sock)
{
int iRet,
ret=-1,//select 傳回值
iLeft,
idx,
iSTTBCS=0;//STTBCS=SendToTargetBuffCurrentSize
char szSendToTargetBuff[BuffSize]=,
szRecvFromClientBuff[BuffSize]=;
fd_set fdread,fdwrite;
printf("/n/n*****************Connection Active*******************/n/n");
while(1)
{
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_SET(sock[0],&fdread);
FD_SET(sock[1],&fdwrite);
if((ret=select(0,&fdread,&fdwrite,NULL,NULL))==SOCKET_ERROR)
{
printf("/nselect() failed:%d",GetLastError());
break;
}
//printf("/nselect() return value ret=%d",ret);
if(ret>0)
{
//sClinet可讀,client有資料要發送過來
if(FD_ISSET(sock[0],&fdread))
{
//接收sock[0]發送來的資料
iRet=recv(sock[0],szRecvFromClientBuff,BuffSize,0);
if(iRet==SOCKET_ERROR)
{
printf("/nrecv() from sock[0] failed:%d",GetLastError());
break;
}
else if(iRet==0)
break;
printf("/nrecv %d bytes from sClinet.",iRet);
//把從client接收到的資料存添加到發往target的緩沖區
memcpy(szSendToTargetBuff+iSTTBCS,szRecvFromClientBuff,iRet);
//重新整理發往target的資料緩沖區目前buff大小
iSTTBCS+=iRet;
//清空接收client資料的緩沖區
memset(szRecvFromClientBuff,0,BuffSize);
}
//sTarget可寫,把從client接收到的資料發送到target
if(FD_ISSET(sock[1],&fdwrite))
{
//轉發資料到target的3389端口
iLeft=iSTTBCS;
idx=0;
while(iLeft>0)
{
iRet=send(sock[1],&szSendToTargetBuff[idx],iLeft,0);
if(iRet==SOCKET_ERROR)
{
printf("/nsend() to target failed:%d",GetLastError());
break;
}
printf("/nsend %d bytes to target",iRet);
iLeft-=iRet;
idx+=iRet;
}
//清空緩沖區
memset(szSendToTargetBuff,0,BuffSize);
//重置發往target的資料緩沖區目前buff大小
iSTTBCS=0;
}
}//end of select ret
Sleep(1);
}//end of data send & recv循環
return 0;
}
//此函數負責從target讀取資料,然後發送給client
DWORD WINAPI TCPDataT2C(SOCKET* sock)
{
int iRet,
ret=-1,//select 傳回值
iLeft,
idx,
iSTCBCS=0;//STCBCS=SendToClientBuffCurrentSize
char szRecvFromTargetBuff[BuffSize]=,
szSendToClientBuff[BuffSize]=;
fd_set fdread,fdwrite;
while(1)
{
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_SET(sock[0],&fdwrite);
FD_SET(sock[1],&fdread);
if((ret=select(0,&fdread,&fdwrite,NULL,NULL))==SOCKET_ERROR)
{
printf("/nselect() failed:%d",GetLastError());
break;
}
if(ret>0)
{
//sTarget可讀,從target接收資料
if(FD_ISSET(sock[1],&fdread))
{
//接收target傳回資料
iRet=recv(sock[1],szRecvFromTargetBuff,BuffSize,0);
if(iRet==SOCKET_ERROR)
{
printf("/nrecv() from target failed:%d",GetLastError());
break;
}
else if(iRet==0)
break;
printf("/nrecv %d bytes from target",iRet);
//把從target接收到的資料添加到發送到client的緩沖區
memcpy(szSendToClientBuff+iSTCBCS,szRecvFromTargetBuff,iRet);
//清空接收target傳回資料緩沖區
memset(szRecvFromTargetBuff,0,BuffSize);
//重新整理發送到client的資料緩沖區目前大小
iSTCBCS+=iRet;
}
//client可寫,發送target傳回資料到client
if(FD_ISSET(sock[0],&fdwrite))
{
//發送target傳回資料到client
iLeft=iSTCBCS;
idx=0;
while(iLeft>0)
{
iRet=send(sock[0],&szSendToClientBuff[idx],iLeft,0);
if(iRet==SOCKET_ERROR)
{
printf("/nsend() to Client failed:%d",GetLastError());
break;
}
printf("/nsend %d bytes to Client",iRet);
iLeft-=iRet;
idx+=iRet;
}
//清空緩沖區
memset(szSendToClientBuff,0,BuffSize);
iSTCBCS=0;
}
}//end of select ret
Sleep(1);
}//end of while
return 0;
}