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 @@
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* 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
* except in compliance with the License.
* You may obtain a copy of the License at
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
......@@ -31,27 +31,26 @@
#include <cstdlib>
//------------------------------------------------------------------------------
void udp_application::handle_receive(char* recv_buffer, const std::size_t bytes_transferred, const endpoint& r_endpoint)
{
Logger::udp().warn( "Missing implementation of interface udp_application\n");
void udp_application::handle_receive(
char* recv_buffer, const std::size_t bytes_transferred,
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)
{
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) {
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";
size_t len = input.length();
std::string output;
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]);
output.push_back(lut[c & 15]);
......@@ -59,8 +58,7 @@ static std::string string_to_hex(const std::string& input)
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 = {};
size_t bytes_received = 0;
......@@ -68,106 +66,117 @@ void udp_server::udp_read_loop(const util::thread_sched_params& sched_params)
while (1) {
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);
} 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 = {};
int sd = 0;
/*
* 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...
*/
Logger::udp().error( "Socket creation failed (%s)\n", strerror (errno));
Logger::udp().error("Socket creation failed (%s)\n", strerror(errno));
return errno;
}
addr.sin_family = AF_INET;
addr.sin_port = htons (port);
addr.sin_port = htons(port);
addr.sin_addr.s_addr = address.s_addr;
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
*/
Logger::udp().error("Socket bind failed (%s) for address %s and port %" PRIu16 " ", strerror (errno), ipv4.c_str(), port);
close (sd);
Logger::udp().error(
"Socket bind failed (%s) for address %s and port %" PRIu16 " ",
strerror(errno), ipv4.c_str(), port);
close(sd);
return errno;
}
sa_family = AF_INET;
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 = {};
int sd = 0;
/*
* 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...
*/
Logger::udp().error( "Socket creation failed (%s)\n", strerror (errno));
Logger::udp().error("Socket creation failed (%s)\n", strerror(errno));
return errno;
}
addr.sin6_family = AF_INET6;
addr.sin6_port = htons (port);
addr.sin6_port = htons(port);
addr.sin6_addr = 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
*/
Logger::udp().error("Socket bind failed (%s) for address %s and port %" PRIu16 "\n", strerror (errno), ipv6.c_str(), port);
close (sd);
Logger::udp().error(
"Socket bind failed (%s) for address %s and port %" PRIu16 "\n",
strerror(errno), ipv6.c_str(), port);
close(sd);
return errno;
}
sa_family = AF_INET6;
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)];
if (inet_pton (AF_INET, address, buf_in_addr) == 1) {
if (inet_pton(AF_INET, address, buf_in_addr) == 1) {
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);
} 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 = {};
memcpy (&addr6, buf_in_addr, sizeof (struct in6_addr));
memcpy(&addr6, buf_in_addr, sizeof(struct in6_addr));
return create_socket(addr6, port_num);
} 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));
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;
Logger::udp().trace( "udp_server::start_receive");
thread_ = std::thread(&udp_server::udp_read_loop,this, sched_params);
Logger::udp().trace("udp_server::start_receive");
thread_ = std::thread(&udp_server::udp_read_loop, this, sched_params);
thread_.detach();
}
......@@ -3,9 +3,9 @@
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* 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
* except in compliance with the License.
* You may obtain a copy of the License at
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
......@@ -47,111 +47,125 @@
#include <utility>
#include <vector>
class udp_application
{
public:
virtual void handle_receive(char* recv_buffer, const std::size_t bytes_transferred, const endpoint& r_endpoint);
virtual void start_receive(udp_application * gtp_stack, const util::thread_sched_params& sched_params);
class udp_application {
public:
virtual void handle_receive(
char* recv_buffer, const std::size_t bytes_transferred,
const endpoint& r_endpoint);
virtual void start_receive(
udp_application* gtp_stack,
const util::thread_sched_params& sched_params);
};
class udp_server
{
public:
class udp_server {
public:
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;
socket_ = create_socket (address, port_);
socket_ = create_socket(address, port_);
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;
} 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));
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)
: app_(nullptr), port_(port_num)
{
: app_(nullptr), port_(port_num) {
recv_buffer_[0] = 0;
socket_ = create_socket (address, port_);
socket_ = create_socket(address, port_);
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;
} 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));
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)
: app_(nullptr), port_(port_num)
{
udp_server(const char* address, const uint16_t port_num)
: app_(nullptr), port_(port_num) {
recv_buffer_[0] = 0;
socket_ = create_socket (address, port_);
socket_ = create_socket(address, port_);
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 {
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));
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()
{
close(socket_);
}
~udp_server() { close(socket_); }
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)
{
ssize_t bytes_written = sendto (socket_, send_buffer, num_bytes, 0, (struct sockaddr *)&r_endpoint.addr_storage, r_endpoint.addr_storage_len);
void async_send_to(
const char* send_buffer, const ssize_t num_bytes,
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) {
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)
{
ssize_t bytes_written = sendto (socket_, send_buffer, num_bytes, 0, (struct sockaddr *)&r_endpoint, sizeof(struct sockaddr_in));
void async_send_to(
const char* send_buffer, const ssize_t num_bytes,
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) {
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)
{
ssize_t bytes_written = sendto (socket_, send_buffer, num_bytes, 0, (struct sockaddr *)&r_endpoint, sizeof(struct sockaddr_in6));
void async_send_to(
const char* send_buffer, const ssize_t num_bytes,
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) {
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:
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 char * address, const uint16_t port_num);
protected:
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 char* address, const uint16_t port_num);
static void handle_send(const char *, /*buffer*/
const int& /*error*/,
std::size_t /*bytes_transferred*/)
{
}
static void handle_send(
const char*, /*buffer*/
const int& /*error*/, std::size_t /*bytes_transferred*/) {}
udp_application* app_;
std::thread thread_;
......@@ -162,5 +176,4 @@ protected:
char recv_buffer_[UDP_RECV_BUFFER_SIZE];
};
#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