📄 address_areq.c
字号:
#include "address_areq.h"#include "address_arep.h"#include "address_conf.h"#include "defs.h"#include "ipv6_utils.h"#include "aodv_timeout.h"#include "timer_queue.h"#include "aodv_socket.h"#include "debug.h"#include "params.h"//extern struct in6_addr gateway_mcast_addr;struct areq_record *areq_record_head;struct areq_blacklist *areq_blacklist_head;AREQ *areq_create(struct in6_addr req_addr){ AREQ *areq; areq = (AREQ *) aodv_socket_new_msg(); areq->type = AODV_AREQ; areq->code=0; areq->checksum=0;/*to be coded!*/ areq->hcnt = 0; areq->res1=0; areq->res2=0; copy_in6_addr(&areq->request_addr,&req_addr); /* Immediately before a node originates a AREQ flood it must increment its sequence number... */ areq->seqno = htonl(areq_info.seqno); areq->id=htonl(areq_info.id); DEBUG(LOG_DEBUG, 0, "areq_create: Assembled AREQ %s", ip6_to_str(req_addr)); #ifdef DEBUG_OUTPUT log_pkt_fields((AODV_msg *) areq); #endif return areq;}void areq_send(struct in6_addr req_addr,int ttl){ AREQ *areq; int i; /* Broadcast on all interfaces */ for (i = 0; i < MAX_NR_INTERFACES; i++) { if (!DEV_NR(i).enabled) continue; areq = areq_create(req_addr); printf("areq_send!!! i=%d ifname:%s\n",i,DEV_NR(i).ifname); aodv_socket_send((AODV_msg *) areq,ipv6_multicast_addr, AREQ_SIZE, ttl,&DEV_NR(i)); areq_record_insert(areq->request_addr, ntohl(areq->seqno)); }}void areq_forward(AREQ *areq, int ttl){ int i; printf("areq forward!!!\n"); /* FORWARD the RREQ if the TTL allows it. */ DEBUG(LOG_INFO, 0, "forwarding AREQ requst_ip=%s, areq_id=%lu", ip6_to_str(areq->req_addr), ntohl(areq->id)); /* Queue the received message in the send buffer */ areq = (AREQ *) aodv_socket_queue_msg((AODV_msg *) areq, AREQ_SIZE); areq->hcnt++; /* Send out on all interfaces */ for (i = 0; i < MAX_NR_INTERFACES; i++) { if (!DEV_NR(i).enabled) continue; aodv_socket_send((AODV_msg *) areq,ipv6_multicast_addr, AREQ_SIZE, ttl,&DEV_NR(i)); }}void areq_process(AREQ *areq, int areqlen, struct in6_addr ip_src,struct in6_addr ip_dst, int ip_ttl, unsigned int ifindex){ rt_table_t *rev_rt; struct in6_addr req_addr; u_int32_t areq_seqno,areq_id,life; int ttl; areq_id = ntohl(areq->id); copy_in6_addr(&req_addr, &areq->request_addr); areq_seqno = ntohl(areq->seqno); ttl=++(areq->hcnt); /* Ignore AREQ's that originated from this node. Either we do this or we buffer our own sent AREQ's as we do with others we receive. */ //if (memcmp(&ip_src, &(DEV_IFINDEX(ifindex).ipaddr), sizeof(struct in6_addr)) ==0) if(to_my_address(ip_src)){ printf("This areq is generated by myself,don't forward again!\n"); return; } if (areqlen < (int) AREQ_SIZE) { log(LOG_WARNING, 0, "areq_process: IP data field too short (%u bytes) from %s to %s,request %s", areqlen, ip6_to_str(ip_src), ip6_to_str(ip_dst),ip6_to_str(req_addr)); return; } DEBUG(LOG_DEBUG, 0,"AREQ_process: ip_src=%s request_dest=%s", ip6_to_str(ip_src),ip6_to_str(req_addr)); /* Ignore already processed AREQs. */ if (areq_record_find(req_addr, areq_seqno)) return; /* Now buffer this AREQ so that we don't process a similar AREQ we get within PATH_TRAVERSAL_TIME. */ areq_record_insert(req_addr, areq_seqno); #ifdef DEBUG_OUTPUT log_pkt_fields((AODV_msg *) areq); #endif //if(memcmp(&req_addr,&(DEV_IFINDEX(ifindex).ipaddr),sizeof(struct in6_addr))==0){ if(to_my_address(req_addr)){ arep_send(req_addr,areq_seqno,areq_id,ip_src,ttl); return; } /* The intermediate node always creates or updates a REVERSE ROUTE entry to the source of the AREQ. */ rev_rt = rt_table_find(req_addr); life =TEMP_ROUTE_LIFETIME; if (rev_rt == NULL) { DEBUG(LOG_DEBUG, 0, "areq_process: rev_rt = NULL; route not found,insert temporary one"); rev_rt = rt_table_insert(req_addr, ip_src, areq->hcnt + 1, areq_seqno, life, TEMP, ifindex); areq_blacklist_insert(req_addr,areq_id); } else { if((rev_rt->flags==TEMP)&&areq_blacklist_find(req_addr,areq_seqno)) /* OK, interested entry found. But we update only if either: (i) the Sequence Number in the AREQ is higher than the destination sequence number of the Request IP Address in the route table, or (ii) the sequence numbers are equal, but the hop count as specified by the AREQ, plus one, is now smaller than the existing hop count in the routing table. */ if (((areq_seqno > rev_rt->dest_seqno) || (areq_seqno == rev_rt->dest_seqno &&(areq->hcnt + 1) < rev_rt->hcnt))) rev_rt = rt_table_update(rev_rt,ip_src,areq->hcnt+1,areq_seqno, life, TEMP); else return; else if((rev_rt->flags==TEMP)||(rev_rt->hcnt!=INFTY)){ arep_send(req_addr,areq_seqno,areq_id,ip_src,ttl); return; } } /* FORWARD the AREQ if the TTL allows it. */ if (ip_ttl > 1) { DEBUG(LOG_INFO, 0, "areq_process: forwarding AREQ rreq_addr=%s, areq_id=%lu", ip6_to_str(req_addr), areq_id); ip_ttl--; areq_forward(areq,ip_ttl); }}/*duplicated address detect*/void dup_addr_detect(){ int ttl; printf("duplicated address detect %d\n",4-areq_info.retries); ttl=NET_DIAMETER; areq_info.areq_timer.handler=arep_wait_timeout; areq_info.areq_timer.data=NULL; timer_add_msec(&areq_info.areq_timer,ADDRESS_DISCOVERY_TIME); areq_send(areq_info.request_addr,ttl);} struct areq_record *areq_record_insert(struct in6_addr req_addr,u_int32_t areq_seqno){ struct areq_record *areq_pkt; /* First check if this areq packet is already buffered */ areq_pkt = areq_record_find(req_addr,areq_seqno); if (areq_pkt) return areq_pkt; if ((areq_pkt =(struct areq_record *) malloc(sizeof(struct areq_record))) < 0) { fprintf(stderr, "areq_record_insert: Malloc failed!!!\n"); exit(-1); } memcpy(&areq_pkt->req_addr, &req_addr, sizeof(struct in6_addr)); areq_pkt->seqno = areq_seqno; areq_pkt->rec_timer.handler = areq_record_timeout; areq_pkt->rec_timer.data = areq_pkt; areq_pkt->next = areq_record_head; areq_record_head = areq_pkt; DEBUG(LOG_INFO, 0, "Buffering AREQ %s areq_seqno=%lu time=%u", ip6_to_str(req_addr), areq_seqno, PATH_TRAVERSAL_TIME); timer_add_msec(&areq_pkt->rec_timer, PATH_TRAVERSAL_TIME); return areq_pkt;}struct areq_record *areq_record_find(struct in6_addr req_addr,u_int32_t areq_seqno){ struct areq_record *areq_pkt; areq_pkt = areq_record_head; while (areq_pkt) { if ( (memcmp(&areq_pkt->req_addr, &req_addr, sizeof(struct in6_addr)) == 0) && (areq_pkt->seqno == areq_seqno)) return areq_pkt; areq_pkt = areq_pkt->next; } return NULL;}int areq_record_remove(struct in6_addr req_addr, u_int32_t areq_seqno){ struct areq_record *curr, *prev; prev = NULL; curr = areq_record_head; while (curr){ if ( (memcmp(&curr->req_addr, &req_addr, sizeof(struct in6_addr)) == 0) && (curr->seqno == areq_seqno)){ if (prev == NULL) areq_record_head = curr->next; else prev->next = curr->next; free(curr); return 0; } prev = curr; curr = curr->next; } return -1;}struct areq_blacklist *areq_blacklist_insert(struct in6_addr req_addr,u_int32_t areq_id){ struct areq_blacklist *bl_entry; /* First check if this areq packet is already buffered */ bl_entry = areq_blacklist_find(req_addr,areq_id); if (bl_entry) return bl_entry; if ((bl_entry = (struct areq_blacklist *) malloc(sizeof(struct areq_blacklist))) < 0) { fprintf(stderr, "rreq_blacklist_insert: Malloc failed!!!\n"); exit(-1); } memcpy(&bl_entry->req_addr, &req_addr, sizeof(struct in6_addr)); bl_entry->id=areq_id; bl_entry->bl_timer.handler = areq_blacklist_timeout; bl_entry->bl_timer.data = bl_entry; bl_entry->next = areq_blacklist_head; areq_blacklist_head = bl_entry; timer_add_msec(&bl_entry->bl_timer, AREQ_BLACKLIST_TIMEOUT); return bl_entry;}struct areq_blacklist *areq_blacklist_find(struct in6_addr req_addr,u_int32_t areq_id){ struct areq_blacklist *bl_entry; bl_entry = areq_blacklist_head; while (bl_entry){ if (memcmp(&bl_entry->req_addr, &req_addr, sizeof(struct in6_addr)) == 0 &&(bl_entry->id==areq_id)) return bl_entry; bl_entry = bl_entry->next; } return NULL;}int areq_blacklist_remove(struct in6_addr req_addr,u_int32_t areq_id){ struct areq_blacklist *curr, *prev; prev = NULL; curr = areq_blacklist_head; while (curr){ if ((memcmp(&curr->req_addr, &req_addr, sizeof(struct in6_addr)) == 0) &&(curr->id=areq_id)){ if (prev == NULL) areq_blacklist_head = curr->next; else prev->next = curr->next; free(curr); return 0; } prev = curr; curr = curr->next; } return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -