]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/getport.c
This commit was generated by cvs2svn to compensate for changes in r152069,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / getport.c
1 /*      $FreeBSD$       */
2
3 #include "ipf.h"
4
5 int getport(fr, name, port)
6 frentry_t *fr;
7 char *name;
8 u_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 }