]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
added ldns_serial which should? implement serial arith. according
authorMiek Gieben <miekg@NLnetLabs.nl>
Wed, 24 Aug 2005 14:01:06 +0000 (14:01 +0000)
committerMiek Gieben <miekg@NLnetLabs.nl>
Wed, 24 Aug 2005 14:01:06 +0000 (14:01 +0000)
to rfc 1982

ldns/util.h.in
tests/Makefile.in
tests/rrsig-test.c
tests/serial.c [new file with mode: 0644]
util.c

index 4f0a81db385852767b862effa61c8a3b7d1075a7..89ef48334da1f2f68bb4f5478f267dba7e4495fc 100644 (file)
@@ -184,8 +184,6 @@ ldns_power(long a, long b) {
        return result;
 }
 
-
-
 /**
  * Returns the int value of the given (hex) digit
  */
@@ -202,4 +200,14 @@ char ldns_int_to_hexdigit(int ch);
  */
 const char * ldns_version(void);
 
+/**
+ * Implements serial arith. RFC 1982
+ * \param[in] s1 the first serial
+ * \param[in] s2 the second serial
+ * \return 0 if s1 == s2
+ * \return -1 if s1 < s2
+ * \return +1 if s1 > s2
+ * \return -2 undef
+ */
+int ldns_serial(uint32_t s1, uint32_t s2);
 #endif /* !_UTIL_H */
index cb405a5cbf11b93f64d35af344944aebd931c66b..005bf8e4089ffb334d6379d976a03eedf5d9b83e 100644 (file)
@@ -20,14 +20,12 @@ LINK            = $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS)
 HEADER         = config.h
 TESTS          = run-test0 run-test7 run-test18 run-test19 run-test20 \
                        dname-label-test notify run-test-read-zone \
-                       rrsig-test
+                       rrsig-test serial
 
 .PHONY:        all clean realclean
 
 all:   $(TESTS)
 
-lua:   lua-rns
-
 run-test0:       run-test0.o 
                $(LINK) -o $@ $+ 
 
@@ -58,11 +56,8 @@ dname-label-test:       dname-label-test.o
 notify:        notify.o
                $(LINK) -o $@ $+
 
-lua-rns:       lua-rns.o
-               $(LINK) `lua-config --libs` -o $@ $+
-       
-lua-rns.o:     lua-rns.c
-               $(COMPILE) `lua-config --include` -c $<
+serial:        serial.o
+               $(LINK) -o $@ $+
 
 ## implicit rule
 %.o:    %.c $(HEADER)
index 8fefc48aa1b56705be4fb0bf3eff52c5c2c4a7bd..bd8bc5dcc4b02c59d0acd45a108ba294a4a499a9 100644 (file)
@@ -21,6 +21,12 @@ int main(void)
        ldns_rdf *incep, *expir;
 
        time_t t_incep, t_expir, t_now;
+       uint32_t tweemacht = 1;
+
+       tweemacht = tweemacht << 31;
+       tweemacht = tweemacht * 2 - 1;
+
+       printf("tweemacht %u\n", tweemacht);
 
        sig = ldns_rr_new_frm_str("jelte.nlnetlabs.nl.     18000   IN      RRSIG   NSEC RSASHA1 3 18000 20050913235001 20050814235001 43791 nlnetlabs.nl. epWGR0WkhWQ1h0eXvU89W57xwI0xuUlWtvvUnABQVmUfZ2nGllIy2KLR5cfgpB5UH7beASrAo78AlPddPCnH50OYNjllesDy9HLderQtjQoi47SPPluLC6v3Fwqq64Zv0wf2fPzJqDSnOOrQPVzIuB3IDv5XD4M5t8Vze8QZ8lA=", 0, NULL);
 
diff --git a/tests/serial.c b/tests/serial.c
new file mode 100644 (file)
index 0000000..f954c3f
--- /dev/null
@@ -0,0 +1,35 @@
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif /* HAVE_STDINT_H */
+
+#include <ldns/dns.h>
+
+void 
+test_serial(uint32_t a, uint32_t b)
+{
+       printf("%d : %d\n", a, b);
+
+       printf("%d\n",
+                       ldns_serial(a,b));
+}
+
+int 
+main(void)
+{
+       /* serial tests */
+       test_serial(1, 1);
+       test_serial(1, 2);
+       test_serial(2, 1);
+       test_serial(0, 0);
+
+       
+}
+
diff --git a/util.c b/util.c
index b63225f46d7850fa6af6db23e2a7a8e53b634fbd..890b340d3902940cf14b0045ebf9a9a0940c1e46 100644 (file)
--- a/util.c
+++ b/util.c
@@ -151,3 +151,54 @@ ldns_version(void)
 {
        return (char*)LDNS_VERSION;
 }
+
+/* compare according to RFC 1982 (32 bits)
+ * it either return 0 meaning
+ * equal
+ * 0: s1 == s2
+ * -1: s1 < s2
+ * +1: s1 > s2
+ * something else (-2 in this case) meaning undef
+ */
+int
+ldns_serial(uint32_t s1, uint32_t s2)
+{
+       uint32_t power;
+
+       /* calculate  2^(SERIAL_BITS - 1) */
+       power = 1;
+       power = power << 31;
+       power = power * 2 - 1;
+
+       /* equality */
+       if (s1 == s2) {
+               return 0;
+       }
+
+       /* s1 is less than s2 */
+       /* (i1 < i2 and i2 - i1 < 2^(SERIAL_BITS - 1)) */
+       if (s1 < s2 &&
+                       ((s2 - s1) < power)) {
+               return -1;
+       }
+       /* i1 > i2 and i1 - i2 > 2^(SERIAL_BITS - 1)) */
+       if (s1 > s2 &&
+                       ((s1 - s2) > power)) {
+               return -1;
+       }
+
+       /* s1 is more than s2 */
+       /* (i1 < i2 and i2 - i1 > 2^(SERIAL_BITS - 1)) */
+       if (s1 < s2 &&
+                       ((s2 - s1) > power)) {
+               return +1;
+       }
+       /* (i1 > i2 and i1 - i2 < 2^(SERIAL_BITS - 1)) */
+       if (s1 > s2 &&
+                       ((s1 - s2) < power)) {
+               return +1;
+       }
+
+       /* unknown */
+       return -2;
+}