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