Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

inet.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1991 Regents of the University of California.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms are permitted
00006  * provided that the above copyright notice and this paragraph are
00007  * duplicated in all such forms and that any documentation,
00008  * advertising materials, and other materials related to such
00009  * distribution and use acknowledge that the software was developed
00010  * by the University of California, Lawrence Berkeley Laboratory,
00011  * Berkeley, CA.  The name of the University may not be used to
00012  * endorse or promote products derived from this software without
00013  * specific prior written permission.
00014  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00015  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00016  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017  */
00018 static const char rcsid[] =
00019     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/emulate/inet.c,v 1.5 2000/02/08 23:35:13 salehi Exp $ (LBL)";
00020 
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <ctype.h>
00024 #ifdef WIN32
00025 #include <windows.h>
00026 #include <winsock.h>
00027 #else
00028 #include <sys/param.h>
00029 #include <netdb.h>
00030 #include <sys/socket.h>
00031 #include <unistd.h>
00032 #endif
00033 
00034 #include "config.h"
00035 #include "inet.h"
00036 
00037 u_int32_t
00038 LookupHostAddr(const char *s)
00039 {
00040         if (isdigit(*s))
00041                 return (u_int32_t)inet_addr(s);
00042         else {
00043                 struct hostent *hp = gethostbyname(s);
00044                 if (hp == 0)
00045                         /*XXX*/
00046                         return (0);
00047                 return *((u_int32_t **)hp->h_addr_list)[0];
00048         }
00049 }
00050 
00051 u_int32_t
00052 LookupLocalAddr(void)
00053 {
00054         static u_int32_t local_addr;
00055         char name[MAXHOSTNAMELEN];
00056         
00057         if (local_addr == 0) {
00058                 (void)gethostname(name, sizeof(name));
00059                 local_addr = LookupHostAddr(name);
00060         }
00061         return (local_addr);
00062 }
00063 
00064 /*
00065  * A faster replacement for inet_ntoa().
00066  * Extracted from tcpdump 2.1.
00067  */
00068 const char *
00069 intoa(u_int32_t addr)
00070 {
00071         register char *cp;
00072         register u_int byte;
00073         register int n;
00074         static char buf[sizeof(".xxx.xxx.xxx.xxx")];
00075 
00076         NTOHL(addr);
00077         cp = &buf[sizeof buf];
00078         *--cp = '\0';
00079 
00080         n = 4;
00081         do {
00082                 byte = addr & 0xff;
00083                 *--cp = byte % 10 + '0';
00084                 byte /= 10;
00085                 if (byte > 0) {
00086                         *--cp = byte % 10 + '0';
00087                         byte /= 10;
00088                         if (byte > 0)
00089                                 *--cp = byte + '0';
00090                 }
00091                 *--cp = '.';
00092                 addr >>= 8;
00093         } while (--n > 0);
00094 
00095         return cp + 1;
00096 }
00097 
00098 char *
00099 InetNtoa(u_int32_t addr)
00100 {
00101         const char *s = intoa(addr);
00102         char *p = (char *)malloc(strlen(s) + 1);
00103         strcpy(p, s);
00104         return p;
00105 }
00106 
00107 char *
00108 LookupHostName(u_int32_t addr)
00109 {
00110         char *p;
00111         struct hostent* hp;
00112 
00113         /*XXX*/
00114         if (IN_MULTICAST(ntohl(addr)))
00115                 return (InetNtoa(addr));
00116 
00117         hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
00118         if (hp == 0) 
00119                 return InetNtoa(addr);
00120         p = (char *)malloc(strlen(hp->h_name) + 1);
00121         strcpy(p, hp->h_name);
00122         return p;
00123 }
00124 
00125 /*  
00126  * in_cksum --
00127  *      Checksum routine for Internet Protocol family headers (C Version)
00128  *      [taken from ping.c]
00129  */ 
00130 u_short  
00131 in_cksum(addr, len)
00132         u_short *addr; 
00133         int len;
00134 {   
00135         register int nleft = len;       
00136         register u_short *w = addr;
00137         register int sum = 0;
00138         u_short answer = 0;
00139     
00140         /*                      
00141          * Our algorithm is simple, using a 32 bit accumulator (sum), we add
00142          * sequential 16 bit words to it, and at the end, fold back all the
00143          * carry bits from the top 16 bits into the lower 16 bits.
00144          */      
00145         while (nleft > 1)  {
00146                 sum += *w++;
00147                 nleft -= 2;
00148         }       
00149     
00150         /* mop up an odd byte, if necessary */
00151         if (nleft == 1) {
00152                 *(u_char *)(&answer) = *(u_char *)w ;
00153                 sum += answer;
00154         }
00155 
00156         /* add back carry outs from top 16 bits to low 16 bits */
00157         sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
00158         sum += (sum >> 16);                     /* add carry */
00159         answer = ~sum;                          /* truncate to 16 bits */
00160         return(answer);
00161 }
00162 
00163 #include <netinet/in_systm.h>
00164 #include <netinet/ip.h>
00165 #include <stdio.h>
00166 void
00167 print_ip(struct ip *ip)
00168 {
00169         char buf[64];
00170         u_short off = ntohs(ip->ip_off);
00171         printf("IP v:%d, ihl:%d, tos:%d, id:%d, off:%d [df:%d, mf:%d], sum:%d, prot:%d\n",
00172                 ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_id),
00173                 off & IP_OFFMASK,
00174                 (off & IP_DF) ? 1 : 0,
00175                 (off & IP_MF) ? 1 : 0,
00176                 ip->ip_sum, ip->ip_p);
00177 #ifdef HAVE_ADDR2ASCII
00178         addr2ascii(AF_INET, &ip->ip_src, 4, buf);
00179 #else
00180         inet_ntoa(ip->ip_src);
00181 #endif /* HAVE_ADDR2ASCII */
00182 #ifdef HAVE_ADDR2ASCII
00183         printf("IP len:%d ttl: %d, src: %s, dst: %s\n",
00184                 ntohs(ip->ip_len), ip->ip_ttl, buf,
00185                         addr2ascii(AF_INET, &ip->ip_dst, 4, 0));
00186 #else
00187         printf("IP len:%d ttl: %d, src: %s, dst: %s\n",
00188                 ntohs(ip->ip_len), ip->ip_ttl, buf,
00189                inet_ntoa(ip->ip_src));
00190 #endif /* HAVE_ADDR2ASCII */
00191 }

Generated on Tue Apr 20 12:14:20 2004 for NS2.26SourcesOriginal by doxygen 1.3.3