FS-5709 --resolve

This commit is contained in:
Anthony Minessale 2013-08-17 02:16:28 +05:00
parent c21f65ac55
commit 8e45f23d02
6 changed files with 189 additions and 1 deletions

View File

@ -83,6 +83,7 @@ SWITCH_DECLARE(switch_status_t) switch_console_add_complete_func(const char *nam
SWITCH_DECLARE(switch_status_t) switch_console_del_complete_func(const char *name);
SWITCH_DECLARE(switch_status_t) switch_console_run_complete_func(const char *func, const char *line,
const char *last_word, switch_console_callback_match_t **matches);
SWITCH_DECLARE(void) switch_console_push_match_unique(switch_console_callback_match_t **matches, const char *new_val);
SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val);
SWITCH_DECLARE(void) switch_console_free_matches(switch_console_callback_match_t **matches);
SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *last_word,

View File

@ -467,14 +467,27 @@ SWITCH_DECLARE(switch_status_t) switch_resolve_host(const char *host, char *buf,
/*!
\brief find local ip of the box
\param buf the buffer to write the ip adress found into
\param buf the buffer to write the ip address found into
\param len the length of the buf
\param mask the CIDR found (AF_INET only)
\param family the address family to return (AF_INET or AF_INET6)
\return SWITCH_STATUS_SUCCESSS for success, otherwise failure
*/
SWITCH_DECLARE(switch_status_t) switch_find_local_ip(_Out_opt_bytecapcount_(len)
char *buf, _In_ int len, _In_opt_ int *mask, _In_ int family);
/*!
\brief find primary ip of the specified interface
\param buf the buffer to write the ip address found into
\param len the length of the buf
\param mask the CIDR found (AF_INET only)
\param ifname interface name to check
\param family the address family to return (AF_INET or AF_INET6)
\return SWITCH_STATUS_SUCCESSS for success, otherwise failure
*/
SWITCH_DECLARE(switch_status_t) switch_find_interface_ip(_Out_opt_bytecapcount_(len)
char *buf, _In_ int len, _In_opt_ int *mask, _In_ const char *ifname, _In_ int family);
/*!
\brief find the char representation of an ip adress
\param buf the buffer to write the ip adress found into

View File

@ -6028,6 +6028,49 @@ SWITCH_STANDARD_API(file_exists_function)
return SWITCH_STATUS_SUCCESS;
}
#define INTERFACE_IP_SYNTAX "[auto|ipv4|ipv6] <ifname>"
SWITCH_STANDARD_API(interface_ip_function)
{
char *mydata = NULL, *argv[3] = { 0 };
int argc = 0;
char addr[INET6_ADDRSTRLEN];
if (!zstr(cmd)) {
mydata = strdup(cmd);
switch_assert(mydata);
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
}
if (argc < 2) {
stream->write_function(stream, "USAGE: interface_ip %s\n", INTERFACE_IP_SYNTAX);
goto end;
}
if (!strcasecmp(argv[0], "ipv4")) {
if (switch_find_interface_ip(addr, sizeof(addr), NULL, argv[1], AF_INET) == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "%s", addr);
}
}
else if (!strcasecmp(argv[0], "ipv6")) {
if (switch_find_interface_ip(addr, sizeof(addr), NULL, argv[1], AF_INET6) == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "%s", addr);
}
}
else if (!strcasecmp(argv[0], "auto")) {
if (switch_find_interface_ip(addr, sizeof(addr), NULL, argv[1], AF_UNSPEC) == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "%s", addr);
}
}
else {
stream->write_function(stream, "USAGE: interface_ip %s\n", INTERFACE_IP_SYNTAX);
}
end:
switch_safe_free(mydata);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load)
{
switch_api_interface_t *commands_api_interface;
@ -6065,6 +6108,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load)
SWITCH_ADD_API(commands_api_interface, "help", "Show help for all the api commands", help_function, "");
SWITCH_ADD_API(commands_api_interface, "host_lookup", "Lookup host", host_lookup_function, "<hostname>");
SWITCH_ADD_API(commands_api_interface, "hostname", "Return the system hostname", hostname_api_function, "");
SWITCH_ADD_API(commands_api_interface, "interface_ip", "Return the primary IP of an interface", interface_ip_function, INTERFACE_IP_SYNTAX);
SWITCH_ADD_API(commands_api_interface, "switchname", "Return the switch name", switchname_api_function, "");
SWITCH_ADD_API(commands_api_interface, "hupall", "hupall", hupall_api_function, "<cause> [<var> <value>]");
SWITCH_ADD_API(commands_api_interface, "in_group", "Determine if a user is in a group", in_group_function, "<user>[@<domain>] <group_name>");
@ -6217,6 +6261,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load)
switch_console_set_complete("add fsctl flush_db_handles");
switch_console_set_complete("add fsctl min_idle_cpu");
switch_console_set_complete("add fsctl send_sighup");
switch_console_set_complete("add interface_ip auto ::console::list_interfaces");
switch_console_set_complete("add interface_ip ipv4 ::console::list_interfaces");
switch_console_set_complete("add interface_ip ipv6 ::console::list_interfaces");
switch_console_set_complete("add load ::console::list_available_modules");
switch_console_set_complete("add nat_map reinit");
switch_console_set_complete("add nat_map republish");

View File

@ -4104,9 +4104,22 @@ switch_status_t config_sofia(sofia_config_t reload, char *profile_name)
}
} else if (!strcasecmp(var, "rtp-ip")) {
char *ip = mod_sofia_globals.guess_ip;
char buf[64];
if (!strcmp(val, "0.0.0.0")) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid IP 0.0.0.0 replaced with %s\n", mod_sofia_globals.guess_ip);
} else if (!strncasecmp(val, "interface:", 10)) {
char *ifname = val+10;
int family = AF_UNSPEC;
if (!strncasecmp(ifname, "auto/", 5)) { ifname += 5; family = AF_UNSPEC; }
if (!strncasecmp(ifname, "ipv4/", 5)) { ifname += 5; family = AF_INET; }
if (!strncasecmp(ifname, "ipv6/", 5)) { ifname += 5; family = AF_INET6; }
if (switch_find_interface_ip(buf, sizeof(buf), NULL, ifname, family) == SWITCH_STATUS_SUCCESS) {
ip = buf;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Using %s IP for interface %s for rtp-ip\n", ip, val+10);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown IP for interface %s for rtp-ip\n", val+10);
}
} else {
ip = strcasecmp(val, "auto") ? val : mod_sofia_globals.guess_ip;
}
@ -4117,9 +4130,22 @@ switch_status_t config_sofia(sofia_config_t reload, char *profile_name)
}
} else if (!strcasecmp(var, "sip-ip")) {
char *ip = mod_sofia_globals.guess_ip;
char buf[64];
if (!strcmp(val, "0.0.0.0")) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid IP 0.0.0.0 replaced with %s\n", mod_sofia_globals.guess_ip);
} else if (!strncasecmp(val, "interface:", 10)) {
char *ifname = val+10;
int family = AF_UNSPEC;
if (!strncasecmp(ifname, "auto/", 5)) { ifname += 5; family = AF_UNSPEC; }
if (!strncasecmp(ifname, "ipv4/", 5)) { ifname += 5; family = AF_INET; }
if (!strncasecmp(ifname, "ipv6/", 5)) { ifname += 5; family = AF_INET6; }
if (switch_find_interface_ip(buf, sizeof(buf), NULL, ifname, family) == SWITCH_STATUS_SUCCESS) {
ip = buf;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Using %s IP for interface %s for sip-ip\n", ip, val+10);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown IP for interface %s for sip-ip\n", val+10);
}
} else {
ip = strcasecmp(val, "auto") ? val : mod_sofia_globals.guess_ip;
}

View File

@ -33,6 +33,7 @@
#include <switch.h>
#include <switch_console.h>
#include <switch_version.h>
#include <switch_private.h>
#define CMD_BUFLEN 1024
#ifdef SWITCH_HAVE_LIBEDIT
@ -619,6 +620,36 @@ SWITCH_DECLARE_NONSTD(switch_status_t) switch_console_list_loaded_modules(const
return SWITCH_STATUS_FALSE;
}
#ifdef HAVE_GETIFADDRS
#include <ifaddrs.h>
#include <net/if.h>
SWITCH_DECLARE_NONSTD(switch_status_t) switch_console_list_interfaces(const char *line, const char *cursor, switch_console_callback_match_t **matches)
{
struct match_helper h = { 0 };
struct ifaddrs *addrs, *addr;
getifaddrs(&addrs);
for(addr = addrs; addr; addr = addr->ifa_next) {
if (addr->ifa_flags & IFF_UP) {
switch_console_push_match_unique(&h.my_matches, addr->ifa_name);
}
}
freeifaddrs(addrs);
if (h.my_matches) {
*matches = h.my_matches;
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_FALSE;
}
#else
SWITCH_DECLARE_NONSTD(switch_status_t) switch_console_list_interfaces(const char *line, const char *cursor, switch_console_callback_match_t **matches)
{
return SWITCH_STATUS_FALSE;
}
#endif
static int uuid_callback(void *pArg, int argc, char **argv, char **columnNames)
{
struct match_helper *h = (struct match_helper *) pArg;
@ -1631,6 +1662,7 @@ SWITCH_DECLARE(switch_status_t) switch_console_init(switch_memory_pool_t *pool)
switch_core_hash_init(&globals.func_hash, pool);
switch_console_add_complete_func("::console::list_available_modules", (switch_console_complete_callback_t) switch_console_list_available_modules);
switch_console_add_complete_func("::console::list_loaded_modules", (switch_console_complete_callback_t) switch_console_list_loaded_modules);
switch_console_add_complete_func("::console::list_interfaces", (switch_console_complete_callback_t) switch_console_list_interfaces);
switch_console_add_complete_func("::console::list_uuid", (switch_console_complete_callback_t) switch_console_list_uuid);
return SWITCH_STATUS_SUCCESS;
}
@ -1741,6 +1773,20 @@ SWITCH_DECLARE(void) switch_console_sort_matches(switch_console_callback_match_t
}
}
SWITCH_DECLARE(void) switch_console_push_match_unique(switch_console_callback_match_t **matches, const char *new_val)
{
/* Ignore the entry if it is already in the list */
if (*matches) {
switch_console_callback_match_node_t *node;
for(node = (*matches)->head; node; node = node->next) {
if (!strcasecmp(node->val, new_val)) return;
}
}
switch_console_push_match(matches, new_val);
}
SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)
{
switch_console_callback_match_node_t *match;

View File

@ -1580,6 +1580,61 @@ SWITCH_DECLARE(switch_status_t) switch_find_local_ip(char *buf, int len, int *ma
return status;
}
#ifdef HAVE_GETIFADDRS
# include <ifaddrs.h>
# include <net/if.h>
#endif
SWITCH_DECLARE(switch_status_t) switch_find_interface_ip(char *buf, int len, int *mask, const char *ifname, int family)
{
switch_status_t status = SWITCH_STATUS_FALSE;
#ifdef HAVE_GETIFADDRS
struct ifaddrs *addrs, *addr;
getifaddrs(&addrs);
for(addr = addrs; addr; addr = addr->ifa_next)
{
if (!(addr->ifa_flags & IFF_UP)) continue; // Address is not UP
if (!addr->ifa_addr) continue; // No address set
if (!addr->ifa_netmask) continue; // No netmask set
if (family != AF_UNSPEC && addr->ifa_addr->sa_family != family) continue; // Not the address family we're looking for
if (strcmp(addr->ifa_name, ifname)) continue; // Not the interface we're looking for
switch(addr->ifa_addr->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &( ((struct sockaddr_in*)(addr->ifa_addr))->sin_addr ), buf, len - 1);
break;
case AF_INET6:
inet_ntop(AF_INET6, &( ((struct sockaddr_in6*)(addr->ifa_addr))->sin6_addr ), buf, len - 1);
break;
default:
continue;
}
if (mask && addr->ifa_netmask->sa_family == AF_INET) {
*mask = ((struct sockaddr_in*)(addr->ifa_addr))->sin_addr.s_addr;
}
status = SWITCH_STATUS_SUCCESS;
break;
}
freeifaddrs(addrs);
#elif defined(__linux__)
// TODO Not implemented, contributions welcome.
#elif defined(WIN32)
// TODO Not implemented, contributions welcome.
#endif
return status;
}
SWITCH_DECLARE(switch_time_t) switch_str_time(const char *in)
{
switch_time_exp_t tm = { 0 };