EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
Macros | Functions
sockctl.c File Reference

File containing the implementation of the socket control utils. More...

#include <stdio.h>
#include <sys/socket.h>
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <sys/ioctl.h>
#include <sys/un.h>
#include "sockctl.h"
#include "allocs.h"
#include "log.h"
#include "net.h"
#include "os.h"
Include dependency graph for sockctl.c:

Macros

#define SOCK_EXTENSION   ".sock"
 
#define TMP_UNIX_SOCK_FOLDER_PREFIX   "/tmp/edgesec/tmp-unix-socks."
 
#define TMP_UNIX_SOCK_FOLDER_TEMPLATE   TMP_UNIX_SOCK_FOLDER_PREFIX "XXXXXX"
 
#define TMP_UNIX_SOCK_NAME   "client-socket" SOCK_EXTENSION
 
#define DOMAIN_REPLY_TIMEOUT   10
 

Functions

void init_domain_addr (struct sockaddr_un *unaddr, const char *addr)
 
int create_domain_client (const char *path)
 Create a unix domain client socket. More...
 
int create_domain_server (const char *server_path)
 Create a domain server object. More...
 
int close_domain_socket (int unix_domain_socket_fd)
 Closes and cleans up a unix domain socket. More...
 
int create_udp_server (unsigned int port)
 Create a udp server object. More...
 
ssize_t read_socket_domain (int sock, char *data, size_t data_len, struct client_address *addr, int flags)
 
ssize_t read_socket_udp (int sock, char *data, size_t data_len, struct client_address *addr, int flags)
 
ssize_t read_socket_data (int sock, char *data, size_t data_len, struct client_address *addr, int flags)
 Read data from the server socket. More...
 
ssize_t read_domain_data_s (int sock, char *data, size_t data_len, char *addr, int flags)
 Read data from the domain server socket with a string address. More...
 
ssize_t write_domain_data_s (int sock, const char *data, size_t data_len, const char *addr)
 Write data to the domain server socket with a string address. More...
 
ssize_t write_socket_domain (int sock, const char *data, size_t data_len, const struct client_address *addr)
 
ssize_t write_socket_udp (int sock, const char *data, size_t data_len, const struct client_address *addr)
 
ssize_t write_socket_data (int sock, const char *data, size_t data_len, const struct client_address *addr)
 Write data to the server socket. More...
 
int writeread_domain_data_str (char *socket_path, const char *write_str, char **reply)
 Write and read a domain data string. More...
 

Detailed Description

File containing the implementation of the socket control utils.

Author
Alexandru Mereacre
Date
2020

Macro Definition Documentation

◆ DOMAIN_REPLY_TIMEOUT

#define DOMAIN_REPLY_TIMEOUT   10

◆ SOCK_EXTENSION

#define SOCK_EXTENSION   ".sock"

◆ TMP_UNIX_SOCK_FOLDER_PREFIX

#define TMP_UNIX_SOCK_FOLDER_PREFIX   "/tmp/edgesec/tmp-unix-socks."

◆ TMP_UNIX_SOCK_FOLDER_TEMPLATE

#define TMP_UNIX_SOCK_FOLDER_TEMPLATE   TMP_UNIX_SOCK_FOLDER_PREFIX "XXXXXX"

Template for mkdtemp() to create tmp folders for temporary unix domain sockets

◆ TMP_UNIX_SOCK_NAME

#define TMP_UNIX_SOCK_NAME   "client-socket" SOCK_EXTENSION

Basename for temporary unix domain sockets

Function Documentation

◆ close_domain_socket()

int close_domain_socket ( int  unix_domain_socket_fd)

Closes and cleans up a unix domain socket.

Closes the given unix domain socket. If the given unix domain socket is a pathname socket, this function also calls unlink() on the pathname.

Parameters
unix_domain_socket_fdThe file descriptor of the unix domain socket to close.
Return values
0on success.
-1on error (see errno for error details).

◆ create_domain_client()

int create_domain_client ( const char *  path)

Create a unix domain client socket.

Parameters
pathThe UNIX domain socket path. If this is NULL:
  • On Linux, a randomly generated abstract Unix domain socket will be used instead.
  • On other Unix platforms, a randomly generated pathname Unix domain socket will be used. Please call close_domain_socket() to unlink() the pathname (and tmp folder) when finished.
Returns
File-descriptor for the client socket.
Return values
-1On error.

◆ create_domain_server()

int create_domain_server ( const char *  server_path)

Create a domain server object.

Parameters
server_pathServer UNIX domain socket path
Returns
int Domain server socket

◆ create_udp_server()

int create_udp_server ( unsigned int  port)

Create a udp server object.

Parameters
portServer port in host endian.
Returns
int UDP server socket

◆ init_domain_addr()

void init_domain_addr ( struct sockaddr_un *  unaddr,
const char *  addr 
)

◆ read_domain_data_s()

ssize_t read_domain_data_s ( int  sock,
char *  data,
size_t  data_len,
char *  addr,
int  flags 
)

Read data from the domain server socket with a string address.

Parameters
sockDomain Server socket
[out]dataData buffer to store read data.
data_lenData buffer length
[out]addrBuffer to store sender address. Must be at least the same size as sun_path (usually 104 bytes on BSD, 108 on Linux).
flagsThe flags for recvfrom function
Returns
ssize_t Size of read data

◆ read_socket_data()

ssize_t read_socket_data ( int  sock,
char *  data,
size_t  data_len,
struct client_address addr,
int  flags 
)

Read data from the server socket.

Parameters
sockServer socket
[out]dataData buffer to store read data.
data_lenData buffer length
[out]addrThe sender address structure
flagsThe flags for recvfrom function
Returns
ssize_t Size of read data

◆ read_socket_domain()

ssize_t read_socket_domain ( int  sock,
char *  data,
size_t  data_len,
struct client_address addr,
int  flags 
)

◆ read_socket_udp()

ssize_t read_socket_udp ( int  sock,
char *  data,
size_t  data_len,
struct client_address addr,
int  flags 
)

◆ write_domain_data_s()

ssize_t write_domain_data_s ( int  sock,
const char *  data,
size_t  data_len,
const char *  addr 
)

Write data to the domain server socket with a string address.

Parameters
sockDomain server socket
dataData buffer to send.
data_lenData buffer length
addrClient address (string). This string will be truncated if it does not fit in sun_path.
Returns
ssize_t Size of written data

◆ write_socket_data()

ssize_t write_socket_data ( int  sock,
const char *  data,
size_t  data_len,
const struct client_address addr 
)

Write data to the server socket.

Parameters
sockServer socket
dataData buffer to send.
data_lenData buffer length
addrThe recipient address structure
Returns
ssize_t Size of written data

◆ write_socket_domain()

ssize_t write_socket_domain ( int  sock,
const char *  data,
size_t  data_len,
const struct client_address addr 
)

◆ write_socket_udp()

ssize_t write_socket_udp ( int  sock,
const char *  data,
size_t  data_len,
const struct client_address addr 
)

◆ writeread_domain_data_str()

int writeread_domain_data_str ( char *  socket_path,
const char *  write_str,
char **  reply 
)

Write and read a domain data string.

Parameters
[in,out]socket_pathThe path to the domain server socket. This path will be overwritten with the socket path of the reply, so it must be at least the same size as sun_path (usually 104 bytes on BSD, 108 on Linux).
[in]write_strThe data to write to the socket.
[out]replyThe pointer to the reply string. You must free() this reply string when done with it.
Returns
int 0 on success, -1 on failure