Move /var/svc/log to /var/log/svc
[unleashed/lotheac.git] / usr / src / cmd / ipf / lib / common / ipft_td.c
blob405f059e95b526ed4ebf155da71a69ec822b0572
1 /*
2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * $Id: ipft_td.c,v 1.15 2004/01/08 13:34:31 darrenr Exp $
7 */
9 /*
10 tcpdump -n
12 00:05:47.816843 128.231.76.76.3291 > 224.2.252.231.36573: udp 36 (encap)
14 tcpdump -nq
16 00:33:48.410771 192.73.213.11.1463 > 224.2.248.153.59360: udp 31 (encap)
18 tcpdump -nqt
20 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
22 tcpdump -nqtt
24 123456789.1234567 128.250.133.13.23 > 128.250.20.20.2419: tcp 27
26 tcpdump -nqte
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
32 #include "ipf.h"
33 #include "ipt.h"
35 #ifndef linux
36 #include <netinet/ip_var.h>
37 #endif
38 #include <netinet/tcpip.h>
41 static const char sccsid[] = "@(#)ipft_td.c 1.8 2/4/96 (C)1995 Darren Reed";
42 static const char rcsid[] = "@(#)$Id: ipft_td.c,v 1.15 2004/01/08 13:34:31 darrenr Exp $";
44 static int tcpd_open __P((char *));
45 static int tcpd_close __P((void));
46 static int tcpd_readip __P((char *, int, char **, int *));
47 static int count_dots __P((char *));
49 struct ipread tcpd = { tcpd_open, tcpd_close, tcpd_readip, 0 };
51 static FILE *tfp = NULL;
52 static int tfd = -1;
55 static int tcpd_open(fname)
56 char *fname;
58 if (tfd != -1)
59 return tfd;
61 if (!strcmp(fname, "-")) {
62 tfd = 0;
63 tfp = stdin;
64 } else {
65 tfd = open(fname, O_RDONLY);
66 tfp = fdopen(tfd, "r");
68 return tfd;
72 static int tcpd_close()
74 (void) fclose(tfp);
75 return close(tfd);
79 static int count_dots(str)
80 char *str;
82 int i = 0;
84 while (*str)
85 if (*str++ == '.')
86 i++;
87 return i;
91 static int tcpd_readip(buf, cnt, ifn, dir)
92 char *buf, **ifn;
93 int cnt, *dir;
95 struct tcpiphdr pkt;
96 ip_t *ip = (ip_t *)&pkt;
97 char src[32], dst[32], misc[256], time[32], link1[32], link2[32];
98 char lbuf[160], *s;
99 int n, slen, extra = 0;
101 if (!fgets(lbuf, sizeof(lbuf) - 1, tfp))
102 return 0;
104 if ((s = strchr(lbuf, '\n')))
105 *s = '\0';
106 lbuf[sizeof(lbuf)-1] = '\0';
108 bzero(&pkt, sizeof(pkt));
110 if ((n = sscanf(lbuf, "%31s > %31s: %255s", src, dst, misc)) != 3)
111 if ((n = sscanf(lbuf, "%31s %31s > %31s: %255s",
112 time, src, dst, misc)) != 4)
113 if ((n = sscanf(lbuf, "%31s %31s: %31s > %31s: %255s",
114 link1, link2, src, dst, misc)) != 5) {
115 n = sscanf(lbuf,
116 "%31s %31s %31s: %31s > %31s: %255s",
117 time, link1, link2, src, dst, misc);
118 if (n != 6)
119 return -1;
122 if (count_dots(dst) == 4) {
123 s = strrchr(src, '.');
124 *s++ = '\0';
125 (void) inet_aton(src, &ip->ip_src);
126 pkt.ti_sport = htons(atoi(s));
127 *--s = '.';
128 s = strrchr(dst, '.');
130 *s++ = '\0';
131 (void) inet_aton(src, &ip->ip_dst);
132 pkt.ti_dport = htons(atoi(s));
133 *--s = '.';
135 } else {
136 (void) inet_aton(src, &ip->ip_src);
137 (void) inet_aton(src, &ip->ip_dst);
139 ip->ip_len = sizeof(ip_t);
140 IP_HL_A(ip, sizeof(ip_t));
142 s = strtok(misc, " :");
143 ip->ip_p = getproto(s);
145 switch (ip->ip_p)
147 case IPPROTO_TCP :
148 case IPPROTO_UDP :
149 s = strtok(NULL, " :");
150 ip->ip_len += atoi(s);
151 if (ip->ip_p == IPPROTO_TCP)
152 extra = sizeof(struct tcphdr);
153 else if (ip->ip_p == IPPROTO_UDP)
154 extra = sizeof(struct udphdr);
155 break;
156 #ifdef IGMP
157 case IPPROTO_IGMP :
158 extra = sizeof(struct igmp);
159 break;
160 #endif
161 case IPPROTO_ICMP :
162 extra = sizeof(struct icmp);
163 break;
164 default :
165 break;
168 slen = IP_HL(ip) + extra + ip->ip_len;
169 return slen;