Sync usage with man page.
[netbsd-mini2440.git] / dist / ipf / lib / getport.c
blobd96bf4f6ab87ea001d153ad9ed1e8e021b324d02
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2002-2005 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 *
8 * Id: getport.c,v 1.1.4.6 2006/06/16 17:21:00 darrenr Exp
9 */
11 #include "ipf.h"
13 int getport(fr, name, port)
14 frentry_t *fr;
15 char *name;
16 u_short *port;
18 struct protoent *p;
19 struct servent *s;
20 u_short p1;
22 if (fr == NULL || fr->fr_type != FR_T_IPF) {
23 s = getservbyname(name, NULL);
24 if (s != NULL) {
25 *port = s->s_port;
26 return 0;
28 return -1;
32 * Some people will use port names in rules without specifying
33 * either TCP or UDP because it is implied by the group head.
34 * If we don't know the protocol, then the best we can do here is
35 * to take either only the TCP or UDP mapping (if one or the other
36 * is missing) or make sure both of them agree.
38 if (fr->fr_proto == 0) {
39 s = getservbyname(name, "tcp");
40 if (s != NULL)
41 p1 = s->s_port;
42 else
43 p1 = 0;
44 s = getservbyname(name, "udp");
45 if (s != NULL) {
46 if (p1 != s->s_port)
47 return -1;
49 if ((p1 == 0) && (s == NULL))
50 return -1;
51 if (p1)
52 *port = p1;
53 else
54 *port = s->s_port;
55 return 0;
58 if ((fr->fr_flx & FI_TCPUDP) != 0) {
60 * If a rule is "tcp/udp" then check that both TCP and UDP
61 * mappings for this protocol name match ports.
63 s = getservbyname(name, "tcp");
64 if (s == NULL)
65 return -1;
66 p1 = s->s_port;
67 s = getservbyname(name, "udp");
68 if (s == NULL || s->s_port != p1)
69 return -1;
70 *port = p1;
71 return 0;
74 p = getprotobynumber(fr->fr_proto);
75 s = getservbyname(name, p ? p->p_name : NULL);
76 if (s != NULL) {
77 *port = s->s_port;
78 return 0;
80 return -1;