libsocket
Files | Functions
libunixsocket

C functions for work with BSD sockets (UNIX domain) More...

Files

file  libunixsocket.c
 

Functions

static signed int check_error (int return_value)
 Checks return value for error. More...
 
static int set_unix_socket_path (struct sockaddr_un *saddr, const char *path_or_name)
 
int create_unix_stream_socket (const char *path, int flags)
 Create and connect a new UNIX STREAM socket. More...
 
int create_unix_dgram_socket (const char *bind_path, int flags)
 Create a UNIX DGRAM socket. More...
 
int connect_unix_dgram_socket (int sfd, const char *path)
 Connect a datagram socket. More...
 
int destroy_unix_socket (int sfd)
 Close a socket. More...
 
int shutdown_unix_stream_socket (int sfd, int method)
 Shut a socket down. More...
 
int create_unix_server_socket (const char *path, int socktype, int flags)
 Create a passive UNIX socket. More...
 
int accept_unix_stream_socket (int sfd, int flags)
 Accept connections on a passive UNIX socket. More...
 
ssize_t recvfrom_unix_dgram_socket (int sfd, void *buf, size_t size, char *from, size_t from_size, int recvfrom_flags)
 Receive datagram from another UNIX socket. More...
 
ssize_t sendto_unix_dgram_socket (int sfd, const void *buf, size_t size, const char *path, int sendto_flags)
 Send datagram to socket. More...
 

Detailed Description

C functions for work with BSD sockets (UNIX domain)

This module contains all functions which are part of the C Unix socket library Note: To obtain informations about errors, use errno. You may find possible values in the syscall's man pages.

Function Documentation

◆ check_error()

static signed int check_error ( int  return_value)
inlinestatic

Checks return value for error.

Every value returned by a syscall is passed to this function. It returns 0 if the return value is ok or -1 if there was an error. If the macro VERBOSE is defined, an appropriate message is printed to STDERR.

Parameters
return_valueA return value from a syscall.
Return values
0The syscall was successful.
-1There was an error.

Definition at line 89 of file libunixsocket.c.

Here is the caller graph for this function:

◆ create_unix_stream_socket()

int create_unix_stream_socket ( const char *  path,
int  flags 
)

Create and connect a new UNIX STREAM socket.

Creates and connects a new STREAM socket with the socket given in path.

If the first byte in path is a null byte, this function assumes that it is an abstract socket address (see unix(7); this is a Linux-specific feature). Following that byte, there must be a normal 0-terminated string.

Return values
>0Success; return value is a socket file descriptor
<0Error.

Definition at line 143 of file libunixsocket.c.

Here is the call graph for this function:

◆ create_unix_dgram_socket()

int create_unix_dgram_socket ( const char *  bind_path,
int  flags 
)

Create a UNIX DGRAM socket.

Parameters
bind_pathIf not NULL, bind to bind_path.
flagsFlags to pass to socket(2) (varies from OS to OS; look in the man pages)
Return values
>0Success. Value is socket.
<0Error.

Definition at line 191 of file libunixsocket.c.

Here is the call graph for this function:

◆ connect_unix_dgram_socket()

int connect_unix_dgram_socket ( int  sfd,
const char *  path 
)

Connect a datagram socket.

Connects a datagram socket to the specified socket so the stream i/o operations may be used (read(2)/write(2))

Parameters
sfdThe socket
pathThe path to connect to
Return values
0Fine
<0Not fine

Definition at line 243 of file libunixsocket.c.

Here is the call graph for this function:

◆ destroy_unix_socket()

int destroy_unix_socket ( int  sfd)

Close a socket.

Actually, it's the same as close(2).

Parameters
sfdThe socket to close.
Return values
0Socket could be closed
<0Socket was already closed.

Definition at line 297 of file libunixsocket.c.

Here is the call graph for this function:

◆ shutdown_unix_stream_socket()

int shutdown_unix_stream_socket ( int  sfd,
int  method 
)

Shut a socket down.

Shut a socket down for reading or writing. If shut down for reading, the program can't read anymore. If shut down for writing no data can be sent anymore and the peer receives EOF.

Parameters
sfdThe socket
methodLIBSOCKET_READ, LIBSOCKET_WRITE or LIBSOCKET_READ|LIBSOCKET_WRITE
Return values
0Success
<0Error

Definition at line 319 of file libunixsocket.c.

Here is the call graph for this function:

◆ create_unix_server_socket()

int create_unix_server_socket ( const char *  path,
int  socktype,
int  flags 
)

Create a passive UNIX socket.

Creating a DGRAM server socket is the same as creating one using create_unix_dgram_socket() but with latter you may also not bind to anywhere.

Parameters
pathPath to bind the socket to
socktypeLIBSOCKET_STREAM or LIBSOCKET_DGRAM
flagsFlags for socket(2).
Return values
>0Success; returned value is a file descriptor for the socket
<0An error occurred.

Definition at line 353 of file libunixsocket.c.

Here is the call graph for this function:

◆ accept_unix_stream_socket()

int accept_unix_stream_socket ( int  sfd,
int  flags 
)

Accept connections on a passive UNIX socket.

Parameters
sfdThe server socket
flagsFlags for accept4(3) (therefore useless on any other system than Linux)
Return values
>0Return value is a socket connected to the client
<0Error at accept[4]()

Definition at line 419 of file libunixsocket.c.

Here is the call graph for this function:

◆ recvfrom_unix_dgram_socket()

ssize_t recvfrom_unix_dgram_socket ( int  sfd,
void *  buf,
size_t  size,
char *  from,
size_t  from_size,
int  recvfrom_flags 
)

Receive datagram from another UNIX socket.

Parameters
sfdThe socket descriptor
bufThe buffer to which the data is written
sizeits size
fromPlace where the path of the sending socket is placed to
from_sizeits size
recvfrom_flagsFlags passed to recvfrom(2)
Return values
nn bytes were received
<0Error at recvfrom(2)

Definition at line 444 of file libunixsocket.c.

Here is the call graph for this function:

◆ sendto_unix_dgram_socket()

ssize_t sendto_unix_dgram_socket ( int  sfd,
const void *  buf,
size_t  size,
const char *  path,
int  sendto_flags 
)

Send datagram to socket.

Parameters
sfdSocket
bufData to be sent
sizeThe length of the buffer buf
pathDestination socket
sendto_flagsFlags passed to sendto(2)
Return values
nn bytes were sent
<0Error at sendto(2).

Definition at line 479 of file libunixsocket.c.

Here is the call graph for this function: