]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-5709 --resolve
authorAnthony Minessale <anthm@freeswitch.org>
Fri, 16 Aug 2013 21:16:28 +0000 (02:16 +0500)
committerAnthony Minessale <anthm@freeswitch.org>
Fri, 16 Aug 2013 21:16:28 +0000 (02:16 +0500)
src/include/switch_console.h
src/include/switch_utils.h
src/mod/applications/mod_commands/mod_commands.c
src/mod/endpoints/mod_sofia/sofia.c
src/switch_console.c
src/switch_utils.c

index e6f6cf476e9cb3c31fc67b0f66172599036f474c..73410bd2485be88a4d3ef0476cf17813b0062f09 100644 (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,
index 40630d8c9f82bac5af9a6c284936675a9872be1d..f4aa3d7f28bfb580281f336cf36cfd7e5c4da2cc 100644 (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
index fc74c3dd65f39621dae2d45d1bd6a9138b82e59c..abc0aace9a7d5ce4b3f25c083770c64ee9b50a8c 100644 (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");
index 40c8bc370dfb3e45243752a7f452a49957cee984..b3992843374cb40e31334adec26f04df6be135df 100644 (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;
                                                }
index b1e2c8e271e241419a5ac1a0a6df7e8cf1767c67..b2c003a6c6c986754a58175dc928d4590f17cd68 100644 (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;
index 8b5fc8e3c90bfada5af8309a150ff4e26997763d..bc3a9364e37d4b7e7d3a24c0afda306ddf9f545a 100644 (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 };