]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ipft_td.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ipft_td.c
1 /*
2  * Copyright (C) 1993-1998 by Darren Reed.
3  *
4  * Redistribution and use in source and binary forms are permitted
5  * provided that this notice is preserved and due credit is given
6  * to the original author and the contributors.
7  */
8
9 /*
10 tcpdump -n
11
12 00:05:47.816843 128.231.76.76.3291 > 224.2.252.231.36573: udp 36 (encap)
13
14 tcpdump -nq
15
16 00:33:48.410771 192.73.213.11.1463 > 224.2.248.153.59360: udp 31 (encap)
17
18 tcpdump -nqt
19
20 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
21
22 tcpdump -nqtt
23
24 123456789.1234567 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
25
26 tcpdump -nqte
27
28 8:0:20:f:65:f7 0:0:c:1:8a:c5 81: 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
29
30 */
31 #include <stdio.h>
32 #include <string.h>
33 #if !defined(__SVR4) && !defined(__GNUC__)
34 #include <strings.h>
35 #endif
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <stddef.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <netinet/in_systm.h>
47 #ifndef linux
48 #include <netinet/ip_var.h>
49 #endif
50 #include <netinet/ip.h>
51 #include <netinet/tcp.h>
52 #include <netinet/udp.h>
53 #include <netinet/ip_icmp.h>
54 #include <net/if.h>
55 #include <netdb.h>
56 #include "ip_compat.h"
57 #include <netinet/tcpip.h>
58 #include "ipf.h"
59 #include "ipt.h"
60
61 #if !defined(lint)
62 static const char sccsid[] = "@(#)ipft_td.c     1.8 2/4/96 (C)1995 Darren Reed";
63 static const char rcsid[] = "@(#)$Id: ipft_td.c,v 2.1 1999/08/04 17:30:04 darrenr Exp $";
64 #endif
65
66 static  int     tcpd_open __P((char *));
67 static  int     tcpd_close __P((void));
68 static  int     tcpd_readip __P((char *, int, char **, int *));
69 static  int     count_dots __P((char *));
70
71 struct  ipread  tcpd = { tcpd_open, tcpd_close, tcpd_readip };
72
73 static  FILE    *tfp = NULL;
74 static  int     tfd = -1;
75
76
77 static  int     tcpd_open(fname)
78 char    *fname;
79 {
80         if (tfd != -1)
81                 return tfd;
82
83         if (!strcmp(fname, "-")) {
84                 tfd = 0;
85                 tfp = stdin;
86         } else {
87                 tfd = open(fname, O_RDONLY);
88                 tfp = fdopen(tfd, "r");
89         }
90         return tfd;
91 }
92
93
94 static  int     tcpd_close()
95 {
96         (void) fclose(tfp);
97         return close(tfd);
98 }
99
100
101 static  int     count_dots(str)
102 char    *str;
103 {
104         int     i = 0;
105
106         while (*str)
107                 if (*str++ == '.')
108                         i++;
109         return i;
110 }
111
112
113 static  int     tcpd_readip(buf, cnt, ifn, dir)
114 char    *buf, **ifn;
115 int     cnt, *dir;
116 {
117         struct  tcpiphdr pkt;
118         ip_t    *ip = (ip_t *)&pkt;
119         struct  protoent *p;
120         char    src[32], dst[32], misc[256], time[32], link1[32], link2[32];
121         char    lbuf[160], *s;
122         int     n, dots, slen, extra = 0;
123
124         if (!fgets(lbuf, sizeof(lbuf) - 1, tfp))
125                 return 0;
126
127         if ((s = strchr(lbuf, '\n')))
128                 *s = '\0';
129         lbuf[sizeof(lbuf)-1] = '\0';
130
131         bzero(&pkt, sizeof(pkt));
132
133         if ((n = sscanf(lbuf, "%s > %s: %s", src, dst, misc)) != 3)
134                 if ((n = sscanf(lbuf, "%s %s > %s: %s",
135                                 time, src, dst, misc)) != 4)
136                         if ((n = sscanf(lbuf, "%s %s: %s > %s: %s",
137                                         link1, link2, src, dst, misc)) != 5) {
138                                 n = sscanf(lbuf, "%s %s %s: %s > %s: %s",
139                                            time, link1, link2, src, dst, misc);
140                                 if (n != 6)
141                                         return -1;
142                         }
143
144         if ((dots = count_dots(dst)) == 4) {
145                 s = strrchr(src, '.');
146                 *s++ = '\0';
147                 (void) inet_aton(src, &ip->ip_src);
148                 pkt.ti_sport = htons(atoi(s));
149                 *--s = '.';
150                 s = strrchr(dst, '.');
151         
152                 *s++ = '\0';
153                 (void) inet_aton(src, &ip->ip_dst);
154                 pkt.ti_dport = htons(atoi(s));
155                 *--s = '.';
156         
157         } else {
158                 (void) inet_aton(src, &ip->ip_src);
159                 (void) inet_aton(src, &ip->ip_dst);
160         }
161         ip->ip_len = ip->ip_hl = sizeof(ip_t);
162
163         s = strtok(misc, " :");
164         if ((p = getprotobyname(s))) {
165                 ip->ip_p = p->p_proto;
166
167                 switch (p->p_proto) {
168                 case IPPROTO_TCP :
169                 case IPPROTO_UDP :
170                         s = strtok(NULL, " :");
171                         ip->ip_len += atoi(s);
172                         if (p->p_proto == IPPROTO_TCP)
173                                 extra = sizeof(struct tcphdr);
174                         else if (p->p_proto == IPPROTO_UDP)
175                                 extra = sizeof(struct udphdr);
176                         break;
177 #ifdef  IGMP
178                 case IPPROTO_IGMP :
179                         extra = sizeof(struct igmp);
180                         break;
181 #endif
182                 case IPPROTO_ICMP :
183                         extra = sizeof(struct icmp);
184                         break;
185                 default :
186                         break;
187                 }
188         }
189         slen = ip->ip_hl + extra + ip->ip_len;
190         return slen;
191 }