Back to Site
Loading...
Searching...
No Matches
socket.h File Reference
#include <string>
#include <functional>
#include <vector>
#include <cstdint>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
Include dependency graph for socket.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  librats::Peer
 UDP peer information. More...
 

Namespaces

namespace  librats
 

Macros

#define INVALID_SOCKET_VALUE   -1
 
#define SOCKET_ERROR_VALUE   -1
 
#define closesocket   close
 

Typedefs

typedef int socket_t
 

Functions

bool librats::init_socket_library ()
 Initialize the socket library.
 
void librats::cleanup_socket_library ()
 Cleanup the socket library.
 
socket_t librats::create_tcp_client (const std::string &host, int port, int timeout_ms=0)
 Create a TCP client socket and connect to a server using dual stack (IPv6 with IPv4 fallback)
 
socket_t librats::create_tcp_client_v4 (const std::string &host, int port, int timeout_ms=0)
 Create a TCP client socket and connect to a server using IPv4 only.
 
socket_t librats::create_tcp_client_v6 (const std::string &host, int port, int timeout_ms=0)
 Create a TCP client socket and connect to a server using IPv6 only.
 
socket_t librats::create_tcp_server (int port, int backlog=5, const std::string &bind_address="")
 Create a TCP server socket and bind to a port using dual stack (IPv6 with IPv4 support)
 
socket_t librats::create_tcp_server_v4 (int port, int backlog=5, const std::string &bind_address="")
 Create a TCP server socket and bind to a port using IPv4 only.
 
socket_t librats::create_tcp_server_v6 (int port, int backlog=5, const std::string &bind_address="")
 Create a TCP server socket and bind to a port using IPv6 only.
 
socket_t librats::accept_client (socket_t server_socket)
 Accept a client connection on a server socket.
 
std::string librats::get_peer_address (socket_t socket)
 Get the peer address (IP:port) from a connected socket.
 
int librats::send_tcp_data (socket_t socket, const std::vector< uint8_t > &data)
 Send data through a TCP socket.
 
std::vector< uint8_t > librats::receive_tcp_data (socket_t socket, size_t buffer_size=1024)
 Receive data from a TCP socket.
 
int librats::send_tcp_message_framed (socket_t socket, const std::vector< uint8_t > &message)
 Send a framed message with length prefix through a TCP socket.
 
std::vector< uint8_t > librats::receive_exact_bytes (socket_t socket, size_t num_bytes)
 Receive exact number of bytes from a TCP socket (blocking until complete)
 
std::vector< uint8_t > librats::receive_tcp_message_framed (socket_t socket)
 Receive a complete framed message from a TCP socket.
 
int librats::send_tcp_string (socket_t socket, const std::string &data)
 Send string data through a TCP socket (converts to binary)
 
std::string librats::receive_tcp_string (socket_t socket, size_t buffer_size=1024)
 Receive data from a TCP socket as string.
 
int librats::send_tcp_string_framed (socket_t socket, const std::string &message)
 Send a framed string message through a TCP socket.
 
std::string librats::receive_tcp_string_framed (socket_t socket)
 Receive a complete framed string message from a TCP socket.
 
socket_t librats::create_udp_socket (int port=0, const std::string &bind_address="")
 Create a UDP socket with dual stack support (IPv6 with IPv4 support)
 
socket_t librats::create_udp_socket_v4 (int port=0, const std::string &bind_address="")
 Create a UDP socket with IPv4 support only.
 
socket_t librats::create_udp_socket_v6 (int port=0, const std::string &bind_address="")
 Create a UDP socket with IPv6 support only.
 
int librats::send_udp_data (socket_t socket, const std::vector< uint8_t > &data, const Peer &peer)
 Send UDP data to a peer.
 
int librats::send_udp_data_to (socket_t socket, const std::vector< uint8_t > &data, const std::string &hostname, int port)
 Send UDP data to a hostname and port directly.
 
std::vector< uint8_t > librats::receive_udp_data (socket_t socket, size_t buffer_size, Peer &sender_peer)
 Receive UDP data from a peer.
 
std::vector< uint8_t > librats::receive_udp_data_with_timeout (socket_t socket, size_t buffer_size, int timeout_ms, std::string *sender_ip=nullptr, int *sender_port=nullptr)
 Receive UDP data with timeout support.
 
void librats::close_socket (socket_t socket, bool force=false)
 Close a socket.
 
bool librats::is_valid_socket (socket_t socket)
 Check if a socket is valid.
 
bool librats::set_socket_nonblocking (socket_t socket)
 Set socket to non-blocking mode.
 
bool librats::set_socket_blocking (socket_t socket)
 Set socket to blocking mode.
 
bool librats::is_tcp_socket (socket_t socket)
 Check if a socket is a TCP socket.
 
bool librats::connect_with_timeout (socket_t socket, struct sockaddr *addr, socklen_t addr_len, int timeout_ms)
 Connect to a socket address with timeout.
 
int librats::get_ephemeral_port (socket_t socket)
 Get the ephemeral port that a socket is bound to.
 

Macro Definition Documentation

◆ closesocket

#define closesocket   close

Definition at line 25 of file socket.h.

◆ INVALID_SOCKET_VALUE

#define INVALID_SOCKET_VALUE   -1

Definition at line 23 of file socket.h.

◆ SOCKET_ERROR_VALUE

#define SOCKET_ERROR_VALUE   -1

Definition at line 24 of file socket.h.

Typedef Documentation

◆ socket_t

typedef int socket_t

Definition at line 22 of file socket.h.