#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <signal.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/time.h>
#include <unistd.h>
#define PORT 5004
#define MAXDATASIZE 100
#define RTMAX 15
#define SS 30
struct xOLD
{
int rtid;
char rtname[44];
char rtip[38];
char ethx[16];
};
struct sendata
{
char USERIP[38];
int RIDX;
int WTOPER;
char SIP[38];
};
int sendtonat(char * userIP,char *natIP, int port,int RIDX);
char MyIP[32];
int main(int argc, char *argv[])
{
if (argc !=3)
{
exit(1);
}
memset(MyIP,0,sizeof(MyIP));
strcat(MyIP,argv[2]);
char buff2[256];
int RTCOUNT=RTMAX;
struct xOLD rtx[RTCOUNT];
int i=1;
int IDX=0;
char buffi[4];
char *p2;
while(1)
{
if (i>=RTCOUNT) break;
rtx[i].rtid=i;
memset(rtx[i].rtname,0,sizeof(rtx[i].rtname));
memset(rtx[i].rtip,0,sizeof(rtx[i].rtip));
memset(rtx[i].ethx,0,sizeof(rtx[i].ethx));
memset(buff2,0,sizeof(buff2));
memset(buffi,0,sizeof(buffi));
sprintf(buffi,"%d",i);
strcat(buff2,"awk -F ^");
strcat(buff2,buffi);
strcat(buff2, " '{print$2}' ");
strcat(buff2,argv[1]);
strcat(buff2," > /tmp/htrx ");
system(buff2);
p2=GetStrByFile("/tmp/htrx");
if (strlen(p2)>0)
{
IDX++;
system("awk -F' ' '{print$1}' /tmp/htrx > /tmp/rtx2");
p2=GetStrByFile("/tmp/rtx2");
strcat(rtx[i].rtname,p2);
system("awk -F' ' '{print$2}' /tmp/htrx > /tmp/rtx2");
p2=GetStrByFile("/tmp/rtx2");
strcat(rtx[i].rtip,p2);
system("awk -F' ' '{print$3}' /tmp/htrx > /tmp/rtx2");
p2=GetStrByFile("/tmp/rtx2");
strcat(rtx[i].ethx,p2);
memset(buff2,0,sizeof(buff2));
strcat(buff2,"/sbin/ip route replace default via ");
strcat(buff2,rtx[i].rtip);
strcat(buff2," ta ");
strcat(buff2,rtx[i].rtname);
strcat(buff2," dev ");
strcat(buff2,rtx[i].ethx);
system(buff2);
}
i++;
}
int prefx=0;
memset(buff2,0,sizeof(buff2));
strcat(buff2,"select prefx from rts where ipx<>'0.0.0.0'");
sqlite3_exec("/usr/tmp/rtx.db",buff2,"/tmp/Tfsqlh");
i=1;
while (1)
{
memset(buff2,0,sizeof(buff2));
memset(buffi,0,sizeof(buffi));
sprintf(buffi,"%d",i);
strcat(buff2,"sed -n '");
strcat(buff2,buffi);
strcat(buff2,"' /tmp/Tfsqlh >/tmp/hhhht");
system(buff2);
prefx=GetIntByFile("/tmp/hhhht");
if (prefx==0) break;
memset(buff2,0,sizeof(buff2));
memset(buffi,0,sizeof(buffi));
sprintf(buffi,"%d",prefx);
strcat(buff2,"/sbin/ip ru | grep ");
strcat(buff2,buffi);
strcat(buff2," >/tmp/hhhht");
system(buff2);
if (get_file_size("/tmp/hhhht")==0)
{
memset(buff2,0,sizeof(buff2));
strcat(buff2,"update rts set ipx='0.0.0.0' where prefx=");
strcat(buff2,buffi);
sqlite3_exec("/usr/tmp/rtx.db",buff2);
}
i++;
}
system("/sbin/ip ro flush cache");
int fd,fds;
int sin_size;
char msg[MAXDATASIZE];
int num=0;
struct hostent * he;
struct sendata send1;
struct sendata recv1;
char *p1=msg;
if((he=gethostbyname("0.0.0.0"))==NULL)
{
return 0;
}
if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1){
return 0;
}
struct sockaddr_in server;
struct sockaddr_in client;
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
memcpy(&server.sin_addr.s_addr,he->h_addr,he->h_length);
if ( bind(fd,(struct sockaddr*)&server,sizeof(server))==-1)
{
return 0;
}
sin_size=sizeof(client);
int hfx=0;
int INorUP=0;
while (1){
num = recvfrom(fd,msg,MAXDATASIZE,0,(struct sockaddr *)&client,&sin_size);
memcpy(&recv1,p1,sizeof(recv1));
memset(buff2,0,sizeof(buff2));
strcat(buff2,"select prefx from rts where ipx='");
strcat(buff2,recv1.USERIP);
strcat(buff2,"");
sqlite3_exec("/usr/tmp/rtx.db",buff2,"/tmp/Tfsql");
prefx=GetIntByFile("/tmp/Tfsql");
if (recv1.WTOPER==2) //del route
{
if (prefx==0) continue;
memset(buff2,0,sizeof(buff2));
memset(buffi,0,sizeof(buffi));
strcat(buff2,"/sbin/ip ru del ");
strcat(buff2," pref ");
sprintf(buffi,"%d",prefx);
strcat(buff2,buffi);
system(buff2);
system("/sbin/ip ro flush cache");
//save pref in sqlite3
memset(buff2,0,sizeof(buff2));
strcat(buff2,"update rts set ipx='0.0.0.0' where ipx='");
strcat(buff2,recv1.USERIP);
strcat(buff2,"'");
sqlite3_exec("/usr/tmp/rtx.db",buff2);
// continue;
}
if (prefx>0)
{
memset(buffi,0,sizeof(buffi));
sprintf(buffi,"%d",prefx);
memset(buff2,0,sizeof(buff2));
strcat(buff2,"/sbin/ip ru del pref ");
strcat(buff2,buffi);
system(buff2);
strcat(buff2,"/sbin/ip ru add from ");
strcat(buff2,recv1.USERIP);
strcat(buff2," pref ");
strcat(buff2,buffi);
strcat(buff2," ta ");
strcat(buff2,rtx[recv1.RIDX].rtname);
system(buff2);
system("/sbin/ip ro flush cache");
strcat(buff2,"update rts set idx=");
memset(buffi,0,sizeof(buffi));
sprintf(buffi,"%d",recv1.RIDX);
strcat(buff2,buffi);
strcat(buff2," where prefx=");
memset(buffi,0,sizeof(buffi));
sprintf(buffi,"%d",prefx);
strcat(buff2,buffi);
sqlite3_exec("/usr/tmp/rtx.db",buff2);
continue;
}
INorUP=1;
hfx=0;
sqlite3_exec("/usr/tmp/rtx.db","select max(prefx) from rts where ipx = '0.0.0.0'","/tmp/Tfsql");
if (get_file_size("/tmp/Tfsql")>1)
{
system("cp /tmp/Tfsql /tmp/hhhh");
hfx=GetIntByFile("/tmp/hhhh");
}
if (hfx==0)
{
sqlite3_exec("/usr/tmp/rtx.db","select min(prefx) from rts where ipx<>'0.0.0.0'","/tmp/Tfsql");
if (get_file_size("/tmp/Tfsql")>1)
{
system("cp /tmp/Tfsql /tmp/hhhh");
hfx=GetIntByFile("/tmp/hhhh");
}
if (hfx==0)
{
INorUP=0;
hfx=25000;
}
else
{
hfx=hfx-1;
memset(buff2,0,siz
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
setr.rar (1个子文件)
setr.c 11KB
共 1 条
- 1
资源评论
朱moyimi
- 粉丝: 61
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功