예제 #1
0
파일: rosedb_tool.c 프로젝트: idtek/knot
static int rosedb_get(struct cache *cache, MDB_txn *txn, int argc, char *argv[])
{
	knot_dname_t key[KNOT_DNAME_MAXLEN] = { '\0' };
	knot_dname_from_str(key, argv[0], sizeof(key));
	knot_dname_to_lower(key);

	char type_str[16] = { '\0' };

	struct iter it;
	int ret = cache_query_fetch(txn, cache->dbi, &it, key);
	while (ret == 0) {
		struct entry entry;
		cache_iter_val(&it, &entry);
		knot_rdata_t *rd = knot_rdataset_at(&entry.data.rrs, 0);
		knot_rrtype_to_string(entry.data.type, type_str, sizeof(type_str));
		printf("%s\t%s\tTTL=%u\tRDLEN=%u\t%s\t%s\n", argv[0], type_str,
		       knot_rdata_ttl(rd), knot_rdata_rdlen(rd), entry.threat_code, entry.syslog_ip);
		if (cache_iter_next(&it) != 0) {
			break;
		}
	}

	cache_iter_free(&it);

	return ret;
}
예제 #2
0
void debug_process_record(zs_scanner_t *s)
{
	uint32_t i;

	char rclass[32];
	char rtype[32];

	if (knot_rrclass_to_string(s->r_class, rclass, sizeof(rclass)) > 0 &&
	    knot_rrtype_to_string(s->r_type, rtype, sizeof(rtype)) > 0) {
		printf("LINE(%03"PRIu64") %s %6u %*s ",
		       s->line_counter, rclass, s->r_ttl, 5, rtype);
	} else {
		printf("LINE(%03"PRIu64") %u %6u %*u ",
		       s->line_counter, s->r_class, s->r_ttl, 5, s->r_type);
	}

	print_wire_dname(s->r_owner, s->r_owner_length);

	printf(" \\# %u ", s->r_data_length);

	for (i = 0; i < s->r_data_length; i++) {
		printf("%02X", (s->r_data)[i]);
	}
	printf("\n");
	fflush(stdout);
}
예제 #3
0
파일: exec.c 프로젝트: gitter-badger/knot
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);
}
예제 #4
0
파일: exec.c 프로젝트: gitter-badger/knot
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);
}
예제 #5
0
파일: rosedb_tool.c 프로젝트: idtek/knot
static int rosedb_list(struct cache *cache, MDB_txn *txn, int argc, char *argv[])
{
	MDB_cursor *cursor = cursor_acquire(txn, cache->dbi);
	MDB_val key, data;
	char dname_str[KNOT_DNAME_MAXLEN] = {'\0'};
	char type_str[16] = { '\0' };

	int ret = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
	while (ret == 0) {
		struct entry entry;
		unpack_entry(&data, &entry);
		knot_dname_to_str(dname_str, key.mv_data, sizeof(dname_str));
		knot_rrtype_to_string(entry.data.type, type_str, sizeof(type_str));
		printf("%s\t%s RDATA=%zuB\t%s\t%s\n", dname_str, type_str,
		       knot_rdataset_size(&entry.data.rrs), entry.threat_code, entry.syslog_ip);

		ret = mdb_cursor_get(cursor, &key, &data, MDB_NEXT);
	}

	cursor_release(cursor);

	return KNOT_EOK;
}
예제 #6
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;
}