summaryrefslogtreecommitdiff
path: root/usr.sbin/ipftest/ipft_ef.c
blob: fb8f67215b8c1d4f65fd9caa2bf314321e2e32cd (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
/*
 * (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.
 */

/*
                                            icmp type
 lnth proto         source     destination   src port   dst port

etherfind -n

   60  tcp   128.250.20.20  128.250.133.13       2419     telnet

etherfind -n -t

 0.32    91   04    131.170.1.10  128.250.133.13
 0.33   566  udp  128.250.37.155   128.250.133.3        901        901
*/
#include <stdio.h>
#include <string.h>
#if !defined(__SVR4) && !defined(__GNUC__)
#include <strings.h>
#endif
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/param.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 <netinet/ip_fil.h>
#include "ipf.h"
#include "ipt.h"

#ifndef lint
static char sccsid[] = "@(#)ipft_ef.c	1.5 10/15/95 (C)1995 Darren Reed";
#endif

static	int	etherf_open(), etherf_close(), etherf_readip();

struct	ipread	etherf = { etherf_open, etherf_close, etherf_readip };

static	FILE	*efp = NULL;
static	int	efd = -1;

#ifdef	NEED_INET_ATON
extern	u_long	inet_aton();
#else
#include <arpa/inet.h>
#endif

static	int	etherf_open(fname)
char	*fname;
{
	if (efd != -1)
		return efd;

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


static	int	etherf_close()
{
	return close(efd);
}


static	int	etherf_readip(buf, cnt, ifn, dir)
char	*buf, **ifn;
int	cnt, *dir;
{
	struct	tcpiphdr pkt;
	struct	ip	*ip = (struct ip *)&pkt;
	struct	protoent *p = NULL;
	char	src[16], dst[16], sprt[16], dprt[16];
	char	lbuf[128], len[8], prot[8], time[8], *s;
	int	slen, extra = 0, i, n;

	if (!fgets(lbuf, sizeof(lbuf) - 1, efp))
		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 %s %s %s", len, prot, src, dst,
			sprt, dprt)) != 6)
		if ((n = sscanf(lbuf, "%s %s %s %s %s %s %s", time,
				len, prot, src, dst, sprt, dprt)) != 7)
			return -1;

	ip->ip_p = atoi(prot);
	if (ip->ip_p == 0) {
		if (!(p = getprotobyname(prot)))
			return -1;
		ip->ip_p = p->p_proto;
	}

	switch (ip->ip_p) {
	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;
	}

#ifdef	NEED_INET_ATON
	ip->ip_src.s_addr = inet_aton(src);
	ip->ip_dst.s_addr = inet_aton(dst);
#else
	(void) inet_aton(src, &ip->ip_src);
	(void) inet_aton(dst, &ip->ip_dst);
#endif
	ip->ip_len = atoi(len);
	ip->ip_hl = sizeof(struct ip);

	slen = ip->ip_hl + extra;
	i = MIN(cnt, slen);
	bcopy((char *)&pkt, buf, i);
	return i;
}