Skip to content

Commit

Permalink
feat(console): Added command getaddrinfo, set/get dnsserver to consol…
Browse files Browse the repository at this point in the history
…e_cmd_ping
  • Loading branch information
espressif-abhikroy committed May 31, 2024
1 parent 3f12ef6 commit a5f7452
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 6 deletions.
18 changes: 15 additions & 3 deletions components/console_cmd_ping/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
idf_component_register(SRCS "console_ping.c"
idf_component_register(SRCS "console_ping.c" "console_getaddrinfo.c" "console_getsetdnsserver.c"
INCLUDE_DIRS "."
PRIV_REQUIRES esp_netif console
WHOLE_ARCHIVE)
PRIV_REQUIRES esp_netif console)

if(CONFIG_PING_CMD_AUTO_REGISTRATION)
target_link_libraries(${COMPONENT_LIB} "-u console_cmd_ping_register")
endif()

if(CONFIG_GETADDRINFO_CMD_AUTO_REGISTRATION)
target_link_libraries(${COMPONENT_LIB} "-u console_cmd_getaddrinfo_register")
endif()

if(CONFIG_DNSSERVER_CMD_AUTO_REGISTRATION)
target_link_libraries(${COMPONENT_LIB} "-u console_cmd_setdnsserver_register")
target_link_libraries(${COMPONENT_LIB} "-u console_cmd_getdnsserver_register")
endif()
21 changes: 21 additions & 0 deletions components/console_cmd_ping/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
menu "Ping command Configuration"

config PING_CMD_AUTO_REGISTRATION
bool "Enable Console command ping Auto-registration"
default y
help
Enabling this allows for the autoregistration of the ping command.

config GETADDRINFO_CMD_AUTO_REGISTRATION
bool "Enable Console command getaddrinfo Auto-registration"
default y
help
Enabling this allows for the autoregistration of the getaddrinfo command.

config DNSSERVER_CMD_AUTO_REGISTRATION
bool "Enable Console command setdnsserver/getdnsserver Auto-registration"
default y
help
Enabling this allows for the autoregistration of the setdnsserver/getdnsserver command.

endmenu
2 changes: 2 additions & 0 deletions components/console_cmd_ping/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ The component provides a console where the 'ping' command can be executed.
### Adding a plugin command or component:
To add a plugin command or any component from IDF component manager into your project, simply include an entry within the `idf_component.yml` file.

Note: **Auto-registration** of a specific plugin command can be disabled from menuconfig.

For more details refer [IDF Component Manager](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html)


Expand Down
149 changes: 149 additions & 0 deletions components/console_cmd_ping/console_getaddrinfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "lwip/inet.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "esp_console.h"
#include "esp_log.h"
#include "argtable3/argtable3.h"
#include <netdb.h>
#include "console_ping.h"


static const char *TAG = "console_getaddrinfo";

#if CONFIG_GETADDRINFO_CMD_AUTO_REGISTRATION
/**
* Static registration of this plugin is achieved by defining the plugin description
* structure and placing it into .console_cmd_desc section.
* The name of the section and its placement is determined by linker.lf file in 'plugins' component.
*/
static const console_cmd_plugin_desc_t __attribute__((section(".console_cmd_desc"), used)) PLUGIN = {
.name = "console_cmd_getddrinfo",
.plugin_regd_fn = &console_cmd_getaddrinfo_register
};
#endif


static struct {
struct arg_str *family;
struct arg_str *flags;
struct arg_str *hostname;
struct arg_end *end;
} getddrinfo_args;

static int do_getddrinfo_cmd(int argc, char **argv)
{
char ip_str[INET6_ADDRSTRLEN];
struct addrinfo hint = {0};
struct addrinfo *res = NULL, *res_tmp = NULL;

int nerrors = arg_parse(argc, argv, (void **)&getddrinfo_args);
if (nerrors != 0) {
arg_print_errors(stderr, getddrinfo_args.end, argv[0]);
return 1;
}

if (getddrinfo_args.family->count > 0) {
if (strcmp(getddrinfo_args.family->sval[0], "AF_INET") == 0) {
hint.ai_family = AF_INET;
} else if (strcmp(getddrinfo_args.family->sval[0], "AF_INET6") == 0) {
hint.ai_family = AF_INET6;
} else if (strcmp(getddrinfo_args.family->sval[0], "AF_UNSPEC") == 0) {
hint.ai_family = AF_UNSPEC;
} else {
ESP_LOGE(TAG, "Unknown family");
return 1;
}
}

if (getddrinfo_args.flags->count > 0) {
for (int i = 0; i < getddrinfo_args.flags->count; i++) {
if (strcmp(getddrinfo_args.flags->sval[i], "AI_PASSIVE") == 0) {
hint.ai_flags |= AI_PASSIVE;
} else if (strcmp(getddrinfo_args.flags->sval[i], "AI_CANONNAME") == 0) {
hint.ai_flags |= AI_CANONNAME;
} else if (strcmp(getddrinfo_args.flags->sval[i], "AI_NUMERICHOST") == 0) {
hint.ai_flags |= AI_NUMERICHOST;
} else if (strcmp(getddrinfo_args.flags->sval[i], "AI_V4MAPPED") == 0) {
hint.ai_flags |= AI_V4MAPPED;
} else if (strcmp(getddrinfo_args.flags->sval[i], "AI_ALL") == 0) {
hint.ai_flags |= AI_ALL;
} else {
ESP_LOGE(TAG, "Unknown flag: %s", getddrinfo_args.flags->sval[i]);
return 1;
}

}
}

/* convert ip4 string or hostname to ip4 or ip6 address */
if (getaddrinfo(getddrinfo_args.hostname->sval[0], NULL, &hint, &res) != 0) {
printf("getddrinfo: unknown host %s\n", getddrinfo_args.hostname->sval[0]);
ESP_LOGE(TAG, "Unknown host");
return 1;
}

while (res) {
res_tmp = res;
res = res->ai_next;

if (res_tmp->ai_family == AF_INET) {
inet_ntop(AF_INET, &((struct sockaddr_in *)res_tmp->ai_addr)->sin_addr, ip_str, INET_ADDRSTRLEN);
printf("\tIP Address: %s\n", ip_str);
printf("\tAddress Family: AF_INET\n");
} else if (res_tmp->ai_family == AF_INET6) {
inet_ntop(AF_INET6, &((struct sockaddr_in6 *)res_tmp->ai_addr)->sin6_addr, ip_str, INET6_ADDRSTRLEN);
printf("\tIP Address: %s\n", ip_str);
printf("\tAddress Family: AF_INET6\n");
} else {
ESP_LOGE(TAG, "ai_family Unknown: %d\n", res_tmp->ai_family);
}

printf("\tProtocol: %d\n", res_tmp->ai_protocol);

if (res_tmp->ai_canonname) {
printf("\tCanonical Name: %s\n", res_tmp->ai_canonname);
}

free(res_tmp);
}

return 0;
}

/**
* @brief Registers the getddrinfo command.
*
* @return
* - esp_err_t
*/
esp_err_t console_cmd_getaddrinfo_register(void)
{
esp_err_t ret;

getddrinfo_args.family = arg_str0("f", "family", "<AF>", "Address family (AF_INET, AF_INET6, AF_UNSPEC).");
getddrinfo_args.flags = arg_strn("F", "flags", "<FLAGS>", 0, 5, "Special flags (AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST, AI_V4MAPPED, AI_ALL).");
getddrinfo_args.hostname = arg_str1(NULL, NULL, "<hostname>", "Host address");
getddrinfo_args.end = arg_end(1);
const esp_console_cmd_t getddrinfo_cmd = {
.command = "getaddrinfo",
.help = "Usage: getaddrinfo [options] <hostname> [service]",
.hint = NULL,
.func = &do_getddrinfo_cmd,
.argtable = &getddrinfo_args
};

ret = esp_console_cmd_register(&getddrinfo_cmd);
if (ret) {
ESP_LOGE(TAG, "Unable to register getddrinfo");
}

return ret;
}
Loading

0 comments on commit a5f7452

Please sign in to comment.