17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (C) 1995-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate */
67c478bd9Sstevel@tonic-gate #if !defined(lint)
77c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)ipft_hx.c 1.1 3/9/96 (C) 1996 Darren Reed";
8*ab25eeb5Syz155240 static const char rcsid[] = "@(#)$Id: ipft_hx.c,v 1.11.4.1 2004/12/09 19:41:20 darrenr Exp $";
97c478bd9Sstevel@tonic-gate #endif
107c478bd9Sstevel@tonic-gate
117c478bd9Sstevel@tonic-gate #include <ctype.h>
127c478bd9Sstevel@tonic-gate
137c478bd9Sstevel@tonic-gate #include "ipf.h"
147c478bd9Sstevel@tonic-gate #include "ipt.h"
157c478bd9Sstevel@tonic-gate
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate extern int opts;
187c478bd9Sstevel@tonic-gate
197c478bd9Sstevel@tonic-gate static int hex_open __P((char *));
207c478bd9Sstevel@tonic-gate static int hex_close __P((void));
217c478bd9Sstevel@tonic-gate static int hex_readip __P((char *, int, char **, int *));
227c478bd9Sstevel@tonic-gate static char *readhex __P((char *, char *));
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate struct ipread iphex = { hex_open, hex_close, hex_readip, 0 };
257c478bd9Sstevel@tonic-gate static FILE *tfp = NULL;
267c478bd9Sstevel@tonic-gate static int tfd = -1;
277c478bd9Sstevel@tonic-gate
hex_open(fname)287c478bd9Sstevel@tonic-gate static int hex_open(fname)
297c478bd9Sstevel@tonic-gate char *fname;
307c478bd9Sstevel@tonic-gate {
317c478bd9Sstevel@tonic-gate if (tfp && tfd != -1) {
327c478bd9Sstevel@tonic-gate rewind(tfp);
337c478bd9Sstevel@tonic-gate return tfd;
347c478bd9Sstevel@tonic-gate }
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate if (!strcmp(fname, "-")) {
377c478bd9Sstevel@tonic-gate tfd = 0;
387c478bd9Sstevel@tonic-gate tfp = stdin;
397c478bd9Sstevel@tonic-gate } else {
407c478bd9Sstevel@tonic-gate tfd = open(fname, O_RDONLY);
417c478bd9Sstevel@tonic-gate if (tfd != -1)
427c478bd9Sstevel@tonic-gate tfp = fdopen(tfd, "r");
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate return tfd;
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate
hex_close()487c478bd9Sstevel@tonic-gate static int hex_close()
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate int cfd = tfd;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate tfd = -1;
537c478bd9Sstevel@tonic-gate return close(cfd);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate
hex_readip(buf,cnt,ifn,dir)577c478bd9Sstevel@tonic-gate static int hex_readip(buf, cnt, ifn, dir)
587c478bd9Sstevel@tonic-gate char *buf, **ifn;
597c478bd9Sstevel@tonic-gate int cnt, *dir;
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate register char *s, *t, *u;
627c478bd9Sstevel@tonic-gate char line[513];
637c478bd9Sstevel@tonic-gate ip_t *ip;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * interpret start of line as possibly "[ifname]" or
677c478bd9Sstevel@tonic-gate * "[in/out,ifname]".
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate if (ifn)
707c478bd9Sstevel@tonic-gate *ifn = NULL;
717c478bd9Sstevel@tonic-gate if (dir)
727c478bd9Sstevel@tonic-gate *dir = 0;
737c478bd9Sstevel@tonic-gate ip = (ip_t *)buf;
747c478bd9Sstevel@tonic-gate while (fgets(line, sizeof(line)-1, tfp)) {
757c478bd9Sstevel@tonic-gate if ((s = strchr(line, '\n'))) {
767c478bd9Sstevel@tonic-gate if (s == line)
777c478bd9Sstevel@tonic-gate return (char *)ip - buf;
787c478bd9Sstevel@tonic-gate *s = '\0';
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate if ((s = strchr(line, '#')))
817c478bd9Sstevel@tonic-gate *s = '\0';
827c478bd9Sstevel@tonic-gate if (!*line)
837c478bd9Sstevel@tonic-gate continue;
847c478bd9Sstevel@tonic-gate if (!(opts & OPT_BRIEF)) {
857c478bd9Sstevel@tonic-gate printf("input: %s", line);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate if ((*line == '[') && (s = strchr(line, ']'))) {
897c478bd9Sstevel@tonic-gate t = line + 1;
907c478bd9Sstevel@tonic-gate if (s - t > 0) {
917c478bd9Sstevel@tonic-gate *s++ = '\0';
927c478bd9Sstevel@tonic-gate if ((u = strchr(t, ',')) && (u < s)) {
937c478bd9Sstevel@tonic-gate u++;
947c478bd9Sstevel@tonic-gate if (ifn)
957c478bd9Sstevel@tonic-gate *ifn = strdup(u);
967c478bd9Sstevel@tonic-gate if (dir) {
977c478bd9Sstevel@tonic-gate if (*t == 'i')
987c478bd9Sstevel@tonic-gate *dir = 0;
997c478bd9Sstevel@tonic-gate else if (*t == 'o')
1007c478bd9Sstevel@tonic-gate *dir = 1;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate } else if (ifn)
1037c478bd9Sstevel@tonic-gate *ifn = t;
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate } else
1067c478bd9Sstevel@tonic-gate s = line;
1077c478bd9Sstevel@tonic-gate t = (char *)ip;
1087c478bd9Sstevel@tonic-gate ip = (ip_t *)readhex(s, (char *)ip);
1097c478bd9Sstevel@tonic-gate if (!(opts & OPT_BRIEF)) {
1107c478bd9Sstevel@tonic-gate if (opts & OPT_ASCII) {
1117c478bd9Sstevel@tonic-gate if (t < (char *)ip)
1127c478bd9Sstevel@tonic-gate putchar('\t');
1137c478bd9Sstevel@tonic-gate while (t < (char *)ip) {
114*ab25eeb5Syz155240 if (ISPRINT(*t) && ISASCII(*t))
1157c478bd9Sstevel@tonic-gate putchar(*t);
1167c478bd9Sstevel@tonic-gate else
1177c478bd9Sstevel@tonic-gate putchar('.');
1187c478bd9Sstevel@tonic-gate t++;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate putchar('\n');
1227c478bd9Sstevel@tonic-gate fflush(stdout);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate return -1;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate
readhex(src,dst)1297c478bd9Sstevel@tonic-gate static char *readhex(src, dst)
1307c478bd9Sstevel@tonic-gate register char *src, *dst;
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate int state = 0;
1337c478bd9Sstevel@tonic-gate char c;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate while ((c = *src++)) {
136*ab25eeb5Syz155240 if (ISSPACE(c)) {
1377c478bd9Sstevel@tonic-gate if (state) {
1387c478bd9Sstevel@tonic-gate dst++;
1397c478bd9Sstevel@tonic-gate state = 0;
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate continue;
1427c478bd9Sstevel@tonic-gate } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
1437c478bd9Sstevel@tonic-gate (c >= 'A' && c <= 'F')) {
144*ab25eeb5Syz155240 c = ISDIGIT(c) ? (c - '0') : (TOUPPER(c) - 55);
1457c478bd9Sstevel@tonic-gate if (state == 0) {
1467c478bd9Sstevel@tonic-gate *dst = (c << 4);
1477c478bd9Sstevel@tonic-gate state++;
1487c478bd9Sstevel@tonic-gate } else {
1497c478bd9Sstevel@tonic-gate *dst++ |= c;
1507c478bd9Sstevel@tonic-gate state = 0;
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate } else
1537c478bd9Sstevel@tonic-gate break;
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate return dst;
1567c478bd9Sstevel@tonic-gate }
157