Skip to content

Exploration #1190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1dcf69a
started exploration
shovon0203 Apr 14, 2025
d564c6e
logger added
shovon0203 Apr 21, 2025
b394331
log added
shovon0203 Apr 22, 2025
b29da17
added custom cmd to send a packet
shovon0203 Apr 24, 2025
607720b
added low level functions
shovon0203 Apr 28, 2025
1b1a21e
added the virtual functions in all other subclasses
shovon0203 Apr 28, 2025
ae88729
fixing errors
shovon0203 Apr 28, 2025
e58577d
fixing errors
shovon0203 Apr 28, 2025
378ed37
fixing errors
shovon0203 Apr 28, 2025
e1ba2e7
fixing errors
shovon0203 Apr 28, 2025
e830ab5
added all sending codes
shovon0203 Apr 28, 2025
0c40767
fixing compile error
shovon0203 Apr 28, 2025
99508f4
removed ngap_impl added codes
shovon0203 Apr 28, 2025
26c02fb
log added
shovon0203 Apr 28, 2025
de2f56e
added more logs
shovon0203 Apr 28, 2025
9fcf5d7
added plmn identity
shovon0203 Apr 28, 2025
62d8ea8
fixing plmn issue
shovon0203 Apr 28, 2025
4aaed97
fixing logger issue
shovon0203 Apr 28, 2025
430f068
fixing the virtual function issue
shovon0203 Apr 28, 2025
e5e7884
added debug log
shovon0203 Apr 28, 2025
f07e489
added logger
shovon0203 Apr 28, 2025
b95fa9d
logger
shovon0203 Apr 29, 2025
a8d7b31
modified
shovon0203 Apr 29, 2025
f6baf83
modified
shovon0203 Apr 29, 2025
3ce81e5
fixing compile error
shovon0203 Apr 29, 2025
8e3dbe6
fixing compile error
shovon0203 Apr 29, 2025
3289014
fixing
shovon0203 Apr 29, 2025
1cb3363
fix
shovon0203 Apr 29, 2025
fd56b6b
fix
shovon0203 Apr 29, 2025
c61d624
custom buffer test
shovon0203 Apr 29, 2025
afb8546
fix
shovon0203 Apr 29, 2025
2e6b943
fix
shovon0203 Apr 29, 2025
23bc375
removing codes
shovon0203 Apr 29, 2025
e3875e9
user thread
shovon0203 Apr 29, 2025
ea9e08d
added logger
shovon0203 Apr 29, 2025
f183afe
fixing compile error
shovon0203 Apr 29, 2025
bf963d1
added server
shovon0203 Apr 30, 2025
f1e5d6a
fixed the port
shovon0203 Apr 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file added apps/.DS_Store
Binary file not shown.
169 changes: 169 additions & 0 deletions apps/gnb/gnb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
#include "srsran/support/tracing/event_tracing.h"
#include "srsran/support/versioning/build_info.h"
#include "srsran/support/versioning/version.h"
#include <thread>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include "srsran/adt/byte_buffer.h"
#include "srsran/adt/span.h"
#include <atomic>
#ifdef DPDK_FOUND
#include "srsran/hal/dpdk/dpdk_eal_factory.h"
Expand Down Expand Up @@ -196,6 +206,122 @@ static void autoderive_cu_up_parameters_after_parsing(cu_up_unit_config& cu_up_c
}
}

void start_tcp_control_server(srsran::srs_cu_cp::ngap_interface* ngap, srslog::basic_logger& gnb_logger)
{
std::thread server_thread([=, &gnb_logger]() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[4096] = {0};

server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
gnb_logger.error("TCP server: Failed to create socket");
return;
}

int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8899);

if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
gnb_logger.error("TCP server: Bind failed");
close(server_fd);
return;
}

if (listen(server_fd, 3) < 0) {
gnb_logger.error("TCP server: Listen failed");
close(server_fd);
return;
}

gnb_logger.info("TCP server started on port 8899. Use 'telnet 127.0.0.1 8899' to send commands.");

while (true) {
new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen);
if (new_socket < 0) {
gnb_logger.error("TCP server: Accept failed");
continue;
}

ssize_t read_size;
while ((read_size = read(new_socket, buffer, sizeof(buffer) - 1)) > 0) {
buffer[read_size] = '\0';
std::string line(buffer);
std::istringstream iss(line);
std::string command;
iss >> command;
if (command == "send") {
std::vector<uint8_t> bytes;
std::string byte_str;
while (iss >> byte_str) {
try {
uint8_t byte = static_cast<uint8_t>(std::stoul(byte_str, nullptr, 16));
bytes.push_back(byte);
} catch (const std::exception& e) {
gnb_logger.error("Parse error '{}': {}", byte_str, e.what());
}
}

if (bytes.empty()) {
gnb_logger.error("No valid bytes received for send");
continue;
}

srsran::byte_buffer buf;
span<const uint8_t> bytes_view(bytes.data(), bytes.size());
if (not buf.append(bytes_view)) {
gnb_logger.error("Failed to build byte_buffer");
continue;
}

if (ngap->send_custom_pdu(std::move(buf))) {
gnb_logger.info("Sent dynamic NGAP packet ({} bytes)", bytes.size());
} else {
gnb_logger.error("Failed to send dynamic NGAP packet");
}
} else if (command == "sendfile") {
std::string filepath;
iss >> filepath;
if (filepath.empty()) {
gnb_logger.error("No filepath provided");
continue;
}
std::ifstream file(filepath, std::ios::binary);
if (!file.is_open()) {
gnb_logger.error("Failed to open file '{}'", filepath);
continue;
}
std::vector<uint8_t> bytes((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());

srsran::byte_buffer buf;
span<const uint8_t> bytes_view(bytes.data(), bytes.size());
if (not buf.append(bytes_view)) {
gnb_logger.error("Failed to build byte_buffer from file");
continue;
}

if (ngap->send_custom_pdu(std::move(buf))) {
gnb_logger.info("Sent NGAP packet from file '{}' ({} bytes)", filepath, bytes.size());
} else {
gnb_logger.error("Failed to send NGAP packet from file");
}
} else {
gnb_logger.error("Unknown command '{}'", command);
}
}
close(new_socket);
}
});
server_thread.detach();
}


int main(int argc, char** argv)
{
// Set the application error handler.
Expand Down Expand Up @@ -495,6 +621,49 @@ int main(int argc, char** argv)
report_error("CU-CP failed to connect to AMF");
}

auto my_plmn_expected = srsran::plmn_identity::parse("00101");

if (my_plmn_expected.has_value()) {
plmn_identity my_plmn = my_plmn_expected.value();

// Get NGAP notifier
srsran::srs_cu_cp::ngap_interface* ngap = o_cucp_obj.get_cu_cp().find_ngap_by_plmn(my_plmn);

if (ngap != nullptr) {
gnb_logger.info("Connected to AMF. Ready to send packets.");

start_tcp_control_server(ngap, gnb_logger);

/*
// Example fake NGAP packet (not real ASN.1, just for testing transmission)
std::vector<uint8_t> my_raw_bytes = {
0x00, 0x08, // Message Length (fake)
0x00, 0x01, // NGAP Procedure Code (fake)
0x01, 0x00, // Criticality (fake)
0x02, 0x00, // Some Information Element (fake)
0x03, 0x04 // Some other field (fake)
};

span<const uint8_t> bytes_view(my_raw_bytes.data(), my_raw_bytes.size());
byte_buffer my_custom_buffer;

if (not my_custom_buffer.append(bytes_view)) {
gnb_logger.error("Failed to build byte_buffer from custom raw bytes");
}

bool success = ngap->send_custom_pdu(std::move(my_custom_buffer));
if (success) {
gnb_logger.info("Successfully sent custom NGAP PDU");
} else {
gnb_logger.error("Failed to send custom NGAP PDU");
}*/
} else {
gnb_logger.info("NGAP interface is not ready");
}
} else {
gnb_logger.info("Failed to parse PLMN identity");
}

// Connect F1-C to O-CU-CP and start listening for new F1-C connection requests.
f1c_gw->attach_cu_cp(o_cucp_obj.get_cu_cp().get_f1c_handler());

Expand Down
Loading