Ejemplo n.º 1
0
static void print_section_dig(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];
        uint16_t rrset_rdata_count = rrset->rrs.rr_count;
        for (uint16_t j = 0; j < rrset_rdata_count; j++) {
            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;
            }
            printf("%s\n", buf);
        }
    }

    free(buf);
}
Ejemplo n.º 2
0
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);
}
Ejemplo n.º 3
0
static int rosedb_log_message(char *stream, size_t *maxlen, knot_pkt_t *pkt,
                              const char *threat_code, struct query_data *qdata)
{
	char dname_buf[KNOT_DNAME_MAXLEN] = {'\0'};
	struct sockaddr_storage addr;
	socklen_t addr_len = sizeof(addr);
	time_t now = time(NULL);
	struct tm tm;
	gmtime_r(&now, &tm);

	/* Field 1 Timestamp (UTC). */
	STREAM_WRITE(stream, maxlen, strftime, "%Y-%m-%d %H:%M:%S\t", &tm);

	/* Field 2/3 Remote, local address. */
	const struct sockaddr *remote = (const struct sockaddr *)qdata->param->remote;
	memcpy(&addr, remote, sockaddr_len(remote));
	int client_port = sockaddr_port(&addr);
	sockaddr_port_set(&addr, 0);
	STREAM_WRITE(stream, maxlen, sockaddr_tostr, &addr);
	STREAM_WRITE(stream, maxlen, snprintf, "\t");
	getsockname(qdata->param->socket, (struct sockaddr *)&addr, &addr_len);
	int server_port = sockaddr_port(&addr);
	sockaddr_port_set(&addr, 0);
	STREAM_WRITE(stream, maxlen, sockaddr_tostr, &addr);
	STREAM_WRITE(stream, maxlen, snprintf, "\t");

	/* Field 4/5 Local, remote port. */
	STREAM_WRITE(stream, maxlen, snprintf, "%d\t%d\t", client_port, server_port);

	/* Field 6 Threat ID. */
	STREAM_WRITE(stream, maxlen, snprintf, "%s\t", threat_code);

	/* Field 7 - 13 NULL */
	STREAM_WRITE(stream, maxlen, snprintf, "\t\t\t\t\t\t\t");

	/* Field 14 QNAME */
	knot_dname_to_str(dname_buf, knot_pkt_qname(qdata->query), sizeof(dname_buf));
	STREAM_WRITE(stream, maxlen, snprintf, "%s\t", dname_buf);

	/* Field 15 Resolution (0 = local, 1 = lookup)*/
	STREAM_WRITE(stream, maxlen, snprintf, "0\t");

	/* Field 16 RDATA.
	 * - Return randomly RDATA in the answer section (probabilistic rotation).
	 * - Empty if no answer.
	 */
	const knot_pktsection_t *ans = knot_pkt_section(pkt, KNOT_ANSWER);
	if (ans->count > 0) {
		const knot_rrset_t *rr = &ans->rr[knot_random_uint16_t() % ans->count];
		int ret = knot_rrset_txt_dump_data(rr, 0, stream, *maxlen, &KNOT_DUMP_STYLE_DEFAULT);
		if (ret < 0) {
			return ret;
		}
		stream_skip(&stream, maxlen, ret);
	}
	STREAM_WRITE(stream, maxlen, snprintf, "\t");

	/* Field 17 Connection type. */
	STREAM_WRITE(stream, maxlen, snprintf, "%s\t",
	             net_is_connected(qdata->param->socket) ? "TCP" : "UDP");

	/* Field 18 Query type. */
	char type_str[16] = { '\0' };
	knot_rrtype_to_string(knot_pkt_qtype(qdata->query), type_str, sizeof(type_str));
	STREAM_WRITE(stream, maxlen, snprintf, "%s\t", type_str);

	/* Field 19 First authority. */
	const knot_pktsection_t *ns = knot_pkt_section(pkt, KNOT_AUTHORITY);
	if (ns->count > 0 && ns->rr[0].type == KNOT_RRTYPE_NS) {
		const knot_dname_t *label = knot_ns_name(&ns->rr[0].rrs, 0);
		memset(dname_buf, 0, sizeof(dname_buf));
		memcpy(dname_buf, label + 1, *label);
		STREAM_WRITE(stream, maxlen, snprintf, "%s", dname_buf);
	}

	return KNOT_EOK;
}