getport.c revision 145511
1/*	$NetBSD$	*/
2
3#include "ipf.h"
4
5int getport(fr, name, port)
6frentry_t *fr;
7char *name;
8u_short *port;
9{
10	struct protoent *p;
11	struct servent *s;
12	u_short p1;
13
14	if (fr == NULL || fr->fr_type != FR_T_IPF) {
15		s = getservbyname(name, NULL);
16		if (s != NULL) {
17			*port = s->s_port;
18			return 0;
19		}
20		return -1;
21	}
22
23	if ((fr->fr_flx & FI_TCPUDP) != 0) {
24		/*
25		 * If a rule is "tcp/udp" then check that both TCP and UDP
26		 * mappings for this protocol name match ports.
27		 */
28		s = getservbyname(name, "tcp");
29		if (s == NULL)
30			return -1;
31		p1 = s->s_port;
32		s = getservbyname(name, "udp");
33		if (s == NULL || s->s_port != p1)
34			return -1;
35		*port = p1;
36		return 0;
37	}
38
39	p = getprotobynumber(fr->fr_proto);
40	s = getservbyname(name, p ? p->p_name : NULL);
41	if (s != NULL) {
42		*port = s->s_port;
43		return 0;
44	}
45	return -1;
46}
47