CNDP  22.08.0
cnet_chnl.h File Reference
#include <stdio.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <netinet/in.h>
#include <bsd/bitstring.h>
#include <pktmbuf.h>
#include <cnet_const.h>
#include <cne_vec.h>
#include <cnet_protosw.h>
#include <cne_graph_worker.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/types.h>
#include "cne_log.h"
#include "cnet_tcp.h"
#include "cnet_udp.h"

Go to the source code of this file.

Macros

#define CHNL_ENABLE_UDP_CHECKSUM   (1 << 0)
 

Typedefs

typedef int(* chnl_cb_t) (int chnl_type, int cd)
 

Enumerations

enum  { SHUT_BIT_RD = 0x01 , SHUT_BIT_WR = 0x02 , SHUT_BIT_RDWR = 0x03 }
 
enum  chnl_type_t {
  CHNL_UDP_RECV_TYPE , CHNL_UDP_CLOSE_TYPE , CHNL_TCP_ACCEPT_TYPE , CHNL_TCP_RECV_TYPE ,
  CHNL_TCP_CLOSE_TYPE , CHNL_CALLBACK_TYPES
}
 

Functions

CNDP_API void chnl_dump (const char *msg, struct chnl *ch)
 Dump out a channel structure. More...
 
CNDP_API int chnl_connect_common (struct chnl *ch, struct in_caddr *to, int32_t tolen)
 Common channel connect routine used by protocols. More...
 
CNDP_API int chnl_bind_common (struct chnl *ch, struct in_caddr *pAddr, int32_t len, struct pcb_hd *pHd)
 Channel Bind common routine used by protocols. More...
 
CNDP_API int channel (int domain, int type, int proto, chnl_cb_t cb)
 The routine to create a channel structure similar to 'socket()'. More...
 
CNDP_API int chnl_bind (int cd, struct sockaddr *addr, int addrlen)
 Bind an address to a channel similar to 'bind()'. More...
 
CNDP_API int chnl_listen (int cd, int backlog)
 Listen on a channel similar to 'listen()'. More...
 
CNDP_API int chnl_accept (int cd, struct sockaddr *sa, socklen_t *addrlen)
 Accept on a channel similar to 'accept()'. More...
 
CNDP_API int chnl_connect (int cd, struct sockaddr *sa, int addrlen)
 Connect to a channel, similar to 'connect()'. More...
 
CNDP_API int chnl_recv (int cd, pktmbuf_t **mbufs, size_t len)
 
CNDP_API int chnl_send (int cd, pktmbuf_t **mbufs, uint16_t nb_mbufs)
 
CNDP_API int chnl_sendto (int cd, struct sockaddr *sa, pktmbuf_t **mbufs, uint16_t nb_mbufs)
 Send data to a channel similar to 'sendto()'. More...
 
CNDP_API int chnl_getchnlname (int cd, struct sockaddr *name, socklen_t *namelen)
 
CNDP_API int chnl_getpeername (int cd, struct sockaddr *sa, socklen_t *salen)
 
CNDP_API int chnl_close (int cd)
 
CNDP_API int chnl_shutdown (int cd, int how)
 Shutdown a channel connection similar to 'shutdown()'. More...
 
CNDP_API int chnl_open (const char *str, int flags, chnl_cb_t fn)
 

Detailed Description

CNET Channel routines and constants.

Definition in file cnet_chnl.h.

Macro Definition Documentation

◆ CHNL_ENABLE_UDP_CHECKSUM

#define CHNL_ENABLE_UDP_CHECKSUM   (1 << 0)

Enable UDP checksum

Examples
examples/cnet-graph/cnet-graph.c.

Definition at line 53 of file cnet_chnl.h.

Typedef Documentation

◆ chnl_cb_t

typedef int(* chnl_cb_t) (int chnl_type, int cd)

Callback function for a channel to accept or receive packets

Parameters
chnl_typeThe type of channel callback defined by chnl_type_t enum.
cdThe channel descriptor value
Returns
0 on success or -1 on error.

Definition at line 50 of file cnet_chnl.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
SHUT_BIT_RD 

Shutdown Read side

SHUT_BIT_WR 

Shutdown Write side

SHUT_BIT_RDWR 

Shutdown Read and write side

Definition at line 59 of file cnet_chnl.h.

◆ chnl_type_t

Channel callback types

Enumerator
CHNL_UDP_RECV_TYPE 

Callback for receiving UDP packets

CHNL_UDP_CLOSE_TYPE 

Callback for UDP close

CHNL_TCP_ACCEPT_TYPE 

Callback type for accepting TCP connection

CHNL_TCP_RECV_TYPE 

Callback for receiving TCP packets

CHNL_TCP_CLOSE_TYPE 

Callback for TCP close

CHNL_CALLBACK_TYPES 

Maximum number of callback types

Definition at line 66 of file cnet_chnl.h.

Function Documentation

◆ chnl_dump()

CNDP_API void chnl_dump ( const char *  msg,
struct chnl *  ch 
)

Dump out a channel structure.

Parameters
msgA message to print if present, can be NULL
chThe chnl structure to dump out.

◆ chnl_connect_common()

CNDP_API int chnl_connect_common ( struct chnl *  ch,
struct in_caddr *  to,
int32_t  tolen 
)

Common channel connect routine used by protocols.

Parameters
chThe channel structure pointer
toThe IP address of the 'to' or foreign address.
tolenThe length of the 'to' address.
Returns
-1 on error or 0 on success

◆ chnl_bind_common()

CNDP_API int chnl_bind_common ( struct chnl *  ch,
struct in_caddr *  pAddr,
int32_t  len,
struct pcb_hd *  pHd 
)

Channel Bind common routine used by protocols.

Parameters
chThe channel structure pointer
pAddrThe IP address to bind
lenThe length of the IP address to bind
pHdThe head of the PCB list to add the channel
Returns
-1 on error or 0 on success

◆ channel()

CNDP_API int channel ( int  domain,
int  type,
int  proto,
chnl_cb_t  cb 
)

The routine to create a channel structure similar to 'socket()'.

Parameters
domainThe domain ID value
typeThe protocol type value
protoThe proto value
cbThe callback routine to call when data is received.
Returns
-1 on error or >= 0 on success
Examples
examples/cnet-quic/quic-cli.c.

◆ chnl_bind()

CNDP_API int chnl_bind ( int  cd,
struct sockaddr *  addr,
int  addrlen 
)

Bind an address to a channel similar to 'bind()'.

Parameters
cdThe channel descriptor index
addrThe IP address to bind to the channel.
addrlenThe IP address length in bytes
Returns
-1 on error or 0 on success
Examples
examples/cnet-quic/quic-cli.c.

◆ chnl_listen()

CNDP_API int chnl_listen ( int  cd,
int  backlog 
)

Listen on a channel similar to 'listen()'.

Parameters
cdThe channel descriptor index
backlogThe number of backlog connections allowed (not fully used)
Returns
-1 on error or 0 on success

◆ chnl_accept()

CNDP_API int chnl_accept ( int  cd,
struct sockaddr *  sa,
socklen_t *  addrlen 
)

Accept on a channel similar to 'accept()'.

Parameters
cdThe channel descriptor index
saThe sockaddr to fill in when accepting a connection.
addrlenThe length of the new accepted connection
Returns
-1 on error or >= 0 on success
Examples
examples/cnet-graph/cnet-graph.c.

◆ chnl_connect()

CNDP_API int chnl_connect ( int  cd,
struct sockaddr *  sa,
int  addrlen 
)

Connect to a channel, similar to 'connect()'.

Parameters
cdThe channel descriptor index
saThe sockaddr to fill in when accepting a connection.
addrlenThe length of the new accepted connection
Returns
-1 on error or 0 on success

◆ chnl_recv()

CNDP_API int chnl_recv ( int  cd,
pktmbuf_t **  mbufs,
size_t  len 
)

This routine receives data from a chnl. It is normally used with connected chnls, because it does not return the source address of the received data.

Parameters
cdThe channel descriptor index
mbufsVector list of pktmbuf_t pointers.
lenNumber of entries in the mbufs array.
Returns
Number of mbufs in the list or -1 on error.
Examples
examples/cnet-graph/cnet-graph.c, and examples/cnet-quic/quic-cli.c.

◆ chnl_send()

CNDP_API int chnl_send ( int  cd,
pktmbuf_t **  mbufs,
uint16_t  nb_mbufs 
)

This routine transmits data to a previously connected chnl.

Parameters
cdThe channel descriptor index
mbufsList of pointer vectors
nb_mbufsNumber of mbufs in the vector list.
Returns
0 on success or -1 on failure.
Note
On failure, errno is set to one of the following. EACCES An attempt was made to send to a broadcast address without the SO_BROADCAST option set. EBADF ch is not a valid chnl descriptor. EDESTADDRREQ The datagram chnl is not connected, and the destination address is not supplied as an argument. EFAULT buf or len is invalid. ENOBUFS Insufficient resources were available to complete the operation. ENOTCONN The stream chnl is not connected. EOPNOTSUPP Operation is not supported on this chnl type. EPIPE The chnl is shut down for writing, or the stream chnl is no longer connected. EWOULDBLOCK The chnl is marked non-blocking, and the operation cannot be completed without blocking.
Examples
examples/cnet-graph/cnet-graph.c.

◆ chnl_sendto()

CNDP_API int chnl_sendto ( int  cd,
struct sockaddr *  sa,
pktmbuf_t **  mbufs,
uint16_t  nb_mbufs 
)

Send data to a channel similar to 'sendto()'.

Parameters
cdThe channel descriptor index
saThe sockaddr to fill in when accepting a connection.
mbufsThe mbuf array to send multiple data buffers
nb_mbufsNumber of mbufs in the mbufs array
Returns
-1 on error or 0 on success
Examples
examples/cnet-quic/quic-cli.c.

◆ chnl_getchnlname()

CNDP_API int chnl_getchnlname ( int  cd,
struct sockaddr *  name,
socklen_t *  namelen 
)

This routine gets the current name for the specified chnl.

Parameters
cdThe channel descriptor index
nameBuffer to receive the chnl name.
namelenLength of name. This is a value/result parameter. On entry, it must be initialized to the size of the buffer pointed to by name. On return, it contains the size of the chnl name.
Note
If namelen is less than the actual length of the address, the value stored at name will be silently truncated.
Returns
-1 on error or 0 on success

◆ chnl_getpeername()

CNDP_API int chnl_getpeername ( int  cd,
struct sockaddr *  sa,
socklen_t *  salen 
)

This routine gets the name of the peer connected to the specified chnl.

Parameters
cdThe channel descriptor index
saBuffer to receive the chnl name.
salenLength of name. This is a value/result parameter. On entry, it must be initialized to the size of the buffer pointed to by name. On return, it contains the size of the chnl name.
Note
If namelen is less than the actual length of the address, the value stored at name will be silently truncated.
Returns
-1 on error or 0 on success

◆ chnl_close()

CNDP_API int chnl_close ( int  cd)

Close a connection to the specified chnl descriptor.

Parameters
cdThe chnl descriptor index
Returns
0 on success or -1 on error
Examples
examples/cnet-graph/cnet-graph.c.

◆ chnl_shutdown()

CNDP_API int chnl_shutdown ( int  cd,
int  how 
)

Shutdown a channel connection similar to 'shutdown()'.

Parameters
cdThe channel descriptor index
howHow to shutdown the connection SHUT_RD, SHUT_WR or SHUT_RDWR.
Returns
-1 on error or 0 on success
Examples
examples/cnet-quic/quic-cli.c.

◆ chnl_open()

CNDP_API int chnl_open ( const char *  str,
int  flags,
chnl_cb_t  fn 
)

Open a channel with a string configuration.

The channel string is similar to socat style strings, but not all string types are support. Some string types supported are: udp4-listen:0.0.0.0:5678 - Open a listening UDP4 channel on port 5678 or udp4-listen:5678 - Open a listening UDP4 channel on port 5678. tcp4-listen:0.0.0.0:4433 - Open a listening TCP4 channel on port 4433 or tcp4-listen:4433 - Open a listening TCP4 channel on port 4433. tcp4-connect:198.18.2.1:2222 - Open TCP4 connection to port 2222 and IP address

Port value can be in hex '0x1234' or decimal format.

Parameters
strThe string to parse to open or create a channel.
flagsSome flags to control the creation of a channel, see CHNL_ENABLE_UDP_CHECKSUM and other flags to be defined.
fnFunction pointer for channel callback.
Returns
-1 on error or channel descriptor value is returned.
Examples
examples/cnet-graph/cnet-graph.c.