[PATCH 09/12] Implement IP.len operator for ip address type.

Sergey Popovich popovich_sergei at mail.ru
Mon Sep 30 20:34:02 CEST 2013


Get length in bits of IP address for the running BIRD in filters:
32 - for IPv4, and 128 - for IPv6.

This is useful to determine address family for which BIRD was build
in filters at runtime.
---
 doc/bird.sgml   |   23 +++++++++++++++++++++++
 filter/filter.c |    1 +
 2 files changed, 24 insertions(+)

diff --git a/doc/bird.sgml b/doc/bird.sgml
index 65fc207..e4839a6 100644
--- a/doc/bird.sgml
+++ b/doc/bird.sgml
@@ -943,6 +943,29 @@ incompatible with each other (that is to prevent
you from shooting in the foot).
 	  is either an IPv4 or IPv6 address. IP addresses are written in the
standard notation (<cf/10.20.30.40/ or <cf/fec0:3:4::1/). You can apply
special operator <cf>.mask(<M>num</M>)</cf>
 	  on values of type ip. It masks out all but first <cf><M>num</M></cf>
bits from the IP
 	  address. So <cf/1.2.3.4.mask(8) = 1.0.0.0/ is true.
+	  Also <cf><m/I/.len</cf> operator could be used to get length in bits
of the
+	  IP address, which is useful in filters to determine address family
+	  (IPv4 or IPv6) of the BIRD instance at runtime. For example
+
+<code>
+function afi()
+{
+	case net.ip.len {
+		 32:	return 4;
+		128:	return 6;
+	}
+}
+
+function is_bird_ipv4()
+{
+	return afi() = 4;
+}
+
+function is_bird_ipv6()
+{
+	return afi() = 6;
+}
+</code>
  	<tag/prefix/ This type can hold a network prefix consisting of IP
address and prefix length. Prefix literals are written as
 	  <cf><M>ipaddress</M>/<M>pxlen</M></cf>, or
diff --git a/filter/filter.c b/filter/filter.c
index 5610f3f..3e8af1e 100644
--- a/filter/filter.c
+++ b/filter/filter.c
@@ -1067,6 +1067,7 @@ interpret(struct f_inst *what)
     case T_PATH:   res.val.i = as_path_getlen(v1.val.ad); break;
     case T_CLIST:  res.val.i = v1.val.ad->length / 4; break;
     case T_ECLIST: res.val.i = v1.val.ad->length / 8; break;
+    case T_IP:     res.val.i = sizeof(ip_addr) * 8; break; /* Use this
to determine AFI in filters */
     default: runtime( "Prefix, path, clist or eclist expected" );
     }
     break;
-- 
1.7.10.4



More information about the Bird-users mailing list