]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/ipft_ef.c
This commit was generated by cvs2svn to compensate for changes in r164146,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / ipft_ef.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 1993-2001 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id: ipft_ef.c,v 1.14 2004/01/08 13:34:31 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 2004/01/08 13:34:31 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         case IPPROTO_UDP :
102                 s = strtok(NULL, " :");
103                 ip->ip_len += atoi(s);
104                 if (ip->ip_p == IPPROTO_TCP)
105                         extra = sizeof(struct tcphdr);
106                 else if (ip->ip_p == IPPROTO_UDP)
107                         extra = sizeof(struct udphdr);
108                 break;
109 #ifdef  IGMP
110         case IPPROTO_IGMP :
111                 extra = sizeof(struct igmp);
112                 break;
113 #endif
114         case IPPROTO_ICMP :
115                 extra = sizeof(struct icmp);
116                 break;
117         default :
118                 break;
119         }
120
121         (void) inet_aton(src, &ip->ip_src);
122         (void) inet_aton(dst, &ip->ip_dst);
123         ip->ip_len = atoi(len);
124         IP_HL_A(ip, sizeof(ip_t));
125
126         slen = IP_HL(ip) + extra;
127         i = MIN(cnt, slen);
128         bcopy((char *)&pkt, buf, i);
129         return i;
130 }