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

📄 api.c

📁 Adhoc无线网络路由协议源码
💻 C
字号:
#include "api.h"#include "common.h"#include "raw_sock.h"#include "tun_dev.h"#include "packet_ops.h"#include "utils.h"#include "krt.h"/* raw socket descriptor to out put packets */int fraw ;extern int krt ;int route_add(addr_t d, addr_t nh, char *dev){	int r=0;	char *dev_temp=NULL;	#ifdef DEBUG		fprintf(stdout, "route_add() : dev is %s \n", dev);#endif			if(nh==0)	{		dev_temp = (char *) malloc(4*sizeof(char));		/* nh =0 implies defferred route, so change the device to tun */		memset(dev_temp,0,4);		sprintf(dev_temp,"%s",TUN_DEV_NAME);		// FIXME : This is here becuase you cannot add a route for		// a host like 10.0.0.23 with 0 next hop.		nh=d ;		r=add_kroute(d,nh,dev_temp);		if( r >= 0)		{#ifdef DEBUG			fprintf(stdout, "added defferred route for %s \n", dot_ip(d));			system("/sbin/route -n");#endif		}	} else	{		r = add_kroute(d,nh,dev);		if( r >= 0)		{#ifdef DEBUG			fprintf(stdout, "added normal route for %s \n", dot_ip(d));			system("/sbin/route -n");#endif		}	}			return r ;}introute_del(addr_t d){	int r;	r = del_kroute(d,0,0);	if( r >= 0)	{#ifdef DEBUG		fprintf(stdout, "deleted defferred route for %s \n", dot_ip(d));		system("/sbin/route -n");#endif	}	return r ;}intopen_route_request (){	/* tun file descriptor, to read in packets */	int ftun ;	//FIXME : put a check to make sure that open_route_request 	//FIXME : is not called twice from within the same program#ifdef DEBUG	fprintf(stdout,"\n\nIn open_route_request\n");#endif		/* Open tun device and return the descriptor */	if ( (ftun = tun_init()) < 0) {		perror("open_route_request(): tun_init()");		return -1 ;	}	#ifdef DEBUG	fprintf(stdout,"open_route_request() : tun_init() done.\n");#endif		/* Open raw socket and return the descriptor */	if ( (fraw = rawsock_init()) < 0) {		perror("open_route_request(): rawsock_init()");		return -1 ;	}	#ifdef DEBUG	fprintf(stdout,"open_route_request() : rawsock_init() done.\n");#endif 	/* for the krt socket : to add or delete routes to the kernel */	if ((krt = init_rtsocket()) < 0)	{			perror("open_route_request() : Initializing krt socket");    	return -1; /* Unable to create socket */	}#ifdef DEBUG	fprintf(stdout,"open_route_request() : init_rtsocket() done; krt ready.\n");#endif		/* initialize the data struct to store the packets */	packet_table_init();#ifdef DEBUG	fprintf(stdout,"open_route_request() : packet_table_init() done.\n");#endif#ifdef DEBUG	fprintf(stdout,"open_route_request() : inserting route_check module in the kernel\n");	fprintf(stderr,"If insmod returned error, check that  route_check is properly installed\n insmod ::");#endif	//FIXME : find better way to insmod	system("/sbin/insmod route_check");	#ifdef DEBUG	fprintf(stdout,"open_route_request() : done.\n\n");#endif		return ftun ;}/* This function should be called by the routing daemon before it exits.* It takes care of doing all the clean up job.* It returns 0 on success,* -1 if something unexpected occurs */intclose_route_request(int fd){		/* Close the tun device */	if(close(fd) < 0 )		perror("close_route_request() : tun device ");#ifdef DEBUG	fprintf(stdout,"\nclose_route_request() : tun device closed\n");#endif	/* Close the raw socket */	if(close(fraw) < 0 )		perror("close_route_request() : raw socket ");#ifdef DEBUG	fprintf(stdout,"close_route_request() : raw socket closed\n");#endif	/* destroy packet_table */	if(destroy_packet_table() < 0)		perror("close_route_request() : destroy_packet_table()");#ifdef DEBUG	fprintf(stdout,"close_route_request() : packet_table destroyed\n");#endif		/* remove the route_check module */	// FIXME : find a way to replace the "system" call	system("/sbin/rmmod route_check");#ifdef DEBUG	fprintf(stdout,"close_route_request() : route_check module removed from the  kernel\n");#endif	#ifdef DEBUG	fprintf(stdout,"close_route_request() : Route request shutdown complete. \n\n");#endif		return 0;}/* blocks until a route request is read from fd* Fills in the route_info structure.* memory should be allocated beforehand by the user ;* return number of bytes read ;* a negative number if there is an error.* 0 if route_discovery is already in progress. */intread_route_request(int fd, struct route_info *r_info){	unsigned char buf[BUFSIZE];	unsigned char *rbuf;	unsigned char *dest, *src;	addr_t dest_ip=0, src_ip=0 ;	int rlen=0;	int retval=0;	/* read from  the tun device	* Note this function is called only when something is available here*/	#ifdef DEBUG		fprintf(stdout, "\n read_route_request() :blocked reading from tun device\n");#endif		rlen = tun_in(fd, buf); 				if(rlen < 0){			perror("read_route_request() : Error reading from tun");			exit(1);		}	#ifdef DEBUG		fprintf(stdout, "read_route_request() :read %d bytes from tun device.\n", rlen);#endif			/* We should store only the number of bytes that haven been read		* and not the whole buf */			//FIXME do not forget to check if this works okay */		rbuf = (unsigned char *) malloc(rlen*sizeof(unsigned char));				memcpy(rbuf, buf, rlen);								dest = get_dest_ip(rbuf) ;		src =  get_src_ip(rbuf);				memcpy(&dest_ip, dest, 4);		memcpy(&src_ip, src, 4);		#ifdef DEBUG	fprintf(stdout,"read_route_request() dest is %lu, %s \n",(unsigned long)dest_ip, dot_ip(dest_ip));	fprintf(stdout,"read_route_request() src is %lu, %s \n",(unsigned long)src_ip, dot_ip(src_ip));#endif					/* write the dest_ip on fuser only if there a route_discovery		* for that dest is not already on progress. */ 		if(!route_discovery_pending(dest))		{#ifdef DEBUG			fprintf(stdout,"route discovery is not pending\n");#endif			r_info->dest_ip = (unsigned long) dest_ip ;			r_info->src_ip = (unsigned long) src_ip;			r_info->protocol = get_protocol(rbuf);			retval =1;		} else		{#ifdef DEBUG			fprintf(stdout,"route discovery is pending\n");#endif			retval=0;		}						/* insert rbuf in the packet table */		if( packet_add(rbuf, rlen) < 0)		{			perror("Error adding to packet_table");			return -1;		}						#ifdef DEBUG		fprintf(stdout, "read_route_request() : added packet to packet_table.\n\n");#endif		return retval ;}	/* the routing daemon informs the ODRM that route discovery is complete* and the kernel route table has been populated * result argument has two values :*  ASL_NO_ROUTE  if routing daemon could not find a route,*  ASL_ROUTE_FOUND  if a route was found for addr_t.*  or it wants the packet to be dropped. *  returns -1 if there is an error. 0 if no problem */introute_discovery_done(addr_t dest, int result){	if((result != ASL_ROUTE_FOUND)&&(result!=ASL_NO_ROUTE))	{		fprintf(stderr,"route_discovery_done() : arg 2 is %d\n",result);		perror("route_discovery_done() : argument 2 invalid\n");		return -1 ;	}				if( clear_packets(dest, result) < 0)		return -1;	return 0;}/* returns the idle time of the route in msec.* vFlag argument has following meaning associated with it's values:* vFlag = 1, retreive idle time since a packet was forwarded last to dest_ip* vFlag = 0, retreive idle time since a packet was received last from dest_ip* returns -1 on error or if there is no entry in /proc for that dest */intquery_route_idle_time(addr_t dest, int recv_flag){	FILE *fp;	char sip[20];	char title[80];	int	 vFlag;	u_int32_t ip ;	// FIXME : I do not know how to scang u_int64_t 	//u_int64_t last_use_time ;	unsigned long last_use_time=0;	int		idle_time = -1;	struct in_addr inp;		fp=fopen("/proc/asl/route_check","r");	if(fp==NULL)	{		fprintf(stderr, "Probably the route_check module has not been insmod\n");		perror("Opening the proc file");		return -1 ;	}			/* ignore the first line */	fgets(title, 120, fp); 		while(fscanf(fp, "%s %lu %d\n", sip, &last_use_time, &vFlag) != EOF)	{#ifdef DEBUG		fprintf(stderr, "query_route_idle_time() : %s %lu %d\n", sip, last_use_time, vFlag);#endif		if( inet_aton(sip, &inp) == 0)		{			perror("inet_aton: Invalid address");			return -1;		}				ip=inp.s_addr ;				if((ip==dest) && (recv_flag == vFlag))		{			idle_time = getcurrtime() - last_use_time;		}	}		fclose(fp);	return idle_time ;		}			

⌨️ 快捷键说明

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