ip.c (24583) | ip.c (31183) |
---|---|
1/* | 1/* |
2 * ip.c (C) 1995 Darren Reed | 2 * ip.c (C) 1995-1997 Darren Reed |
3 * | 3 * |
4 * The author provides this program as-is, with no gaurantee for its 5 * suitability for any specific purpose. The author takes no responsibility 6 * for the misuse/abuse of this program and provides it for the sole purpose 7 * of testing packet filter policies. This file maybe distributed freely 8 * providing it is not modified and that this notice remains in tact. | 4 * Redistribution and use in source and binary forms are permitted 5 * provided that this notice is preserved and due credit is given 6 * to the original author and the contributors. |
9 */ | 7 */ |
10#if !defined(lint) && defined(LIBC_SCCS) 11static char sccsid[] = "%W% %G% (C)1995"; | 8#if !defined(lint) 9static const char sccsid[] = "%W% %G% (C)1995"; 10static const char rcsid[] = "@(#)$Id: ip.c,v 2.0.2.11 1997/10/23 11:42:44 darrenr Exp $"; |
12#endif 13#include <errno.h> 14#include <stdio.h> 15#include <stdlib.h> 16#include <unistd.h> 17#include <string.h> 18#include <sys/types.h> 19#include <netinet/in_systm.h> 20#include <sys/socket.h> 21#include <net/if.h> 22#include <netinet/in.h> 23#include <netinet/ip.h> 24#include <netinet/tcp.h> 25#include <netinet/udp.h> 26#include <netinet/ip_icmp.h> | 11#endif 12#include <errno.h> 13#include <stdio.h> 14#include <stdlib.h> 15#include <unistd.h> 16#include <string.h> 17#include <sys/types.h> 18#include <netinet/in_systm.h> 19#include <sys/socket.h> 20#include <net/if.h> 21#include <netinet/in.h> 22#include <netinet/ip.h> 23#include <netinet/tcp.h> 24#include <netinet/udp.h> 25#include <netinet/ip_icmp.h> |
26#include <sys/param.h> |
|
27#ifndef linux | 27#ifndef linux |
28#include 29#include | 28# include <netinet/if_ether.h> 29# include <netinet/ip_var.h> 30# if __FreeBSD_version >= 300000 31# include <net/if_var.h> 32# endif |
30#endif 31#include "ipsend.h" 32 33 34static char *ipbuf = NULL, *ethbuf = NULL; 35 36 37u_short chksum(buf,len) --- 24 unchanged lines hidden (view full) --- 62 63 if (!ethbuf) 64 ethbuf = (char *)calloc(1, 65536+1024); 65 s = ethbuf; 66 eh = (ether_header_t *)s; 67 68 bcopy((char *)buf, s + sizeof(*eh), len); 69 if (gwip.s_addr == last_gw.s_addr) | 33#endif 34#include "ipsend.h" 35 36 37static char *ipbuf = NULL, *ethbuf = NULL; 38 39 40u_short chksum(buf,len) --- 24 unchanged lines hidden (view full) --- 65 66 if (!ethbuf) 67 ethbuf = (char *)calloc(1, 65536+1024); 68 s = ethbuf; 69 eh = (ether_header_t *)s; 70 71 bcopy((char *)buf, s + sizeof(*eh), len); 72 if (gwip.s_addr == last_gw.s_addr) |
70 bcopy(last_arp, (char *)&eh->ether_dhost, 6); 71 else if (arp((char *)&gwip, (char *)&eh->ether_dhost) == -1) | 73 bcopy(last_arp, (char *)A_A eh->ether_dhost, 6); 74 else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1) |
72 { 73 perror("arp"); 74 return -2; 75 } | 75 { 76 perror("arp"); 77 return -2; 78 } |
76 eh->ether_type = ETHERTYPE_IP; | 79 eh->ether_type = htons(ETHERTYPE_IP); |
77 last_gw.s_addr = gwip.s_addr; 78 err = sendip(nfd, s, sizeof(*eh) + len); 79 return err; 80} 81 82 83/* 84 */ --- 9 unchanged lines hidden (view full) --- 94 ether_header_t *eh; 95 ip_t ipsv; 96 int err; 97 98 if (!ipbuf) 99 ipbuf = (char *)malloc(65536); 100 eh = (ether_header_t *)ipbuf; 101 | 80 last_gw.s_addr = gwip.s_addr; 81 err = sendip(nfd, s, sizeof(*eh) + len); 82 return err; 83} 84 85 86/* 87 */ --- 9 unchanged lines hidden (view full) --- 97 ether_header_t *eh; 98 ip_t ipsv; 99 int err; 100 101 if (!ipbuf) 102 ipbuf = (char *)malloc(65536); 103 eh = (ether_header_t *)ipbuf; 104 |
102 bzero((char *)&eh->ether_shost, sizeof(eh->ether_shost)); | 105 bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost)); |
103 if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr)) | 106 if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr)) |
104 bcopy(last_arp, (char *)&eh->ether_dhost, 6); 105 else if (arp((char *)&gwip, (char *)&eh->ether_dhost) == -1) | 107 bcopy(last_arp, (char *)A_A eh->ether_dhost, 6); 108 else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1) |
106 { 107 perror("arp"); 108 return -2; 109 } | 109 { 110 perror("arp"); 111 return -2; 112 } |
110 bcopy((char *)&eh->ether_dhost, last_arp, sizeof(last_arp)); 111 eh->ether_type = ETHERTYPE_IP; | 113 bcopy((char *)A_A eh->ether_dhost, last_arp, sizeof(last_arp)); 114 eh->ether_type = htons(ETHERTYPE_IP); |
112 113 bcopy((char *)ip, (char *)&ipsv, sizeof(*ip)); 114 last_gw.s_addr = gwip.s_addr; 115 ip->ip_len = htons(ip->ip_len); 116 ip->ip_off = htons(ip->ip_off); | 115 116 bcopy((char *)ip, (char *)&ipsv, sizeof(*ip)); 117 last_gw.s_addr = gwip.s_addr; 118 ip->ip_len = htons(ip->ip_len); 119 ip->ip_off = htons(ip->ip_off); |
117 if (!ip->ip_v) 118 ip->ip_v = IPVERSION; 119 if (!ip->ip_id) 120 ip->ip_id = htons(id++); 121 if (!ip->ip_ttl) 122 ip->ip_ttl = 60; | 120 if (!(frag & 2)) { 121 if (!ip->ip_v) 122 ip->ip_v = IPVERSION; 123 if (!ip->ip_id) 124 ip->ip_id = htons(id++); 125 if (!ip->ip_ttl) 126 ip->ip_ttl = 60; 127 } |
123 124 if (!frag || (sizeof(*eh) + ntohs(ip->ip_len) < mtu)) 125 { 126 ip->ip_sum = 0; 127 ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2); 128 129 bcopy((char *)ip, ipbuf + sizeof(*eh), ntohs(ip->ip_len)); 130 err = sendip(nfd, ipbuf, sizeof(*eh) + ntohs(ip->ip_len)); --- 211 unchanged lines hidden --- | 128 129 if (!frag || (sizeof(*eh) + ntohs(ip->ip_len) < mtu)) 130 { 131 ip->ip_sum = 0; 132 ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2); 133 134 bcopy((char *)ip, ipbuf + sizeof(*eh), ntohs(ip->ip_len)); 135 err = sendip(nfd, ipbuf, sizeof(*eh) + ntohs(ip->ip_len)); --- 211 unchanged lines hidden --- |