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

📄 inet.c

📁 ucsb大学开发的aodv路由协议代码。
💻 C
字号:
/* * Copyright (C) 2001, University of California, Santa Barbara *  * 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. *  * Other copyrights might apply to parts of this software and are so * noted when applicable. *//* * Parts of this program has been derived from PIM sparse-mode pimd. * The pimd program is covered by the license in the accompanying file * named "LICENSE.pimd". *   * The pimd program is COPYRIGHT 1998 by University of Southern California. * */#include <arpa/inet.h>#include <net/if.h>#include <netdb.h>#include <netinet/in.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/socket.h>#include <sys/types.h>#include <sys/ioctl.h>#include <unistd.h>#include "aodvSocket.h"#include "const.h"#include "debug.h"#define MAXHOSTNAMELEN 100#define C(x)    ((x) & 0xff)char *inet_fmt_h(u_int32_t addr, char *s);char *inet_fmt_n(u_int32_t addr, char *s);u_int32_t my_ip = -1;u_int32_t getip() {  struct ifreq if_info;  struct sockaddr addr;  struct sockaddr_in *addr_in;  static char str[128];  if((my_ip == -1) && (aodv_socket >= 0)){    //initialize my_ip    strncpy((char *)&if_info.ifr_name	    ,(const char *)&interface	    ,sizeof(if_info.ifr_name));    if(ioctl(aodv_socket,SIOCGIFADDR,&if_info) == 0){      addr=if_info.ifr_addr;      addr_in=(struct sockaddr_in *)&addr;      if(inet_ntop(AF_INET		   ,&addr_in->sin_addr.s_addr		   ,str,sizeof(str)) 	 != NULL){	my_ip=inet_addr(str);      }    }    trace(TRACE_INIT,"getip:initializing my_ip to %s",inet_fmt_n(my_ip,s1));  }  return my_ip;}/* * Verify that a given netmask is plausible; * make sure that it is a series of 1's followed by * a series of 0's with no discontiguous 1's. */int inet_valid_mask(u_int32_t mask) {  if (~(((mask & -mask) - 1) | mask) != 0) {    /* Mask is not contiguous */    return (FALSE);  }  return (TRUE);}/* * Convert an IP address in u_int32_t (host) format into a printable string. */char *inet_fmt_h(u_int32_t addr, char *s) {  register u_char *a;      a = (u_char *)&addr;  sprintf(s, "%u.%u.%u.%u",a[3], a[2], a[1], a[0] );  return (s);}/* * Convert an IP address in u_int32_t (network) format into a printable string. */char *inet_fmt_n(u_int32_t addr, char *s) {  register u_char *a;      a = (u_char *)&addr;  sprintf(s, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);  return (s);}/* * Convert an IP subnet number in u_int32_t (network) format into a printable * string including the netmask as a number of bits. */#ifdef NOSUCHDEF	/* replaced by netname() */char *inet_fmts(u_int32_t addr,u_int32_t mask,char *s){  register u_char *a, *m;  int bits;  if ((addr == 0) && (mask == 0)) {    sprintf(s, "default");    return (s);  }  a = (u_char *)&addr;  m = (u_char *)&mask;  bits = 33 - ffs(ntohl(mask));  if      (m[3] != 0) sprintf(s, "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3],			      bits);  else if (m[2] != 0) sprintf(s, "%u.%u.%u/%d",    a[0], a[1], a[2], bits);  else if (m[1] != 0) sprintf(s, "%u.%u/%d",       a[0], a[1], bits);  else                sprintf(s, "%u/%d",          a[0], bits);      return (s);}#endif /* NOSUCHDEF *//* * Convert the printable string representation of an IP address into the * u_int32_t (network) format.  Return 0xffffffff on error.  (To detect the * legal address with that value, you must explicitly compare the string * with "255.255.255.255".) * The return value is in network order. */u_int32_t inet_parse(char *s,int n){  u_int32_t a = 0;  u_int a0 = 0, a1 = 0, a2 = 0, a3 = 0;  int i;  char c;  i = sscanf(s, "%u.%u.%u.%u%c", &a0, &a1, &a2, &a3, &c);  if (i < n || i > 4 || a0 > 255 || a1 > 255 || a2 > 255 || a3 > 255)    return (0xffffffff);  ((u_char *)&a)[0] = a0;  ((u_char *)&a)[1] = a1;  ((u_char *)&a)[2] = a2;  ((u_char *)&a)[3] = a3;  return (a);}/* * inet_cksum extracted from: *			P I N G . C * * Author - *	Mike Muuss *	U. S. Army Ballistic Research Laboratory *	December, 1983 * Modified at Uc Berkeley * * (ping.c) Status - *	Public Domain.  Distribution Unlimited. * *			I N _ C K S U M * * Checksum routine for Internet Protocol family headers (C Version) * */intinet_cksum(u_int16_t *addr,u_int len){  register int nleft = (int)len;  register u_int16_t *w = addr;  u_int16_t answer = 0;  register int sum = 0;  /*   *  Our algorithm is simple, using a 32 bit accumulator (sum),   *  we add sequential 16 bit words to it, and at the end, fold   *  back all the carry bits from the top 16 bits into the lower   *  16 bits.   */  while (nleft > 1)  {    sum += *w++;    nleft -= 2;  }  /* mop up an odd byte, if necessary */  if (nleft == 1) {    *(u_char *) (&answer) = *(u_char *)w ;    sum += answer;  }  /*   * add back carry outs from top 16 bits to low 16 bits   */  sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */  sum += (sum >> 16);			/* add carry */  answer = ~sum;				/* truncate to 16 bits */  return (answer);}/* * Called by following netname() to create a mask specified network address. */void trimdomain(char *cp){  static char domain[MAXHOSTNAMELEN + 1];  static int first = 1;  char *s;      if (first) {    first = 0;    if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&	(s = (char *)strchr(domain, '.')))      (void) strcpy(domain, s + 1);    else      domain[0] = 0;  }  if (domain[0]) {    while ((cp = (char *)strchr(cp, '.'))) {      if (!strcasecmp(cp + 1, domain)) {	*cp = 0;        /* hit it */	break;      } else {	cp++;      }    }  }}

⌨️ 快捷键说明

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