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
00016
00017
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
00029
00030
00031
00032 while (nleft > 1) {
00033 sum += *w++;
00034 nleft -= 2;
00035 }
00036
00037
00038 if (nleft == 1) {
00039 *(u_char *)(&answer) = *(u_char *)w ;
00040 sum += answer;
00041 }
00042
00043
00044 sum = (sum >> 16) + (sum & 0xffff);
00045 sum += (sum >> 16);
00046 answer = ~sum;
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
00070
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;
00078 iph->ip_tos = 0;
00079 iph->ip_len = htons(len);
00080 iph->ip_id = (u_short) Scheduler::instance().clock();
00081 iph->ip_off = 0x0000;
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