]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ipfilter/lib/ipft_ef.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ipfilter / lib / ipft_ef.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2000-2006 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id: ipft_ef.c,v 1.14.2.2 2006/06/16 17:21:02 darrenr Exp $
9  */
10
11 /*
12                                             icmp type
13  lnth proto         source     destination   src port   dst port
14
15 etherfind -n
16
17    60  tcp   128.250.20.20  128.250.133.13       2419     telnet
18
19 etherfind -n -t
20
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
23 */
24
25 #include "ipf.h"
26 #include "ipt.h"
27
28 #ifndef linux
29 #include <netinet/ip_var.h>
30 #endif
31 #include <netinet/tcpip.h>
32
33
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
38
39 static  int     etherf_open __P((char *));
40 static  int     etherf_close __P((void));
41 static  int     etherf_readip __P((char *, int, char **, int *));
42
43 struct  ipread  etherf = { etherf_open, etherf_close, etherf_readip, 0 };
44
45 static  FILE    *efp = NULL;
46 static  int     efd = -1;
47
48
49 static  int     etherf_open(fname)
50 char    *fname;
51 {
52         if (efd != -1)
53                 return efd;
54
55         if (!strcmp(fname, "-")) {
56                 efd = 0;
57                 efp = stdin;
58         } else {
59                 efd = open(fname, O_RDONLY);
60                 efp = fdopen(efd, "r");
61         }
62         return efd;
63 }
64
65
66 static  int     etherf_close()
67 {
68         return close(efd);
69 }
70
71
72 static  int     etherf_readip(buf, cnt, ifn, dir)
73 char    *buf, **ifn;
74 int     cnt, *dir;
75 {
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;
81
82         if (!fgets(lbuf, sizeof(lbuf) - 1, efp))
83                 return 0;
84
85         if ((s = strchr(lbuf, '\n')))
86                 *s = '\0';
87         lbuf[sizeof(lbuf)-1] = '\0';
88
89         bzero(&pkt, sizeof(pkt));
90
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;
96
97         ip->ip_p = getproto(prot);
98
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;
124         }
125
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));
130
131         slen = IP_HL(ip) + extra;
132         i = MIN(cnt, slen);
133         bcopy((char *)&pkt, buf, i);
134         return i;
135 }