xref: /illumos-gate/usr/src/cmd/ipf/lib/common/ports.c (revision 7c478bd9)
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: ports.c,v 1.9 2002/01/28 06:50:47 darrenr Exp $
7  */
8 
9 #include <ctype.h>
10 
11 #include "ipf.h"
12 
13 
14 /*
15  * check for possible presence of the port fields in the line
16  */
17 int	ports(seg, proto, pp, cp, tp, linenum)
18 char	***seg;
19 char	*proto;
20 u_short	*pp;
21 int	*cp;
22 u_short	*tp;
23 int     linenum;
24 {
25 	int	comp = -1;
26 
27 	if (!*seg || !**seg || !***seg)
28 		return 0;
29 	if (!strcasecmp(**seg, "port") && *(*seg + 1) && *(*seg + 2)) {
30 		(*seg)++;
31 		if (isalnum(***seg) && *(*seg + 2)) {
32 			if (portnum(**seg, proto, pp, linenum) == 0)
33 				return -1;
34 			(*seg)++;
35 			if (!strcmp(**seg, "<>"))
36 				comp = FR_OUTRANGE;
37 			else if (!strcmp(**seg, "><"))
38 				comp = FR_INRANGE;
39 			else {
40 				fprintf(stderr,
41 					"%d: unknown range operator (%s)\n",
42 					linenum, **seg);
43 				return -1;
44 			}
45 			(*seg)++;
46 			if (**seg == NULL) {
47 				fprintf(stderr, "%d: missing 2nd port value\n",
48 					linenum);
49 				return -1;
50 			}
51 			if (portnum(**seg, proto, tp, linenum) == 0)
52 				return -1;
53 		} else if (!strcmp(**seg, "=") || !strcasecmp(**seg, "eq"))
54 			comp = FR_EQUAL;
55 		else if (!strcmp(**seg, "!=") || !strcasecmp(**seg, "ne"))
56 			comp = FR_NEQUAL;
57 		else if (!strcmp(**seg, "<") || !strcasecmp(**seg, "lt"))
58 			comp = FR_LESST;
59 		else if (!strcmp(**seg, ">") || !strcasecmp(**seg, "gt"))
60 			comp = FR_GREATERT;
61 		else if (!strcmp(**seg, "<=") || !strcasecmp(**seg, "le"))
62 			comp = FR_LESSTE;
63 		else if (!strcmp(**seg, ">=") || !strcasecmp(**seg, "ge"))
64 			comp = FR_GREATERTE;
65 		else {
66 			fprintf(stderr, "%d: unknown comparator (%s)\n",
67 					linenum, **seg);
68 			return -1;
69 		}
70 		if (comp != FR_OUTRANGE && comp != FR_INRANGE) {
71 			(*seg)++;
72 			if (portnum(**seg, proto, pp, linenum) == 0)
73 				return -1;
74 		}
75 		*cp = comp;
76 		(*seg)++;
77 	}
78 	return 0;
79 }
80