Re: pfctl + bgpd for loop ugliness

2023-04-21 Thread Alexandr Nedvedicky
Hello,

On Tue, Apr 18, 2023 at 02:43:26PM +0200, Theo Buehler wrote:
> On Tue, Apr 18, 2023 at 02:06:46PM +0200, Claudio Jeker wrote:
> > This and the others are IIRC streight from pfctl. So if someone wants a
> > free commit :) 
> 
> How about this. pfctl and bgpd are the same, except that the bgpd one
> has a bsearch() nitems on top.  pfctl regress is happy.
> 
> Index: sbin/pfctl/pfctl_parser.c
> ===
> RCS file: /cvs/src/sbin/pfctl/pfctl_parser.c,v
> retrieving revision 1.347
> diff -u -p -r1.347 pfctl_parser.c
> --- sbin/pfctl/pfctl_parser.c 9 Nov 2022 23:00:00 -   1.347
> +++ sbin/pfctl/pfctl_parser.c 18 Apr 2023 12:37:19 -
> @@ -62,6 +62,10 @@
>  #include "pfctl_parser.h"
>  #include "pfctl.h"
>  
> +#ifndef nitems
> +#define nitems(_a)   (sizeof((_a)) / sizeof((_a)[0]))
> +#endif
> +

I like it.

OK sashan



Re: pfctl + bgpd for loop ugliness

2023-04-18 Thread Denis Fondras
Le Tue, Apr 18, 2023 at 02:43:26PM +0200, Theo Buehler a écrit :
> On Tue, Apr 18, 2023 at 02:06:46PM +0200, Claudio Jeker wrote:
> > This and the others are IIRC streight from pfctl. So if someone wants a
> > free commit :) 
> 
> How about this. pfctl and bgpd are the same, except that the bgpd one
> has a bsearch() nitems on top.  pfctl regress is happy.
> 

Looks good to me. OK denis@

> Index: sbin/pfctl/pfctl_parser.c
> ===
> RCS file: /cvs/src/sbin/pfctl/pfctl_parser.c,v
> retrieving revision 1.347
> diff -u -p -r1.347 pfctl_parser.c
> --- sbin/pfctl/pfctl_parser.c 9 Nov 2022 23:00:00 -   1.347
> +++ sbin/pfctl/pfctl_parser.c 18 Apr 2023 12:37:19 -
> @@ -62,6 +62,10 @@
>  #include "pfctl_parser.h"
>  #include "pfctl.h"
>  
> +#ifndef nitems
> +#define nitems(_a)   (sizeof((_a)) / sizeof((_a)[0]))
> +#endif
> +
>  void  print_op (u_int8_t, const char *, const char *);
>  void  print_port (u_int8_t, u_int16_t, u_int16_t, const char *, int);
>  void  print_ugid (u_int8_t, id_t, id_t, const char *);
> @@ -224,17 +228,15 @@ copy_satopfaddr(struct pf_addr *pfa, str
>  const struct icmptypeent *
>  geticmptypebynumber(u_int8_t type, sa_family_t af)
>  {
> - unsigned inti;
> + size_t  i;
>  
>   if (af != AF_INET6) {
> - for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0]));
> - i++) {
> + for (i = 0; i < nitems(icmp_type); i++) {
>   if (type == icmp_type[i].type)
>   return (_type[i]);
>   }
>   } else {
> - for (i=0; i < (sizeof (icmp6_type) /
> - sizeof(icmp6_type[0])); i++) {
> + for (i = 0; i < nitems(icmp6_type); i++) {
>   if (type == icmp6_type[i].type)
>return (_type[i]);
>   }
> @@ -245,17 +247,15 @@ geticmptypebynumber(u_int8_t type, sa_fa
>  const struct icmptypeent *
>  geticmptypebyname(char *w, sa_family_t af)
>  {
> - unsigned inti;
> + size_t  i;
>  
>   if (af != AF_INET6) {
> - for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0]));
> - i++) {
> + for (i = 0; i < nitems(icmp_type); i++) {
>   if (!strcmp(w, icmp_type[i].name))
>   return (_type[i]);
>   }
>   } else {
> - for (i=0; i < (sizeof (icmp6_type) /
> - sizeof(icmp6_type[0])); i++) {
> + for (i = 0; i < nitems(icmp6_type); i++) {
>   if (!strcmp(w, icmp6_type[i].name))
>   return (_type[i]);
>   }
> @@ -266,18 +266,16 @@ geticmptypebyname(char *w, sa_family_t a
>  const struct icmpcodeent *
>  geticmpcodebynumber(u_int8_t type, u_int8_t code, sa_family_t af)
>  {
> - unsigned inti;
> + size_t  i;
>  
>   if (af != AF_INET6) {
> - for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0]));
> - i++) {
> + for (i = 0; i < nitems(icmp_code); i++) {
>   if (type == icmp_code[i].type &&
>   code == icmp_code[i].code)
>   return (_code[i]);
>   }
>   } else {
> - for (i=0; i < (sizeof (icmp6_code) /
> - sizeof(icmp6_code[0])); i++) {
> + for (i = 0; i < nitems(icmp6_code); i++) {
>   if (type == icmp6_code[i].type &&
>   code == icmp6_code[i].code)
>   return (_code[i]);
> @@ -289,18 +287,16 @@ geticmpcodebynumber(u_int8_t type, u_int
>  const struct icmpcodeent *
>  geticmpcodebyname(u_long type, char *w, sa_family_t af)
>  {
> - unsigned inti;
> + size_t  i;
>  
>   if (af != AF_INET6) {
> - for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0]));
> - i++) {
> + for (i = 0; i < nitems(icmp_code); i++) {
>   if (type == icmp_code[i].type &&
>   !strcmp(w, icmp_code[i].name))
>   return (_code[i]);
>   }
>   } else {
> - for (i=0; i < (sizeof (icmp6_code) /
> - sizeof(icmp6_code[0])); i++) {
> + for (i = 0; i < nitems(icmp6_code); i++) {
>   if (type == icmp6_code[i].type &&
>   !strcmp(w, icmp6_code[i].name))
>   return (_code[i]);
> Index: usr.sbin/bgpd/parse.y
> ===
> RCS file: /cvs/src/usr.sbin/bgpd/parse.y,v
> retrieving revision 1.447
> diff -u -p -r1.447 parse.y
> --- usr.sbin/bgpd/parse.y 18 Apr 2023 12:11:27 -  1.447
> +++ usr.sbin/bgpd/parse.y 18 Apr 2023 12:37:19 -
> @@ -52,6 +52,10 @@
>  #include 

pfctl + bgpd for loop ugliness

2023-04-18 Thread Theo Buehler
On Tue, Apr 18, 2023 at 02:06:46PM +0200, Claudio Jeker wrote:
> This and the others are IIRC streight from pfctl. So if someone wants a
> free commit :) 

How about this. pfctl and bgpd are the same, except that the bgpd one
has a bsearch() nitems on top.  pfctl regress is happy.

Index: sbin/pfctl/pfctl_parser.c
===
RCS file: /cvs/src/sbin/pfctl/pfctl_parser.c,v
retrieving revision 1.347
diff -u -p -r1.347 pfctl_parser.c
--- sbin/pfctl/pfctl_parser.c   9 Nov 2022 23:00:00 -   1.347
+++ sbin/pfctl/pfctl_parser.c   18 Apr 2023 12:37:19 -
@@ -62,6 +62,10 @@
 #include "pfctl_parser.h"
 #include "pfctl.h"
 
+#ifndef nitems
+#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
 voidprint_op (u_int8_t, const char *, const char *);
 voidprint_port (u_int8_t, u_int16_t, u_int16_t, const char *, int);
 voidprint_ugid (u_int8_t, id_t, id_t, const char *);
@@ -224,17 +228,15 @@ copy_satopfaddr(struct pf_addr *pfa, str
 const struct icmptypeent *
 geticmptypebynumber(u_int8_t type, sa_family_t af)
 {
-   unsigned inti;
+   size_t  i;
 
if (af != AF_INET6) {
-   for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0]));
-   i++) {
+   for (i = 0; i < nitems(icmp_type); i++) {
if (type == icmp_type[i].type)
return (_type[i]);
}
} else {
-   for (i=0; i < (sizeof (icmp6_type) /
-   sizeof(icmp6_type[0])); i++) {
+   for (i = 0; i < nitems(icmp6_type); i++) {
if (type == icmp6_type[i].type)
 return (_type[i]);
}
@@ -245,17 +247,15 @@ geticmptypebynumber(u_int8_t type, sa_fa
 const struct icmptypeent *
 geticmptypebyname(char *w, sa_family_t af)
 {
-   unsigned inti;
+   size_t  i;
 
if (af != AF_INET6) {
-   for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0]));
-   i++) {
+   for (i = 0; i < nitems(icmp_type); i++) {
if (!strcmp(w, icmp_type[i].name))
return (_type[i]);
}
} else {
-   for (i=0; i < (sizeof (icmp6_type) /
-   sizeof(icmp6_type[0])); i++) {
+   for (i = 0; i < nitems(icmp6_type); i++) {
if (!strcmp(w, icmp6_type[i].name))
return (_type[i]);
}
@@ -266,18 +266,16 @@ geticmptypebyname(char *w, sa_family_t a
 const struct icmpcodeent *
 geticmpcodebynumber(u_int8_t type, u_int8_t code, sa_family_t af)
 {
-   unsigned inti;
+   size_t  i;
 
if (af != AF_INET6) {
-   for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0]));
-   i++) {
+   for (i = 0; i < nitems(icmp_code); i++) {
if (type == icmp_code[i].type &&
code == icmp_code[i].code)
return (_code[i]);
}
} else {
-   for (i=0; i < (sizeof (icmp6_code) /
-   sizeof(icmp6_code[0])); i++) {
+   for (i = 0; i < nitems(icmp6_code); i++) {
if (type == icmp6_code[i].type &&
code == icmp6_code[i].code)
return (_code[i]);
@@ -289,18 +287,16 @@ geticmpcodebynumber(u_int8_t type, u_int
 const struct icmpcodeent *
 geticmpcodebyname(u_long type, char *w, sa_family_t af)
 {
-   unsigned inti;
+   size_t  i;
 
if (af != AF_INET6) {
-   for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0]));
-   i++) {
+   for (i = 0; i < nitems(icmp_code); i++) {
if (type == icmp_code[i].type &&
!strcmp(w, icmp_code[i].name))
return (_code[i]);
}
} else {
-   for (i=0; i < (sizeof (icmp6_code) /
-   sizeof(icmp6_code[0])); i++) {
+   for (i = 0; i < nitems(icmp6_code); i++) {
if (type == icmp6_code[i].type &&
!strcmp(w, icmp6_code[i].name))
return (_code[i]);
Index: usr.sbin/bgpd/parse.y
===
RCS file: /cvs/src/usr.sbin/bgpd/parse.y,v
retrieving revision 1.447
diff -u -p -r1.447 parse.y
--- usr.sbin/bgpd/parse.y   18 Apr 2023 12:11:27 -  1.447
+++ usr.sbin/bgpd/parse.y   18 Apr 2023 12:37:19 -
@@ -52,6 +52,10 @@
 #include "rde.h"
 #include "log.h"
 
+#ifndef nitems
+#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
 #define MACRO_NAME_LEN 128
 
 TAILQ_HEAD(files, file)