全部博文(403)
分类: C/C++
2006-08-07 15:15:44
#include
#include
#include
#include
//Windows和LINUX系統所提供的Socket API不一樣,需要分別聲明
#ifdef _WIN32
#include
#include
#else
#include
#include
#include
#endif
//定義一段緩衝區
#define BUFSZ 65535
char buf[ BUFSZ ];
int main(int argc, char** argv)
{
//這是Server的資料,包括IP地址以及用作TCP和UDP連接的端口號
short tcp_proxy_port=8888, udp_proxy_port=8811;
char udp_proxy_ip[]="127.0.0.1";
short clt_udp_port;
//在Windows系統下,使用網絡前要先用WSAStartup()來進行初始化,
//WSA是Windows Socket API的意思,本文使用WinSock 2.0版本
//在VC中可以用WINSOCK_VERSION來指定版本,不過GCC FOR WIN32沒有定義這個宏
//我們需要自己指定
#ifdef _WIN32
WSADATA WSAData;
if( WSAStartup(0x0110, &WSAData) ) {
//if( WSAStartup(WINSOCK_VERSION, &WSAData) ) {
p_error("WSA error");
exit(-1);
}
#endif
//啟動一個TCP SOCKET,用來和QQ進行握手,並記錄QQ用來和我們溝通的UDP端口號(clt_udp_port)
Launch_TCP( tcp_proxy_port, udp_proxy_ip, udp_proxy_port, &clt_udp_port );
//握手成功後,啟動一個UDP SOCKET,用作數據傳輸,是真正起PROXY作用的部份
Launch_UDP( udp_proxy_port, udp_proxy_ip, clt_udp_port );
return 0;
}
void p_error( const char *err_msg )
{
printf( "ERR=>%s\n", err_msg );
}
void debug_showbin( const char *dbuf, int n, const char *name, const char *end )
{
int i;
printf( "%s ==> %d bytes: ", name, n );
for( i=0; iprintf( "(0x%x)", (unsigned char)dbuf[i] );
printf( "%s", end );
}
void debug_showip( const struct sockaddr_in *dbuf, const char *name, const char *end )
{
printf("[ %s ==> %s:%u ]%s", name, inet_ntoa(dbuf->sin_addr), ntohs(dbuf->sin_port), end
);
}
< TCP/IP Session - START >
RECV ==> 3 bytes: (0x5)(0x1)(0x0)
SEND ==> 2 bytes: (0x5)(0x0)
RECV ==> 10 bytes: (0x5)(0x3)(0x0)(0x1)(0x0)(0x0)(0x0)(0x0)(0x6)(0x32)
SEND ==> 10 bytes: (0x5)(0x0)(0x0)(0x1)(0x7f)(0x0)(0x0)(0x1)(0x22)(0x6b)
< TCP/IP Session - END >
void Launch_TCP( int service_port, const char *udp_proxy_ip, int udp_proxy_port, short *clt_udp_port )
{
//port is NOT network orders
struct sockaddr_in servaddr,clientaddr;
int clientlen;
int listenfd, connfd;
int n;
//定義socket, bind, listen, accept,關於這些操作的資料太多了,不詳述
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(service_port);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd < 0) {
p_error("socket error");
exit(-1);
}
if( bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) {
p_error("bind error");
exit(-1);
}
if( listen(listenfd, 5) < 0 ) {
p_error("listen error");
exit(-1);
}
connfd = accept( listenfd, (struct sockaddr *)&clientaddr, &clientlen );
if( connfd < 0 ) {
p_error("accept error");
exit(-1);
}
printf("< TCP/IP Session - START >\n\n");
//接受第一句請求
n = recv( connfd, buf, BUFSZ, 0 );
debug_showbin( buf, n, "RECV", "\n" );
//目前我們只支持無身份驗證的請求,即"05 01 00"
if( buf[0]==0x5 && buf[1]==0x1 && buf[2]==0x0) {
buf[0] = 0x5;
buf[1] = 0x0;
//返回"05 00",代表成功
send( connfd, buf, 2, 0 );
debug_showbin( buf, 2, "SEND", "\n\n" );
} else {
p_error("Session ERROR!\n");
exit(-1);
}
//接受第二句請求
n = recv( connfd, buf, BUFSZ, 0 );
debug_showbin( buf, n, "RECV", "\n" );
//只處理UDP請求(0x03)
if( buf[0]==0x5 && buf[1]==0x3 ) { //Client request a UDP Proxy
short udp_port;
long udp_ip;
//提取並儲存客戶端的UDP端口號
int seg=4;
if( buf[3] == 0x3 )
seg = buf[4]+1;
memcpy( clt_udp_port, &buf[4+seg], 2 );
*clt_udp_port = ntohs( *clt_udp_port );
buf[0] = 0x5;
buf[1] = 0x0;
buf[2] = 0x0;
buf[3] = 0x1;
//把本機UDP SOCKET的IP和PORT返回給QQ
udp_ip = inet_addr( udp_proxy_ip );
udp_port = htons( udp_proxy_port );
memcpy( &buf[4], &udp_ip, 4 );
memcpy( &buf[8], &udp_port, 2 );
send(connfd, buf, 10, 0 );
debug_showbin( buf, 10, "SEND", "\n\n" );
} else {
p_error("Session ERROR: Client doesn't need a UDP Proxy!\n");
exit(-1);
}
//握手過程完成
close(connfd);
close(listenfd);
printf("< TCP/IP Session - END >\n\n");
}
void Launch_UDP( int udp_proxy_port, const char *udp_proxy_ip, int clt_udp_port )
{
//port is NOT network orders
//記錄本機,客戶端,遠端服務器和封包來源地址
struct sockaddr_in servaddr,clientaddr,remoteaddr,inaddr;
int inlen;
int listenfd;
int n;
fd_set set;
//把接收來的數據寫在緩衝區第11個Byte之後,前10 Bytes用來存放Header
char *thisbuf = &buf[10];
int thissize = BUFSZ - 10;
printf("< UDP Session - START >\n\n");
//建立一個UDP SOCKET,注意UDP協議不需要listen, accept和conenct
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons( udp_proxy_port );
servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
memset(&remoteaddr, 0, sizeof(remoteaddr));
remoteaddr.sin_family = AF_INET;
listenfd = socket(AF_INET, SOCK_DGRAM, 0);
if(listenfd < 0) {
p_error("socket error");
exit(-1);
}
if( bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) {
p_error("bind error");
exit(-1);
}
//使用select來監控Socket是否有資料可讀
FD_ZERO(&set);
FD_SET(listenfd, &set);
while( 1 ) {
if( select( listenfd+1, &set, NULL, NULL, NULL) < 0 ) {
p_error("select error");
exit(-1);
}
if( FD_ISSET( listenfd, &set ) ) {
//UDP協議可使用recvfrom()來接收數據,並獲得來源地地址
n = recvfrom( listenfd, thisbuf, thissize, 0, (struct sockaddr *)&inaddr, &inlen );
if( n >=0 ) {
debug_showip( &inaddr, "Received From", "\n" );
//資料來自客戶端
if( (thisbuf[0]==0x0) && (thisbuf[1]==0x0) &&
(htons(inaddr.sin_port)==clt_udp_port) )
{
//保存客戶端的地址
memcpy( &clientaddr, &inaddr, sizeof(clientaddr) );
if( thisbuf[3] != 0x1 ) {
//如果目的地地址類型為域名,先進行解析獲得IP再發送
struct hostent *h;
char tmp[256];
int seg;
strncpy( tmp, &thisbuf[5], thisbuf[4] );
tmp[ thisbuf[4] ] = 0;
h = gethostbyname ( tmp ); //
if( h == NULL )
p_error("unknown domain name\n");
else
{
remoteaddr.sin_addr.s_addr = (*(struct in_addr*)h->h_addr).s_addr;
seg = thisbuf[4]+1;
memcpy( &remoteaddr.sin_port, &thisbuf[4+seg], 2 );
debug_showbin( thisbuf, 4+seg+2, "RECV CLIENT [ Header ]","\n");
debug_showbin( &thisbuf[4+seg+2], n-(4+seg+2), "RECV CLIENT [ Data ]","\n");
debug_showip( &remoteaddr, "Send to DOMAIN", "\n\n");
sendto( listenfd, &thisbuf[4+seg+2], n-(4+seg+2), 0, (struct sockaddr*)&remoteaddr,sizeof(remoteaddr));
}
} else {
//目的地地址為IPv4,直接把資料發送過去
memcpy( &remoteaddr.sin_port, &thisbuf[8], 2 );
memcpy( &remoteaddr.sin_addr.s_addr, &thisbuf[4], 4 );
debug_showbin( thisbuf, 10, "RECV CLIENT [ Header ]","\n");
debug_showbin( &thisbuf[10], n-10, "RECV CLIENT [ Data ]","\n");
debug_showip( &remoteaddr, "Send to IP", "\n\n");
sendto( listenfd, &thisbuf[10], n-10, 0, (struct sockaddr*)&remoteaddr,sizeof(remoteaddr));
}
}
else
{ //資料來自遠端服務器
debug_showbin(thisbuf, n, "RECV REMOTE","\n");
debug_showip(&clientaddr, "Send to CLT","\n\n");
//編寫Header
buf[0] = 0x0;
buf[1] = 0x0;
buf[2] = 0x0;
buf[3] = 0x1;
memcpy( &buf[4], &udp_proxy_ip, 4 );
memcpy( &buf[8], &udp_proxy_port, 2 );
//發送到客戶端
sendto( listenfd, buf, n+10, 0, (struct sockaddr*)&clientaddr,sizeof(clientaddr));
} } }
}
close(listenfd);
printf("< UDP Session - END >\n\n");
}