static void print_error_host(const uint16_t code, const knot_pkt_t *packet, const style_t *style) { const char *rcode_str = "Unknown"; char type[32] = "Unknown"; char *owner; lookup_table_t *rcode; owner = knot_dname_to_str_alloc(knot_pkt_qname(packet)); if (style->style.ascii_to_idn != NULL) { style->style.ascii_to_idn(&owner); } rcode = lookup_by_id(knot_rcode_names, code); if (rcode != NULL) { rcode_str = rcode->name; } knot_rrtype_to_string(knot_pkt_qtype(packet), type, sizeof(type)); if (code == KNOT_RCODE_NOERROR) { printf("Host %s has no %s record\n", owner, type); } else { printf("Host %s type %s error: %s\n", owner, type, rcode_str); } free(owner); }
static void print_section_host(const knot_rrset_t *rrsets, const uint16_t count, const style_t *style) { size_t buflen = 8192; char *buf = calloc(buflen, 1); for (size_t i = 0; i < count; i++) { const knot_rrset_t *rrset = &rrsets[i]; lookup_table_t *descr; char type[32] = "NULL"; char *owner; owner = knot_dname_to_str_alloc(rrset->owner); if (style->style.ascii_to_idn != NULL) { style->style.ascii_to_idn(&owner); } descr = lookup_by_id(rtypes, rrset->type); uint16_t rrset_rdata_count = rrset->rrs.rr_count; for (uint16_t j = 0; j < rrset_rdata_count; j++) { if (rrset->type == KNOT_RRTYPE_CNAME && style->hide_cname) { continue; } while (knot_rrset_txt_dump_data(rrset, j, buf, buflen, &(style->style)) < 0) { buflen += 4096; // Oversize protection. if (buflen > 100000) { WARN("can't print whole section\n"); break; } char *newbuf = realloc(buf, buflen); if (newbuf == NULL) { WARN("can't print whole section\n"); break; } buf = newbuf; } if (descr != NULL) { printf("%s %s %s\n", owner, descr->name, buf); } else { knot_rrtype_to_string(rrset->type, type, sizeof(type)); printf("%s has %s record %s\n", owner, type, buf); } } free(owner); } free(buf); }
static void print_section_opt(const knot_rrset_t *rr, const uint8_t rcode) { uint8_t ercode = knot_edns_get_ext_rcode(rr); uint16_t ext_rcode_id = knot_edns_whole_rcode(ercode, rcode); const char *ext_rcode_str = "Unused"; lookup_table_t *ext_rcode; if (ercode > 0) { ext_rcode = lookup_by_id(knot_rcode_names, ext_rcode_id); if (ext_rcode != NULL) { ext_rcode_str = ext_rcode->name; } else { ext_rcode_str = "Unknown"; } } printf("Version: %u; flags: %s; UDP size: %u B; ext-rcode: %s\n", knot_edns_get_version(rr), (knot_edns_do(rr) != 0) ? "do" : "", knot_edns_get_payload(rr), ext_rcode_str); knot_rdata_t *rdata = knot_rdataset_at(&rr->rrs, 0); assert(rdata != NULL); uint16_t data_len = knot_rdata_rdlen(rdata); uint8_t *data = knot_rdata_data(rdata); int pos = 0; while (pos < data_len - KNOT_EDNS_OPTION_HDRLEN) { uint16_t opt_code = wire_read_u16(data + pos); uint16_t opt_len = wire_read_u16(data + pos + 2); uint8_t *opt_data = data + pos + 4; switch (opt_code) { case KNOT_EDNS_OPTION_NSID: printf(";; NSID: "); short_hex_print(opt_data, opt_len); if (opt_len > 0) { printf(";; : "); txt_print(opt_data, opt_len); } break; case KNOT_EDNS_OPTION_CLIENT_SUBNET: printf(";; CLIENT-SUBNET: "); print_edns_client_subnet(opt_data, opt_len); break; default: printf(";; Option (%u): ", opt_code); short_hex_print(opt_data, opt_len); } pos += 4 + opt_len; } }
const char * rrclass_to_string(uint16_t rrclass) { static char buf[20]; lookup_table_type *entry = lookup_by_id(dns_rrclasses, rrclass); if (entry) { assert(strlen(entry->name) < sizeof(buf)); strlcpy(buf, entry->name, sizeof(buf)); } else { snprintf(buf, sizeof(buf), "CLASS%d", (int) rrclass); } return buf; }
static int rdata_certificate_type_to_string(buffer_type *output, rdata_atom_type rdata, rr_type* ATTR_UNUSED(rr)) { uint16_t id = read_uint16(rdata_atom_data(rdata)); lookup_table_type *type = lookup_by_id(dns_certificate_types, id); if (type) { buffer_printf(output, "%s", type->name); } else { buffer_printf(output, "%u", (unsigned) id); } return 1; }
static int rdata_algorithm_to_string(buffer_type *output, rdata_atom_type rdata, rr_type* ATTR_UNUSED(rr)) { uint8_t id = *rdata_atom_data(rdata); lookup_table_type *alg = lookup_by_id(dns_algorithms, id); if (alg) { buffer_printf(output, "%s", alg->name); } else { buffer_printf(output, "%u", (unsigned) id); } return 1; }
int notify_process_answer(knot_pkt_t *pkt, struct answer_data *adata) { if (pkt == NULL || adata == NULL) { return KNOT_STATE_FAIL; } /* Check RCODE. */ uint8_t rcode = knot_wire_get_rcode(pkt->wire); if (rcode != KNOT_RCODE_NOERROR) { lookup_table_t *lut = lookup_by_id(knot_rcode_names, rcode); if (lut != NULL) { NOTIFY_RLOG(LOG_WARNING, "server responded with %s", lut->name); } return KNOT_STATE_FAIL; } NS_NEED_TSIG_SIGNED(&adata->param->tsig_ctx, 0); return KNOT_STATE_DONE; /* No processing. */ }
void log_file(int priority, const char *message) { size_t length; lookup_table_type *priority_info; const char *priority_text = "unknown"; assert(global_ident); assert(current_log_file); priority_info = lookup_by_id(log_priority_table, priority); if (priority_info) { priority_text = priority_info->name; } /* Bug #104, add time_t timestamp */ fprintf(current_log_file, "[%d] %s[%d]: %s: %s", (int)time(NULL), global_ident, (int) getpid(), priority_text, message); length = strlen(message); if (length == 0 || message[length - 1] != '\n') { fprintf(current_log_file, "\n"); } fflush(current_log_file); }
static int axfr_answer_packet(knot_pkt_t *pkt, struct xfr_proc *proc) { assert(pkt != NULL); assert(proc != NULL); /* Update counters. */ proc->npkts += 1; proc->nbytes += pkt->size; /* Init zone creator. */ zcreator_t zc = {.z = proc->contents, .master = false, .ret = KNOT_EOK }; const knot_pktsection_t *answer = knot_pkt_section(pkt, KNOT_ANSWER); for (uint16_t i = 0; i < answer->count; ++i) { const knot_rrset_t *rr = &answer->rr[i]; if (rr->type == KNOT_RRTYPE_SOA && node_rrtype_exists(zc.z->apex, KNOT_RRTYPE_SOA)) { return KNOT_NS_PROC_DONE; } else { int ret = zcreator_step(&zc, rr); if (ret != KNOT_EOK) { return KNOT_NS_PROC_FAIL; } } } return KNOT_NS_PROC_MORE; } int axfr_answer_process(knot_pkt_t *pkt, struct answer_data *adata) { if (pkt == NULL || adata == NULL) { return KNOT_NS_PROC_FAIL; } /* Check RCODE. */ uint8_t rcode = knot_wire_get_rcode(pkt->wire); if (rcode != KNOT_RCODE_NOERROR) { lookup_table_t *lut = lookup_by_id(knot_rcode_names, rcode); if (lut != NULL) { AXFRIN_LOG(LOG_ERR, "server responded with %s", lut->name); } return KNOT_NS_PROC_FAIL; } /* Initialize processing with first packet. */ if (adata->ext == NULL) { NS_NEED_TSIG_SIGNED(&adata->param->tsig_ctx, 0); AXFRIN_LOG(LOG_INFO, "starting"); int ret = axfr_answer_init(adata); if (ret != KNOT_EOK) { AXFRIN_LOG(LOG_ERR, "failed (%s)", knot_strerror(ret)); return KNOT_NS_PROC_FAIL; } } else { NS_NEED_TSIG_SIGNED(&adata->param->tsig_ctx, 100); } /* Process answer packet. */ int ret = axfr_answer_packet(pkt, (struct xfr_proc *)adata->ext); if (ret == KNOT_NS_PROC_DONE) { NS_NEED_TSIG_SIGNED(&adata->param->tsig_ctx, 0); /* This was the last packet, finalize zone and publish it. */ int fret = axfr_answer_finalize(adata); if (fret != KNOT_EOK) { ret = KNOT_NS_PROC_FAIL; } } return ret; }
static void print_header(const knot_pkt_t *packet, const style_t *style, const uint16_t ext_rcode) { char flags[64] = ""; uint8_t opcode_id; const char *rcode_str = "Unknown"; const char *opcode_str = "Unknown"; lookup_table_t *rcode, *opcode; // Get RCODE from Header and check for Extended RCODE from OPT RR. rcode = lookup_by_id(knot_rcode_names, ext_rcode); if (rcode != NULL) { rcode_str = rcode->name; } // Get OPCODE. opcode_id = knot_wire_get_opcode(packet->wire); opcode = lookup_by_id(knot_opcode_names, opcode_id); if (opcode != NULL) { opcode_str = opcode->name; } // Get flags. size_t flags_rest = sizeof(flags); const size_t flag_len = 4; if (knot_wire_get_qr(packet->wire) != 0 && flags_rest > flag_len) { flags_rest -= strlcat(flags, " qr", flags_rest); } if (knot_wire_get_aa(packet->wire) != 0 && flags_rest > flag_len) { flags_rest -= strlcat(flags, " aa", flags_rest); } if (knot_wire_get_tc(packet->wire) != 0 && flags_rest > flag_len) { flags_rest -= strlcat(flags, " tc", flags_rest); } if (knot_wire_get_rd(packet->wire) != 0 && flags_rest > flag_len) { flags_rest -= strlcat(flags, " rd", flags_rest); } if (knot_wire_get_ra(packet->wire) != 0 && flags_rest > flag_len) { flags_rest -= strlcat(flags, " ra", flags_rest); } if (knot_wire_get_z(packet->wire) != 0 && flags_rest > flag_len) { flags_rest -= strlcat(flags, " z", flags_rest); } if (knot_wire_get_ad(packet->wire) != 0 && flags_rest > flag_len) { flags_rest -= strlcat(flags, " ad", flags_rest); } if (knot_wire_get_cd(packet->wire) != 0 && flags_rest > flag_len) { strlcat(flags, " cd", flags_rest); } uint16_t id = knot_wire_get_id(packet->wire); uint16_t qdcount = knot_wire_get_qdcount(packet->wire); uint16_t ancount = knot_wire_get_ancount(packet->wire); uint16_t nscount = knot_wire_get_nscount(packet->wire); uint16_t arcount = knot_wire_get_arcount(packet->wire); if (knot_pkt_has_tsig(packet)) { arcount++; } // Print formatted info. switch (style->format) { case FORMAT_NSUPDATE: printf(";; ->>HEADER<<- opcode: %s; status: %s; id: %u\n" ";; Flags:%1s; " "ZONE: %u; PREREQ: %u; UPDATE: %u; ADDITIONAL: %u\n", opcode_str, rcode_str, id, flags, qdcount, ancount, nscount, arcount); break; default: printf(";; ->>HEADER<<- opcode: %s; status: %s; id: %u\n" ";; Flags:%1s; " "QUERY: %u; ANSWER: %u; AUTHORITY: %u; ADDITIONAL: %u\n", opcode_str, rcode_str, id, flags, qdcount, ancount, nscount, arcount); break; } }