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

📄 packet_ops.c

📁 Adhoc无线网络路由协议源码
💻 C
字号:
/* this file contains various operations that can be performed on * the data struct storing the outstanding packets.** The data struct is a hash table.* Each item is a pointer to a queue for packets for a dest.* the dest is also they key for the hash table.*/#include "common.h"#include "packet_ops.h"#include "raw_sock.h"#include "api.h"#include "utils.h"#include "queue.h"/* data structure to store all the packets */htab *packet_table ;extern int fraw;/* initializes the packet_table */intpacket_table_init(){	packet_table = hashcreate(8) ;	return 0;}/* add a packet to the packet_table * return 0 on success */intpacket_add(unsigned char *buf, unsigned int len){	unsigned char *key ;	int keyl ;	struct queue *packetQ=NULL;	struct packet *packet=NULL;		/* fill in the packet */	packet = (struct packet *) malloc(sizeof(struct packet));	packet->buf=buf;	packet->len = len ;		key = get_dest_ip(buf);	//	keyl = strlen((unsigned char *)key);	keyl = 4;		/* check if this key already exists in the packet table */	if (hfind(packet_table, key, keyl))	{		/* read the current item (i.e the one found) in the packet_tab */		packetQ = (struct queue *)hstuff(packet_table);#ifdef DEBUG		fprintf(stdout,"packet_add() : hfind successfull, dest exists in hash\t");#endif		/* insert the packet in the queue */		if(qinsert(packetQ, packet)==0)		{			perror("Could not insert packet in the queue");			return -1;		}	}   	else /* otherwise add it */	{		/* initialize the queue */		packetQ=qinit(packetQ);		if(packetQ == NULL)		{			perror("packet_add() : qinit() failed");			return -1 ;		}				/* insert the packet in the queue */		if(qinsert(packetQ, packet)==0)		{#ifdef DEBUG			fprintf(stderr,"packet_add() : qinsert() : Could not insert packet in the queue\n");#endif			return -1;		}				/* add the queue to the hash table */		if (hadd(packet_table, key, keyl, (void *)packetQ)!=TRUE)		{			perror("Hash table adding problems");			return -1;		}#ifdef DEBUG		fprintf(stdout,"packet_add() : new dest, done hadd()\n");#endif	}	return 0;}/* sends out all the packets for a given destination* on the raw socket.* and frees the corresponding memory.** returns 0 if succesfull. */intclear_packets(addr_t dest_ip, int result){	int keyl;	unsigned char *key=NULL;	struct queue *packetQ ;	key =(unsigned char *)&dest_ip;//	keyl = strlen((unsigned char *)key);	keyl = 4 ;	#ifdef DEBUG	fprintf(stdout,"clear_packets() : key is %s, keyl is %d\n", key, keyl);#endif		/* find the entry in the packet_table */	if(hfind(packet_table, key, keyl))	{	#ifdef DEBUG		fprintf(stdout,"clear_packets(): packet_found, dest exists in hash\n");#endif		/* read the current item (i.e the one found) in the packet_tab */		packetQ = (struct queue *)hstuff(packet_table);		/* send all the packets in the queue and then free the queue */		send_all(packetQ, result);		/* free the current key, ie dest_ip */		free(hkey(packet_table));		/* free the current item */        free(hstuff(packet_table));		/* delete the current item */        hdel(packet_table);		return 0 ;	}	else	{		return -1 ;	}}/* Given a queue, send out all the queued packets on the raw socket* if result == ASL_NO_ROUTE then just delete those packets */intsend_all(struct queue *q, int  result){	int r;	struct packet *packet ;		while(qremove(q, &packet)!=NULL)	{#ifdef DEBUG		fprintf(stdout,"send_all() : got a packet from packetQ.\n");#endif		if(result == ASL_ROUTE_FOUND)		{			r = rawsock_out(fraw, packet->buf, packet->len, 0);#ifdef DEBUG		fprintf(stdout,"send_all() : wrote %d bytes on rawsock \n", r);#endif			if ( r < 0)				return -1;		} else if(result==ASL_NO_ROUTE)		{			/* if the result returned by route_discovery_done was ASL_NO_ROUTE			* then just free the memory used for storing those packets */			free(packet);			// FIXME : is free(packet->buf) needed ??		}	}	return 0;}	/* this function destroys the packet_table and frees all memory associated with it */intdestroy_packet_table(void){	unsigned char *key ;	struct queue *packetQ ;	struct packet *packet=NULL;		/* traverse through every item in the hash table	* and empty them */	if (hfirst(packet_table)) do      {        key   = hkey(packet_table);        packetQ = (struct queue *) hstuff(packet_table);			/* delete items from the queue */			while(qremove(packetQ, &packet))					free(packet);					// FIXME : is free(packet->buf) needed ??      }      while (hnext(packet_table));	/* destroy the table itself */	hashdestroy(packet_table);		return 0;}/* copy the dest_ip address from an IP packet to an array and return it */unsigned char *get_dest_ip(unsigned char *buf){	unsigned char *dest_ip;	dest_ip = (unsigned char *)malloc(4*sizeof(unsigned char));	memcpy(dest_ip, buf+16, 4);	/* buf+16: dest ip */	return dest_ip;}	/* copy the src_ip address from an IP packet to an array and return it */unsigned char *get_src_ip(unsigned char *buf){	unsigned char *src_ip;	src_ip = (unsigned char *)malloc(4*sizeof(unsigned char));	memcpy(src_ip, buf+12, 4);	/* buf+12: src ip */	return src_ip;}/* return the protocol field from the IP packet */u_int8_tget_protocol(unsigned char *buf){	return (u_int8_t) buf[9]; // 9th byte in the IP header is the protocol}/* returns 1 if route disocvery is pending for that destination.* If an entry exists in the hash table, we conclude that route disocvery*  is pending */introute_discovery_pending(unsigned char *dest){	int r;	/* find the entry in the packet_table.	* If entry exists than route_disovery is pending */	//FIXME the constant 4 is not a good idea	r = hfind(packet_table, dest, 4);#ifdef DEBUG 	fprintf(stdout,"route_discovery_pending() : hfind() returns %d\n",r);#endif		return r;	//return hfind(packet_table, dest, strlen(dest));}		

⌨️ 快捷键说明

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