Allow to use service names like 'bgp' for port definitions.
Adapted from pfctl/parse.y.

-- 
:wq Claudio

? obj
Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/parse.y,v
retrieving revision 1.445
diff -u -p -r1.445 parse.y
--- parse.y     5 Apr 2023 08:04:28 -0000       1.445
+++ parse.y     5 Apr 2023 08:05:14 -0000
@@ -37,6 +37,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <limits.h>
+#include <netdb.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -161,6 +162,7 @@ static void  add_roa_set(struct prefixse
 static struct rtr_config       *get_rtr(struct bgpd_addr *);
 static int      insert_rtr(struct rtr_config *);
 static int      merge_aspa_set(uint32_t, struct aspa_tas_l *, time_t);
+static int      getservice(char *);
 
 static struct bgpd_config      *conf;
 static struct network_head     *netconf;
@@ -247,6 +249,7 @@ typedef struct {
 %type  <v.number>              yesno inout restricted expires enforce
 %type  <v.number>              validity aspa_validity
 %type  <v.number>              addpathextra addpathmax
+%type  <v.number>              port
 %type  <v.string>              string
 %type  <v.addr>                address
 %type  <v.prefix>              prefix addrspec
@@ -692,12 +695,7 @@ rtropt             : DESCR STRING          {
                        }
                        currtr->local_addr = $2;
                }
-               | PORT NUMBER {
-                       if ($2 < 1 || $2 > USHRT_MAX) {
-                               yyerror("port must be between %u and %u",
-                                   1, USHRT_MAX);
-                               YYERROR;
-                       }
+               | PORT port {
                        currtr->remote_port = $2;
                }
                ;
@@ -750,16 +748,10 @@ conf_main : AS as4number          {
                        memcpy(&la->sa, sa, la->sa_len);
                        TAILQ_INSERT_TAIL(conf->listen_addrs, la, entry);
                }
-               | LISTEN ON address PORT NUMBER {
+               | LISTEN ON address PORT port   {
                        struct listen_addr      *la;
                        struct sockaddr         *sa;
 
-                       if ($5 < 1 || $5 > USHRT_MAX) {
-                               yyerror("port must be between %u and %u",
-                                   1, USHRT_MAX);
-                               YYERROR;
-                       }
-
                        if ((la = calloc(1, sizeof(struct listen_addr))) ==
                            NULL)
                                fatal("parse conf_main listen on calloc");
@@ -1147,6 +1139,24 @@ network          : NETWORK prefix filter_set     {
                }
                ;
 
+port           : NUMBER                        {
+                       if ($1 < 1 || $1 > USHRT_MAX) {
+                               yyerror("port must be between %u and %u",
+                                   1, USHRT_MAX);
+                               YYERROR;
+                       }
+                       $$ = $1;
+               }
+               | STRING                        {
+                       if (($$ = getservice($1)) == -1) {
+                               yyerror("unknown port '%s'", $1);
+                               free($1);
+                               YYERROR;
+                       }
+                       free($1);
+               }
+               ;
+
 inout          : IN            { $$ = 1; }
                | OUT           { $$ = 0; }
                ;
@@ -1954,12 +1964,7 @@ peeropts : REMOTEAS as4number    {
                        else
                                curpeer->conf.flags &= ~PEERFLAG_NO_AS_SET;
                }
-               | PORT NUMBER {
-                       if ($2 < 1 || $2 > USHRT_MAX) {
-                               yyerror("port must be between %u and %u",
-                                   1, USHRT_MAX);
-                               YYERROR;
-                       }
+               | PORT port {
                        curpeer->conf.remote_port = $2;
                }
                | RDE EVALUATE STRING {
@@ -5151,4 +5156,17 @@ merge_aspa_set(uint32_t as, struct aspa_
                aspa->expires = expires;
 
        return 0;
+}
+
+static int
+getservice(char *n)
+{
+       struct servent  *s;
+
+       s = getservbyname(n, "tcp");
+       if (s == NULL)
+               s = getservbyname(n, "udp");
+       if (s == NULL)
+               return (-1);
+       return s->s_port;
 }

Reply via email to