PlmnId to mnc conversion and e2term crashes occasionally when gNB is disconnected

Change-Id: I8119ea5ac2400dbcd762b3e5207c82e9f83cbb38
Signed-off-by: sandeeku <sandeep.15.kumar@nokia.com>
diff --git a/RIC-E2-TERMINATION/BuildRunName.h b/RIC-E2-TERMINATION/BuildRunName.h
index 98c640e..c3b2cef 100644
--- a/RIC-E2-TERMINATION/BuildRunName.h
+++ b/RIC-E2-TERMINATION/BuildRunName.h
@@ -36,17 +36,17 @@
     ///auto mcc3 = (unsigned char)((data[1] & (unsigned char)0xF0) >> (unsigned char)4);
     auto mcc3 = (unsigned char)((unsigned char)(data[1] & (unsigned char)0x0F));
 
-    auto mnc1 = (unsigned char)(data[2] & (unsigned char)0x0F);
-    auto mnc2 =  (unsigned char)(((unsigned char)(data[2] & (unsigned char)0xF0) >> (unsigned char)4));
+    auto mnc1 = (unsigned char)(((unsigned char)((unsigned char)data[1] & (unsigned char)0xF0)) >> (unsigned char)4);
+    auto mnc2 =  (unsigned char)((unsigned char)data[2] & (unsigned char)0x0F) ;
     //auto mnc3 = (unsigned char)(((unsigned char)(data[1] & (unsigned char)0x0F) >> (unsigned char)4) );
-    auto mnc3 = (unsigned char)((data[1] & (unsigned char)0xF0) >> (unsigned char)4);
+    auto mnc3 = (unsigned char)(((unsigned char)((unsigned char)data[2] & (unsigned char)0xF0)) >> (unsigned char)4);
 
     int j = 0;
-    if (mnc3 != 15) {
+    if (mnc1 != 15) {
         j = snprintf(plmnId, 20, "%s%1d%1d%1d_%1d%1d%1d", type, mcc1, mcc2, mcc3, mnc1, mnc2, mnc3);
     }
     else {
-        j = snprintf(plmnId, 20, "%s%1d%1d%1d_0%1d%1d", type, mcc1, mcc2, mcc3, mnc1, mnc2);
+        j = snprintf(plmnId, 20, "%s%1d%1d%1d_0%1d%1d", type, mcc1, mcc2, mcc3, mnc2, mnc3);
     }
 
     return j;
diff --git a/RIC-E2-TERMINATION/sctpThread.cpp b/RIC-E2-TERMINATION/sctpThread.cpp
index 088ce0e..89feebb 100644
--- a/RIC-E2-TERMINATION/sctpThread.cpp
+++ b/RIC-E2-TERMINATION/sctpThread.cpp
@@ -283,6 +283,7 @@
 std::atomic<int64_t> num_of_messages{0};
 std::atomic<int64_t> num_of_XAPP_messages{0};
 static long transactionCounter = 0;
+pthread_mutex_t thread_lock;
 
 int buildListeningPort(sctp_params_t &sctpParams) {
     sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
@@ -617,6 +618,10 @@
 
     sctpParams.sctpMap = new mapWrapper();
 
+    if (pthread_mutex_init(&thread_lock, NULL) != 0) {
+        mdclog_write(MDCLOG_ERR, "failed to init thread lock");
+        exit(-1);
+    }
     std::vector<std::thread> threads(num_cpus);
 //    std::vector<std::thread> threads;
 
@@ -640,7 +645,7 @@
     for (auto &t : threads) {
         t.join();
     }
-
+    pthread_mutex_destroy(&thread_lock);
     return 0;
 }
 #endif
@@ -845,6 +850,7 @@
             if(events)
             {
                 free(events);
+                events = nullptr;
             }
             return;
 #endif            
@@ -887,10 +893,12 @@
                             /* We have processed all incoming connections. */
                             if(peerInfo)
                                 free(peerInfo);
+                                peerInfo = nullptr;
                             break;
                         } else {
                             if(peerInfo)
                                 free(peerInfo);
+                                peerInfo = nullptr;
                             mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
                             break;
                         }
@@ -900,6 +908,7 @@
                         close(peerInfo->fileDescriptor);
                         if(peerInfo)
                             free(peerInfo);
+                            peerInfo = nullptr;
                         break;
 #endif                        
                     }
@@ -916,6 +925,7 @@
                         close(peerInfo->fileDescriptor);
                         if(peerInfo)
                             free(peerInfo);
+                            peerInfo = nullptr;
                         break;
                     }
                     if (mdclog_level_get() >= MDCLOG_DEBUG) {
@@ -930,6 +940,7 @@
                                    0) != 0) {
                         if(peerInfo)
                             free(peerInfo);
+                            peerInfo = nullptr;
                         break;
                     }
                     break;
@@ -1195,7 +1206,7 @@
                       ReportingMessages_t &message,
                       RmrMessagesBuffer_t &rmrMessageBuffer,
                       sctp_params_t *params) {
-    if (event.data.fd != params->rmrListenFd) {
+    if ((event.data.fd != params->rmrListenFd) && (event.data.ptr != nullptr)) {
         auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
         mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
                      event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
@@ -1213,7 +1224,7 @@
         }
 #endif
         close(peerInfo->fileDescriptor);
-        params->sctpMap->erase(peerInfo->enodbName);
+        //params->sctpMap->erase(peerInfo->enodbName);
         cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
     } else {
         mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
@@ -1247,17 +1258,30 @@
  * @param m
  */
 void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
+    if(val != nullptr)
+    {
     char *dummy;
     auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
     char searchBuff[2048]{};
 
     snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
+    if(m->find(searchBuff))
+    {
     m->erase(searchBuff);
+    }
 
+    if(m->find(val->enodbName))
+    {
+    mdclog_write(MDCLOG_DEBUG, "remove key enodbName = %s from %s at line %d", val->enodbName, __FUNCTION__, __LINE__);
     m->erase(val->enodbName);
+    }
 #ifndef UNIT_TEST
-    free(val);
+    if(val) {
+       free(val);
+       val = nullptr;
+    }
 #endif
+    }
 }
 
 /**
@@ -1309,6 +1333,7 @@
             auto tmp = m->find(key);
             if (tmp) {
                 free(tmp);
+                tmp = nullptr;
             }
             m->erase(key);
 #ifndef UNIT_TEST
@@ -1371,7 +1396,9 @@
     int streamId;
 
     // get the identity of the interface
+    if (events->data.ptr != nullptr){
     message.peerInfo = (ConnectedCU_t *)events->data.ptr;
+    }
 
     struct timespec start{0, 0};
     struct timespec decodeStart{0, 0};
@@ -1531,8 +1558,18 @@
 #endif        
 
         /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
+#ifndef UNIT_TEST
+        pthread_mutex_lock(&thread_lock);
+        if (fcntl(message.peerInfo->fileDescriptor, F_GETFD) != -1) {
+            mdclog_write(MDCLOG_DEBUG, "Closing connection - descriptor = %d", message.peerInfo->fileDescriptor);
+            close(message.peerInfo->fileDescriptor);
+            cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
+        }
+        pthread_mutex_unlock(&thread_lock);
+#else
         close(message.peerInfo->fileDescriptor);
         cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
+#endif        
     }
     if (loglevel >= MDCLOG_DEBUG) {
         clock_gettime(CLOCK_MONOTONIC, &end);
@@ -1586,6 +1623,7 @@
                 // out of memory
                 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
                 free(buffer);
+                buffer = nullptr;
                 return;
             }
             buffer = newBuffer;
@@ -1670,6 +1708,7 @@
         rmr_free_msg(rmrMsg);
     }
     free(buffer);
+    buffer = nullptr;
 
     return;
 }
@@ -2963,6 +3002,7 @@
                         mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
                     }
                     free(peerInfo);
+                    peerInfo = nullptr;
                 }
             }
 
@@ -3121,6 +3161,7 @@
             auto tmp = sctpMap->find(key);
             if (tmp) {
                 free(tmp);
+                tmp = nullptr;
                 sctpMap->erase(key);
             }
         } else {
@@ -3168,8 +3209,9 @@
         auto tmp = sctpMap->find(key);
         if (tmp) {
             free(tmp);
-        }
+            tmp = nullptr;
         sctpMap->erase(key);
+        }
         mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
         return -1;
     }