nmsg  0.9.0
dns.c
Go to the documentation of this file.
1 /* dns nmsg message module */
2 
3 /*
4  * Copyright (c) 2009 by Farsight Security, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /* Import. */
20 
21 #include <wdns.h>
22 
23 #include "dns.pb-c.h"
24 
25 /* Exported via module context. */
26 
27 static NMSG_MSGMOD_FIELD_PRINTER(dns_name_print);
28 static NMSG_MSGMOD_FIELD_PRINTER(dns_type_print);
29 static NMSG_MSGMOD_FIELD_PRINTER(dns_class_print);
30 static NMSG_MSGMOD_FIELD_PRINTER(dns_rdata_print);
31 
32 /* Data. */
33 
34 struct nmsg_msgmod_field dns_fields[] = {
35  {
37  .name = "qname",
38  .print = dns_name_print
39  },
40  {
41  .type = nmsg_msgmod_ft_uint16,
42  .name = "qclass",
43  .print = dns_class_print
44  },
45  {
46  .type = nmsg_msgmod_ft_uint16,
47  .name = "qtype",
48  .print = dns_type_print
49  },
50  {
51  .type = nmsg_msgmod_ft_uint16,
52  .name = "section",
53  },
54  {
55  .type = nmsg_msgmod_ft_bytes,
56  .name = "rrname",
57  .print = dns_name_print
58  },
59  {
60  .type = nmsg_msgmod_ft_uint16,
61  .name = "rrclass",
62  .print = dns_class_print
63  },
64  {
65  .type = nmsg_msgmod_ft_uint16,
66  .name = "rrtype",
67  .print = dns_type_print
68  },
69  {
70  .type = nmsg_msgmod_ft_uint32,
71  .name = "rrttl",
72  },
73  {
74  .type = nmsg_msgmod_ft_bytes,
75  .name = "rdata",
77  .print = dns_rdata_print
78  },
79  NMSG_MSGMOD_FIELD_END
80 };
81 
82 /* Export. */
83 
84 struct nmsg_msgmod_plugin nmsg_msgmod_ctx = {
85  NMSG_MSGMOD_REQUIRED_INIT,
86  .vendor = NMSG_VENDOR_BASE,
87  .msgtype = { NMSG_VENDOR_BASE_DNS_ID, NMSG_VENDOR_BASE_DNS_NAME },
88 
89  .pbdescr = &nmsg__base__dns__descriptor,
90  .fields = dns_fields
91 };
92 
93 /* Private. */
94 
95 static nmsg_res
96 dns_name_print(nmsg_message_t msg,
97  struct nmsg_msgmod_field *field,
98  void *ptr,
99  struct nmsg_strbuf *sb,
100  const char *endline)
101 {
102  ProtobufCBinaryData *rrname = ptr;
103  char name[WDNS_PRESLEN_NAME];
105 
106  if (rrname->data != NULL &&
107  rrname->len > 0 &&
108  rrname->len <= WDNS_MAXLEN_NAME)
109  {
110  wdns_domain_to_str(rrname->data, rrname->len, name);
111  res = nmsg_strbuf_append(sb, "%s: %s%s", field->name,
112  name, endline);
113  }
114  return (res);
115 }
116 
117 static nmsg_res
118 dns_type_print(nmsg_message_t msg,
119  struct nmsg_msgmod_field *field,
120  void *ptr,
121  struct nmsg_strbuf *sb,
122  const char *endline)
123 {
124  uint16_t *rrtype = ptr;
125  const char *s;
127 
128  s = wdns_rrtype_to_str(*rrtype);
129  res = nmsg_strbuf_append(sb, "%s: %s (%hu)%s",
130  field->name,
131  s ? s : "<UNKNOWN>",
132  *rrtype, endline);
133  return (res);
134 }
135 
136 static nmsg_res
137 dns_class_print(nmsg_message_t msg,
138  struct nmsg_msgmod_field *field,
139  void *ptr,
140  struct nmsg_strbuf *sb,
141  const char *endline)
142 {
143  uint16_t *rrclass = ptr;
144  const char *s;
146 
147  s = wdns_rrclass_to_str(*rrclass);
148  res = nmsg_strbuf_append(sb, "%s: %s (%hu)%s",
149  field->name,
150  s ? s : "<UNKNOWN>",
151  *rrclass, endline);
152  return (res);
153 }
154 
155 static nmsg_res
156 dns_rdata_print(nmsg_message_t msg,
157  struct nmsg_msgmod_field *field __attribute__((unused)),
158  void *ptr,
159  struct nmsg_strbuf *sb,
160  const char *endline)
161 {
162  Nmsg__Base__Dns *dns = (Nmsg__Base__Dns *) nmsg_message_get_payload(msg);
163  ProtobufCBinaryData *rdata = ptr;
164  nmsg_res res;
165  char *buf;
166 
167  if (dns == NULL)
168  return (nmsg_res_failure);
169 
170  if (dns->has_rrtype == false || dns->has_rrclass == false)
171  return (nmsg_res_failure);
172 
173  buf = wdns_rdata_to_str(rdata->data, rdata->len, dns->rrtype, dns->rrclass);
174  if (buf == NULL)
175  goto parse_error;
176 
177  res = nmsg_strbuf_append(sb, "rdata: %s%s", buf, endline);
178  free(buf);
179  return (res);
180 
181 parse_error:
182  free(buf);
183  nmsg_strbuf_append(sb, "rdata: ### PARSE ERROR ###\n");
184  return (nmsg_res_parse_error);
185 }
186 
Structure exported by message modules to implement a new message type.
nmsg_res
nmsg result code
Definition: res.h:25
success
Definition: res.h:26
Protobuf byte array.
Definition: msgmod.h:78
Structure mapping protocol buffer schema fields to nmsg_msgmod_field_type values for "transparent" mo...
Protobuf uint32.
Definition: msgmod.h:102
generic failure
Definition: res.h:27
Protobuf uint32.
Definition: msgmod.h:99
unable to parse input
Definition: res.h:36
nmsg_msgmod_field_type type
Intended (nmsg) type of this protobuf field.
const char * name
Name of the field.
void * nmsg_message_get_payload(nmsg_message_t msg)
WARNING: experts only.
#define NMSG_MSGMOD_FIELD_REPEATED
field is repeated
Definition: msgmod.h:123
nmsg_res nmsg_strbuf_append(struct nmsg_strbuf *sb, const char *fmt,...)
Append to a string buffer.
Definition: strbuf.c:44
String buffer.
Definition: strbuf.h:32