static bool check_ttl(knot_rdata_t *rdata, uint8_t ext_rcode, uint8_t ver, uint16_t flags, char *msg, int *done) { /* TTL should be stored in machine byte order. We need network byte order to compare its parts. */ uint8_t ttl_wire[4] = { 0, 0, 0, 0 }; wire_write_u32(ttl_wire, knot_rdata_ttl(rdata)); /* Convert Flags from EDNS parameters to wire format for comparison. */ uint8_t flags_wire[2] = { 0, 0 }; wire_write_u16(flags_wire, flags); bool success = true; /* TTL = Ext RCODE + Version + Flags */ bool check = (ttl_wire[OFFSET_ERCODE] == ext_rcode); ok(check, "%s: extended RCODE", msg); success &= check; (*done)++; check = (ttl_wire[OFFSET_VER] == ver); ok(check, "%s: version", msg); success &= check; (*done)++; check = (memcmp(flags_wire, ttl_wire + OFFSET_FLAGS, 2) == 0); ok(check, "%s: flags", msg); success &= check; (*done)++; return success; }
static void set_value_to_ttl(knot_rrset_t *opt_rr, size_t offset, uint8_t value) { uint32_t ttl = 0; uint8_t *ttl_ptr = (uint8_t *)&ttl; // TTL is stored in machine byte order. Convert it to wire order first. wire_write_u32(ttl_ptr, knot_rrset_ttl(opt_rr)); // Set the Extended RCODE in the converted TTL memcpy(ttl_ptr + offset, &value, sizeof(uint8_t)); // Convert it back to machine byte order uint32_t ttl_local = wire_read_u32(ttl_ptr); // Store the TTL to the RDATA knot_rdata_set_ttl(knot_rdataset_at(&opt_rr->rrs, 0), ttl_local); }
/*----------------------------------------------------------------------------*/ _public_ uint8_t knot_edns_get_version(const knot_rrset_t *opt_rr) { assert(opt_rr != NULL); uint32_t ttl = 0; uint8_t *ttl_ptr = (uint8_t *)&ttl; // TTL is stored in machine byte order. Convert it to wire order first. wire_write_u32(ttl_ptr, knot_rrset_ttl(opt_rr)); uint8_t version; memcpy(&version, ttl_ptr + EDNS_OFFSET_VERSION, sizeof(uint8_t)); return version; }
/*----------------------------------------------------------------------------*/ _public_ uint8_t knot_edns_get_ext_rcode(const knot_rrset_t *opt_rr) { assert(opt_rr != NULL); uint32_t ttl = 0; uint8_t *ttl_ptr = (uint8_t *)&ttl; // TTL is stored in machine byte order. Convert it to wire order first. wire_write_u32(ttl_ptr, knot_rrset_ttl(opt_rr)); uint8_t rcode; memcpy(&rcode, ttl_ptr + EDNS_OFFSET_RCODE, sizeof(uint8_t)); return rcode; }
static inline void pack_bin(char **stream, const void *data, uint32_t len) { wire_write_u32((uint8_t *)*stream, len); *stream += sizeof(uint32_t); memcpy(*stream, data, len); *stream += len; }