Line data Source code
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 0 : static void vl_api_dns_resolve_name_reply_t_handler
43 : (vl_api_dns_resolve_name_reply_t * mp)
44 : {
45 0 : vat_main_t *vam = dns_test_main.vat_main;
46 0 : i32 retval = (i32) clib_net_to_host_u32 (mp->retval);
47 0 : if (retval == 0)
48 : {
49 0 : if (mp->ip4_set)
50 0 : clib_warning ("resolved: %U", format_ip4_address, mp->ip4_address);
51 0 : if (mp->ip6_set)
52 0 : clib_warning ("resolved: %U", format_ip6_address, mp->ip6_address);
53 : }
54 0 : if (vam->async_mode)
55 0 : vam->async_errors += (retval < 0);
56 : else
57 : {
58 0 : vam->retval = retval;
59 0 : vam->result_ready = 1;
60 : }
61 0 : }
62 :
63 0 : static void vl_api_dns_resolve_ip_reply_t_handler
64 : (vl_api_dns_resolve_ip_reply_t * mp)
65 : {
66 0 : vat_main_t *vam = dns_test_main.vat_main;
67 0 : i32 retval = (i32) clib_net_to_host_u32 (mp->retval);
68 0 : if (retval == 0)
69 0 : clib_warning ("resolved: %s", mp->name);
70 0 : if (vam->async_mode)
71 0 : vam->async_errors += (retval < 0);
72 : else
73 : {
74 0 : vam->retval = retval;
75 0 : vam->result_ready = 1;
76 : }
77 0 : }
78 :
79 : static int
80 0 : api_dns_enable_disable (vat_main_t * vam)
81 : {
82 : vl_api_dns_enable_disable_t *mp;
83 0 : unformat_input_t *i = vam->input;
84 0 : int enable = 1;
85 : int ret;
86 :
87 : /* Parse args required to build the message */
88 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
89 : {
90 0 : if (unformat (i, "disable"))
91 0 : enable = 0;
92 0 : else if (unformat (i, "enable"))
93 0 : enable = 1;
94 : else
95 0 : break;
96 : }
97 :
98 : /* Construct the API message */
99 0 : M (DNS_ENABLE_DISABLE, mp);
100 0 : mp->enable = enable;
101 :
102 : /* send it... */
103 0 : S (mp);
104 :
105 : /* Wait for a reply... */
106 0 : W (ret);
107 0 : return ret;
108 : }
109 :
110 : static int
111 0 : api_dns_resolve_name (vat_main_t * vam)
112 : {
113 0 : unformat_input_t *line_input = vam->input;
114 : vl_api_dns_resolve_name_t *mp;
115 0 : u8 *name = 0;
116 : int ret;
117 :
118 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
119 : {
120 0 : if (unformat (line_input, "%s", &name))
121 : ;
122 : else
123 0 : break;
124 : }
125 :
126 0 : if (name == 0)
127 : {
128 0 : errmsg ("missing name to resolve");
129 0 : return -99;
130 : }
131 :
132 0 : if (vec_len (name) > 127)
133 : {
134 0 : errmsg ("name too long");
135 0 : return -99;
136 : }
137 :
138 : /* Construct the API message */
139 0 : M (DNS_RESOLVE_NAME, mp);
140 0 : memcpy (mp->name, name, vec_len (name));
141 0 : vec_free (name);
142 :
143 : /* send it... */
144 0 : S (mp);
145 : /* Wait for the reply */
146 0 : W (ret);
147 0 : return ret;
148 : }
149 :
150 : static int
151 0 : api_dns_resolve_ip (vat_main_t * vam)
152 : {
153 0 : unformat_input_t *line_input = vam->input;
154 : vl_api_dns_resolve_ip_t *mp;
155 0 : int is_ip6 = -1;
156 : ip4_address_t addr4;
157 : ip6_address_t addr6;
158 : int ret;
159 :
160 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
161 : {
162 0 : if (unformat (line_input, "%U", unformat_ip6_address, &addr6))
163 0 : is_ip6 = 1;
164 0 : else if (unformat (line_input, "%U", unformat_ip4_address, &addr4))
165 0 : is_ip6 = 0;
166 : else
167 0 : break;
168 : }
169 :
170 0 : if (is_ip6 == -1)
171 : {
172 0 : errmsg ("missing address");
173 0 : return -99;
174 : }
175 :
176 : /* Construct the API message */
177 0 : M (DNS_RESOLVE_IP, mp);
178 0 : mp->is_ip6 = is_ip6;
179 0 : if (is_ip6)
180 0 : memcpy (mp->address, &addr6, sizeof (addr6));
181 : else
182 0 : memcpy (mp->address, &addr4, sizeof (addr4));
183 :
184 : /* send it... */
185 0 : S (mp);
186 : /* Wait for the reply */
187 0 : W (ret);
188 0 : return ret;
189 : }
190 :
191 : static int
192 0 : api_dns_name_server_add_del (vat_main_t * vam)
193 : {
194 0 : unformat_input_t *i = vam->input;
195 : vl_api_dns_name_server_add_del_t *mp;
196 0 : u8 is_add = 1;
197 : ip6_address_t ip6_server;
198 : ip4_address_t ip4_server;
199 0 : int ip6_set = 0;
200 0 : int ip4_set = 0;
201 0 : int ret = 0;
202 :
203 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
204 : {
205 0 : if (unformat (i, "%U", unformat_ip6_address, &ip6_server))
206 0 : ip6_set = 1;
207 0 : else if (unformat (i, "%U", unformat_ip4_address, &ip4_server))
208 0 : ip4_set = 1;
209 0 : else if (unformat (i, "del"))
210 0 : is_add = 0;
211 : else
212 : {
213 0 : clib_warning ("parse error '%U'", format_unformat_error, i);
214 0 : return -99;
215 : }
216 : }
217 :
218 0 : if (ip4_set && ip6_set)
219 : {
220 0 : errmsg ("Only one server address allowed per message");
221 0 : return -99;
222 : }
223 0 : if ((ip4_set + ip6_set) == 0)
224 : {
225 0 : errmsg ("Server address required");
226 0 : return -99;
227 : }
228 :
229 : /* Construct the API message */
230 0 : M (DNS_NAME_SERVER_ADD_DEL, mp);
231 :
232 0 : if (ip6_set)
233 : {
234 0 : memcpy (mp->server_address, &ip6_server, sizeof (ip6_address_t));
235 0 : mp->is_ip6 = 1;
236 : }
237 : else
238 : {
239 0 : memcpy (mp->server_address, &ip4_server, sizeof (ip4_address_t));
240 0 : mp->is_ip6 = 0;
241 : }
242 :
243 0 : mp->is_add = is_add;
244 :
245 : /* send it... */
246 0 : S (mp);
247 :
248 : /* Wait for a reply, return good/bad news */
249 0 : W (ret);
250 0 : 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 : */
|