From: Miek Gieben Date: Wed, 24 Aug 2005 14:01:06 +0000 (+0000) Subject: added ldns_serial which should? implement serial arith. according X-Git-Tag: release-1.0.0~246 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad0dbbc492fbeab7ed4af260494a5b3d7348317b;p=thirdparty%2Fldns.git added ldns_serial which should? implement serial arith. according to rfc 1982 --- diff --git a/ldns/util.h.in b/ldns/util.h.in index 4f0a81db..89ef4833 100644 --- a/ldns/util.h.in +++ b/ldns/util.h.in @@ -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 */ diff --git a/tests/Makefile.in b/tests/Makefile.in index cb405a5c..005bf8e4 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -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) diff --git a/tests/rrsig-test.c b/tests/rrsig-test.c index 8fefc48a..bd8bc5dc 100644 --- a/tests/rrsig-test.c +++ b/tests/rrsig-test.c @@ -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 index 00000000..f954c3f2 --- /dev/null +++ b/tests/serial.c @@ -0,0 +1,35 @@ +#include "config.h" + +#include +#include +#include +#include +#include + +#ifdef HAVE_STDINT_H +#include +#endif /* HAVE_STDINT_H */ + +#include + +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 b63225f4..890b340d 100644 --- 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; +}