dns: remove api boilerplate
[vpp.git] / src / plugins / dns / dns_test.c
1 /*
2  * dns.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <stdbool.h>
22 #include <vnet/ip/ip.h>
23
24 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
25
26 /* Declare message IDs */
27 #include <dns/dns.api_enum.h>
28 #include <dns/dns.api_types.h>
29
30 typedef struct
31 {
32   /* API message ID base */
33   u16 msg_id_base;
34   vat_main_t *vat_main;
35 } dns_test_main_t;
36
37 dns_test_main_t dns_test_main;
38
39 #define __plugin_msg_base dns_test_main.msg_id_base
40 #include <vlibapi/vat_helper_macros.h>
41
42 static void vl_api_dns_resolve_name_reply_t_handler
43   (vl_api_dns_resolve_name_reply_t * mp)
44 {
45   vat_main_t *vam = dns_test_main.vat_main;
46   i32 retval = (i32) clib_net_to_host_u32 (mp->retval);
47   if (retval == 0)
48     {
49       if (mp->ip4_set)
50         clib_warning ("resolved: %U", format_ip4_address, mp->ip4_address);
51       if (mp->ip6_set)
52         clib_warning ("resolved: %U", format_ip6_address, mp->ip6_address);
53     }
54   if (vam->async_mode)
55     vam->async_errors += (retval < 0);
56   else
57     {
58       vam->retval = retval;
59       vam->result_ready = 1;
60     }
61 }
62
63 static void vl_api_dns_resolve_ip_reply_t_handler
64   (vl_api_dns_resolve_ip_reply_t * mp)
65 {
66   vat_main_t *vam = dns_test_main.vat_main;
67   i32 retval = (i32) clib_net_to_host_u32 (mp->retval);
68   if (retval == 0)
69     clib_warning ("resolved: %s", mp->name);
70   if (vam->async_mode)
71     vam->async_errors += (retval < 0);
72   else
73     {
74       vam->retval = retval;
75       vam->result_ready = 1;
76     }
77 }
78
79 static int
80 api_dns_enable_disable (vat_main_t * vam)
81 {
82   vl_api_dns_enable_disable_t *mp;
83   unformat_input_t *i = vam->input;
84   int enable = 1;
85   int ret;
86
87   /* Parse args required to build the message */
88   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
89     {
90       if (unformat (i, "disable"))
91         enable = 0;
92       else if (unformat (i, "enable"))
93         enable = 1;
94       else
95         break;
96     }
97
98   /* Construct the API message */
99   M (DNS_ENABLE_DISABLE, mp);
100   mp->enable = enable;
101
102   /* send it... */
103   S (mp);
104
105   /* Wait for a reply... */
106   W (ret);
107   return ret;
108 }
109
110 static int
111 api_dns_resolve_name (vat_main_t * vam)
112 {
113   unformat_input_t *line_input = vam->input;
114   vl_api_dns_resolve_name_t *mp;
115   u8 *name = 0;
116   int ret;
117
118   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
119     {
120       if (unformat (line_input, "%s", &name))
121         ;
122       else
123         break;
124     }
125
126   if (vec_len (name) > 127)
127     {
128       errmsg ("name too long");
129       return -99;
130     }
131
132   /* Construct the API message */
133   M (DNS_RESOLVE_NAME, mp);
134   memcpy (mp->name, name, vec_len (name));
135   vec_free (name);
136
137   /* send it... */
138   S (mp);
139   /* Wait for the reply */
140   W (ret);
141   return ret;
142 }
143
144 static int
145 api_dns_resolve_ip (vat_main_t * vam)
146 {
147   unformat_input_t *line_input = vam->input;
148   vl_api_dns_resolve_ip_t *mp;
149   int is_ip6 = -1;
150   ip4_address_t addr4;
151   ip6_address_t addr6;
152   int ret;
153
154   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
155     {
156       if (unformat (line_input, "%U", unformat_ip6_address, &addr6))
157         is_ip6 = 1;
158       else if (unformat (line_input, "%U", unformat_ip4_address, &addr4))
159         is_ip6 = 0;
160       else
161         break;
162     }
163
164   if (is_ip6 == -1)
165     {
166       errmsg ("missing address");
167       return -99;
168     }
169
170   /* Construct the API message */
171   M (DNS_RESOLVE_IP, mp);
172   mp->is_ip6 = is_ip6;
173   if (is_ip6)
174     memcpy (mp->address, &addr6, sizeof (addr6));
175   else
176     memcpy (mp->address, &addr4, sizeof (addr4));
177
178   /* send it... */
179   S (mp);
180   /* Wait for the reply */
181   W (ret);
182   return ret;
183 }
184
185 static int
186 api_dns_name_server_add_del (vat_main_t * vam)
187 {
188   unformat_input_t *i = vam->input;
189   vl_api_dns_name_server_add_del_t *mp;
190   u8 is_add = 1;
191   ip6_address_t ip6_server;
192   ip4_address_t ip4_server;
193   int ip6_set = 0;
194   int ip4_set = 0;
195   int ret = 0;
196
197   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
198     {
199       if (unformat (i, "%U", unformat_ip6_address, &ip6_server))
200         ip6_set = 1;
201       else if (unformat (i, "%U", unformat_ip4_address, &ip4_server))
202         ip4_set = 1;
203       else if (unformat (i, "del"))
204         is_add = 0;
205       else
206         {
207           clib_warning ("parse error '%U'", format_unformat_error, i);
208           return -99;
209         }
210     }
211
212   if (ip4_set && ip6_set)
213     {
214       errmsg ("Only one server address allowed per message");
215       return -99;
216     }
217   if ((ip4_set + ip6_set) == 0)
218     {
219       errmsg ("Server address required");
220       return -99;
221     }
222
223   /* Construct the API message */
224   M (DNS_NAME_SERVER_ADD_DEL, mp);
225
226   if (ip6_set)
227     {
228       memcpy (mp->server_address, &ip6_server, sizeof (ip6_address_t));
229       mp->is_ip6 = 1;
230     }
231   else
232     {
233       memcpy (mp->server_address, &ip4_server, sizeof (ip4_address_t));
234       mp->is_ip6 = 0;
235     }
236
237   mp->is_add = is_add;
238
239   /* send it... */
240   S (mp);
241
242   /* Wait for a reply, return good/bad news  */
243   W (ret);
244   return ret;
245 }
246
247 #include <dns/dns.api_test.c>
248
249 /*
250  * fd.io coding-style-patch-verification: ON
251  *
252  * Local Variables:
253  * eval: (c-set-style "gnu")
254  * End:
255  */