Skip to content

Commit

Permalink
Update Lcx.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
lisf0 authored Nov 14, 2019
1 parent 6163c23 commit 53688ff
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions Lcx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")

//宏定义
//宏定义

typedef struct{
SOCKET s1;
SOCKET s2;
}Stu_sock;

//函数声明
//函数声明
void version();
int param(int argc,char **argv); //对命令行参数
void Funlisten(int port1, int port2); //监听功能函数
bool bindAndFunlisten(SOCKET s,int port);//绑定socket的地址结构
void datatrans(LPVOID data); //数据转发函数
void slave(char* hostIp,char * slaveIp,int destionPort,int slavePort);//slave函数
int param(int argc,char **argv); //对命令行参数
void Funlisten(int port1, int port2); //监听功能函数
bool bindAndFunlisten(SOCKET s,int port);//绑定socket的地址结构
void datatrans(LPVOID data); //数据转发函数
void slave(char* hostIp,char * slaveIp,int destionPort,int slavePort);//slave函数
bool checkIP(char * str);
int client_connect(int sockfd,char* server,int port);//连接服务端
int client_connect(int sockfd,char* server,int port);//连接服务端

void main(int argc,char **argv)
{

version();
//参数判断
//参数判断
WSADATA wsadata;
WSAStartup(MAKEWORD(1, 1), &wsadata);
char hostIp[20]={0};
Expand All @@ -50,7 +50,7 @@ void main(int argc,char **argv)

int param(int argc,char **argv)
{
//stricmp==strcmp ignore case忽略大小写
//stricmp==strcmp ignore case忽略大小写
if (argc==4&&stricmp(argv[1],"-listen")==0)
{
// cout<<"Funlisten"<<endl;
Expand All @@ -67,14 +67,14 @@ int param(int argc,char **argv)
}
/************************************************************************/
/* */
/* listen 功能模块 */
/* listen 功能模块 */
/************************************************************************/

void Funlisten(int port1,int port2)
{
Stu_sock stu_sock;

//创建套接字
//创建套接字
SOCKET sock1=socket(AF_INET,SOCK_STREAM,0);
SOCKET sock2=socket(AF_INET,SOCK_STREAM,0);
if (sock1<0||sock1<0)
Expand All @@ -83,20 +83,20 @@ void Funlisten(int port1,int port2)
return;
}

//绑定端口到socket并监听
//绑定端口到socket并监听
if (!bindAndFunlisten(sock1,port1)||!bindAndFunlisten(sock2,port2))
{
return;
}
//都监听好了接下来……
//都监听好了接下来……


int SizeOfAddr=sizeof(sockaddr);
while (true)
{
cout<<"[+] Waiting for Client ......"<<endl;
sockaddr_in remoteSockAddr;
//sock1等待连接
//sock1等待连接
SOCKET recvSock1=accept(sock1,(sockaddr *)&remoteSockAddr,&SizeOfAddr);
cout<<recvSock1<<endl;
if (recvSock1<0)
Expand All @@ -116,22 +116,22 @@ void Funlisten(int port1,int port2)
}
cout<<"[+] Accept a Client on port"<<port1<<" from "<<inet_ntoa(remoteSockAddr.sin_addr)<<endl;

//两个都连上来了
//两个都连上来了
cout<<"[+] Accept Connect OK!"<<endl;


stu_sock.s1=recvSock1; stu_sock.s2=recvSock2;
DWORD dwThreadID;

//创建一个转发数据的线程
//创建一个转发数据的线程
HANDLE hThread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)datatrans,(LPVOID)&stu_sock,0,&dwThreadID);
if (hThread==NULL)
{
TerminateThread(hThread,0);
return;//线程错了直接退出
return;//线程错了直接退出
}
cout<<"[+] CreateThread OK!"<<endl;
Sleep(800);//挂起当前线程
Sleep(800);//挂起当前线程

//
}
Expand All @@ -142,7 +142,7 @@ void Funlisten(int port1,int port2)

bool bindAndFunlisten(SOCKET s,int port)
{
//地址结构
//地址结构
sockaddr_in addr;
memset(&addr,0,sizeof(addr));
addr.sin_port=htons(port);
Expand All @@ -152,14 +152,14 @@ bool bindAndFunlisten(SOCKET s,int port)
char on =1;
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));

//绑定地址结构
//绑定地址结构
if (bind(s,(const sockaddr *)&addr,sizeof(sockaddr))<0)
{
cout<<"[-] Socket bind error."<<endl;
return false;
}

//监听端口
//监听端口
if (listen(s,5)<0)
{
cout<<"[-] Listen error."<<endl;
Expand All @@ -173,8 +173,8 @@ void datatrans(LPVOID data)
char host_slave[20]={0};
char host_hacker[20]={0};
Stu_sock *stuSock=(Stu_sock *)data;
SOCKET s1=stuSock->s1; //接受的是slave的数据
SOCKET s2=stuSock->s2; //发送出去的socket
SOCKET s1=stuSock->s1; //接受的是slave的数据
SOCKET s2=stuSock->s2; //发送出去的socket
int sentacount1=0;
// cout<<stuSock->s1<<endl;
// cout<<stuSock->s2<<endl;
Expand Down Expand Up @@ -233,12 +233,12 @@ void datatrans(LPVOID data)
break;
}

//没出错,没超时
//没出错,没超时
if (FD_ISSET(s1,&readfd)&&flag)///////////////////////////////////1
{
// if (totalread1<20408)
{
readsize=recv(s1, RecvBuffer, 20480, 0);//接受host的请求。。
readsize=recv(s1, RecvBuffer, 20480, 0);//接受host的请求。。
if (readsize==-1)
{
break;
Expand All @@ -254,10 +254,10 @@ void datatrans(LPVOID data)
}


if (FD_ISSET(s1,&writefd)&&flag&&(readsize>0))///////////////////////////////////2
if (FD_ISSET(s2,&writefd)&&flag&&(readsize>0))///////////////////////////////////2
{

int sendsize=send(s2,SendBuffer,readsize,0);//发给slave
int sendsize=send(s2,SendBuffer,readsize,0);//发给slave
if (sendsize==0)
{
break;
Expand All @@ -275,7 +275,7 @@ void datatrans(LPVOID data)
if (FD_ISSET(s2,&readfd)&&(!flag))///////////////////////////////////3
{
{
readsize=recv(s2, RecvBuffer, 20480, 0);//接受slave返回数据
readsize=recv(s2, RecvBuffer, 20480, 0);//接受slave返回数据

memcpy(SendBuffer,RecvBuffer,readsize);
cout<<" [+] Recv "<<readsize<<" bytes "<<"from host."<<endl;
Expand All @@ -285,7 +285,7 @@ void datatrans(LPVOID data)
}
if (FD_ISSET(s1,&writefd)&&(!flag)&&(readsize>0))///////////////////////////////////4
{
readsize=send(s1,SendBuffer,readsize,0);//发给host
readsize=send(s1,SendBuffer,readsize,0);//发给host
if (readsize==0)
{
break;
Expand Down Expand Up @@ -313,7 +313,7 @@ void datatrans(LPVOID data)

/************************************************************************/
/* */
/* slave 功能模块 */
/* slave 功能模块 */
/************************************************************************/
void slave(char* hostIp,char * slaveIp,int destionPort,int slavePort)
{
Expand All @@ -325,7 +325,7 @@ void slave(char* hostIp,char * slaveIp,int destionPort,int slavePort)
int l;
while(TRUE)
{
//创建套接字
//创建套接字
SOCKET sock1=socket(AF_INET,SOCK_STREAM,0);
SOCKET sock2=socket(AF_INET,SOCK_STREAM,0);

Expand All @@ -341,24 +341,24 @@ void slave(char* hostIp,char * slaveIp,int destionPort,int slavePort)
{
closesocket(sock1);
closesocket(sock2);
continue;/*跳过这次循环*/
continue;/*跳过这次循环*/
}

memset(buffer,0,20480);
while(1)
{
//把sock清零,加入set集
//把sock清零,加入set集
FD_ZERO(&fd);
FD_SET(sock1, &fd);

//select事件 读 写 异常
//select事件 读 写 异常
if (select(sock1+1, &fd, NULL, NULL, NULL) == SOCKET_ERROR)
{
//不懂
//不懂
if (errno == WSAEINTR) continue;
break;
}
//FD_ISSET返回值>0 表示SET里的可读写
//FD_ISSET返回值>0 表示SET里的可读写
if (FD_ISSET(sock1, &fd))
{
l=recv(sock1, buffer, 20480, 0);
Expand Down Expand Up @@ -410,7 +410,7 @@ void slave(char* hostIp,char * slaveIp,int destionPort,int slavePort)
}
}

//检查IP地址格式是否正确
//检查IP地址格式是否正确
bool checkIP(char * str)
{
if(INADDR_NONE == inet_addr(str))
Expand All @@ -419,24 +419,24 @@ bool checkIP(char * str)
}

int client_connect(int sockfd,char* server,int port)
{ /*sock*/ /*远程IP*/ /*远程端口*/
{ /*sock*/ /*远程IP*/ /*远程端口*/

//声明
//声明
struct sockaddr_in cliaddr;
struct hostent *host;

if(!(host=gethostbyname(server))) //获得远程主机的IP
if(!(host=gethostbyname(server))) //获得远程主机的IP
{
// printf("[-] Gethostbyname(%s) error:%s\n",server,strerror(errno));
return(0);
}
//给地址结构赋值
//给地址结构赋值
memset(&cliaddr, 0, sizeof(struct sockaddr));
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(port);/*远程端口*/
cliaddr.sin_port=htons(port);/*远程端口*/
cliaddr.sin_addr=*((struct in_addr *)host->h_addr);//host ip

//去连接远程正在listen的主机
//去连接远程正在listen的主机
if(connect(sockfd,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr))<0)
{
// printf("[-] Connect error.\r\n");
Expand Down

0 comments on commit 53688ff

Please sign in to comment.