If the /var/lib/opensaf/node_id file doesn't exist when DTM starts, DTM will
create the file and use the IPv4 address as node ID. When using IPv6, the file
must still be configured manually. IPv6 support may be added in a future ticket.
---
 src/dtm/dtmnd/dtm.h              |  1 +
 src/dtm/dtmnd/dtm_main.cc        | 98 ++++++++++++++++++++++++++--------------
 src/dtm/dtmnd/dtm_node_db.cc     |  7 +++
 src/dtm/dtmnd/dtm_read_config.cc |  6 ---
 4 files changed, 71 insertions(+), 41 deletions(-)

diff --git a/src/dtm/dtmnd/dtm.h b/src/dtm/dtmnd/dtm.h
index d710f3adf..28c811e65 100644
--- a/src/dtm/dtmnd/dtm.h
+++ b/src/dtm/dtmnd/dtm.h
@@ -104,6 +104,7 @@ extern DTM_NODE_DB *dtm_node_getnext_by_id(uint32_t 
node_id);
 extern uint32_t dtm_node_add(DTM_NODE_DB *node, int i);
 extern uint32_t dtm_node_delete(DTM_NODE_DB *nnode, int i);
 extern DTM_NODE_DB *dtm_node_new(const DTM_NODE_DB *new_node);
+extern void dtm_print_config(DTM_INTERNODE_CB *config);
 extern int dtm_read_config(DTM_INTERNODE_CB *config,
                            const char *dtm_config_file);
 extern uint32_t dtm_service_discovery_init(DTM_INTERNODE_CB *dtms_cb);
diff --git a/src/dtm/dtmnd/dtm_main.cc b/src/dtm/dtmnd/dtm_main.cc
index cf0b81f8a..3260a81f1 100644
--- a/src/dtm/dtmnd/dtm_main.cc
+++ b/src/dtm/dtmnd/dtm_main.cc
@@ -21,10 +21,16 @@
  * ========================================================================
  */
 
+#include <arpa/inet.h>
 #include <sched.h>
+#include <sys/stat.h>
 #include <unistd.h>
+#include <cerrno>
+#include <cstdint>
 #include <cstdlib>
+#include <fstream>
 #include "base/daemon.h"
+#include "base/logtrace.h"
 #include "base/ncs_main_papi.h"
 #include "base/ncsencdec_pub.h"
 #include "base/osaf_poll.h"
@@ -35,6 +41,13 @@
 #include "nid/agent/nid_api.h"
 #include "osaf/configmake.h"
 
+namespace {
+
+void UpdateNodeIdFile(DTM_INTERNODE_CB *cb);
+uint32_t GetNodeIdFromAddress(DTM_INTERNODE_CB *cb);
+
+}  // namespace
+
 /* ========================================================================
  *   DEFINITIONS
  * ========================================================================
@@ -99,39 +112,6 @@ DTM_INTERNODE_CB::DTM_INTERNODE_CB()
 DTM_INTERNODE_CB::~DTM_INTERNODE_CB() { delete multicast_; }
 
 /**
- * Function to init the dtm process
- *
- * @param dtms_cb
- *
- * @return NCSCC_RC_SUCCESS
- * @return NCSCC_RC_FAILURE
- *
- */
-static uint32_t dtm_init(DTM_INTERNODE_CB *dtms_cb) {
-  uint32_t rc = NCSCC_RC_SUCCESS;
-
-  TRACE_ENTER();
-
-  if (ncs_leap_startup() != NCSCC_RC_SUCCESS) {
-    LOG_ER("DTM: LEAP svcs startup failed \n");
-    rc = NCSCC_RC_FAILURE;
-    goto done;
-  }
-
-  /* Initialize  control block */
-  if ((rc = dtm_cb_init(dtms_cb)) != NCSCC_RC_SUCCESS) {
-    rc = NCSCC_RC_FAILURE;
-    LOG_ER("DTM: dtm_cb_init FAILED");
-    goto done;
-  }
-
-done:
-
-  TRACE_LEAVE2("rc : %d", rc);
-  return rc;
-}
-
-/**
  * Function to destroy node discovery thread
  *
  *
@@ -256,8 +236,8 @@ int main(int argc, char *argv[]) {
   dtms_gl_cb = new DTM_INTERNODE_CB;
   DTM_INTERNODE_CB *dtms_cb = dtms_gl_cb;
 
-  if (dtms_cb == nullptr || dtm_init(dtms_cb) != NCSCC_RC_SUCCESS) {
-    LOG_ER("DTM: dtm_init failed");
+  if (dtms_cb == nullptr) {
+    LOG_ER("Failed to allocate memory");
     goto done3;
   }
 
@@ -268,6 +248,20 @@ int main(int argc, char *argv[]) {
     goto done3;
   }
 
+  UpdateNodeIdFile(dtms_cb);
+
+  if (ncs_leap_startup() != NCSCC_RC_SUCCESS) {
+    LOG_ER("DTM: LEAP svcs startup failed \n");
+    goto done3;
+  }
+
+  if (dtm_cb_init(dtms_cb) != NCSCC_RC_SUCCESS) {
+    LOG_ER("DTM: dtm_cb_init failed");
+    goto done3;
+  }
+
+  dtm_print_config(dtms_cb);
+
   /*************************************************************/
   /* Set up the initial bcast or mcast sender socket */
   /*************************************************************/
@@ -352,3 +346,37 @@ done3:
   (void)nid_notify("TRANSPORT", NCSCC_RC_FAILURE, nullptr);
   exit(1);
 }
+
+namespace {
+
+void UpdateNodeIdFile(DTM_INTERNODE_CB *cb) {
+  struct stat stat_buf;
+  int stat_result = stat(PKGLOCALSTATEDIR "/node_id", &stat_buf);
+  if (stat_result == -1 && errno == ENOENT) {
+    uint32_t node_id = GetNodeIdFromAddress(cb);
+    if (node_id != 0) {
+      std::ofstream str;
+      try {
+        str.open(PKGLOCALSTATEDIR "/node_id", std::ofstream::out);
+        str << std::hex << node_id << std::endl;
+      } catch (std::ofstream::failure) {
+      }
+      str.close();
+    }
+  }
+}
+
+uint32_t GetNodeIdFromAddress(DTM_INTERNODE_CB *cb) {
+  uint32_t node_id = 0;
+  if (cb->i_addr_family == AF_INET) {
+    struct in_addr addr_ipv4;
+    int rc = inet_pton(AF_INET, cb->ip_addr.c_str(), &addr_ipv4);
+    if (rc == 1) node_id = ntohl(addr_ipv4.s_addr);
+    TRACE("Using node address 0x%x as node ID", node_id);
+  } else {
+    LOG_ER(PKGLOCALSTATEDIR "/node_id must exist when using IPv6 addresses");
+  }
+  return node_id;
+}
+
+}  // namespace
diff --git a/src/dtm/dtmnd/dtm_node_db.cc b/src/dtm/dtmnd/dtm_node_db.cc
index 67b017119..1c9da4dac 100644
--- a/src/dtm/dtmnd/dtm_node_db.cc
+++ b/src/dtm/dtmnd/dtm_node_db.cc
@@ -21,6 +21,7 @@
 #include <cstddef>
 #include <cstdlib>
 #include <cstring>
+#include "base/ncs_main_papi.h"
 #include "base/usrbuf.h"
 #include "dtm/dtmnd/dtm.h"
 
@@ -60,6 +61,12 @@ uint32_t dtm_cb_init(DTM_INTERNODE_CB *dtms_cb) {
 
   TRACE_ENTER();
 
+  dtms_cb->node_id = ncs_get_node_id();
+  if (dtms_cb->node_id == 0) {
+    LOG_ER("node_id cannot be zero");
+    return NCSCC_RC_FAILURE;
+  }
+
   memset(&nodeid_param, 0, sizeof(NCS_PATRICIA_PARAMS));
   memset(&ipaddr_param, 0, sizeof(NCS_PATRICIA_PARAMS));
 
diff --git a/src/dtm/dtmnd/dtm_read_config.cc b/src/dtm/dtmnd/dtm_read_config.cc
index 69f08b749..6f0045af1 100644
--- a/src/dtm/dtmnd/dtm_read_config.cc
+++ b/src/dtm/dtmnd/dtm_read_config.cc
@@ -249,7 +249,6 @@ int dtm_read_config(DTM_INTERNODE_CB *config, const char 
*dtm_config_file) {
   config->sock_sndbuf_size = 0;
   config->sock_rcvbuf_size = 0;
   config->scope_link = false;
-  config->node_id = m_NCS_GET_NODE_ID;
   intranode_max_processes = 100;
   fp = fopen(PKGSYSCONFDIR "/node_name", "r");
   if (fp == nullptr) {
@@ -548,9 +547,6 @@ int dtm_read_config(DTM_INTERNODE_CB *config, const char 
*dtm_config_file) {
   if ((config->cluster_id) == 0) {
     LOG_ER("DTM: dtm_read_config: cluster_id is missing in conf file");
     fieldmissing = 1;
-  } else if ((config->node_id) == 0) {
-    LOG_ER("DTM: dtm_read_config: node_id is missing in configuration");
-    fieldmissing = 1;
   } else if ((config->dgram_port_sndr) == 0) {
     LOG_ER("DTM: dtm_read_config: dgram_port_sndr is missing in conf file");
     fieldmissing = 1;
@@ -571,8 +567,6 @@ int dtm_read_config(DTM_INTERNODE_CB *config, const char 
*dtm_config_file) {
   if (fieldmissing == 1) return -1;
   /* All done. */
 
-  dtm_print_config(config);
-
   TRACE_LEAVE();
   return (err);
 }
-- 
2.13.3


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Opensaf-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to