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

📄 ipv6_utils.c

📁 支持IPv6的adov路由协议(本人修改后)
💻 C
字号:
/***************************************************************************** * * 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 * * Author: Peter Lee       <peter.lee@shaw.ca> * *****************************************************************************/#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <netinet/ip6.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include "ipv6_utils.h"#include "defs.h"#include "address_conf.h"#define _PATH_PROCNET_IFINET6   "/proc/net/if_inet6"#define IPV6_ADDR_ANY		0x0000U#define IPV6_ADDR_UNICAST      	0x0001U#define IPV6_ADDR_MULTICAST    	0x0002U#define IPV6_ADDR_ANYCAST	0x0004U#define IPV6_ADDR_GLOBAL		0x0000U#define IPV6_ADDR_LOOPBACK	0x0010U#define IPV6_ADDR_LINKLOCAL	0x0020U#define IPV6_ADDR_SITELOCAL	0x0040U#define IPV6_ADDR_COMPATv4	0x0080U#define IPV6_ADDR_SCOPE_MASK	0x00f0U#define IPV6_ADDR_MAPPED	0x1000U#define IPV6_ADDR_RESERVED	0x2000Ustruct in6_addr if_link_addr;/** * ntoh_in6_addr - convert IPv6 address from network to host byte order * @src: address to convert * @tgt: buffer where to store converted address * * Converts an IPv6 address from network byte order to host byte * order.   **/struct in6_addr *ntoh_in6_addr(struct in6_addr *tgt, struct in6_addr *src){  //int i;  memset(tgt, 0, sizeof(struct in6_addr));  if (src == NULL) {    fprintf(stderr, /*__FUNCTION__ZJH*/ " src == NULL\n");    return NULL;  }  if (tgt == NULL) {    perror(__FUNCTION__);    return NULL;  }  //PL: IPv6 are always in network byte order  /* This code is unneccesary      for (i = 0; i < 4; i++)     tgt->s6_addr32[i] = ntohl(src->s6_addr32[i]);  */  memcpy(tgt,src, sizeof(struct in6_addr));  return tgt;}/** * hton_in6_addr - convert IPv6 address from host to network byte order * @src: address to convert * @tgt: buffer where to store converted address * * Converts an IPv6 address from host byte order to network byte * order.  Returns newly allocated network byte order address.  User * should free() the memory after use. **/struct in6_addr *hton_in6_addr(struct in6_addr *tgt, struct in6_addr *src){  //int i;    if (src == NULL) {    fprintf(stderr, /*__FUNCTION__ZJH*/ " addr == NULL\n");    return NULL;  }  if (tgt == NULL) {    perror(__FUNCTION__);    return NULL;  }  //PL: IPv6 are always in network byte order  /* This code is unnecessary      for (i = 0; i < 4; i++)       tgt->s6_addr32[i] = htonl(src->s6_addr32[i]);  */  memcpy(tgt,src, sizeof(struct in6_addr));    return tgt;}/* * Pass in the interface name and value will be return in interface_info * Input:  ifname * Output: interface_info *///struct if6_info *get_if6_info(char *ifname, struct if6_info *interface_info)struct if6_info *get_if6_info(char *ifname){    FILE *f;    char addr6[40], devname[20];    char addr6p[8][5];	struct if6_info *if_info_head=NULL,*if_info_tail=NULL,*if_info_tmp=NULL;    struct ip6_addr *ip6_addr_tail=NULL;    if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL)       {		if((if_info_tmp=(struct if6_info *)malloc(sizeof(struct if6_info)))==NULL){			printf("(struct if6_info) malloc failed!\n");			exit(-1);		}		memset(if_info_tmp,0,sizeof(struct if6_info));/*	while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",		      addr6p[0], addr6p[1], addr6p[2], addr6p[3],		      addr6p[4], addr6p[5], addr6p[6], addr6p[7],		      &(interface_info->if_idx), &(interface_info->plen), 		      &(interface_info->scope), &(interface_info->dad_status), 		      devname) != EOF)	  {	    if ((interface_info->scope == IPV6_ADDR_SITELOCAL) &&	    //if ((interface_info->scope == IPV6_ADDR_LINKLOCAL) &&		(!strcmp(devname, ifname)) )	      {		sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",			addr6p[0], addr6p[1], addr6p[2], addr6p[3],			addr6p[4], addr6p[5], addr6p[6], addr6p[7]);		memcpy(interface_info->devname, devname, sizeof(devname));		inet_pton(AF_INET6, addr6, &(interface_info->ipv6_addr));		//PL: info for the request device found, return		fclose(f);		return 0;	      }	  }*/	while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",		      addr6p[0], addr6p[1], addr6p[2], addr6p[3],		      addr6p[4], addr6p[5], addr6p[6], addr6p[7],		      &(if_info_tmp->if_idx), &(if_info_tmp->plen), 		      &(if_info_tmp->scope), &(if_info_tmp->dad_status), 		      devname) != EOF){		if (((if_info_tmp->scope == IPV6_ADDR_SITELOCAL) ||			(if_info_tmp->scope == IPV6_ADDR_GLOBAL)||			(if_info_tmp->scope==IPV6_ADDR_LINKLOCAL)) &&			(!strcmp(devname, ifname)) ){			sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",				addr6p[0], addr6p[1], addr6p[2], addr6p[3],				addr6p[4], addr6p[5], addr6p[6], addr6p[7]);			memcpy(if_info_tmp->devname, devname, sizeof(devname));			if(if_info_tmp->scope==IPV6_ADDR_LINKLOCAL){				inet_pton(AF_INET6,addr6,&if_link_addr);				continue;			}			inet_pton(AF_INET6, addr6, &(if_info_tmp->ipv6_addr));			if(if_info_head==NULL){				//printf("if_info_head==NULL!\n");				if(((if_info_tail=(struct if6_info *)malloc(sizeof(struct if6_info)))==NULL)||				((ip6_addr_tail=(struct ip6_addr *)malloc(sizeof(struct ip6_addr)))==NULL)){					printf("(struct if6_info) malloc failed!\n");					exit(-1);				}				if_info_head=if_info_tail;				old_ip6addr_head=ip6_addr_tail;			}else{				//printf("create list...\n");				if(((if_info_tail->next=(struct if6_info *)malloc(sizeof(struct if6_info)))==NULL)||				((ip6_addr_tail->next=(struct ip6_addr *)malloc(sizeof(struct ip6_addr)))==NULL)){					printf("(struct if6_info) malloc failed!\n");					exit(-1);				}				if_info_tail=if_info_tail->next;				ip6_addr_tail=ip6_addr_tail->next;			}			memcpy(if_info_tail,if_info_tmp,sizeof(struct if6_info));			memcpy(&(ip6_addr_tail->ip6_in6addr),&(if_info_tail->ipv6_addr),				sizeof(struct in6_addr));			ip6_addr_tail->plen=if_info_tail->plen;			ip6_addr_tail->next=NULL;		}		memset(if_info_tmp,0,sizeof(struct if6_info));	}    }    fclose(f);	//if((old_ip6addr_head==NULL)&&(if_info_head==NULL)) 	//	printf("no ipv6 address!\n");    return if_info_head;}/** * hton_in6_addr - convert IPv6 address from host to network byte order * @src: address to convert * @tgt: buffer where to store converted address * * Converts an IPv6 address from host byte order to network byte * order.  Returns newly allocated network byte order address.  User * should free() the memory after use. **/void copy_in6_addr(struct in6_addr *tgt, struct in6_addr *src){  memcpy(tgt,src, sizeof(struct in6_addr));}//PL: This is for IPv6, maximum can handle 4 IPv6 address in 1 printf() call.char *NS_CLASS ip6_to_str(struct in6_addr addr){    char *str;    static char ip6_buf[4][40];    static int count = 0;    int which;    which = (count%4);    bzero(ip6_buf[which], 40);    sprintf(ip6_buf[which], "%x:%x:%x:%x:%x:%x:%x:%x",	       (int)ntohs(addr.s6_addr16[0]), (int)ntohs(addr.s6_addr16[1]),	       (int)ntohs(addr.s6_addr16[2]), (int)ntohs(addr.s6_addr16[3]),	       (int)ntohs(addr.s6_addr16[4]), (int)ntohs(addr.s6_addr16[5]),	       (int)ntohs(addr.s6_addr16[6]), (int)ntohs(addr.s6_addr16[7]));    //printf("count = %d, which = %d\n", count, which);    str = ip6_buf[which];    count++;    return str;}void print_ipv6_addr(struct in6_addr ipv6_addr){  int i;  for(i=0; i<16; i++)    {      printf("%x", ipv6_addr.s6_addr[i]);    }  printf("\n");}

⌨️ 快捷键说明

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