C functions for work with BSD sockets (UNIX domain)
More...
|
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...
|
|
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.
◆ 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_value | A return value from a syscall. |
- Return values
-
0 | The syscall was successful. |
-1 | There was an error. |
Definition at line 89 of file libunixsocket.c.
◆ 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
-
>0 | Success; return value is a socket file descriptor |
<0 | Error. |
Definition at line 143 of file libunixsocket.c.
◆ create_unix_dgram_socket()
int create_unix_dgram_socket |
( |
const char * |
bind_path, |
|
|
int |
flags |
|
) |
| |
Create a UNIX DGRAM socket.
- Parameters
-
bind_path | If not NULL , bind to bind_path . |
flags | Flags to pass to socket(2) (varies from OS to OS; look in the man pages) |
- Return values
-
>0 | Success. Value is socket. |
<0 | Error. |
Definition at line 191 of file libunixsocket.c.
◆ 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
-
sfd | The socket |
path | The path to connect to |
- Return values
-
Definition at line 243 of file libunixsocket.c.
◆ destroy_unix_socket()
int destroy_unix_socket |
( |
int |
sfd | ) |
|
Close a socket.
Actually, it's the same as close(2)
.
- Parameters
-
- Return values
-
0 | Socket could be closed |
<0 | Socket was already closed. |
Definition at line 297 of file libunixsocket.c.
◆ 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
-
sfd | The socket |
method | LIBSOCKET_READ , LIBSOCKET_WRITE or LIBSOCKET_READ|LIBSOCKET_WRITE |
- Return values
-
Definition at line 319 of file libunixsocket.c.
◆ 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
-
path | Path to bind the socket to |
socktype | LIBSOCKET_STREAM or LIBSOCKET_DGRAM |
flags | Flags for socket(2) . |
- Return values
-
>0 | Success; returned value is a file descriptor for the socket |
<0 | An error occurred. |
Definition at line 353 of file libunixsocket.c.
◆ accept_unix_stream_socket()
int accept_unix_stream_socket |
( |
int |
sfd, |
|
|
int |
flags |
|
) |
| |
Accept connections on a passive UNIX socket.
- Parameters
-
sfd | The server socket |
flags | Flags for accept4(3) (therefore useless on any other system than Linux) |
- Return values
-
>0 | Return value is a socket connected to the client |
<0 | Error at accept[4]() |
Definition at line 419 of file libunixsocket.c.
◆ 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
-
sfd | The socket descriptor |
buf | The buffer to which the data is written |
size | its size |
from | Place where the path of the sending socket is placed to |
from_size | its size |
recvfrom_flags | Flags passed to recvfrom(2) |
- Return values
-
n | n bytes were received |
<0 | Error at recvfrom(2) |
Definition at line 444 of file libunixsocket.c.
◆ 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
-
sfd | Socket |
buf | Data to be sent |
size | The length of the buffer buf |
path | Destination socket |
sendto_flags | Flags passed to sendto(2) |
- Return values
-
n | n bytes were sent |
<0 | Error at sendto(2) . |
Definition at line 479 of file libunixsocket.c.