xref: /titanic_41/usr/src/cmd/ipf/lib/common/ipft_ef.c (revision eb0cc229f19c437a6b538d3ac0d0443268290b7e)
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: ipft_ef.c,v 1.14 2004/01/08 13:34:31 darrenr Exp $
7  */
8 
9 /*
10                                             icmp type
11  lnth proto         source     destination   src port   dst port
12 
13 etherfind -n
14 
15    60  tcp   128.250.20.20  128.250.133.13       2419     telnet
16 
17 etherfind -n -t
18 
19  0.32    91   04    131.170.1.10  128.250.133.13
20  0.33   566  udp  128.250.37.155   128.250.133.3        901        901
21 */
22 
23 #include "ipf.h"
24 #include "ipt.h"
25 
26 #ifndef linux
27 #include <netinet/ip_var.h>
28 #endif
29 #include <netinet/tcpip.h>
30 
31 
32 #if !defined(lint)
33 static const char sccsid[] = "@(#)ipft_ef.c	1.6 2/4/96 (C)1995 Darren Reed";
34 static const char rcsid[] = "@(#)$Id: ipft_ef.c,v 1.14 2004/01/08 13:34:31 darrenr Exp $";
35 #endif
36 
37 static	int	etherf_open __P((char *));
38 static	int	etherf_close __P((void));
39 static	int	etherf_readip __P((char *, int, char **, int *));
40 
41 struct	ipread	etherf = { etherf_open, etherf_close, etherf_readip, 0 };
42 
43 static	FILE	*efp = NULL;
44 static	int	efd = -1;
45 
46 
47 static	int	etherf_open(fname)
48 char	*fname;
49 {
50 	if (efd != -1)
51 		return efd;
52 
53 	if (!strcmp(fname, "-")) {
54 		efd = 0;
55 		efp = stdin;
56 	} else {
57 		efd = open(fname, O_RDONLY);
58 		efp = fdopen(efd, "r");
59 	}
60 	return efd;
61 }
62 
63 
64 static	int	etherf_close()
65 {
66 	return close(efd);
67 }
68 
69 
70 static	int	etherf_readip(buf, cnt, ifn, dir)
71 char	*buf, **ifn;
72 int	cnt, *dir;
73 {
74 	struct	tcpiphdr pkt;
75 	ip_t	*ip = (ip_t *)&pkt;
76 	char	src[16], dst[16], sprt[16], dprt[16];
77 	char	lbuf[128], len[8], prot[8], time[8], *s;
78 	int	slen, extra = 0, i;
79 
80 	if (!fgets(lbuf, sizeof(lbuf) - 1, efp))
81 		return 0;
82 
83 	if ((s = strchr(lbuf, '\n')))
84 		*s = '\0';
85 	lbuf[sizeof(lbuf)-1] = '\0';
86 
87 	bzero(&pkt, sizeof(pkt));
88 
89 	if (sscanf(lbuf, "%7s %7s %15s %15s %15s %15s", len, prot, src, dst,
90 		   sprt, dprt) != 6)
91 		if (sscanf(lbuf, "%7s %7s %7s %15s %15s %15s %15s", time,
92 			   len, prot, src, dst, sprt, dprt) != 7)
93 			return -1;
94 
95 	ip->ip_p = getproto(prot);
96 
97 	switch (ip->ip_p) {
98 	case IPPROTO_TCP :
99 	case IPPROTO_UDP :
100 		s = strtok(NULL, " :");
101 		ip->ip_len += atoi(s);
102 		if (ip->ip_p == IPPROTO_TCP)
103 			extra = sizeof(struct tcphdr);
104 		else if (ip->ip_p == IPPROTO_UDP)
105 			extra = sizeof(struct udphdr);
106 		break;
107 #ifdef	IGMP
108 	case IPPROTO_IGMP :
109 		extra = sizeof(struct igmp);
110 		break;
111 #endif
112 	case IPPROTO_ICMP :
113 		extra = sizeof(struct icmp);
114 		break;
115 	default :
116 		break;
117 	}
118 
119 	(void) inet_aton(src, &ip->ip_src);
120 	(void) inet_aton(dst, &ip->ip_dst);
121 	ip->ip_len = atoi(len);
122 	IP_HL_A(ip, sizeof(ip_t));
123 
124 	slen = IP_HL(ip) + extra;
125 	i = MIN(cnt, slen);
126 	bcopy((char *)&pkt, buf, i);
127 	return i;
128 }
129