📄 krt.c
字号:
/* * function to communicate and make changes to tke kernel IP routing table * */#include"krt.h"/* Declaration of global variable */int krt;/* * init_rtsocket * * Description: * Initiates the socket through which commands to the * kernels routing table are given. * * Arguments: * Return: * int - File descriptor to the kernel routing table * -1 on failure */int init_rtsocket(){ int fd = 0; int type = SOCK_DGRAM; /* Get socket for UDP or TCP */ if ((fd = socket(AF_INET, type, 0)) < 0) return -1; /* Unable to create socket */ return fd;}/* * gen_krtentry * * Description: * Generates an entry for the kernel's routing table to be used * both att addition and deletion of routes. * * Arguments: * u_int32_t dst_ip - IP address to the destination * u_int32_t gw_ip - IP address to gateway of the route * * Returns: * struct rtentry* - pointer to the entry */struct rtentry*gen_krtentry(u_int32_t dst_ip, u_int32_t gw_ip, char *dev){ struct rtentry *new_rtentry; struct sockaddr_in dst; struct sockaddr_in gw; struct sockaddr_in genmask; if((new_rtentry = malloc(sizeof(struct rtentry))) == NULL) return NULL; /* Malloc failed */ dst.sin_family = AF_INET; gw.sin_family = AF_INET; genmask.sin_family = AF_INET; dst.sin_addr.s_addr = dst_ip; gw.sin_addr.s_addr = gw_ip; /* modification by vikas to enable addition of default route */ if(dst_ip == 0) { genmask.sin_addr.s_addr = dst_ip ; new_rtentry->rt_flags = RTF_UP ; } else { genmask.sin_addr.s_addr = inet_addr("255.255.255.255"); new_rtentry->rt_flags = RTF_UP | RTF_HOST | RTF_GATEWAY; } new_rtentry->rt_metric = 0; new_rtentry->rt_dev = dev; new_rtentry->rt_dst = *(struct sockaddr*) &dst; new_rtentry->rt_gateway = *(struct sockaddr*) &gw; new_rtentry->rt_genmask = *(struct sockaddr*) &genmask; return new_rtentry;}/* * add_kroute * * Description: * Adds a route to the kernel's routing table. * * Arguments: * u_int32_t dst_ip - IP address to the destination * u_int32_t gw_ip - IP address to the gateway of the route* char *dev - name of the device * * Returns: * int - 0 on success * -1 on failure */intadd_kroute(u_int32_t dst_ip, u_int32_t gw_ip, char *dev){ struct rtentry *new_krtentry; if ((new_krtentry = gen_krtentry(dst_ip, gw_ip, dev)) == NULL) { fprintf(stderr,"add_kroute new_krt_entry"); return -1; } if(ioctl(krt, SIOCADDRT, (char*) new_krtentry) == -1) { perror("ioctl in add_kroute"); return -1; }#ifdef DEBUG fprintf(stdout,"added route to kernel\n");#endif return 0;}/* * del_kroute * * Description: * Deletes a route from the kernel's routing table. * * Arguments: * u_int32_t dst_ip - IP address to the destination * * Return: * int - 0 on success * -1 on failure */ intdel_kroute(u_int32_t dst_ip, u_int32_t gw_ip, char *dev){ struct rtentry *new_krtentry; if ((new_krtentry = gen_krtentry(dst_ip, gw_ip, dev)) == NULL) { fprintf(stderr,"del_kroute new_krt_entry"); return -1; } if(ioctl(krt, SIOCDELRT, (char*) new_krtentry) == -1) /* SIOCDELRT failed */ { perror("del_kroute : ioctl"); return -1; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -