@@ -2524,37 +2524,61 @@ VLIB_CLI_COMMAND (show_dns_cache_command) =
25242524/* *INDENT-ON* */
25252525
25262526static clib_error_t *
2527- dns_enable_disable_command_fn (vlib_main_t * vm , unformat_input_t * input ,
2528- vlib_cli_command_t * cmd )
2527+ dns_enable_disable_common (vlib_main_t * vm , u32 enable_disable )
25292528{
25302529 dns_main_t * dm = & dns_main ;
2531- u32 enable_disable ;
25322530 int rv ;
25332531
2534- enable_disable = 0 ;
2535-
2536- while (unformat_check_input (input ) != UNFORMAT_END_OF_INPUT )
2537- {
2538- if (unformat (input , "enable" ))
2539- enable_disable = 1 ;
2540- else if (unformat (input , "disable" ))
2541- enable_disable = 0 ;
2542- else
2543- return clib_error_return (0 , "unknown input `%U'" ,
2544- format_unformat_error , input );
2545- }
2546-
25472532 rv = dns_enable_disable (vm , dm , enable_disable );
25482533 if (rv )
25492534 return clib_error_return (0 , "%U" , format_vnet_api_errno , rv );
25502535
25512536 return 0 ;
25522537}
25532538
2554- VLIB_CLI_COMMAND (dns_enable_disable_command ) = {
2539+ static clib_error_t *
2540+ dns_enable_command_fn (vlib_main_t * vm , unformat_input_t * input ,
2541+ vlib_cli_command_t * cmd )
2542+ {
2543+ if (unformat_check_input (input ) != UNFORMAT_END_OF_INPUT )
2544+ return clib_error_return (0 , "unknown input `%U'" ,
2545+ format_unformat_error , input );
2546+
2547+ return dns_enable_disable_common (vm , 1 );
2548+ }
2549+
2550+ static clib_error_t *
2551+ dns_disable_command_fn (vlib_main_t * vm , unformat_input_t * input ,
2552+ vlib_cli_command_t * cmd )
2553+ {
2554+ if (unformat_check_input (input ) != UNFORMAT_END_OF_INPUT )
2555+ return clib_error_return (0 , "unknown input `%U'" ,
2556+ format_unformat_error , input );
2557+
2558+ return dns_enable_disable_common (vm , 0 );
2559+ }
2560+
2561+ /*
2562+ * "dns" deliberately has no function of its own. If it had one, the CLI
2563+ * dispatcher would fall back to running it whenever a failing sub-command
2564+ * (e.g. "dns cache") returns an error, silently toggling name resolution
2565+ * with already-consumed input.
2566+ */
2567+ VLIB_CLI_COMMAND (dns_command ) = {
25552568 .path = "dns" ,
25562569 .short_help = "dns [enable][disable]" ,
2557- .function = dns_enable_disable_command_fn ,
2570+ };
2571+
2572+ VLIB_CLI_COMMAND (dns_enable_command ) = {
2573+ .path = "dns enable" ,
2574+ .short_help = "dns enable" ,
2575+ .function = dns_enable_command_fn ,
2576+ };
2577+
2578+ VLIB_CLI_COMMAND (dns_disable_command ) = {
2579+ .path = "dns disable" ,
2580+ .short_help = "dns disable" ,
2581+ .function = dns_disable_command_fn ,
25582582};
25592583
25602584static clib_error_t *
@@ -2684,6 +2708,9 @@ dns_cache_add_del_command_fn (vlib_main_t * vm,
26842708 {
26852709 if (unformat (input , "%v" , & name ))
26862710 {
2711+ /* The cache hash uses strlen/strcmp, so the key must be
2712+ * a NULL-terminated C string */
2713+ dns_terminate_c_string (& name );
26872714 rv = dns_delete_by_name (dm , name );
26882715 switch (rv )
26892716 {
@@ -2715,13 +2742,17 @@ dns_cache_add_del_command_fn (vlib_main_t * vm,
27152742 /* Note: dns_add_static_entry consumes the name vector if OK... */
27162743 if (unformat (input , "%U" , unformat_dns_reply , & dns_reply_data , & name ))
27172744 {
2745+ /* The cache hash uses strlen/strcmp, so the key must be
2746+ * a NULL-terminated C string */
2747+ dns_terminate_c_string (& name );
27182748 rv = dns_add_static_entry (dm , name , dns_reply_data );
27192749 switch (rv )
27202750 {
27212751 case VNET_API_ERROR_ENTRY_ALREADY_EXISTS :
2752+ error = clib_error_return (0 , "%v already in the cache..." , name );
27222753 vec_free (name );
27232754 vec_free (dns_reply_data );
2724- return clib_error_return ( 0 , "%v already in the cache..." , name ) ;
2755+ return error ;
27252756 case 0 :
27262757 return 0 ;
27272758
0 commit comments