aboutsummaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2008-06-10 12:48:50 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2008-06-10 12:48:50 +0000
commit5262d4ab16e0eec7e84d2ec9eb673294e564fc11 (patch)
tree6628d8e6cf74abb51f1267effcf152b32c7401ba /main
parent497b8c298c1e4cf660802aa4c4644c44bc1e024f (diff)
Merge another change from team/russell/events ...
DUNDi uses a concept called the Entity ID for unique server identifiers. I have pulled out the handling of EIDs and made it something available to all of Asterisk. There is now a global Entity ID that can be used for other purposes as well, such as code providing distributed device state, which is why I did this. The global Entity ID is set automatically, just like it was done in DUNDi, but it can also be set in asterisk.conf. DUNDi will now use this global EID unless one is specified in dundi.conf. The current EID for the system can be seen in the "core show settings" CLI command. It is also available in the dialplan via the ENTITYID variable. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@121439 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r--main/asterisk.c15
-rw-r--r--main/netsock.c81
-rw-r--r--main/pbx.c3
3 files changed, 99 insertions, 0 deletions
diff --git a/main/asterisk.c b/main/asterisk.c
index 7d3079938..14fbbf98e 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -169,6 +169,8 @@ long option_minmemfree; /*!< Minimum amount of free system memory - stop acce
/*! @} */
+struct ast_eid g_eid;
+
/* XXX tmpdir is a subdir of the spool directory, and no way to remap it */
char record_cache_dir[AST_CACHE_DIR_LEN] = DEFAULT_TMP_DIR;
@@ -386,6 +388,7 @@ static char *handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_c
{
char buf[BUFSIZ];
struct ast_tm tm;
+ char eid_str[128];
switch (cmd) {
case CLI_INIT:
@@ -397,6 +400,8 @@ static char *handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_c
return NULL;
}
+ ast_eid_to_str(eid_str, sizeof(eid_str), &g_eid);
+
ast_cli(a->fd, "\nPBX Core settings\n");
ast_cli(a->fd, "-----------------\n");
ast_cli(a->fd, " Version: %s\n", ast_get_version());
@@ -425,6 +430,7 @@ static char *handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_c
}
ast_cli(a->fd, " System: %s/%s built by %s on %s %s\n", ast_build_os, ast_build_kernel, ast_build_user, ast_build_machine, ast_build_date);
ast_cli(a->fd, " System name: %s\n", ast_config_AST_SYSTEM_NAME);
+ ast_cli(a->fd, " Entity ID: %s\n", eid_str);
ast_cli(a->fd, " Default language: %s\n", defaultlanguage);
ast_cli(a->fd, " Language prefix: %s\n", ast_language_is_prefix ? "Enabled" : "Disabled");
ast_cli(a->fd, " User name and group: %s/%s\n", ast_config_AST_RUN_USER, ast_config_AST_RUN_GROUP);
@@ -2608,6 +2614,8 @@ static void ast_readconfig(void)
ast_copy_string(cfg_paths.socket_path, DEFAULT_SOCKET, sizeof(cfg_paths.socket_path));
ast_copy_string(cfg_paths.run_dir, DEFAULT_RUN_DIR, sizeof(cfg_paths.run_dir));
+ ast_set_default_eid(&g_eid);
+
/* no asterisk.conf? no problem, use buildtime config! */
if (!cfg) {
return;
@@ -2773,6 +2781,13 @@ static void ast_readconfig(void)
option_minmemfree = 0;
}
#endif
+ } else if (!strcasecmp(v->name, "entityid")) {
+ struct ast_eid tmp_eid;
+ if (!ast_str_to_eid(&tmp_eid, v->value)) {
+ ast_verbose("Successfully set global EID to '%s'\n", v->value);
+ g_eid = tmp_eid;
+ } else
+ ast_verbose("Invalid Entity ID '%s' provided\n", v->value);
}
}
for (v = ast_variable_browse(cfg, "compat"); v; v = v->next) {
diff --git a/main/netsock.c b/main/netsock.c
index eefec0833..58972781e 100644
--- a/main/netsock.c
+++ b/main/netsock.c
@@ -207,3 +207,84 @@ void ast_netsock_unref(struct ast_netsock *ns)
{
ASTOBJ_UNREF(ns, ast_netsock_destroy);
}
+
+char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
+{
+ int x;
+ char *os = s;
+ if (maxlen < 18) {
+ if (s && (maxlen > 0))
+ *s = '\0';
+ } else {
+ for (x = 0; x < 5; x++) {
+ sprintf(s, "%02x:", eid->eid[x]);
+ s += 3;
+ }
+ sprintf(s, "%02x", eid->eid[5]);
+ }
+ return os;
+}
+
+void ast_set_default_eid(struct ast_eid *eid)
+{
+#if defined(SIOCGIFHWADDR)
+ int s, x = 0;
+ char eid_str[20];
+ struct ifreq ifr;
+
+ s = socket(AF_INET, SOCK_STREAM, 0);
+ if (s < 0)
+ return;
+ for (x = 0; x < 10; x++) {
+ memset(&ifr, 0, sizeof(ifr));
+ snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth%d", x);
+ if (ioctl(s, SIOCGIFHWADDR, &ifr))
+ continue;
+ memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
+ ast_debug(1, "Seeding global EID '%s' from '%s' using 'siocgifhwaddr'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), ifr.ifr_name);
+ close(s);
+ return;
+ }
+ close(s);
+#else
+#if defined(ifa_broadaddr) && !defined(SOLARIS)
+ char eid_str[20];
+ struct ifaddrs *ifap;
+
+ if (getifaddrs(&ifap) == 0) {
+ struct ifaddrs *p;
+ for (p = ifap; p; p = p->ifa_next) {
+ if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
+ struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
+ memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
+ ast_debug(1, "Seeding global EID '%s' from '%s' using 'getifaddrs'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), p->ifa_name);
+ freeifaddrs(ifap);
+ return;
+ }
+ }
+ freeifaddrs(ifap);
+ }
+#endif
+#endif
+ ast_log(LOG_NOTICE, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
+}
+
+int ast_str_to_eid(struct ast_eid *eid, const char *s)
+{
+ unsigned int eid_int[6];
+ int x;
+
+ if (sscanf(s, "%x:%x:%x:%x:%x:%x", &eid_int[0], &eid_int[1], &eid_int[2],
+ &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
+ return -1;
+
+ for (x = 0; x < 6; x++)
+ eid->eid[x] = eid_int[x];
+
+ return 0;
+}
+
+int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
+{
+ return memcmp(eid1, eid2, sizeof(*eid1));
+}
diff --git a/main/pbx.c b/main/pbx.c
index f49ead16e..b2aebbd9b 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -2314,6 +2314,9 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
s = workspace;
} else if (!strcmp(var, "SYSTEMNAME")) {
s = ast_config_AST_SYSTEM_NAME;
+ } else if (!strcmp(var, "ENTITYID")) {
+ ast_eid_to_str(workspace, workspacelen, &g_eid);
+ s = workspace;
}
}
/* if not found, look into chanvars or global vars */