Skip to content

Commit

Permalink
Added option to print string length of Query and Answer
Browse files Browse the repository at this point in the history
  • Loading branch information
gamelinux committed Mar 16, 2017
1 parent 945fcea commit 215e351
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,10 @@ void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
json_t *json_proto;
json_t *json_class;
json_t *json_query;
json_t *json_query_len;
json_t *json_type;
json_t *json_answer;
json_t *json_answer_len;
json_t *json_ttl;
json_t *json_count;
size_t data_flags = 0;
Expand Down Expand Up @@ -925,6 +927,13 @@ void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
json_decref(json_query);
}

/* Print query length */
if (config.fieldsf & FIELD_QUERY_LEN) {
json_query_len = json_integer(strlen(l->qname));
json_object_set(jdata, JSON_QUERY_LEN, json_query_len);
json_decref(json_query_len);
}

/* Print type */
if (config.fieldsf & FIELD_TYPE) {
json_type = json_string(rr_type);
Expand All @@ -940,6 +949,13 @@ void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
json_decref(json_answer);
}

/* Print answer length */
if (config.fieldsf & FIELD_ANSWER_LEN) {
json_answer_len = json_integer(strlen(rr_rcode));
json_object_set(jdata, JSON_ANSWER_LEN, json_answer_len);
json_decref(json_answer_len);
}

/* Print TTL */
if (config.fieldsf & FIELD_TTL) {
json_ttl = json_integer(PASSET_ERR_TTL);
Expand All @@ -962,6 +978,13 @@ void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
json_decref(json_answer);
}

/* Print answer length */
if (config.fieldsf & FIELD_ANSWER_LEN) {
json_answer_len = json_integer(strlen(p->answer));
json_object_set(jdata, JSON_ANSWER_LEN, json_answer_len);
json_decref(json_answer_len);
}

/* Print TTL */
if (config.fieldsf & FIELD_TTL) {
json_ttl = json_integer(p->rr->_ttl);
Expand Down Expand Up @@ -1061,6 +1084,13 @@ void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
offset += snprintf(output+offset, sizeof(buffer) - offset, "%s", l->qname);
}

/* Print query length */
if (config.fieldsf & FIELD_QUERY_LEN) {
if (offset != 0)
offset += snprintf(output+offset, sizeof(buffer) - offset, "%s", d);
offset += snprintf(output+offset, sizeof(buffer) - offset, "%zd", strlen(l->qname));
}

/* Print type */
if (config.fieldsf & FIELD_TYPE) {
if (offset != 0)
Expand All @@ -1078,6 +1108,16 @@ void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
offset += snprintf(output+offset, sizeof(buffer) - offset, "%s", p->answer);
}

/* Print answer length*/
if (config.fieldsf & FIELD_ANSWER_LEN) {
if (offset != 0)
offset += snprintf(output+offset, sizeof(buffer) - offset, "%s", d);
if (is_err_record)
offset += snprintf(output+offset, sizeof(buffer) - offset, "%zd", strlen(rr_rcode));
else
offset += snprintf(output+offset, sizeof(buffer) - offset, "%zd", strlen(p->answer));
}

/* Print TTL */
if (config.fieldsf & FIELD_TTL) {
if (offset != 0)
Expand Down Expand Up @@ -1519,6 +1559,11 @@ void parse_field_flags(char *args)
dlog("[D] Enabling field: FIELD_QUERY\n");
ok++;
break;
case 'L': /* Query Length */
config.fieldsf |= FIELD_QUERY_LEN;
dlog("[D] Enabling field: FIELD_QUERY_LEN\n");
ok++;
break;
case 'T': /* Type */
config.fieldsf |= FIELD_TYPE;
dlog("[D] Enabling field: FIELD_TYPE\n");
Expand All @@ -1529,6 +1574,11 @@ void parse_field_flags(char *args)
dlog("[D] Enabling field: FIELD_ANSWER\n");
ok++;
break;
case 'l': /* Answer Lenght */
config.fieldsf |= FIELD_ANSWER_LEN;
dlog("[D] Enabling field: FIELD_ANSWER_LEN\n");
ok++;
break;
case 't': /* TTL */
config.fieldsf |= FIELD_TTL;
dlog("[D] Enabling field: FIELD_TTL\n");
Expand Down
4 changes: 4 additions & 0 deletions src/dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
#define FIELD_TIMESTAMP_YMDHMS 0x0400
#define FIELD_PROTO 0x0800
#define FIELD_HOSTNAME 0x1000
#define FIELD_QUERY_LEN 0x2000
#define FIELD_ANSWER_LEN 0x4000

/* Static values for print_passet() */
#define PASSET_ERR_TTL 0
Expand All @@ -89,8 +91,10 @@
#define JSON_PROTO "proto"
#define JSON_CLASS "class"
#define JSON_QUERY "query"
#define JSON_QUERY_LEN "query_len"
#define JSON_TYPE "type"
#define JSON_ANSWER "answer"
#define JSON_ANSWER_LEN "answer_len"
#define JSON_TTL "ttl"
#define JSON_COUNT "count"
#define JSON_HOSTNAME "hostname"
Expand Down
2 changes: 1 addition & 1 deletion src/passivedns.c
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ void usage()
olog(" H: YMD-HMS Stamp S: Timestamp(s) M: Timestamp(ms) c: Client IP \n");
olog(" s: Server IP C: Class Q: Query T: Type \n");
olog(" A: Answer t: TTL p: Protocol n: Count\n");
olog(" h: hostname\n");
olog(" h: hostname L: QueryLength l: AnswerLength\n");
olog("\n");
olog(" FLAGS:\n");
olog("\n");
Expand Down

0 comments on commit 215e351

Please sign in to comment.