Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.

Commit

Permalink
[2.4.0] provide support for unix sockets (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cylix committed Apr 10, 2017
1 parent b9b304c commit fd5d2d2
Showing 1 changed file with 61 additions and 24 deletions.
85 changes: 61 additions & 24 deletions sources/network/unix/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>

namespace tacopie {
Expand Down Expand Up @@ -106,28 +107,45 @@ tcp_socket::send(const std::vector<char>& data, std::size_t size_to_write) {

void
tcp_socket::connect(const std::string& host, std::uint32_t port) {
//! Reset host and port
m_host = host;
m_port = port;

create_socket_if_necessary();
check_or_set_type(type::CLIENT);

struct sockaddr_un server_addr_un;
struct sockaddr_in server_addr_in;

//! Handle case of unix sockets if port is 0
bool is_unix_socket = m_port == 0;
if (is_unix_socket) {
std::memset(&server_addr_un, 0, sizeof(server_addr_un));
server_addr_un.sun_family = AF_UNIX;
strncpy(server_addr_un.sun_path, host.c_str(), sizeof(server_addr_un.sun_path) - 1);
}
else {
struct addrinfo* result = nullptr;
struct addrinfo hints;

struct addrinfo* result = nullptr;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;

memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
if (getaddrinfo(host.c_str(), nullptr, &hints, &result) != 0) { __TACOPIE_THROW(error, "getaddrinfo() failure"); }

if (getaddrinfo(host.c_str(), nullptr, &hints, &result) != 0) { __TACOPIE_THROW(error, "getaddrinfo() failure"); }
std::memset(&server_addr_in, 0, sizeof(server_addr_in));
server_addr_in.sin_addr = ((struct sockaddr_in*) (result->ai_addr))->sin_addr;
server_addr_in.sin_port = htons(port);
server_addr_in.sin_family = AF_INET;

struct sockaddr_in server_addr;
std::memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_addr = ((struct sockaddr_in*) (result->ai_addr))->sin_addr;
server_addr.sin_port = htons(port);
server_addr.sin_family = AF_INET;
freeaddrinfo(result);
}

freeaddrinfo(result);
socklen_t addr_len = is_unix_socket ? sizeof(server_addr_un) : sizeof(server_addr_in);
const struct sockaddr* server_addr = is_unix_socket ? (const struct sockaddr*) &server_addr_un : (const struct sockaddr*) &server_addr_in;

if (::connect(m_fd, (const struct sockaddr*) &server_addr, sizeof(server_addr)) == -1) { __TACOPIE_THROW(error, "connect() failure"); }
if (::connect(m_fd, server_addr, addr_len) == -1) { __TACOPIE_THROW(error, "connect() failure"); }
}

//!
Expand All @@ -136,24 +154,42 @@ tcp_socket::connect(const std::string& host, std::uint32_t port) {

void
tcp_socket::bind(const std::string& host, std::uint32_t port) {
//! Reset host and port
m_host = host;
m_port = port;

create_socket_if_necessary();
check_or_set_type(type::SERVER);

struct addrinfo* result = nullptr;
struct sockaddr_un server_addr_un;
struct sockaddr_in server_addr_in;

if (getaddrinfo(host.c_str(), nullptr, nullptr, &result) != 0) {
__TACOPIE_THROW(error, "getaddrinfo() failure");
//! Handle case of unix sockets if port is 0
bool is_unix_socket = m_port == 0;
if (is_unix_socket) {
std::memset(&server_addr_un, 0, sizeof(server_addr_un));
server_addr_un.sun_family = AF_UNIX;
strncpy(server_addr_un.sun_path, host.c_str(), sizeof(server_addr_un.sun_path) - 1);
}
else {
struct addrinfo* result = nullptr;

if (getaddrinfo(host.c_str(), nullptr, nullptr, &result) != 0) {
__TACOPIE_THROW(error, "getaddrinfo() failure");
}

struct sockaddr_in server_addr;
std::memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_addr = ((struct sockaddr_in*) (result->ai_addr))->sin_addr;
server_addr.sin_port = htons(port);
server_addr.sin_family = AF_INET;
std::memset(&server_addr_in, 0, sizeof(server_addr_in));
server_addr_in.sin_addr = ((struct sockaddr_in*) (result->ai_addr))->sin_addr;
server_addr_in.sin_port = htons(port);
server_addr_in.sin_family = AF_INET;

freeaddrinfo(result);
}

freeaddrinfo(result);
socklen_t addr_len = is_unix_socket ? sizeof(server_addr_un) : sizeof(server_addr_in);
const struct sockaddr* server_addr = is_unix_socket ? (const struct sockaddr*) &server_addr_un : (const struct sockaddr*) &server_addr_in;

if (::bind(m_fd, (const struct sockaddr*) &server_addr, sizeof(server_addr)) == -1) { __TACOPIE_THROW(error, "bind() failure"); }
if (::bind(m_fd, server_addr, addr_len) == -1) { __TACOPIE_THROW(error, "bind() failure"); }
}

void
Expand Down Expand Up @@ -204,7 +240,8 @@ tcp_socket::create_socket_if_necessary(void) {
if (m_fd != __TACOPIE_INVALID_FD) { return; }

//! new TCP socket
m_fd = socket(AF_INET, SOCK_STREAM, 0);
//! handle case of unix sockets by checking whether the port is 0 or not
m_fd = socket(m_port == 0 ? AF_UNIX : AF_INET, SOCK_STREAM, 0);
m_type = type::UNKNOWN;

if (m_fd == __TACOPIE_INVALID_FD) { __TACOPIE_THROW(error, "tcp_socket::create_socket_if_necessary: socket() failure"); }
Expand Down

0 comments on commit fd5d2d2

Please sign in to comment.