Deleted Added
full compact
ip.c (170268) ip.c (255332)
1/* $FreeBSD: head/contrib/ipfilter/ipsend/ip.c 170268 2007-06-04 02:54:36Z darrenr $ */
1/* $FreeBSD: head/contrib/ipfilter/ipsend/ip.c 255332 2013-09-06 23:11:19Z cy $ */
2
3/*
4 * ip.c (C) 1995-1998 Darren Reed
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 */
8#if !defined(lint)
9static const char sccsid[] = "%W% %G% (C)1995";
2
3/*
4 * ip.c (C) 1995-1998 Darren Reed
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 */
8#if !defined(lint)
9static const char sccsid[] = "%W% %G% (C)1995";
10static const char rcsid[] = "@(#)$Id: ip.c,v 2.8.2.2 2007/02/17 12:41:51 darrenr Exp $";
10static const char rcsid[] = "@(#)$Id$";
11#endif
12#include <sys/param.h>
13#include <sys/types.h>
14#include <netinet/in_systm.h>
15#include <sys/socket.h>
11#endif
12#include <sys/param.h>
13#include <sys/types.h>
14#include <netinet/in_systm.h>
15#include <sys/socket.h>
16#ifdef __osf__
17# include "radix_ipf_local.h"
18#endif
19#include <net/if.h>
20#include <netinet/in.h>
21#include <netinet/ip.h>
22#include <sys/param.h>
23#ifndef linux
16#include <net/if.h>
17#include <netinet/in.h>
18#include <netinet/ip.h>
19#include <sys/param.h>
20#ifndef linux
21# include <net/route.h>
24# include <netinet/if_ether.h>
25# include <netinet/ip_var.h>
26# if __FreeBSD_version >= 300000
27# include <net/if_var.h>
28# endif
29#endif
30#include <errno.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <string.h>
35#include "ipsend.h"
36
37
38static char *ipbuf = NULL, *ethbuf = NULL;
39
40
41u_short chksum(buf,len)
22# include <netinet/if_ether.h>
23# include <netinet/ip_var.h>
24# if __FreeBSD_version >= 300000
25# include <net/if_var.h>
26# endif
27#endif
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33#include "ipsend.h"
34
35
36static char *ipbuf = NULL, *ethbuf = NULL;
37
38
39u_short chksum(buf,len)
42u_short *buf;
43int len;
40 u_short *buf;
41 int len;
44{
45 u_long sum = 0;
46 int nwords = len >> 1;
47
48 for(; nwords > 0; nwords--)
49 sum += *buf++;
50 sum = (sum>>16) + (sum & 0xffff);
51 sum += (sum >>16);
52 return (~sum);
53}
54
55
56int send_ether(nfd, buf, len, gwip)
42{
43 u_long sum = 0;
44 int nwords = len >> 1;
45
46 for(; nwords > 0; nwords--)
47 sum += *buf++;
48 sum = (sum>>16) + (sum & 0xffff);
49 sum += (sum >>16);
50 return (~sum);
51}
52
53
54int send_ether(nfd, buf, len, gwip)
57int nfd, len;
58char *buf;
59struct in_addr gwip;
55 int nfd, len;
56 char *buf;
57 struct in_addr gwip;
60{
61 static struct in_addr last_gw;
62 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
63 ether_header_t *eh;
64 char *s;
65 int err;
66
67 if (!ethbuf)

--- 16 unchanged lines hidden (view full) ---

84 err = sendip(nfd, s, sizeof(*eh) + len);
85 return err;
86}
87
88
89/*
90 */
91int send_ip(nfd, mtu, ip, gwip, frag)
58{
59 static struct in_addr last_gw;
60 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
61 ether_header_t *eh;
62 char *s;
63 int err;
64
65 if (!ethbuf)

--- 16 unchanged lines hidden (view full) ---

82 err = sendip(nfd, s, sizeof(*eh) + len);
83 return err;
84}
85
86
87/*
88 */
89int send_ip(nfd, mtu, ip, gwip, frag)
92int nfd, mtu;
93ip_t *ip;
94struct in_addr gwip;
95int frag;
90 int nfd, mtu;
91 ip_t *ip;
92 struct in_addr gwip;
93 int frag;
96{
97 static struct in_addr last_gw, local_ip;
98 static char local_arp[6] = { 0, 0, 0, 0, 0, 0};
99 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
100 static u_short id = 0;
101 ether_header_t *eh;
102 ip_t ipsv;
103 int err, iplen;

--- 141 unchanged lines hidden (view full) ---

245 return err;
246}
247
248
249/*
250 * send a tcp packet.
251 */
252int send_tcp(nfd, mtu, ip, gwip)
94{
95 static struct in_addr last_gw, local_ip;
96 static char local_arp[6] = { 0, 0, 0, 0, 0, 0};
97 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
98 static u_short id = 0;
99 ether_header_t *eh;
100 ip_t ipsv;
101 int err, iplen;

--- 141 unchanged lines hidden (view full) ---

243 return err;
244}
245
246
247/*
248 * send a tcp packet.
249 */
250int send_tcp(nfd, mtu, ip, gwip)
253int nfd, mtu;
254ip_t *ip;
255struct in_addr gwip;
251 int nfd, mtu;
252 ip_t *ip;
253 struct in_addr gwip;
256{
257 static tcp_seq iss = 2;
258 tcphdr_t *t, *t2;
259 int thlen, i, iplen, hlen;
260 u_32_t lbuf[20];
261 ip_t *ip2;
262
263 iplen = ip->ip_len;

--- 34 unchanged lines hidden (view full) ---

298 return send_ip(nfd, mtu, ip, gwip, 1);
299}
300
301
302/*
303 * send a udp packet.
304 */
305int send_udp(nfd, mtu, ip, gwip)
254{
255 static tcp_seq iss = 2;
256 tcphdr_t *t, *t2;
257 int thlen, i, iplen, hlen;
258 u_32_t lbuf[20];
259 ip_t *ip2;
260
261 iplen = ip->ip_len;

--- 34 unchanged lines hidden (view full) ---

296 return send_ip(nfd, mtu, ip, gwip, 1);
297}
298
299
300/*
301 * send a udp packet.
302 */
303int send_udp(nfd, mtu, ip, gwip)
306int nfd, mtu;
307ip_t *ip;
308struct in_addr gwip;
304 int nfd, mtu;
305 ip_t *ip;
306 struct in_addr gwip;
309{
310 struct tcpiphdr *ti;
311 int thlen;
312 u_long lbuf[20];
313
314 ti = (struct tcpiphdr *)lbuf;
315 bzero((char *)ti, sizeof(*ti));
316 thlen = sizeof(udphdr_t);

--- 13 unchanged lines hidden (view full) ---

330 return send_ip(nfd, mtu, ip, gwip, 1);
331}
332
333
334/*
335 * send an icmp packet.
336 */
337int send_icmp(nfd, mtu, ip, gwip)
307{
308 struct tcpiphdr *ti;
309 int thlen;
310 u_long lbuf[20];
311
312 ti = (struct tcpiphdr *)lbuf;
313 bzero((char *)ti, sizeof(*ti));
314 thlen = sizeof(udphdr_t);

--- 13 unchanged lines hidden (view full) ---

328 return send_ip(nfd, mtu, ip, gwip, 1);
329}
330
331
332/*
333 * send an icmp packet.
334 */
335int send_icmp(nfd, mtu, ip, gwip)
338int nfd, mtu;
339ip_t *ip;
340struct in_addr gwip;
336 int nfd, mtu;
337 ip_t *ip;
338 struct in_addr gwip;
341{
342 struct icmp *ic;
343
344 ic = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
345
346 ic->icmp_cksum = 0;
347 ic->icmp_cksum = chksum((u_short *)ic, sizeof(struct icmp));
348
349 return send_ip(nfd, mtu, ip, gwip, 1);
350}
351
352
353int send_packet(nfd, mtu, ip, gwip)
339{
340 struct icmp *ic;
341
342 ic = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
343
344 ic->icmp_cksum = 0;
345 ic->icmp_cksum = chksum((u_short *)ic, sizeof(struct icmp));
346
347 return send_ip(nfd, mtu, ip, gwip, 1);
348}
349
350
351int send_packet(nfd, mtu, ip, gwip)
354int nfd, mtu;
355ip_t *ip;
356struct in_addr gwip;
352 int nfd, mtu;
353 ip_t *ip;
354 struct in_addr gwip;
357{
358 switch (ip->ip_p)
359 {
360 case IPPROTO_TCP :
361 return send_tcp(nfd, mtu, ip, gwip);
362 case IPPROTO_UDP :
363 return send_udp(nfd, mtu, ip, gwip);
364 case IPPROTO_ICMP :
365 return send_icmp(nfd, mtu, ip, gwip);
366 default :
367 return send_ip(nfd, mtu, ip, gwip, 1);
368 }
369}
355{
356 switch (ip->ip_p)
357 {
358 case IPPROTO_TCP :
359 return send_tcp(nfd, mtu, ip, gwip);
360 case IPPROTO_UDP :
361 return send_udp(nfd, mtu, ip, gwip);
362 case IPPROTO_ICMP :
363 return send_icmp(nfd, mtu, ip, gwip);
364 default :
365 return send_ip(nfd, mtu, ip, gwip, 1);
366 }
367}