141edb306SCy Schubert
241edb306SCy Schubert /*
341edb306SCy Schubert * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert *
541edb306SCy Schubert * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert *
741edb306SCy Schubert * $Id$
841edb306SCy Schubert */
941edb306SCy Schubert
1041edb306SCy Schubert #include "ipf.h"
1141edb306SCy Schubert #include <ctype.h>
1241edb306SCy Schubert
13efeb8bffSCy Schubert int
getport(frentry_t * fr,char * name,u_short * port,char * proto)14efeb8bffSCy Schubert getport(frentry_t *fr, char *name, u_short *port, char *proto)
1541edb306SCy Schubert {
1641edb306SCy Schubert struct protoent *p;
1741edb306SCy Schubert struct servent *s;
1841edb306SCy Schubert u_short p1;
1941edb306SCy Schubert
2041edb306SCy Schubert if (fr == NULL || fr->fr_type != FR_T_IPF) {
2141edb306SCy Schubert s = getservbyname(name, proto);
2241edb306SCy Schubert if (s != NULL) {
2341edb306SCy Schubert *port = s->s_port;
24*2582ae57SCy Schubert return (0);
2541edb306SCy Schubert }
2641edb306SCy Schubert
2741edb306SCy Schubert if (ISDIGIT(*name)) {
2841edb306SCy Schubert int portval = atoi(name);
2941edb306SCy Schubert if (portval < 0 || portval > 65535)
30*2582ae57SCy Schubert return (-1);
3141edb306SCy Schubert *port = htons((u_short)portval);
32*2582ae57SCy Schubert return (0);
3341edb306SCy Schubert }
34*2582ae57SCy Schubert return (-1);
3541edb306SCy Schubert }
3641edb306SCy Schubert
3741edb306SCy Schubert /*
3841edb306SCy Schubert * Some people will use port names in rules without specifying
3941edb306SCy Schubert * either TCP or UDP because it is implied by the group head.
4041edb306SCy Schubert * If we don't know the protocol, then the best we can do here is
4141edb306SCy Schubert * to take either only the TCP or UDP mapping (if one or the other
4241edb306SCy Schubert * is missing) or make sure both of them agree.
4341edb306SCy Schubert */
4441edb306SCy Schubert if (fr->fr_proto == 0) {
4541edb306SCy Schubert s = getservbyname(name, "tcp");
4641edb306SCy Schubert if (s != NULL)
4741edb306SCy Schubert p1 = s->s_port;
4841edb306SCy Schubert else
4941edb306SCy Schubert p1 = 0;
5041edb306SCy Schubert s = getservbyname(name, "udp");
5141edb306SCy Schubert if (s != NULL) {
5241edb306SCy Schubert if (p1 != s->s_port)
53*2582ae57SCy Schubert return (-1);
5441edb306SCy Schubert }
5541edb306SCy Schubert if ((p1 == 0) && (s == NULL))
56*2582ae57SCy Schubert return (-1);
5741edb306SCy Schubert if (p1)
5841edb306SCy Schubert *port = p1;
5941edb306SCy Schubert else
6041edb306SCy Schubert *port = s->s_port;
61*2582ae57SCy Schubert return (0);
6241edb306SCy Schubert }
6341edb306SCy Schubert
6441edb306SCy Schubert if ((fr->fr_flx & FI_TCPUDP) != 0) {
6541edb306SCy Schubert /*
6641edb306SCy Schubert * If a rule is "tcp/udp" then check that both TCP and UDP
6741edb306SCy Schubert * mappings for this protocol name match ports.
6841edb306SCy Schubert */
6941edb306SCy Schubert s = getservbyname(name, "tcp");
7041edb306SCy Schubert if (s == NULL)
71*2582ae57SCy Schubert return (-1);
7241edb306SCy Schubert p1 = s->s_port;
7341edb306SCy Schubert s = getservbyname(name, "udp");
7441edb306SCy Schubert if (s == NULL || s->s_port != p1)
75*2582ae57SCy Schubert return (-1);
7641edb306SCy Schubert *port = p1;
77*2582ae57SCy Schubert return (0);
7841edb306SCy Schubert }
7941edb306SCy Schubert
8041edb306SCy Schubert p = getprotobynumber(fr->fr_proto);
8141edb306SCy Schubert s = getservbyname(name, p ? p->p_name : NULL);
8241edb306SCy Schubert if (s != NULL) {
8341edb306SCy Schubert *port = s->s_port;
84*2582ae57SCy Schubert return (0);
8541edb306SCy Schubert }
86*2582ae57SCy Schubert return (-1);
8741edb306SCy Schubert }
88