CNDP  22.08.0
csock.h File Reference
#include <stdint.h>
#include <stddef.h>
#include <sys/types.h>

Go to the source code of this file.

Data Structures

struct  csock_cfg
 

Macros

#define __csock_unused   __attribute__((__unused__))
 

Typedefs

typedef void csock_t
 
typedef void *() csock_client_fn_t(csock_t *c)
 
typedef ssize_t() csock_write_t(csock_t *c, char *data, size_t len)
 
typedef ssize_t() csock_read_t(csock_t *c, char *data, size_t len)
 
typedef int() csock_close_t(csock_t *c)
 
typedef struct csock_cfg csock_cfg_t
 

Enumerations

enum  {
  CSOCK_MAX_HOST_NAME_LENGTH = 128 , CSOCK_MAX_SOCK_INFO_LENGTH = 1024 , DEFAULT_CSOCK_RX_LEN = 4096 , DEFAULT_CSOCK_TX_LEN = 4096 ,
  CSOCK_IS_SERVER = (1 << 0) , CSOCK_IS_CLIENT = (1 << 1) , CSOCK_NON_BLOCKING = (1 << 2) , CSOCK_GROUP_WRITE = (1 << 3) ,
  CSOCK_EOF = (1 << 4) , CSOCK_STDIO_TYPE = (1 << 5)
}
 

Functions

CSOCK_API csock_tcsock_create (csock_cfg_t *cfg)
 
CSOCK_API void csock_destroy (csock_t *c)
 
CSOCK_API int csock_server_start (csock_t *c)
 
CSOCK_API csock_tcsock_accept (csock_t *s)
 
CSOCK_API ssize_t csock_read (csock_t *c, char *data, size_t len)
 
CSOCK_API ssize_t csock_write (csock_t *c, char *data, size_t len)
 
CSOCK_API int csock_close (csock_t *c)
 
CSOCK_API struct sockaddr * csock_get_peer (csock_t *c)
 
CSOCK_API int csock_eof (csock_t *_c)
 
CSOCK_API int csock_get_fd (csock_t *c)
 
CSOCK_API int csock_set_fd (csock_t *_c, int fd)
 
CSOCK_API int csock_is_closed (csock_t *c)
 

Detailed Description

CNDP Socket-related utility functions

Routines to support a stdio connection with a TTY or socket connection.

Note: This library is shared between CNDP and non-CNDP applications and can not contain CNDP routines or macros.

Definition in file csock.h.

Macro Definition Documentation

◆ __csock_unused

#define __csock_unused   __attribute__((__unused__))

short definition to mark a function parameter unused

Definition at line 32 of file csock.h.

Typedef Documentation

◆ csock_t

typedef void csock_t

Forward declarations

Definition at line 48 of file csock.h.

◆ csock_client_fn_t

typedef void*() csock_client_fn_t(csock_t *c)

The client function to call for a connection, which is a standard pthread function.

Parameters
cThe csock_t pointer, which is a opaque void pointer
Returns
NULL on success or a value for pthread_exit() call failure

Definition at line 58 of file csock.h.

◆ csock_write_t

typedef ssize_t() csock_write_t(csock_t *c, char *data, size_t len)

Template for the write function

Definition at line 63 of file csock.h.

◆ csock_read_t

typedef ssize_t() csock_read_t(csock_t *c, char *data, size_t len)

Template for the read function

Definition at line 68 of file csock.h.

◆ csock_close_t

typedef int() csock_close_t(csock_t *c)

Template for the close function

Definition at line 73 of file csock.h.

◆ csock_cfg_t

typedef struct csock_cfg csock_cfg_t

Configuration structure

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
CSOCK_MAX_HOST_NAME_LENGTH 

Maximum host name length

CSOCK_MAX_SOCK_INFO_LENGTH 

Maximum host address length, could be a path

DEFAULT_CSOCK_RX_LEN 

Default size of the RX buffer

DEFAULT_CSOCK_TX_LEN 

Default size of the TX buffer

CSOCK_IS_SERVER 

The connection is a server socket

CSOCK_IS_CLIENT 

The connection is a client socket

CSOCK_NON_BLOCKING 

The connection is non blocking

CSOCK_GROUP_WRITE 

The connection is a group write

CSOCK_EOF 

The connection has gotten a EOF

CSOCK_STDIO_TYPE 

Use stdin/stdout and not a socket

Definition at line 34 of file csock.h.

Function Documentation

◆ csock_create()

CSOCK_API csock_t* csock_create ( csock_cfg_t cfg)

Create a cloud socket data structure

Parameters
cfgClient configuration information.
Returns
The pointer to the csock_t structure or NULL on error.
Examples
examples/cli/main.c.

◆ csock_destroy()

CSOCK_API void csock_destroy ( csock_t c)

Destroy the csock structure and release resources.

Parameters
cThe csock_t pointer, which is a opaque void pointer
Returns
N/A
Examples
examples/cli/main.c.

◆ csock_server_start()

CSOCK_API int csock_server_start ( csock_t c)

Start the cloud server connection

Parameters
cThe csock_t pointer, which is a opaque pointer
Returns
0 on success or -1 on error
Examples
examples/cli/main.c.

◆ csock_accept()

CSOCK_API csock_t* csock_accept ( csock_t s)

Accept a connection from the remote client.

Parameters
sThe csock_t pointer, which is the server side data
Returns
NULL on error or a new csock_t structure pointer.

◆ csock_read()

CSOCK_API ssize_t csock_read ( csock_t c,
char *  data,
size_t  len 
)

Read data from a socket or TTY devices

Parameters
cThe csock_t pointer, which is a opaque pointer
dataThe data buffer to place the received data.
lenSize of the data buffer.
Returns
-1 on error or the number of bytes in the data buffer. The data buffer is not null terminated.

◆ csock_write()

CSOCK_API ssize_t csock_write ( csock_t c,
char *  data,
size_t  len 
)

Write data to a socket or TTY devices

Parameters
cThe csock_t pointer, which is a opaque pointer
dataThe data buffer to get the data to send.
lenSize of the data in the buffer.
Returns
-1 on error or the number of bytes written.

◆ csock_close()

CSOCK_API int csock_close ( csock_t c)

Close a socket or tty connection

Parameters
cThe csock_t pointer, which is a opaque pointer
Returns
0 on success or -1 on error
Examples
examples/cli/main.c.

◆ csock_get_peer()

CSOCK_API struct sockaddr* csock_get_peer ( csock_t c)

Get the peer connection address

Parameters
cThe csock_t pointer, which is a opaque pointer
Returns
NULL on error or pointer to sockaddr structure

◆ csock_eof()

CSOCK_API int csock_eof ( csock_t _c)

Return non-zero if connection had a EOF condition

Parameters
_cThe csock_t pointer, which is a opaque pointer
Returns
non-zero if EOF

◆ csock_get_fd()

CSOCK_API int csock_get_fd ( csock_t c)

Return the current FD value for the connection.

Parameters
cThe csock_t pointer, which is a opaque pointer
Returns
-1 on error or FD value
Examples
examples/cli/main.c.

◆ csock_set_fd()

CSOCK_API int csock_set_fd ( csock_t _c,
int  fd 
)

Set the connection FD value

Parameters
_cThe csock_t pointer, which is a opaque pointer
fdThe FD value for a connection
Returns
0 on success or -1 on error

◆ csock_is_closed()

CSOCK_API int csock_is_closed ( csock_t c)

Return non-zero if the connection is closed

Parameters
cThe csock_t structure pointer
Returns
1 if the connection is closed or 0 if not closed