---
# System prepended metadata

title: SCTP socket interface
tags: [2. Plan]

---

SCTP
===

###### tags: `free5gc` `openairinterface`

:::info
- **Location:** RT LAB
- **Date:** August 12, 2024
- **Virtual requirment:**
1. VM1 (sctp server)  
    - VM OS : Ubuntu 18.04.6
    - libsctp-dev :1.0.18
    - IP : 192.168.60.22
2. VM2 (sctp client)
    - VM OS : Ubuntu 20.04.6
    - libsctp-dev :1.0.18
    - IP : 192.168.60.14
- **Author:** Saffana Zyan DINI
- **Contact:** Zyzy <M11102815@gapps.ntust.edu.tw>
- **Reference:** 
    - [SCTP Socket Interfaces](https://docs.oracle.com/cd/E19253-01/817-4415/sockets-18/index.html)
    - [Code Examples of SCTP Use](https://docs.oracle.com/cd/E19253-01/817-4415/sockets-27/index.html)
:::

# Abstrak

:dart: SCTP socket interface
---
In 5G network architectures, the communication between the Central Unit (CU) and Distributed Unit (DU) is critical for maintaining network performance and reliability. The Stream Control Transmission Protocol (SCTP) is central to supporting various procedures essential for this interaction. Key procedures include Initial Context Setup, Bearer Resource Command, User Plane Path Configuration, and Signaling Message Exchange. SCTP ensures reliable and ordered delivery of signaling messages, which is vital for establishing and managing user contexts and bearer resources. In the context of the gNB system in UERANSIM, SCTP facilitates the NGAP (Next Generation Application Protocol) or N1 and N2 interfaces to the AMF (Access and Mobility Management Function). User modifications are focused on transitioning from NGAP procedures to RRC (Radio Resource Control) procedures or the F1AP (F1 Application Protocol) interface to the OCU (Operational Control Unit). SCTP’s multi-homing capability supports load balancing and failover, enhancing network resilience. Its message-oriented approach ensures precise signaling for QoS and handovers, while its failure recovery and path management features contribute to overall network stability. SCTP’s robust transport mechanisms are thus integral to the efficient and resilient operation of CU and DU interactions in 5G networks.


:books: Problem
---
* free5gc before connect cu
![image](https://hackmd.io/_uploads/r16qHJI3A.png)
* after connect cu
![image](https://hackmd.io/_uploads/H120BJUn0.png)
* before connect ueransim
![image](https://hackmd.io/_uploads/B1xzLy83R.png)
sudo netstat --sctp -tulpn
![image](https://hackmd.io/_uploads/B188LkUhC.png)
* after connect ueransim
![image](https://hackmd.io/_uploads/ryvc81LnC.png)
* ueransim gnb log
![image](https://hackmd.io/_uploads/B1RjUk8n0.png)
actual cu for F1AP connection
![image](https://hackmd.io/_uploads/rJj2PyUnC.png)


:mag: Understand SCTP work by testing simple code
---
### VM1
* install lib-sctp
To create an SCTP socket interface, we need to install libsctp-tools and libsctp-dev by using the following command:

```
sudo apt install libsctp-dev libsctp-dev
```
![image](https://hackmd.io/_uploads/ByX4lV23A.png)

* create sctp_server.c file

```
sudo nano sctp_server.c
```
* put this code inside the sctp_server.c file
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/sctp.h>   // Include this for SCTP definitions
#include <arpa/inet.h>


#define PORT 5000
#define MAX_BUFFER 1024
#define BACKLOG 5

int main() {
    int listen_fd, conn_fd;
    struct sockaddr_in server_addr, client_addr;
    char buffer[MAX_BUFFER];
    socklen_t addr_len;
    
    // Create SCTP socket
    listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
    if (listen_fd < 0) {
        perror("Socket creation failed");
        exit(1);
    }

    // Initialize server address structure
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(PORT);

    // Bind the socket
    if (bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        perror("Bind failed");
        close(listen_fd);
        exit(1);
    }

    // Listen for incoming connections
    if (listen(listen_fd, BACKLOG) < 0) {
        perror("Listen failed");
        close(listen_fd);
        exit(1);
    }

    printf("SCTP server is listening on port %d...\n", PORT);

    // Accept connection
    addr_len = sizeof(client_addr);
    conn_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &addr_len);
    if (conn_fd < 0) {
        perror("Accept failed");
        close(listen_fd);
        exit(1);
    }

    // Receive data from the client
    int bytes_received = recv(conn_fd, buffer, sizeof(buffer), 0);
    if (bytes_received > 0) {
        printf("Received message: %s\n", buffer);
    }

    // Send response to client
    const char *response = "Hello from the server!";
    send(conn_fd, response, strlen(response), 0);

    // Close the connection
    close(conn_fd);
    close(listen_fd);
    return 0;
}

```
* Compile and Run the Code
```
gcc -o sctp_server sctp_server.c -lsctp
``` 
* On VM1 (192.168.60.22), run the server:
```
./sctp_server
```
* Result
![image](https://hackmd.io/_uploads/r1UrmVn3C.png)
### VM2

To create an SCTP socket interface, we need to install libsctp-tools and libsctp-dev by using the following command:

```
sudo apt install libsctp-dev libsctp-dev
```
![image](https://hackmd.io/_uploads/ByX4lV23A.png)

* create sctp_client.c file
```
sudo nano sctp_client.c
```
put this code inside the sctp_client.c file
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/sctp.h>   // Include this for SCTP definitions
#include <arpa/inet.h>


#define PORT 5000
#define MAX_BUFFER 1024

int main() {
    int sock_fd;
    struct sockaddr_in server_addr;
    char buffer[MAX_BUFFER] = "Hello from the client!";

    // Create SCTP socket
    sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
    if (sock_fd < 0) {
        perror("Socket creation failed");
        exit(1);
    }

    // Initialize server address structure
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("192.168.60.22");  // Replace with server's IP
    server_addr.sin_port = htons(PORT);

    // Connect to the server
    if (connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        perror("Connect failed");
        close(sock_fd);
        exit(1);
    }

    // Send data to the server
    send(sock_fd, buffer, strlen(buffer), 0);

    // Receive response from server
    int bytes_received = recv(sock_fd, buffer, sizeof(buffer), 0);
    if (bytes_received > 0) {
        printf("Received message: %s\n", buffer);
    }

    // Close the socket
    close(sock_fd);
    return 0;
}

```
* Compile and Run the Code
```
gcc -o sctp_server sctp_client.c -lsctp
``` 
* On VM1 (192.168.60.14), run the client:
```
./sctp_client
```
* Result
![image](https://hackmd.io/_uploads/SytF7N2n0.png)
* check active internet connection
![image](https://hackmd.io/_uploads/r1GCVE22C.png)

:mag: Trobleshooting
---

![image](https://hackmd.io/_uploads/HkSCXEnnA.png)
if you find this following error, you need to add this  library to it
```
#include <netinet/sctp.h>   // Include this for SCTP definitions
```
:mag: Concluision
---
In the context of 5G network simulations using UERANSIM and OpenAirInterface, it is crucial to adapt the communication protocols to meet specific requirements. To enhance the gNB (gNodeB) functionality, there is a need to transition from the NGAP (Next Generation Application Protocol) procedures to RRC (Radio Resource Control) procedures. Concurrently, modifications must be made to the response handling on the CU (Central Unit) within OpenAirInterface. The initial step in this process involves identifying and examining the relevant code that manages these NGAP procedures. It is anticipated that the code handling these procedures resides within a specific file, which will be the focus of subsequent modifications to ensure the proper implementation of RRC procedures and appropriate response adjustments.

Therefore, we need to change the NGAP procedures to RRC on the gNB (UERANSIM) and modify the responses on the CU (OpenAirInterface). The first step is to locate the code that handles these procedures. From my estimation, one of the relevant code files might be:
![image](https://hackmd.io/_uploads/SJvguE2h0.png)
