📄 api.c.safe
字号:
#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){ char *dev=NULL; int r; dev = (char *) malloc(4*sizeof(char)); if(nh==0) { /* nh =0 implies defferred route, so change the device to tun */ //FIXME size of dev = 4 is arbitrary sprintf(dev,"%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 ; } else { sprintf(dev,"%s","eth1"); } r = add_kroute(d,nh,dev); if( r >= 0) {#ifdef DEBUG fprintf(stdout, "added defferred 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 (void){ /* 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,"In 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");#endif //FIXME : find better way to insmod system("/sbin/insmod route_check"); 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,"close_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");#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, "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 %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, "added packet to packet_table.\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.* returns -1 on error or if there is no entry in /proc for that dest */intquery_route_idle_time(addr_t dest){ FILE *fp; char *sip=NULL; char title[80]; 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 ; struct in_addr inp; fp=fopen("/proc/asd/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 */ fscanf(fp,"%s \n", title); while(fscanf(fp, "%s %lu \n", sip, &last_use_time) != EOF) {#ifdef DEBUG fprintf(stderr, "%s %lu \n", sip, last_use_time);#endif if( inet_aton(sip, &inp) == 0) { perror("inet_aton: Invalid address"); return -1; } ip=inp.s_addr ; if(ip==dest) return( getcurrtime() - last_use_time ); } /* entry does not exist in /proc */ return -1 ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -