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