⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apconn.c

📁 本人在做一快无线网络通信的代码
💻 C
字号:
/*APConn.c*/#include <string.h>#include <netinet/in.h>#include <netdb.h>#include <sys/types.h>#include <sys/socket.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <sys/wait.h>#include <unistd.h>#include <signal.h>#include <sys/stat.h>#include <sys/param.h>#include <sys/ioctl.h>#include <net/if.h>#include <net/if_arp.h>#define MYPORT 8700#define HERPORT 5500#define HEPORT 8000#define BUF_LEN 100#define STACONN 1#define RECONN 2#define APCONN 3#define MAXINTERFACES 16typedef struct{	int code;	struct in_addr ipofsta_eth1;	struct in_addr netofsta_eth0;	struct in_addr ipofap;}ConnMsg;typedef struct{	int code;	struct in_addr ipofsta_eth1;	struct in_addr ipofsta_eth0;}BCMsg;void init_daemon(void){	int pid;	int i;	if(pid=fork())		exit(0);	//是父进程,结束父进程	else if(pid< 0)		exit(1);	//fork失败,退出	//是第一子进程,后台继续执行	setsid();		//第一子进程成为新的会话组长和进程组长	//并与控制终端分离	if(pid=fork())		exit(0);	//是第一子进程,结束第一子进程	else if(pid< 0)		exit(1);	//fork失败,退出	//是第二子进程,继续	//第二子进程不再是会话组长	for(i=0;i< NOFILE;++i)//关闭打开的文件描述符	close(i);	chdir("/tmp/yaffs");	//改变工作目录到/tmp	umask(0);		//重设文件创建掩模	return;} char *getgw();int getroute(char *argv[]){	FILE *PROC;	int i;	char line[512];	int ifnum = 0;	char destip[16], gwip[16];		strcpy(destip, argv[0]);	strcpy(gwip, argv[1]);	PROC = fopen("/proc/net/route","r");	fgets(line,sizeof(line),PROC); 	while (!feof(PROC)) 	{		char iface[8];		unsigned int dest, gateway, mask;		int i,field = 0;		char *fields[10];		char *byteoffset;				fgets(line,sizeof(line),PROC);		for (i=0;i<sizeof(line);i++) 		{						fields[field++] = line + i;			if ((byteoffset = strchr(line + i,'\t')) == NULL)					i = sizeof(line);// No more fields to parse 			else if (field > 9)				// Don't need any more fields 				// Why 9 and not 8?  Well, because during the 9th loop, we figure the end of the 8th field 			i = sizeof(line);			else{				*byteoffset = '\0';				i = byteoffset - line;			}		}				//sscanf(fields[0],"%s",iface);		sscanf(fields[1],"%x",&dest);		sscanf(fields[2],"%x",&gateway);		//sscanf(fields[7],"%x",&mask);		if(!strcmp((char*)inet_ntoa(*((struct in_addr *)&dest)),destip))  		{				if(!strcmp((char *)inet_ntoa(*((struct in_addr *)&gateway)),gwip))			{				fclose(PROC);				return 0; //the same host router has been built			}			if(strcmp((char *)inet_ntoa(*((struct in_addr *)&gateway)),gwip))			{				fclose(PROC);				return 1; //have different host router			}		}			}	fclose(PROC);	return -1; //no host router now}struct in_addr getip(char argv[4]){	int fd = 0, intrface, retn=0;	struct ifreq buf[MAXINTERFACES];	struct arpreq arp;	struct ifconf ifc;	int ifnum;	char * StaIP[2];	struct in_addr tmpaddr;	int flag = 0;	bzero(&tmpaddr, sizeof(tmpaddr));	while(flag == 0){	   if((fd = socket(AF_INET, SOCK_DGRAM, 0)) >=0)	   {		ifc.ifc_len = sizeof(buf);		ifc.ifc_buf = (caddr_t)buf;		if(!ioctl(fd, SIOCGIFCONF, (char *)&ifc))		{			intrface = ifc.ifc_len/sizeof(struct ifreq);			ifnum = (int)intrface - 1;						while (intrface --> 0)			{								if(!(ioctl(fd, SIOCGIFADDR, (char*)&buf[intrface])))				{					if(!strcmp(buf[intrface].ifr_name, argv))					{						bzero(&tmpaddr, sizeof(tmpaddr));						tmpaddr = ((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr;						//puts("eth1:");puts((char *)inet_ntoa(tmpaddr)); 											}				}			}		}	   }	   close(fd);	   if(strcmp((char *)inet_ntoa(tmpaddr), "0.0.0.0"))	  	flag = 1;	            	   	}	//puts((char*)inet_ntoa(tmpaddr));puts("\n");		 return tmpaddr;}unsigned long setStaRoute(BCMsg tmpMsg){	struct in_addr inaddr1,inaddr2;	char *net_sta, *ip_sta;	unsigned long netmask,sta_net;	char command[1024];	char * route[2];	char ipofsta[16], netofsta[16];	int flag = 0;	netmask=0;	sta_net=0;	memset(command,0,1024);	memset(&inaddr1,0,sizeof(inaddr1));	memset(&inaddr2,0,sizeof(inaddr2));		netmask=0<<24|255<<16|255<<8|255;	sta_net=tmpMsg.ipofsta_eth0.s_addr& netmask;		inaddr1.s_addr= sta_net;	inaddr2=tmpMsg.ipofsta_eth1;		strcpy(ipofsta, (char*)inet_ntoa(inaddr1));	strcpy(netofsta, (char*)inet_ntoa(inaddr2));	route[0] = ipofsta;	route[1] = netofsta;	//puts(route[0]);puts(route[1]);	flag = getroute(route);	if(flag == 1 || flag == -1)	{		bzero(&command, sizeof(command));		sprintf(command,"route add -net %s netmask 255.255.255.0 gw %s dev eth1", route[0], route[1]);		system(command);		puts(command);	}	return sta_net;	}int main(int argc,char*argv[]){	int sockfd,sock;	struct sockaddr_in saddr;		/*my address info*/	struct sockaddr_in faddr;		/*connector's addr info*/	struct sockaddr_in haddr;		/*发给代理时使用*/	struct sockaddr_in gaddr;		/*返回给STA时使用*/	int addr_len,numbytes;	char buf[BUF_LEN];	char syscmd[100];	int fbroadcast=1;	int conn = STACONN;	int aconn = APCONN;	char *GetwayAddr;	BCMsg SMsg;	ConnMsg connMsg;	unsigned long netofsta;	FILE *fp;	struct in_addr ip_eth1;	//init_daemon();//初始化为Daemon	ip_eth1 = getip("eth1");	sprintf(syscmd, "ifconfig tunl0 %s netmask 255.255.255.255 pointopoint 192.168.3.4", (char*)inet_ntoa(ip_eth1));	system(syscmd);	//puts(syscmd);	syscmd = NULL;	sprintf(syscmd, "route del -host 192.168.3.4");	system(syscmd);	GetwayAddr = "0";	//bzero(&saddr,sizeof(struct sockaddr_in));	saddr.sin_family = AF_INET;	saddr.sin_port = htons(MYPORT);	/*short,network byte order*/		saddr.sin_addr.s_addr = htonl(INADDR_ANY);	/*auto fill with my ip*/		//bzero(&gaddr,sizeof(struct sockaddr_in));	gaddr.sin_family = AF_INET;	gaddr.sin_port = htons(HERPORT);	/*short,network byte order*/		bzero(&haddr,sizeof(struct sockaddr_in));	haddr.sin_family = AF_INET;	haddr.sin_port = htons(HEPORT);	if ((sock = socket(AF_INET,SOCK_DGRAM,0))==-1){		fprintf(fp, "socket call error");		exit(1);	}	if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) ==-1){		fprintf(fp, "socket call error");		exit(1);	}	//setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&fbroadcast,sizeof(int));	//	printf("IP=%s,Port=%d\n",inet_ntoa(saddr.sin_addr),ntohs(saddr.sin_port));		if (bind(sockfd,(struct sockaddr*)&saddr, sizeof(saddr)) == -1){				fprintf(fp, "bind");		exit(1);	}	addr_len = sizeof(struct sockaddr_in);	while(1)	{		if((fp=fopen("apconn.log","a")) == NULL) continue;		if ((numbytes=recvfrom(sockfd,buf,BUF_LEN,0,(struct sockaddr *)&faddr,&addr_len)) == -1){			fprintf(fp,"recvfrom");			continue;		}		printf("got packet from %s\npacket is %d bytes long\n",inet_ntoa(faddr.sin_addr),numbytes);		memcpy(&SMsg.code,buf,sizeof(int));//get code from buf		if(SMsg.code != conn) continue;//not STA's info,go on recv		memcpy(&SMsg, buf, sizeof(BCMsg));		//memcpy(&SMsg.ipofsta_eth1,buf+sizeof(SMsg.code),sizeof(SMsg.ipofsta_eth1)); 		//puts((char*)inet_ntoa(SMsg.ipofsta_eth1));//get ip from buf		//memcpy(&SMsg.ipofsta_eth0, buf+ sizeof(SMsg.ipofsta_eth1), sizeof(SMsg.ipofsta_eth0));		//puts((char*)inet_ntoa(SMsg.ipofsta_eth0));		netofsta = setStaRoute(SMsg);//puts(netofsta);				//fprintf(fp,"%s\n", inet_ntoa(SMsg.ipOfSTA));		gaddr.sin_addr.s_addr = faddr.sin_addr.s_addr;	//设置发送的目标地址					if(( numbytes = sendto(sockfd,0,0,0,(struct sockaddr *)&gaddr,addr_len))==-1){			fprintf(fp,"AP sendto STA");			continue;		}		printf("send packet to %s\n",inet_ntoa(gaddr.sin_addr));		//fprintf(fp,"send packet to %s\n",inet_ntoa(gaddr.sin_addr));		//AP send info to STA 		bzero(buf,sizeof(buf));			//buf set zero		connMsg.code=APCONN;	//puts("point2");printf("%d\n",connMsg.code);		connMsg.ipofsta_eth1 = SMsg.ipofsta_eth1;	//puts("point3");puts((char*)inet_ntoa(connMsg.ipofsta_eth1));		//set code ,is AP to STA info		connMsg.netofsta_eth0.s_addr= netofsta;//puts((char*)inet_ntoa(connMsg.netofsta_eth0));	//puts("point4");puts((char*)inet_ntoa(connMsg.netofsta_eth0));		connMsg.ipofap = getip("eth0");	//puts("point5");puts((char*)inet_ntoa(connMsg.ipofap));		memcpy(buf,&connMsg,sizeof(ConnMsg));				//set buf			GetwayAddr = getgw();		//取得代理的目的地址,即本身的网关		haddr.sin_addr.s_addr = inet_addr(GetwayAddr);//puts("point8");		if(( numbytes = sendto(sock,buf,100,0,(struct sockaddr *)&haddr,addr_len))==-1){			fprintf(fp,"AP sendto Agent");			continue;		}		printf("send packet to %s\n",inet_ntoa(haddr.sin_addr));	//ap to agent with info code '2',ip of STA		//fprintf(fp,"send packet to %s\n",inet_ntoa(haddr.sin_addr));		fclose(fp);		usleep(200000);	}	close(sockfd);	close(sock);}char *getgw(){FILE *PROC;char  *def;char line[512];def = (char*)malloc(15);PROC = fopen("/proc/net/route","r"); 	//puts("point62");//fgets(line,sizeof(line),PROC);while (!feof(PROC)) {  	char iface[8];	unsigned int dest, gateway, mask;	int i,field = 0;	char *fields[10];		fgets(line,sizeof(line),PROC);	for (i=0;i<sizeof(line);i++) {		char *byteoffset;		fields[field++] = line + i;		if ((byteoffset = strchr(line + i,'\t')) == NULL)			// No more fields to parse 			i = sizeof(line);		else if (field > 9)			// Don't need any more fields 			// Why 9 and not 8?  Well, because during the 9th loop, we figure the end of the 8th field 			i = sizeof(line);			else {				*byteoffset = '\0';				i = byteoffset - line;				}		}							sscanf(fields[2],"%x",&gateway);			if(gateway!=0x00000000){		  	memcpy(def,(char*)inet_ntoa(*((struct in_addr *)&gateway)),15);				}	}	fclose(PROC);if (def==NULL) 	return 0;else	return(def);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -