4 * Copyright (C) 1995-2001 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * $Id: ipft_tx.c,v 1.15.2.7 2005/12/18 14:53:39 darrenr Exp $
11 static const char sccsid[] = "@(#)ipft_tx.c 1.7 6/5/96 (C) 1993 Darren Reed";
12 static const char rcsid[] = "@(#)$Id: ipft_tx.c,v 1.15.2.7 2005/12/18 14:53:39 darrenr Exp $";
21 #include <netinet/ip_var.h>
23 #include <netinet/tcpip.h>
28 static char *tx_proto = "";
30 static int text_open __P((char *)), text_close __P((void));
31 static int text_readip __P((char *, int, char **, int *));
32 static int parseline __P((char *, ip_t *, char **, int *));
34 static char myflagset[] = "FSRPAUEC";
35 static u_char myflags[] = { TH_FIN, TH_SYN, TH_RST, TH_PUSH,
36 TH_ACK, TH_URG, TH_ECN, TH_CWR };
38 struct ipread iptext = { text_open, text_close, text_readip, R_DO_CKSUM };
39 static FILE *tfp = NULL;
42 static u_32_t tx_hostnum __P((char *, int *));
43 static u_short tx_portnum __P((char *));
47 * returns an ip address as a long var as a result of either a DNS lookup or
48 * straight inet_addr() call
50 static u_32_t tx_hostnum(host, resolved)
57 if (!strcasecmp("any", host))
60 return inet_addr(host);
62 if (gethost(host, &ipa) == -1) {
64 fprintf(stderr, "can't resolve hostname: %s\n", host);
72 * find the port number given by the name, either from getservbyname() or
75 static u_short tx_portnum(name)
81 return (u_short)atoi(name);
82 sp = getservbyname(name, tx_proto);
84 return ntohs(sp->s_port);
85 (void) fprintf(stderr, "unknown service \"%s\".\n", name);
90 char *tx_icmptypes[] = {
91 "echorep", (char *)NULL, (char *)NULL, "unreach", "squench",
92 "redir", (char *)NULL, (char *)NULL, "echo", "routerad",
93 "routersol", "timex", "paramprob", "timest", "timestrep",
94 "inforeq", "inforep", "maskreq", "maskrep", "END"
97 static int text_open(fname)
100 if (tfp && tfd != -1) {
105 if (!strcmp(fname, "-")) {
109 tfd = open(fname, O_RDONLY);
111 tfp = fdopen(tfd, "r");
117 static int text_close()
126 static int text_readip(buf, cnt, ifn, dir)
134 while (fgets(line, sizeof(line)-1, tfp)) {
135 if ((s = strchr(line, '\n')))
137 if ((s = strchr(line, '\r')))
139 if ((s = strchr(line, '#')))
143 if ((opts & OPT_DEBUG) != 0)
144 printf("input: %s\n", line);
147 if (!parseline(line, (ip_t *)buf, ifn, dir))
149 return sizeof(ip_t) + sizeof(tcphdr_t);
159 static int parseline(line, ip, ifn, out)
165 tcphdr_t th, *tcp = &th;
166 struct icmp icmp, *ic = &icmp;
167 char *cps[20], **cpp, c, ipopts[68];
172 bzero((char *)ip, MAX(sizeof(*tcp), sizeof(*ic)) + sizeof(*ip));
173 bzero((char *)tcp, sizeof(*tcp));
174 bzero((char *)ic, sizeof(*ic));
175 bzero(ipopts, sizeof(ipopts));
176 IP_HL_A(ip, sizeof(*ip) >> 2);
177 IP_V_A(ip, IPVERSION);
178 for (i = 0, cps[0] = strtok(line, " \b\t\r\n"); cps[i] && i < 19; )
179 cps[++i] = strtok(NULL, " \b\t\r\n");
186 if (!ISALPHA(c) || (TOLOWER(c) != 'o' && TOLOWER(c) != 'i')) {
187 fprintf(stderr, "bad direction \"%s\"\n", *cpp);
190 *out = (TOLOWER(c) == 'o') ? 1 : 0;
195 if (!strcasecmp(*cpp, "on")) {
199 *ifn = strdup(*cpp++);
205 ip->ip_len = sizeof(ip_t);
206 if (!strcasecmp(*cpp, "tcp") || !strcasecmp(*cpp, "udp") ||
207 !strcasecmp(*cpp, "icmp")) {
209 ip->ip_p = IPPROTO_TCP;
210 ip->ip_len += sizeof(struct tcphdr);
212 } else if (c == 'u') {
213 ip->ip_p = IPPROTO_UDP;
214 ip->ip_len += sizeof(struct udphdr);
217 ip->ip_p = IPPROTO_ICMP;
218 ip->ip_len += ICMPERR_IPICMPHLEN;
222 } else if (ISDIGIT(**cpp) && !index(*cpp, '.')) {
223 ip->ip_p = atoi(*cpp);
226 ip->ip_p = IPPROTO_IP;
230 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) {
233 last = strchr(*cpp, ',');
235 fprintf(stderr, "tcp/udp with no source port\n");
239 tcp->th_sport = htons(tx_portnum(last));
240 if (ip->ip_p == IPPROTO_TCP) {
241 tcp->th_win = htons(4096);
242 TCP_OFF_A(tcp, sizeof(*tcp) >> 2);
245 ip->ip_src.s_addr = tx_hostnum(*cpp, &r);
250 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) {
253 last = strchr(*cpp, ',');
255 fprintf(stderr, "tcp/udp with no destination port\n");
259 tcp->th_dport = htons(tx_portnum(last));
261 ip->ip_dst.s_addr = tx_hostnum(*cpp, &r);
263 if (*cpp && ip->ip_p == IPPROTO_TCP) {
267 for (s = *cpp; *s; s++)
268 if ((t = strchr(myflagset, *s)))
269 tcp->th_flags |= myflags[t - myflagset];
272 if (tcp->th_flags == 0)
274 if (tcp->th_flags & TH_URG)
275 tcp->th_urp = htons(1);
276 } else if (*cpp && ip->ip_p == IPPROTO_ICMP) {
277 extern char *tx_icmptypes[];
281 t = strchr(*cpp, ',');
285 for (s = tx_icmptypes, i = 0; !*s || strcmp(*s, "END");
287 if (*s && !strcasecmp(*cpp, *s)) {
290 ic->icmp_code = atoi(t + 1);
299 if (*cpp && !strcasecmp(*cpp, "opt")) {
303 olen = buildopts(*cpp, ipopts, (IP_HL(ip) - 5) << 2);
305 bcopy(ipopts, (char *)(ip + 1), olen);
306 IP_HL_A(ip, IP_HL(ip) + (olen >> 2));
309 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
310 bcopy((char *)tcp, ((char *)ip) + (IP_HL(ip) << 2),
312 else if (ip->ip_p == IPPROTO_ICMP)
313 bcopy((char *)ic, ((char *)ip) + (IP_HL(ip) << 2),
315 ip->ip_len = htons(ip->ip_len);