summaryrefslogtreecommitdiff
path: root/usr.sbin/ipftest/ipft_td.c
blob: 6ce649dea8adf214c5bb9c9a3462ba0923fbe45f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * (C)opyright 1993,1994,1995 by Darren Reed.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and due credit is given
 * to the original author and the contributors.
 */

/*
tcpdump -n

00:05:47.816843 128.231.76.76.3291 > 224.2.252.231.36573: udp 36 (encap)

tcpdump -nq

00:33:48.410771 192.73.213.11.1463 > 224.2.248.153.59360: udp 31 (encap)

tcpdump -nqt

128.250.133.13.23 > 128.250.20.20.2419: tcp 27

tcpdump -nqtt

123456789.1234567 128.250.133.13.23 > 128.250.20.20.2419: tcp 27

tcpdump -nqte

8:0:20:f:65:f7 0:0:c:1:8a:c5 81: 128.250.133.13.23 > 128.250.20.20.2419: tcp 27

*/
#include <stdio.h>
#include <string.h>
#if !defined(__SVR4) && !defined(__GNUC__)
#include <strings.h>
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip_var.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcpip.h>
#include <net/if.h>
#include <netdb.h>
#include "ip_fil.h"
#include "ipf.h"
#include "ipt.h"

#ifndef lint
static char sccsid[] = "@(#)ipft_td.c	1.8 2/4/96 (C)1995 Darren Reed";
static	char	rcsid[] = "$Id: ipft_td.c,v 1.4 1996/07/18 04:59:24 dm Exp $";
#endif

static	int	tcpd_open(), tcpd_close(), tcpd_readip();
#ifdef	NEED_INET_ATON
extern	u_long	inet_aton();
#else
#include <arpa/inet.h>
#endif

struct	ipread	tcpd = { tcpd_open, tcpd_close, tcpd_readip };

static	FILE	*tfp = NULL;
static	int	tfd = -1;


static	int	tcpd_open(fname)
char	*fname;
{
	if (tfd != -1)
		return tfd;

	if (!strcmp(fname, "-")) {
		tfd = 0;
		tfp = stdin;
	} else {
		tfd = open(fname, O_RDONLY);
		tfp = fdopen(tfd, "r");
	}
	return tfd;
}


static	int	tcpd_close()
{
	(void) fclose(tfp);
	return close(tfd);
}


static	int	count_dots(str)
char	*str;
{
	int	i = 0;

	while (*str)
		if (*str++ == '.')
			i++;
	return i;
}


static	int	tcpd_readip(buf, cnt, ifn, dir)
char	*buf, **ifn;
int	cnt, *dir;
{
	struct	tcpiphdr pkt;
	struct	ip	*ip = (struct ip *)&pkt;
	struct	protoent *p;
	char	src[32], dst[32], misc[256], time[32], link1[32], link2[32];
	char	lbuf[160], *s;
	int	n, dots, slen, extra = 0;

	if (!fgets(lbuf, sizeof(lbuf) - 1, tfp))
		return 0;

	if ((s = strchr(lbuf, '\n')))
		*s = '\0';
	lbuf[sizeof(lbuf)-1] = '\0';

	bzero(&pkt, sizeof(pkt));

	if ((n = sscanf(lbuf, "%s > %s: %s", src, dst, misc)) != 3)
		if ((n = sscanf(lbuf, "%s %s > %s: %s",
				time, src, dst, misc)) != 4)
			if ((n = sscanf(lbuf, "%s %s: %s > %s: %s",
					link1, link2, src, dst, misc)) != 5) {
				n = sscanf(lbuf, "%s %s %s: %s > %s: %s",
					   time, link1, link2, src, dst, misc);
				if (n != 6)
					return -1;
			}

	if ((dots = count_dots(dst)) == 4) {
		s = strrchr(src, '.');
		*s++ = '\0';
		(void) inet_aton(src, &ip->ip_src);
		pkt.ti_sport = htons(atoi(s));
		*--s = '.';
		s = strrchr(dst, '.');
	
		*s++ = '\0';
		(void) inet_aton(src, &ip->ip_dst);
		pkt.ti_dport = htons(atoi(s));
		*--s = '.';
	
	} else {
		(void) inet_aton(src, &ip->ip_src);
		(void) inet_aton(src, &ip->ip_dst);
	}
	ip->ip_len = ip->ip_hl = sizeof(struct ip);

	s = strtok(misc, " :");
	if ((p = getprotobyname(s))) {
		ip->ip_p = p->p_proto;

		switch (p->p_proto) {
		case IPPROTO_TCP :
		case IPPROTO_UDP :
			s = strtok(NULL, " :");
			ip->ip_len += atoi(s);
			if (p->p_proto == IPPROTO_TCP)
				extra = sizeof(struct tcphdr);
			else if (p->p_proto == IPPROTO_UDP)
				extra = sizeof(struct udphdr);
			break;
#ifdef	IGMP
		case IPPROTO_IGMP :
			extra = sizeof(struct igmp);
			break;
#endif
		case IPPROTO_ICMP :
			extra = sizeof(struct icmp);
			break;
		default :
			break;
		}
	}
	slen = ip->ip_hl + extra + ip->ip_len;
	return slen;
}