From 366b06c873523655c8ddf55c17fd7e879c4da2db Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Fri, 15 Nov 2013 14:57:17 +0000
Subject: [PATCH] - Fixed handling of non success result for S1AP/SCTP new
 association - Handled not connected state in SCTP when recvmsg returns -1

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@4416 818b1a75-f10b-46b9-bf7c-635c3b92a50f
---
 openair-cn/MME_APP/mme_app_main.c         |  4 +--
 openair-cn/S1AP/s1ap_eNB.c                |  1 +
 openair-cn/S1AP/s1ap_eNB_default_values.h |  2 +-
 openair-cn/SCTP/sctp_eNB_task.c           | 44 +++++++++++++++++++----
 4 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/openair-cn/MME_APP/mme_app_main.c b/openair-cn/MME_APP/mme_app_main.c
index 298687e618..8a7e34ba80 100644
--- a/openair-cn/MME_APP/mme_app_main.c
+++ b/openair-cn/MME_APP/mme_app_main.c
@@ -89,8 +89,8 @@ void *mme_app_thread(void *args)
                 }
             } break;
             case TERMINATE_MESSAGE: {
-                /* Termination message received -> release any data allocated */
-                
+                /* Termination message received TODO -> release any data allocated */
+                itti_exit_task();
             } break;
             default: {
                 MME_APP_DEBUG("Unkwnon message ID %d:%s\n",
diff --git a/openair-cn/S1AP/s1ap_eNB.c b/openair-cn/S1AP/s1ap_eNB.c
index 5c7f38f1e5..9ad3267ef0 100644
--- a/openair-cn/S1AP/s1ap_eNB.c
+++ b/openair-cn/S1AP/s1ap_eNB.c
@@ -194,6 +194,7 @@ void s1ap_eNB_handle_sctp_association_resp(instance_t instance, sctp_new_associa
                   sctp_new_association_resp->sctp_state,
                   instance,
                   sctp_new_association_resp->ulp_cnx_id);
+        return;
     }
 
     /* Update parameters */
diff --git a/openair-cn/S1AP/s1ap_eNB_default_values.h b/openair-cn/S1AP/s1ap_eNB_default_values.h
index f1b38a73c8..41d80eabd5 100644
--- a/openair-cn/S1AP/s1ap_eNB_default_values.h
+++ b/openair-cn/S1AP/s1ap_eNB_default_values.h
@@ -16,7 +16,7 @@
 
 #define SCTP_OUT_STREAMS        (64)
 #define SCTP_IN_STREAMS         (64)
-#define SCTP_MAX_ATTEMPTS       (5)
+#define SCTP_MAX_ATTEMPTS       (2)
 #define SCTP_RECV_BUFFER_SIZE   (1024)
 
 #endif /* S1AP_ENB_DEFAULT_VALUES_H_ */
diff --git a/openair-cn/SCTP/sctp_eNB_task.c b/openair-cn/SCTP/sctp_eNB_task.c
index e813c61196..d52eca54ac 100644
--- a/openair-cn/SCTP/sctp_eNB_task.c
+++ b/openair-cn/SCTP/sctp_eNB_task.c
@@ -240,8 +240,12 @@ void sctp_handle_new_association_req(
                 sctp_itti_send_association_resp(
                     requestor, instance, -1, sctp_new_association_req_p->ulp_cnx_id,
                     SCTP_STATE_UNREACHABLE, 0, 0);
+                /* Add the socket to list of fd monitored by ITTI */
+                itti_unsubscribe_event_fd(TASK_SCTP, sd);
                 close(sd);
                 return;
+            } else {
+                SCTP_DEBUG("connectx in progress...\n");
             }
         }
     } else {
@@ -406,8 +410,23 @@ inline void sctp_eNB_read_from_socket(struct sctp_cnx_list_elm_s *sctp_cnx)
                      (struct sockaddr *)&addr, &from_len,
                      &sinfo, &flags);
     if (n < 0) {
-        SCTP_DEBUG("An error occured during read\n");
-        SCTP_ERROR("sctp_recvmsg: %s:%d\n", strerror(errno), errno);
+        if (errno == ENOTCONN) {
+            itti_unsubscribe_event_fd(TASK_SCTP, sctp_cnx->sd);
+
+            sctp_itti_send_association_resp(
+                sctp_cnx->task_id, sctp_cnx->instance, -1,
+                sctp_cnx->cnx_id, SCTP_STATE_UNREACHABLE, 0, 0);
+
+            close(sctp_cnx->sd);
+            sctp_nb_cnx--;
+            free(sctp_cnx);
+        } else {
+            SCTP_DEBUG("An error occured during read\n");
+            SCTP_ERROR("sctp_recvmsg: %s:%d\n", strerror(errno), errno);
+        }
+        return;
+    } else if (n == 0) {
+        SCTP_DEBUG("return of sctp_recvmsg is 0...\n");
         return;
     }
     if (flags & MSG_NOTIFICATION) {
@@ -419,9 +438,19 @@ inline void sctp_eNB_read_from_socket(struct sctp_cnx_list_elm_s *sctp_cnx)
 
         /* Client deconnection */
         if (SCTP_SHUTDOWN_EVENT == snp->sn_header.sn_type) {
-            DevMessage("Other peer has requested a com down -> not handled\n");
-//             return sctp_handle_com_down(snp->sn_shutdown_event.sse_assoc_id);
             itti_unsubscribe_event_fd(TASK_SCTP, sctp_cnx->sd);
+
+            close(sctp_cnx->sd);
+
+            sctp_itti_send_association_resp(
+                sctp_cnx->task_id, sctp_cnx->instance, sctp_cnx->assoc_id,
+                sctp_cnx->cnx_id, SCTP_STATE_SHUTDOWN,
+                0, 0);
+
+            STAILQ_REMOVE(&sctp_cnx_list, sctp_cnx, sctp_cnx_list_elm_s, entries);
+            sctp_nb_cnx--;
+
+            free(sctp_cnx);
         }
         /* Association has changed. */
         else if (SCTP_ASSOC_CHANGE == snp->sn_header.sn_type) {
@@ -515,7 +544,8 @@ void *sctp_eNB_task(void *arg)
 
         /* Check if there is a packet to handle */
         if (received_msg != NULL) {
-            switch (ITTI_MSG_ID(received_msg)) {
+            switch (ITTI_MSG_ID(received_msg))
+            {
                 case TERMINATE_MESSAGE:
                     itti_exit_task();
                     break;
@@ -530,8 +560,8 @@ void *sctp_eNB_task(void *arg)
                                    &received_msg->msg.sctp_data_req);
                 } break;
                 default:
-                    SCTP_ERROR("Received unhandled message with id %d\n",
-                               ITTI_MSG_ID(received_msg));
+                    SCTP_ERROR("Received unhandled message %d:%s\n",
+                               ITTI_MSG_ID(received_msg), ITTI_MSG_NAME(received_msg));
                     break;
             }
         }
-- 
GitLab