xref: /titanic_51/usr/src/cmd/ipf/lib/common/ipft_ef.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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.11 2003/06/02 12:22:49 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 #undef	ICMP_MAXTYPE
27 #include <netinet/ip_icmp.h>
28 #include <netinet/ip_var.h>
29 #include <netinet/udp.h>
30 #include <netinet/tcpip.h>
31 
32 
33 #if !defined(lint)
34 static const char sccsid[] = "@(#)ipft_ef.c	1.6 2/4/96 (C)1995 Darren Reed";
35 static const char rcsid[] = "@(#)$Id: ipft_ef.c,v 1.11 2003/06/02 12:22:49 darrenr Exp $";
36 #endif
37 
38 static	int	etherf_open __P((char *));
39 static	int	etherf_close __P((void));
40 static	int	etherf_readip __P((char *, int, char **, int *));
41 
42 struct	ipread	etherf = { etherf_open, etherf_close, etherf_readip, 0 };
43 
44 static	FILE	*efp = NULL;
45 static	int	efd = -1;
46 
47 
48 static	int	etherf_open(fname)
49 char	*fname;
50 {
51 	if (efd != -1)
52 		return efd;
53 
54 	if (!strcmp(fname, "-")) {
55 		efd = 0;
56 		efp = stdin;
57 	} else {
58 		efd = open(fname, O_RDONLY);
59 		efp = fdopen(efd, "r");
60 	}
61 	return efd;
62 }
63 
64 
65 static	int	etherf_close()
66 {
67 	return close(efd);
68 }
69 
70 
71 static	int	etherf_readip(buf, cnt, ifn, dir)
72 char	*buf, **ifn;
73 int	cnt, *dir;
74 {
75 	struct	tcpiphdr pkt;
76 	ip_t	*ip = (ip_t *)&pkt;
77 	char	src[16], dst[16], sprt[16], dprt[16];
78 	char	lbuf[128], len[8], prot[8], time[8], *s;
79 	int	slen, extra = 0, i;
80 
81 	if (!fgets(lbuf, sizeof(lbuf) - 1, efp))
82 		return 0;
83 
84 	if ((s = strchr(lbuf, '\n')))
85 		*s = '\0';
86 	lbuf[sizeof(lbuf)-1] = '\0';
87 
88 	bzero(&pkt, sizeof(pkt));
89 
90 	if (sscanf(lbuf, "%7s %7s %15s %15s %15s %15s", len, prot, src, dst,
91 		   sprt, dprt) != 6)
92 		if (sscanf(lbuf, "%7s %7s %7s %15s %15s %15s %15s", time,
93 			   len, prot, src, dst, sprt, dprt) != 7)
94 			return -1;
95 
96 	ip->ip_p = getproto(prot);
97 
98 	switch (ip->ip_p) {
99 	case IPPROTO_TCP :
100 	case IPPROTO_UDP :
101 		s = strtok(NULL, " :");
102 		ip->ip_len += atoi(s);
103 		if (ip->ip_p == IPPROTO_TCP)
104 			extra = sizeof(struct tcphdr);
105 		else if (ip->ip_p == IPPROTO_UDP)
106 			extra = sizeof(struct udphdr);
107 		break;
108 #ifdef	IGMP
109 	case IPPROTO_IGMP :
110 		extra = sizeof(struct igmp);
111 		break;
112 #endif
113 	case IPPROTO_ICMP :
114 		extra = sizeof(struct icmp);
115 		break;
116 	default :
117 		break;
118 	}
119 
120 	(void) inet_aton(src, &ip->ip_src);
121 	(void) inet_aton(dst, &ip->ip_dst);
122 	ip->ip_len = atoi(len);
123 	IP_HL_A(ip, sizeof(ip_t));
124 
125 	slen = IP_HL(ip) + extra;
126 	i = MIN(cnt, slen);
127 	bcopy((char *)&pkt, buf, i);
128 	return i;
129 }
130