internet.cc

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <sys/types.h>
00003 #include <netinet/in.h>
00004 #include <netinet/in_systm.h>   
00005 #include <netinet/ip.h>
00006 #include <netinet/ip_icmp.h>
00007 #include <netinet/ip_icmp.h>
00008 #include <arpa/inet.h>
00009 
00010 
00011 #include "emulate/internet.h"
00012 #include "scheduler.h"
00013 
00014 /*  
00015  * in_cksum --
00016  *      Checksum routine for Internet Protocol family headers (C Version)
00017  *      [taken from ping.c]
00018  */ 
00019 u_short  
00020 Internet::in_cksum(u_short* addr, int len)
00021 {   
00022         register int nleft = len;       
00023         register u_short *w = addr;
00024         register int sum = 0;
00025         u_short answer = 0;
00026     
00027         /*                      
00028          * Our algorithm is simple, using a 32 bit accumulator (sum), we add
00029          * sequential 16 bit words to it, and at the end, fold back all the
00030          * carry bits from the top 16 bits into the lower 16 bits.
00031          */      
00032         while (nleft > 1)  {
00033                 sum += *w++;
00034                 nleft -= 2;
00035         }       
00036     
00037         /* mop up an odd byte, if necessary */
00038         if (nleft == 1) {
00039                 *(u_char *)(&answer) = *(u_char *)w ;
00040                 sum += answer;
00041         }
00042 
00043         /* add back carry outs from top 16 bits to low 16 bits */
00044         sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
00045         sum += (sum >> 16);                     /* add carry */
00046         answer = ~sum;                          /* truncate to 16 bits */
00047         return(answer);
00048 }
00049 #include <netinet/in_systm.h>
00050 #include <netinet/ip.h>
00051 void
00052 Internet::print_ip(ip *ip)
00053 {   
00054         u_short off = ntohs(ip->ip_off);
00055         printf("IP v:%d, ihl:%d, tos:0x%x, id:%d, off:%d [df:%d, mf:%d], "
00056            "sum:%d, prot:%d\n",
00057                 ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_id),
00058                 off & IP_OFFMASK,
00059                 (off & IP_DF) ? 1 : 0,
00060                 (off & IP_MF) ? 1 : 0,
00061                 ip->ip_sum, ip->ip_p);
00062     printf("IP src:%s, ", inet_ntoa(ip->ip_src));
00063     printf("dst: %s\n", inet_ntoa(ip->ip_dst));
00064         printf("IP len:%d ttl: %d\n",
00065                 ntohs(ip->ip_len), ip->ip_ttl);
00066 }   
00067 
00068 /*
00069  * cons up a basic-looking ip header, no options
00070  * multi-byte quantities are assumed to be in HOST byte order
00071  */
00072 void
00073 Internet::makeip(ip* iph, u_short len, u_char ttl, u_char proto, in_addr& src, 
00074 in_addr& dst)
00075 {
00076         u_char *p = (u_char*) iph;
00077         *p = 0x45;      /* ver + hl */
00078         iph->ip_tos = 0;
00079         iph->ip_len = htons(len);
00080         iph->ip_id = (u_short) Scheduler::instance().clock();   // why not?
00081         iph->ip_off = 0x0000;   // mf and df bits off, offset zero
00082         iph->ip_ttl = ttl;
00083         iph->ip_p = proto;
00084         memcpy(&iph->ip_src, &src, 4);
00085         memcpy(&iph->ip_dst, &dst, 4);
00086         iph->ip_sum = Internet::in_cksum((u_short*) iph, 20);
00087         return;
00088 }
00089 

Generated on Tue Mar 6 16:47:46 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6