Commit 238c743b authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen

Code cleanup/format for PFCP/UDP

parent 6937f31e
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under * The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file * the OAI Public License, Version 1.1 (the "License"); you may not use this
* except in compliance with the License. * file except in compliance with the License. You may obtain a copy of the
* You may obtain a copy of the License at * License at
* *
* http://www.openairinterface.org/?page_id=698 * http://www.openairinterface.org/?page_id=698
* *
...@@ -31,143 +31,152 @@ ...@@ -31,143 +31,152 @@
#include <cstdlib> #include <cstdlib>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void udp_application::handle_receive(char* recv_buffer, const std::size_t bytes_transferred, const endpoint& r_endpoint) void udp_application::handle_receive(
{ char* recv_buffer, const std::size_t bytes_transferred,
Logger::udp().warn( "Missing implementation of interface udp_application\n"); const endpoint& r_endpoint) {
Logger::udp().warn("Missing implementation of interface udp_application\n");
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void udp_application::start_receive(udp_application * gtp_stack, const util::thread_sched_params& sched_params) void udp_application::start_receive(
{ udp_application* gtp_stack, const util::thread_sched_params& sched_params) {
Logger::udp().warn( "Missing implementation of interface udp_application\n"); Logger::udp().warn("Missing implementation of interface udp_application\n");
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
static std::string string_to_hex(const std::string& input) static std::string string_to_hex(const std::string& input) {
{ static const char* const lut = "0123456789ABCDEF";
static const char* const lut = "0123456789ABCDEF"; size_t len = input.length();
size_t len = input.length();
std::string output;
std::string output; output.reserve(2 * len);
output.reserve(2 * len); for (size_t i = 0; i < len; ++i) {
for (size_t i = 0; i < len; ++i) const unsigned char c = input[i];
{ output.push_back(lut[c >> 4]);
const unsigned char c = input[i]; output.push_back(lut[c & 15]);
output.push_back(lut[c >> 4]); }
output.push_back(lut[c & 15]); return output;
}
return output;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void udp_server::udp_read_loop(const util::thread_sched_params& sched_params) void udp_server::udp_read_loop(const util::thread_sched_params& sched_params) {
{ endpoint r_endpoint = {};
endpoint r_endpoint = {}; size_t bytes_received = 0;
size_t bytes_received = 0;
sched_params.apply(TASK_NONE, Logger::udp()); sched_params.apply(TASK_NONE, Logger::udp());
while (1) { while (1) {
r_endpoint.addr_storage_len = sizeof(struct sockaddr_storage); r_endpoint.addr_storage_len = sizeof(struct sockaddr_storage);
if ((bytes_received = recvfrom (socket_, recv_buffer_, UDP_RECV_BUFFER_SIZE, 0, (struct sockaddr *)&r_endpoint.addr_storage, &r_endpoint.addr_storage_len)) > 0) { if ((bytes_received = recvfrom(
socket_, recv_buffer_, UDP_RECV_BUFFER_SIZE, 0,
(struct sockaddr*) &r_endpoint.addr_storage,
&r_endpoint.addr_storage_len)) > 0) {
app_->handle_receive(recv_buffer_, bytes_received, r_endpoint); app_->handle_receive(recv_buffer_, bytes_received, r_endpoint);
} else { } else {
Logger::udp().error( "Recvfrom failed %s\n", strerror (errno)); Logger::udp().error("Recvfrom failed %s\n", strerror(errno));
} }
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
int udp_server::create_socket (const struct in_addr &address, const uint16_t port) int udp_server::create_socket(
{ const struct in_addr& address, const uint16_t port) {
struct sockaddr_in addr = {}; struct sockaddr_in addr = {};
int sd = 0; int sd = 0;
/* /*
* Create UDP socket * Create UDP socket
*/ */
if ((sd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { if ((sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
/* /*
* Socket creation has failed... * Socket creation has failed...
*/ */
Logger::udp().error( "Socket creation failed (%s)\n", strerror (errno)); Logger::udp().error("Socket creation failed (%s)\n", strerror(errno));
return errno; return errno;
} }
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons (port); addr.sin_port = htons(port);
addr.sin_addr.s_addr = address.s_addr; addr.sin_addr.s_addr = address.s_addr;
std::string ipv4 = conv::toString(address); std::string ipv4 = conv::toString(address);
Logger::udp().debug("Creating new listen socket on address %s and port %" PRIu16 " ", ipv4.c_str(), port); Logger::udp().debug(
"Creating new listen socket on address %s and port %" PRIu16 " ",
ipv4.c_str(), port);
if (bind (sd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in)) < 0) { if (bind(sd, (struct sockaddr*) &addr, sizeof(struct sockaddr_in)) < 0) {
/* /*
* Bind failed * Bind failed
*/ */
Logger::udp().error("Socket bind failed (%s) for address %s and port %" PRIu16 " ", strerror (errno), ipv4.c_str(), port); Logger::udp().error(
close (sd); "Socket bind failed (%s) for address %s and port %" PRIu16 " ",
strerror(errno), ipv4.c_str(), port);
close(sd);
return errno; return errno;
} }
sa_family = AF_INET; sa_family = AF_INET;
return sd; return sd;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
int udp_server::create_socket (const struct in6_addr &address, const uint16_t port) int udp_server::create_socket(
{ const struct in6_addr& address, const uint16_t port) {
struct sockaddr_in6 addr = {}; struct sockaddr_in6 addr = {};
int sd = 0; int sd = 0;
/* /*
* Create UDP socket * Create UDP socket
*/ */
if ((sd = socket (AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) { if ((sd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
/* /*
* Socket creation has failed... * Socket creation has failed...
*/ */
Logger::udp().error( "Socket creation failed (%s)\n", strerror (errno)); Logger::udp().error("Socket creation failed (%s)\n", strerror(errno));
return errno; return errno;
} }
addr.sin6_family = AF_INET6; addr.sin6_family = AF_INET6;
addr.sin6_port = htons (port); addr.sin6_port = htons(port);
addr.sin6_addr = address; addr.sin6_addr = address;
std::string ipv6 = conv::toString(address); std::string ipv6 = conv::toString(address);
Logger::udp().debug("Creating new listen socket on address %s and port %" PRIu16 "\n", ipv6.c_str(), port); Logger::udp().debug(
"Creating new listen socket on address %s and port %" PRIu16 "\n",
ipv6.c_str(), port);
if (bind (sd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in6)) < 0) { if (bind(sd, (struct sockaddr*) &addr, sizeof(struct sockaddr_in6)) < 0) {
/* /*
* Bind failed * Bind failed
*/ */
Logger::udp().error("Socket bind failed (%s) for address %s and port %" PRIu16 "\n", strerror (errno), ipv6.c_str(), port); Logger::udp().error(
close (sd); "Socket bind failed (%s) for address %s and port %" PRIu16 "\n",
strerror(errno), ipv6.c_str(), port);
close(sd);
return errno; return errno;
} }
sa_family = AF_INET6; sa_family = AF_INET6;
return sd; return sd;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
int udp_server::create_socket (const char * address, const uint16_t port_num) int udp_server::create_socket(const char* address, const uint16_t port_num) {
{
unsigned char buf_in_addr[sizeof(struct in6_addr)]; unsigned char buf_in_addr[sizeof(struct in6_addr)];
if (inet_pton (AF_INET, address, buf_in_addr) == 1) { if (inet_pton(AF_INET, address, buf_in_addr) == 1) {
struct in_addr addr4 = {}; struct in_addr addr4 = {};
memcpy (&addr4, buf_in_addr, sizeof (struct in_addr)); memcpy(&addr4, buf_in_addr, sizeof(struct in_addr));
return create_socket(addr4, port_num); return create_socket(addr4, port_num);
} else if (inet_pton (AF_INET6, address, buf_in_addr) == 1) { } else if (inet_pton(AF_INET6, address, buf_in_addr) == 1) {
struct in6_addr addr6 = {}; struct in6_addr addr6 = {};
memcpy (&addr6, buf_in_addr, sizeof (struct in6_addr)); memcpy(&addr6, buf_in_addr, sizeof(struct in6_addr));
return create_socket(addr6, port_num); return create_socket(addr6, port_num);
} else { } else {
Logger::udp().error( "udp_server::create_socket(%s:%d)", address, port_num); Logger::udp().error("udp_server::create_socket(%s:%d)", address, port_num);
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
throw std::system_error(socket_, std::generic_category(), "UDP socket creation failed!"); throw std::system_error(
socket_, std::generic_category(), "UDP socket creation failed!");
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void udp_server::start_receive(udp_application * app, const util::thread_sched_params& sched_params) void udp_server::start_receive(
{ udp_application* app, const util::thread_sched_params& sched_params) {
app_ = app; app_ = app;
Logger::udp().trace( "udp_server::start_receive"); Logger::udp().trace("udp_server::start_receive");
thread_ = std::thread(&udp_server::udp_read_loop,this, sched_params); thread_ = std::thread(&udp_server::udp_read_loop, this, sched_params);
thread_.detach(); thread_.detach();
} }
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under * The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file * the OAI Public License, Version 1.1 (the "License"); you may not use this
* except in compliance with the License. * file except in compliance with the License. You may obtain a copy of the
* You may obtain a copy of the License at * License at
* *
* http://www.openairinterface.org/?page_id=698 * http://www.openairinterface.org/?page_id=698
* *
...@@ -47,120 +47,133 @@ ...@@ -47,120 +47,133 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
class udp_application {
public:
virtual void handle_receive(
class udp_application char* recv_buffer, const std::size_t bytes_transferred,
{ const endpoint& r_endpoint);
public: virtual void start_receive(
virtual void handle_receive(char* recv_buffer, const std::size_t bytes_transferred, const endpoint& r_endpoint); udp_application* gtp_stack,
virtual void start_receive(udp_application * gtp_stack, const util::thread_sched_params& sched_params); const util::thread_sched_params& sched_params);
}; };
class udp_server class udp_server {
{ public:
public:
udp_server(const struct in_addr& address, const uint16_t port_num) udp_server(const struct in_addr& address, const uint16_t port_num)
: app_(nullptr), port_(port_num) : app_(nullptr), port_(port_num) {
{
recv_buffer_[0] = 0; recv_buffer_[0] = 0;
socket_ = create_socket (address, port_); socket_ = create_socket(address, port_);
if (socket_ > 0) { if (socket_ > 0) {
Logger::udp().debug( "udp_server::udp_server(%s:%d)", conv::toString(address).c_str(), port_); Logger::udp().debug(
"udp_server::udp_server(%s:%d)", conv::toString(address).c_str(),
port_);
sa_family = AF_INET; sa_family = AF_INET;
} else { } else {
Logger::udp().error( "udp_server::udp_server(%s:%d)", conv::toString(address).c_str(), port_); Logger::udp().error(
"udp_server::udp_server(%s:%d)", conv::toString(address).c_str(),
port_);
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
throw std::system_error(socket_, std::generic_category(), "GTPV1-U socket creation failed!"); throw std::system_error(
socket_, std::generic_category(), "GTPV1-U socket creation failed!");
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
udp_server(const struct in6_addr& address, const uint16_t port_num) udp_server(const struct in6_addr& address, const uint16_t port_num)
: app_(nullptr), port_(port_num) : app_(nullptr), port_(port_num) {
{
recv_buffer_[0] = 0; recv_buffer_[0] = 0;
socket_ = create_socket (address, port_); socket_ = create_socket(address, port_);
if (socket_ > 0) { if (socket_ > 0) {
Logger::udp().debug( "udp_server::udp_server(%s:%d)", conv::toString(address).c_str(), port_); Logger::udp().debug(
"udp_server::udp_server(%s:%d)", conv::toString(address).c_str(),
port_);
sa_family = AF_INET6; sa_family = AF_INET6;
} else { } else {
Logger::udp().error( "udp_server::udp_server(%s:%d)", conv::toString(address).c_str(), port_); Logger::udp().error(
"udp_server::udp_server(%s:%d)", conv::toString(address).c_str(),
port_);
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
throw std::system_error(socket_, std::generic_category(), "GTPV1-U socket creation failed!"); throw std::system_error(
socket_, std::generic_category(), "GTPV1-U socket creation failed!");
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
udp_server(const char * address, const uint16_t port_num) udp_server(const char* address, const uint16_t port_num)
: app_(nullptr), port_(port_num) : app_(nullptr), port_(port_num) {
{
recv_buffer_[0] = 0; recv_buffer_[0] = 0;
socket_ = create_socket (address, port_); socket_ = create_socket(address, port_);
if (socket_ > 0) { if (socket_ > 0) {
Logger::udp().debug( "udp_server::udp_server(%s:%d)", address, port_); Logger::udp().debug("udp_server::udp_server(%s:%d)", address, port_);
} else { } else {
Logger::udp().error( "udp_server::udp_server(%s:%d)", address, port_); Logger::udp().error("udp_server::udp_server(%s:%d)", address, port_);
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
throw std::system_error(socket_, std::generic_category(), "GTPV1-U socket creation failed!"); throw std::system_error(
socket_, std::generic_category(), "GTPV1-U socket creation failed!");
} }
} }
~udp_server() ~udp_server() { close(socket_); }
{
close(socket_);
}
void udp_read_loop(const util::thread_sched_params& thread_sched_params); void udp_read_loop(const util::thread_sched_params& thread_sched_params);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void async_send_to(const char* send_buffer, const ssize_t num_bytes, const endpoint& r_endpoint) void async_send_to(
{ const char* send_buffer, const ssize_t num_bytes,
ssize_t bytes_written = sendto (socket_, send_buffer, num_bytes, 0, (struct sockaddr *)&r_endpoint.addr_storage, r_endpoint.addr_storage_len); const endpoint& r_endpoint) {
ssize_t bytes_written = sendto(
socket_, send_buffer, num_bytes, 0,
(struct sockaddr*) &r_endpoint.addr_storage,
r_endpoint.addr_storage_len);
if (bytes_written != num_bytes) { if (bytes_written != num_bytes) {
Logger::udp().error( "sendto failed(%d:%s)\n", errno, strerror (errno)); Logger::udp().error("sendto failed(%d:%s)\n", errno, strerror(errno));
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void async_send_to(const char* send_buffer, const ssize_t num_bytes, const struct sockaddr_in& r_endpoint) void async_send_to(
{ const char* send_buffer, const ssize_t num_bytes,
ssize_t bytes_written = sendto (socket_, send_buffer, num_bytes, 0, (struct sockaddr *)&r_endpoint, sizeof(struct sockaddr_in)); const struct sockaddr_in& r_endpoint) {
ssize_t bytes_written = sendto(
socket_, send_buffer, num_bytes, 0, (struct sockaddr*) &r_endpoint,
sizeof(struct sockaddr_in));
if (bytes_written != num_bytes) { if (bytes_written != num_bytes) {
Logger::udp().error( "sendto failed(%d:%s)\n", errno, strerror (errno)); Logger::udp().error("sendto failed(%d:%s)\n", errno, strerror(errno));
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void async_send_to(const char* send_buffer, const ssize_t num_bytes, const struct sockaddr_in6& r_endpoint) void async_send_to(
{ const char* send_buffer, const ssize_t num_bytes,
ssize_t bytes_written = sendto (socket_, send_buffer, num_bytes, 0, (struct sockaddr *)&r_endpoint, sizeof(struct sockaddr_in6)); const struct sockaddr_in6& r_endpoint) {
ssize_t bytes_written = sendto(
socket_, send_buffer, num_bytes, 0, (struct sockaddr*) &r_endpoint,
sizeof(struct sockaddr_in6));
if (bytes_written != num_bytes) { if (bytes_written != num_bytes) {
Logger::udp().error( "sendto failed(%d:%s)\n", errno, strerror (errno)); Logger::udp().error("sendto failed(%d:%s)\n", errno, strerror(errno));
} }
} }
void start_receive(udp_application * gtp_stack, const util::thread_sched_params& sched_params); void start_receive(
udp_application* gtp_stack,
const util::thread_sched_params& sched_params);
protected: protected:
int create_socket (const struct in_addr& address, const uint16_t port); int create_socket(const struct in_addr& address, const uint16_t port);
int create_socket (const struct in6_addr& address, const uint16_t port); int create_socket(const struct in6_addr& address, const uint16_t port);
int create_socket (const char * address, const uint16_t port_num); int create_socket(const char* address, const uint16_t port_num);
static void handle_send(const char *, /*buffer*/ static void handle_send(
const int& /*error*/, const char*, /*buffer*/
std::size_t /*bytes_transferred*/) const int& /*error*/, std::size_t /*bytes_transferred*/) {}
{
}
udp_application* app_; udp_application* app_;
std::thread thread_; std::thread thread_;
int socket_; int socket_;
uint16_t port_; uint16_t port_;
sa_family_t sa_family; sa_family_t sa_family;
#define UDP_RECV_BUFFER_SIZE 8192 #define UDP_RECV_BUFFER_SIZE 8192
char recv_buffer_[UDP_RECV_BUFFER_SIZE]; char recv_buffer_[UDP_RECV_BUFFER_SIZE];
}; };
#endif /* FILE_UDP_HPP_SEEN */ #endif /* FILE_UDP_HPP_SEEN */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment