]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
converted constant tables, support kres.type.TYPE1234
authorMarek Vavruša <mvavrusa@cloudflare.com>
Thu, 23 Nov 2017 07:50:58 +0000 (23:50 -0800)
committerMarek Vavruša <mvavrusa@cloudflare.com>
Fri, 24 Nov 2017 03:30:29 +0000 (19:30 -0800)
The difficulty with using structs as constant tables is that access
to non-existent fields throws an error. This is difficult to handle
without wrapping every access in a pcall, for example in predict module:

```
error: /usr/local/lib/kdns_modules/predict.lua:34: 'struct rr_type' has no member named 'TYPE65535'
```

So I converted the constant tables into regular Lua tables,
and added a metatable for RR types to allow looking up unnamed types,
in the TYPE%d format. Looking up non-existent fields will now
return nil instead of throwing an error.

daemon/lua/kres.lua.in

index 43a062fb1d195a1e157ba8c36cd4136be97d78be..d45e9dc1f2f95f537ad4bbff94196d50981fa391 100644 (file)
@@ -12,86 +12,6 @@ local knot = ffi.load(libknot_SONAME)
 
 -- Various declarations that are very stable.
 ffi.cdef[[
-
-/*
- * Record types and classes.
- */
-struct rr_class {
-       static const int IN         =   1;
-       static const int CH         =   3;
-       static const int NONE       = 254;
-       static const int ANY        = 255;
-};
-struct rr_type {
-       static const int A          =   1;
-       static const int NS         =   2;
-       static const int CNAME      =   5;
-       static const int SOA        =   6;
-       static const int NULL       =  10;
-       static const int PTR        =  12;
-       static const int HINFO      =  13;
-       static const int MINFO      =  14;
-       static const int MX         =  15;
-       static const int TXT        =  16;
-       static const int RP         =  17;
-       static const int AFSDB      =  18;
-       static const int RT         =  21;
-       static const int SIG        =  24;
-       static const int KEY        =  25;
-       static const int AAAA       =  28;
-       static const int LOC        =  29;
-       static const int SRV        =  33;
-       static const int NAPTR      =  35;
-       static const int KX         =  36;
-       static const int CERT       =  37;
-       static const int DNAME      =  39;
-       static const int OPT        =  41;
-       static const int APL        =  42;
-       static const int DS         =  43;
-       static const int SSHFP      =  44;
-       static const int IPSECKEY   =  45;
-       static const int RRSIG      =  46;
-       static const int NSEC       =  47;
-       static const int DNSKEY     =  48;
-       static const int DHCID      =  49;
-       static const int NSEC3      =  50;
-       static const int NSEC3PARAM =  51;
-       static const int TLSA       =  52;
-       static const int CDS        =  59;
-       static const int CDNSKEY    =  60;
-       static const int SPF        =  99;
-       static const int NID        = 104;
-       static const int L32        = 105;
-       static const int L64        = 106;
-       static const int LP         = 107;
-       static const int EUI48      = 108;
-       static const int EUI64      = 109;
-       static const int TKEY       = 249;
-       static const int TSIG       = 250;
-       static const int IXFR       = 251;
-       static const int AXFR       = 252;
-       static const int ANY        = 255;
-};
-struct pkt_section {
-       static const int ANSWER     = 0;
-       static const int AUTHORITY  = 1;
-       static const int ADDITIONAL = 2;
-};
-struct pkt_rcode {
-       static const int NOERROR    =  0;
-       static const int FORMERR    =  1;
-       static const int SERVFAIL   =  2;
-       static const int NXDOMAIN   =  3;
-       static const int NOTIMPL    =  4;
-       static const int REFUSED    =  5;
-       static const int YXDOMAIN   =  6;
-       static const int YXRRSET    =  7;
-       static const int NXRRSET    =  8;
-       static const int NOTAUTH    =  9;
-       static const int NOTZONE    = 10;
-       static const int BADVERS    = 16;
-};
-
 /*
  * Data structures
  */
@@ -117,6 +37,97 @@ int inet_pton(int af, const char *src, void *dst);
 
 require('kres-gen')
 
+-- Constant tables
+local const_class = {
+       IN         =   1,
+       CH         =   3,
+       NONE       = 254,
+       ANY        = 255,
+}
+local const_type = {
+       A          =   1,
+       NS         =   2,
+       CNAME      =   5,
+       SOA        =   6,
+       PTR        =  12,
+       HINFO      =  13,
+       MINFO      =  14,
+       MX         =  15,
+       TXT        =  16,
+       RP         =  17,
+       AFSDB      =  18,
+       RT         =  21,
+       SIG        =  24,
+       KEY        =  25,
+       AAAA       =  28,
+       LOC        =  29,
+       SRV        =  33,
+       NAPTR      =  35,
+       KX         =  36,
+       CERT       =  37,
+       DNAME      =  39,
+       OPT        =  41,
+       APL        =  42,
+       DS         =  43,
+       SSHFP      =  44,
+       IPSECKEY   =  45,
+       RRSIG      =  46,
+       NSEC       =  47,
+       DNSKEY     =  48,
+       DHCID      =  49,
+       NSEC3      =  50,
+       NSEC3PARAM =  51,
+       TLSA       =  52,
+       CDS        =  59,
+       CDNSKEY    =  60,
+       SPF        =  99,
+       NID        = 104,
+       L32        = 105,
+       L64        = 106,
+       LP         = 107,
+       EUI48      = 108,
+       EUI64      = 109,
+       TKEY       = 249,
+       TSIG       = 250,
+       IXFR       = 251,
+       AXFR       = 252,
+       ANY        = 255,
+}
+local const_section = {
+       ANSWER     = 0,
+       AUTHORITY  = 1,
+       ADDITIONAL = 2,
+}
+local const_rcode = {
+       NOERROR    =  0,
+       FORMERR    =  1,
+       SERVFAIL   =  2,
+       NXDOMAIN   =  3,
+       NOTIMPL    =  4,
+       REFUSED    =  5,
+       YXDOMAIN   =  6,
+       YXRRSET    =  7,
+       NXRRSET    =  8,
+       NOTAUTH    =  9,
+       NOTZONE    = 10,
+       BADVERS    = 16,
+       BADCOOKIE  = 23,
+}
+
+-- Metatype for RR types to allow anonymous types
+setmetatable(const_type, {
+       __index = function (t, k)
+               local v = rawget(t, k)
+               if v then return v end
+               -- Allow TYPE%d notation
+               if string.find(k, 'TYPE', 1, true) then
+                       return tonumber(k:sub(5))
+               end
+               -- Unknown type
+               return
+       end
+})
+
 -- Metatype for sockaddr
 local addr_buf = ffi.new('char[16]')
 local sockaddr_t = ffi.typeof('struct sockaddr')
@@ -322,10 +333,10 @@ end
 -- Module API
 kres = {
        -- Constants
-       class = ffi.new('struct rr_class'),
-       type = ffi.new('struct rr_type'),
-       section = ffi.new('struct pkt_section'),
-       rcode = ffi.new('struct pkt_rcode'),
+       class = const_class,
+       type = const_type,
+       section = const_section,
+       rcode = const_rcode,
 
        -- Create a struct kr_qflags from a single flag name or a list of names.
        mk_qflags = function (names)