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

📄 k_route.c

📁 支持IPv6的adov路由协议(本人修改后)
💻 C
字号:
/***************************************************************************** * * Copyright (C) 2001 Uppsala University & Ericsson AB. * Copyright (C) 2003 Simon Fraser University and NewMIC * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * Authors: Erik Nordstr鰉, <erik.nordstrom@it.uu.se> *        : Peter Lee       <peter.lee@shaw.ca> *           * *****************************************************************************/#include <sys/ioctl.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <net/route.h>#include <net/if_arp.h>#include <unistd.h>#include <errno.h>#include <stdio.h>#include <linux/unistd.h>#include <linux/sysctl.h>#include "defs.h"#include "debug.h"#include "k_route.h"//PL:#ifdef _IPV6#include "ipv6_utils.h"#include "print_route6.h"#endif /* _IPV6 *//* * Manipulating the kernel routing table through ioctl() operations... *///PL:#ifdef _IPV6int k_add_rte(struct in6_addr dst, struct in6_addr gw, 	      u_int16_t plen, short int hcnt,	      unsigned int ifindex){    int sock, ret;    //int fd, flush = 1;    struct in6_rtmsg rte;    DEBUG(LOG_DEBUG, 0, "k_add_rte : Adding a kernel route %s : %s : %d",	  ip6_to_str(dst), ip6_to_str(gw), plen);    /* Flush the Routing Cache */    /*      if ((fd = open("/proc/sys/net/ipv6/route/flush", O_WRONLY)) < 0)	DEBUG(LOG_DEBUG, 0, "k_add_rte: open /proc/sys/net/ipv6/route/flush file failed.");    if (write(fd, &flush, sizeof(flush)) < 0)	DEBUG(LOG_DEBUG, 0, "k_add_rte: write /proc/sys/net/ipv6/route/flush file failed.");    close(fd);    */    memset(&rte, 0, sizeof(struct in6_rtmsg));    memcpy(&rte.rtmsg_dst, &dst, sizeof(struct in6_addr));    memcpy(&rte.rtmsg_gateway, &gw, sizeof(struct in6_addr));    rte.rtmsg_dst_len = plen;    rte.rtmsg_ifindex = ifindex;    rte.rtmsg_metric = hcnt + 1;    //PL: can try to remove RTF_DYNAMIC and see what happen    if ((memcmp(&gw, &dst, sizeof(struct in6_addr)) == 0) ||	(memcmp(&gw, &ipv6_default_addr, sizeof(struct in6_addr)) == 0))        rte.rtmsg_flags = RTF_HOST | RTF_UP | RTF_DYNAMIC;    else      {        rte.rtmsg_flags = RTF_HOST | RTF_GATEWAY | RTF_UP | RTF_DYNAMIC;      }    sock = socket(AF_INET6, SOCK_DGRAM, 0);    if (sock < 0) {	fprintf(stderr, "k_add_rte: can't make socket\n");	return -1;    }    /* Send message by ioctl(). */    ret = ioctl(sock, SIOCADDRT, &rte);#ifdef DEBUG_PRINTROUTE6    //PL: debug print the routing table    print_route6();#endif    if (ret < 0)      {	switch (errno) 	  {	  case EEXIST:	  case ENETUNREACH:	  case EPERM:	  default:	    close(sock);	    DEBUG(LOG_DEBUG, 0, "k_add_rte failed!\n");	    return ret;	  }	close(sock);	return 1;      }    close(sock);#ifdef DEMO    DEBUG(LOG_INFO, 0, "Add a new route dst=%s, gw=%s!",	  ip6_to_str(dst), ip6_to_str(gw));#endif    DEBUG(LOG_DEBUG, 0, "k_add_rte : Adding a kernel route sucess!\n");   return 0;}#elseint k_add_rte(u_int32_t dst, u_int32_t gw, u_int32_t nm, short int hcnt,	      unsigned int ifindex){    int sock, ret;    struct rtentry rte;    struct sockaddr_in dest, mask, gtwy;    if (dst == gw)	gw = 0;    memset(&rte, 0, sizeof(struct rtentry));    dest.sin_family = AF_INET;    dest.sin_addr.s_addr = htonl(dst);    gtwy.sin_family = AF_INET;    gtwy.sin_addr.s_addr = htonl(gw);    mask.sin_family = AF_INET;    mask.sin_addr.s_addr = htonl(nm);    memcpy(&rte.rt_dst, &dest, sizeof(struct sockaddr_in));    memcpy(&rte.rt_gateway, &gtwy, sizeof(struct sockaddr_in));    memcpy(&rte.rt_genmask, &mask, sizeof(struct sockaddr_in));    rte.rt_dev = DEV_IFINDEX(ifindex).ifname;    rte.rt_metric = hcnt + 1;    if (gw == 0)	rte.rt_flags = RTF_HOST | RTF_UP | RTF_DYNAMIC;    else	rte.rt_flags = RTF_HOST | RTF_GATEWAY | RTF_UP | RTF_DYNAMIC;    sock = socket(AF_INET, SOCK_DGRAM, 0);    if (sock < 0) {	fprintf(stderr, "can't make socket\n");	return -1;    }    /* Send message by ioctl(). */    ret = ioctl(sock, SIOCADDRT, &rte);    if (ret < 0) {	switch (errno) {	case EEXIST:	case ENETUNREACH:	case EPERM:	default:	    close(sock);	    return ret;	}	close(sock);	return 1;    }    close(sock);    return 0;}#endif /* _IPV6 *///PL:#ifdef _IPV6int k_del_rte(struct in6_addr dst, struct in6_addr gw, u_int16_t plen,	      unsigned int ifindex, unsigned int metric){    int sock, ret;    //int fd, flush=1;    struct in6_rtmsg rte;    DEBUG(LOG_DEBUG, 0, "k_del_rte : Removing a kernel route %s : %s : %d : %d",	  ip6_to_str(dst), ip6_to_str(gw), plen, metric);    /* Flush the Routing Cache */    /* Might need this in the future    if ((fd = open("/proc/sys/net/ipv6/route/flush", O_WRONLY)) < 0)	DEBUG(LOG_DEBUG, 0, "k_del_rte: open /proc/sys/net/ipv6/route/flush file failed.");    if (write(fd, &flush, sizeof(flush)) < 0)	DEBUG(LOG_DEBUG, 0, "k_del_rte: write /proc/sys/net/ipv6/route/flush file failed.");    close(fd);    */    memset(&rte, 0, sizeof(struct in6_rtmsg));    rte.rtmsg_dst_len = plen;    rte.rtmsg_ifindex = ifindex;    /*    if (memcmp(&gw, &dst, sizeof(struct in6_addr)) == 0)      {	memcpy(&gw, &ipv6_default_addr, sizeof(struct in6_addr));        rte.rtmsg_flags = RTF_HOST;      }    else if (memcmp(&gw, &ipv6_default_addr, sizeof(struct in6_addr)) == 0)        rte.rtmsg_flags = RTF_HOST;    else      {        rte.rtmsg_flags = RTF_HOST | RTF_GATEWAY;	rte.rtmsg_metric = 0;      }        memcpy(&rte.rtmsg_dst, &dst, sizeof(struct in6_addr));    memcpy(&rte.rtmsg_gateway, &gw, sizeof(struct in6_addr));    */    if ((memcmp(&gw, &dst, sizeof(struct in6_addr)) == 0) ||	(memcmp(&gw, &ipv6_default_addr, sizeof(struct in6_addr)) == 0))      {	memcpy(&rte.rtmsg_gateway, &ipv6_default_addr, sizeof(struct in6_addr));        rte.rtmsg_flags = RTF_HOST;	rte.rtmsg_metric = metric;      }    else      {	memcpy(&rte.rtmsg_gateway, &gw, sizeof(struct in6_addr));        rte.rtmsg_flags = RTF_HOST | RTF_GATEWAY;	rte.rtmsg_metric = 0;      }       memcpy(&rte.rtmsg_dst, &dst, sizeof(struct in6_addr));    sock = socket(AF_INET6, SOCK_DGRAM, 0);    if (sock < 0) {	fprintf(stderr, "k_del_rte: can't make socket\n");	return -1;    }    /* Send message by ioctl(). */    ret = ioctl(sock, SIOCDELRT, &rte);#ifdef DEBUG_PRINTROUTE6    //PL: debug print the routing table    print_route6();#endif    if (ret < 0) {	switch (errno) {	case EEXIST:	case ENETUNREACH:	case EPERM:	default:	    close(sock);	    DEBUG(LOG_DEBUG, 0, "k_del_rte failed!\n");	    return ret;	}	close(sock);	return -1;    } else {	close(sock);    }#ifdef DEMO    DEBUG(LOG_INFO, 0, "Delete a old route dst=%s, gw=%s!",	  ip6_to_str(dst), ip6_to_str(gw));#endif    DEBUG(LOG_DEBUG, 0, "k_del_rte : Removing a kernel route sucess!\n");    return 0;}#elseint k_del_rte(u_int32_t dst, u_int32_t gw, u_int32_t nm){    int sock, ret;    struct rtentry rte;    struct sockaddr_in dest, mask, gtwy;    /*  printf("k_del_rte: Removing a kernel route (%s : %s : %s)!\n",  *//*  	 ip_to_str(dst), ip_to_str(gw), ip_to_str(nm)); */    memset(&rte, 0, sizeof(struct rtentry));    dest.sin_family = AF_INET;    dest.sin_addr.s_addr = htonl(dst);    gtwy.sin_family = AF_INET;    gtwy.sin_addr.s_addr = htonl(gw);    mask.sin_family = AF_INET;    mask.sin_addr.s_addr = htonl(nm);    memcpy(&rte.rt_dst, &dest, sizeof(struct sockaddr_in));    memcpy(&rte.rt_gateway, &gtwy, sizeof(struct sockaddr_in));    memcpy(&rte.rt_genmask, &mask, sizeof(struct sockaddr_in));    rte.rt_flags = RTF_HOST;    sock = socket(AF_INET, SOCK_DGRAM, 0);    if (sock < 0) {	fprintf(stderr, "k_del_rte: can't make socket\n");	return -1;    }    /* Send message by ioctl(). */    ret = ioctl(sock, SIOCDELRT, &rte);    if (ret < 0) {	switch (errno) {	case EEXIST:	case ENETUNREACH:	case EPERM:	default:	    close(sock);	    return ret;	}	close(sock);	return -1;    } else {	close(sock);    }    return 0;}#endif /* _IPV6 *//* This is an ugly way of updating a route... *///PL:#ifdef _IPV6/* PL: ipv6 call del and add directly instead of chg int k_chg_rte(struct in6_addr dst, struct in6_addr gw, u_int16_t plen, 	      short int hcnt, unsigned int ifindex){    int ret;    //if ((ret = k_del_rte(dst, 0, 0)) < 0)    //PL: Why has to pass in 0 as gateway??    if ((ret = k_del_rte(dst, ipv6_default_addr, 0)) < 0)	return ret;    if ((ret = k_add_rte(dst, gw, plen, hcnt, ifindex)) < 0)	return ret;    return 0;}*/#elseint k_chg_rte(u_int32_t dst, u_int32_t gw, u_int32_t nm, short int hcnt,	      unsigned int ifindex){    int ret;    if ((ret = k_del_rte(dst, 0, 0)) < 0)	return ret;    if ((ret = k_add_rte(dst, gw, nm, hcnt, ifindex)) < 0)	return ret;    return 0;}#endif /* _IPV6 *///PL:#ifdef _IPV6int k_del_arp(struct in6_addr dst){    int sock, ret;    struct arpreq req;    //struct sockaddr_in *dest;    struct sockaddr_in6 *dest;    //u_int32_t __dst;    //PL: What is this for???    struct in6_addr __dst;    memset(&req, 0, sizeof(struct arpreq));    //PL: Not sure why have to do it this way??    //dest = (struct sockaddr_in *) &req.arp_pa;    //dest->sin_family = AF_INET;    dest = (struct sockaddr_in6 *) &req.arp_pa;    dest->sin6_family = AF_INET6;    //__dst = htonl(dst);    hton_in6_addr(&__dst, &dst);    //memcpy(&dest->sin_addr, &__dst, sizeof(struct in_addr));    memcpy(&dest->sin6_addr, &__dst, sizeof(struct in6_addr));    //sock = socket(AF_INET, SOCK_DGRAM, 0);    sock = socket(AF_INET6, SOCK_DGRAM, 0);    if (sock < 0) {	fprintf(stderr, "k_del_arp: can't make socket\n");	return -1;    }    /* Send message by ioctl(). */    ret = ioctl(sock, SIOCDARP, &req);    if (ret < 0) {	switch (errno) {	case EEXIST:	case ENETUNREACH:	case EPERM:	default:	    close(sock);	    perror("k_del_arp: Sock err:");	    return ret;	}	close(sock);	return -1;    } else {	close(sock);    }    return 0;}#elseint k_del_arp( dst){    int sock, ret;    struct arpreq req;    struct sockaddr_in *dest;    u_int32_t __dst;    memset(&req, 0, sizeof(struct arpreq));    dest = (struct sockaddr_in *) &req.arp_pa;    dest->sin_family = AF_INET;    __dst = htonl(dst);    memcpy(&dest->sin_addr, &__dst, sizeof(struct in_addr));    sock = socket(AF_INET, SOCK_DGRAM, 0);    if (sock < 0) {	fprintf(stderr, "k_del_arp: can't make socket\n");	return -1;    }    /* Send message by ioctl(). */    ret = ioctl(sock, SIOCDARP, &req);    if (ret < 0) {	switch (errno) {	case EEXIST:	case ENETUNREACH:	case EPERM:	default:	    close(sock);	    perror("k_del_arp: Sock err:");	    return ret;	}	close(sock);	return -1;    } else {	close(sock);    }    return 0;}#endif /*_IPV6 */

⌨️ 快捷键说明

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