hs-test: cache docker build in local filesystem
[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 (name == 0)
127     {
128       errmsg ("missing name to resolve");
129       return -99;
130     }
131
132   if (vec_len (name) > 127)
133     {
134       errmsg ("name too long");
135       return -99;
136     }
137
138   /* Construct the API message */
139   M (DNS_RESOLVE_NAME, mp);
140   memcpy (mp->name, name, vec_len (name));
141   vec_free (name);
142
143   /* send it... */
144   S (mp);
145   /* Wait for the reply */
146   W (ret);
147   return ret;
148 }
149
150 static int
151 api_dns_resolve_ip (vat_main_t * vam)
152 {
153   unformat_input_t *line_input = vam->input;
154   vl_api_dns_resolve_ip_t *mp;
155   int is_ip6 = -1;
156   ip4_address_t addr4;
157   ip6_address_t addr6;
158   int ret;
159
160   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
161     {
162       if (unformat (line_input, "%U", unformat_ip6_address, &addr6))
163         is_ip6 = 1;
164       else if (unformat (line_input, "%U", unformat_ip4_address, &addr4))
165         is_ip6 = 0;
166       else
167         break;
168     }
169
170   if (is_ip6 == -1)
171     {
172       errmsg ("missing address");
173       return -99;
174     }
175
176   /* Construct the API message */
177   M (DNS_RESOLVE_IP, mp);
178   mp->is_ip6 = is_ip6;
179   if (is_ip6)
180     memcpy (mp->address, &addr6, sizeof (addr6));
181   else
182     memcpy (mp->address, &addr4, sizeof (addr4));
183
184   /* send it... */
185   S (mp);
186   /* Wait for the reply */
187   W (ret);
188   return ret;
189 }
190
191 static int
192 api_dns_name_server_add_del (vat_main_t * vam)
193 {
194   unformat_input_t *i = vam->input;
195   vl_api_dns_name_server_add_del_t *mp;
196   u8 is_add = 1;
197   ip6_address_t ip6_server;
198   ip4_address_t ip4_server;
199   int ip6_set = 0;
200   int ip4_set = 0;
201   int ret = 0;
202
203   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
204     {
205       if (unformat (i, "%U", unformat_ip6_address, &ip6_server))
206         ip6_set = 1;
207       else if (unformat (i, "%U", unformat_ip4_address, &ip4_server))
208         ip4_set = 1;
209       else if (unformat (i, "del"))
210         is_add = 0;
211       else
212         {
213           clib_warning ("parse error '%U'", format_unformat_error, i);
214           return -99;
215         }
216     }
217
218   if (ip4_set && ip6_set)
219     {
220       errmsg ("Only one server address allowed per message");
221       return -99;
222     }
223   if ((ip4_set + ip6_set) == 0)
224     {
225       errmsg ("Server address required");
226       return -99;
227     }
228
229   /* Construct the API message */
230   M (DNS_NAME_SERVER_ADD_DEL, mp);
231
232   if (ip6_set)
233     {
234       memcpy (mp->server_address, &ip6_server, sizeof (ip6_address_t));
235       mp->is_ip6 = 1;
236     }
237   else
238     {
239       memcpy (mp->server_address, &ip4_server, sizeof (ip4_address_t));
240       mp->is_ip6 = 0;
241     }
242
243   mp->is_add = is_add;
244
245   /* send it... */
246   S (mp);
247
248   /* Wait for a reply, return good/bad news  */
249   W (ret);
250   return ret;
251 }
252
253 #include <dns/dns.api_test.c>
254
255 /*
256  * fd.io coding-style-patch-verification: ON
257  *
258  * Local Variables:
259  * eval: (c-set-style "gnu")
260  * End:
261  */