Sync usage with man page.
[netbsd-mini2440.git] / dist / ipf / lib / ipft_ef.c
blobb328d60c511b44c1c51b6bf0cab122b3105a22be
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2000-2006 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Id: ipft_ef.c,v 1.14.2.2 2006/06/16 17:21:02 darrenr Exp
9 */
12 icmp type
13 lnth proto source destination src port dst port
15 etherfind -n
17 60 tcp 128.250.20.20 128.250.133.13 2419 telnet
19 etherfind -n -t
21 0.32 91 04 131.170.1.10 128.250.133.13
22 0.33 566 udp 128.250.37.155 128.250.133.3 901 901
25 #include "ipf.h"
26 #include "ipt.h"
28 #ifndef linux
29 #include <netinet/ip_var.h>
30 #endif
31 #include <netinet/tcpip.h>
34 #if !defined(lint)
35 static const char sccsid[] = "@(#)ipft_ef.c 1.6 2/4/96 (C)1995 Darren Reed";
36 static const char rcsid[] = "@(#)Id: ipft_ef.c,v 1.14.2.2 2006/06/16 17:21:02 darrenr Exp";
37 #endif
39 static int etherf_open __P((char *));
40 static int etherf_close __P((void));
41 static int etherf_readip __P((char *, int, char **, int *));
43 struct ipread etherf = { etherf_open, etherf_close, etherf_readip, 0 };
45 static FILE *efp = NULL;
46 static int efd = -1;
49 static int etherf_open(fname)
50 char *fname;
52 if (efd != -1)
53 return efd;
55 if (!strcmp(fname, "-")) {
56 efd = 0;
57 efp = stdin;
58 } else {
59 efd = open(fname, O_RDONLY);
60 efp = fdopen(efd, "r");
62 return efd;
66 static int etherf_close()
68 return close(efd);
72 static int etherf_readip(buf, cnt, ifn, dir)
73 char *buf, **ifn;
74 int cnt, *dir;
76 struct tcpiphdr pkt;
77 ip_t *ip = (ip_t *)&pkt;
78 char src[16], dst[16], sprt[16], dprt[16];
79 char lbuf[128], len[8], prot[8], time[8], *s;
80 int slen, extra = 0, i;
82 if (!fgets(lbuf, sizeof(lbuf) - 1, efp))
83 return 0;
85 if ((s = strchr(lbuf, '\n')))
86 *s = '\0';
87 lbuf[sizeof(lbuf)-1] = '\0';
89 bzero(&pkt, sizeof(pkt));
91 if (sscanf(lbuf, "%7s %7s %15s %15s %15s %15s", len, prot, src, dst,
92 sprt, dprt) != 6)
93 if (sscanf(lbuf, "%7s %7s %7s %15s %15s %15s %15s", time,
94 len, prot, src, dst, sprt, dprt) != 7)
95 return -1;
97 ip->ip_p = getproto(prot);
99 switch (ip->ip_p) {
100 case IPPROTO_TCP :
101 if (isdigit(*sprt))
102 pkt.ti_sport = htons(atoi(sprt) & 65535);
103 if (isdigit(*dprt))
104 pkt.ti_dport = htons(atoi(dprt) & 65535);
105 extra = sizeof(struct tcphdr);
106 break;
107 case IPPROTO_UDP :
108 if (isdigit(*sprt))
109 pkt.ti_sport = htons(atoi(sprt) & 65535);
110 if (isdigit(*dprt))
111 pkt.ti_dport = htons(atoi(dprt) & 65535);
112 extra = sizeof(struct udphdr);
113 break;
114 #ifdef IGMP
115 case IPPROTO_IGMP :
116 extra = sizeof(struct igmp);
117 break;
118 #endif
119 case IPPROTO_ICMP :
120 extra = sizeof(struct icmp);
121 break;
122 default :
123 break;
126 (void) inet_aton(src, &ip->ip_src);
127 (void) inet_aton(dst, &ip->ip_dst);
128 ip->ip_len = atoi(len);
129 IP_HL_A(ip, sizeof(ip_t));
131 slen = IP_HL(ip) + extra;
132 i = MIN(cnt, slen);
133 bcopy((char *)&pkt, buf, i);
134 return i;