SWILIB API (X75)    
Loading...
Searching...
No Matches
Socket

Detailed Description

Functions for working with BSD-like sockets.

Usage: #include <swilib.h> or #include <swilib/socket.h>

Data Structures

struct  in_addr
 Internet address (a structure for historical reasons) More...
 
struct  sockaddr
 Socket address, generic. More...
 
struct  sockaddr_in
 Socket address, internet style. More...
 
struct  hostent
 DNS response. More...
 

Macros

#define INADDR_ANY   (uint32_t) 0x00000000
 Definitions of bits in internet address integers.
 
#define INADDR_BROADCAST   (uint32_t) 0xffffffff
 
#define INET_ADDRSTRLEN   16
 Maximum length of the IPv4 address.
 
#define MAX_IPv4_STR_LEN   16
 Maximum length of the IPv4 address.
 
#define MAX_IPv6_STR_LEN   64
 Maximum length of the IPv6 address.
 
#define IP_ADDR(A, B, C, D)   (((unsigned long)A << 24) | ((unsigned long)B << 16) | ((unsigned long) C << 8) | ((unsigned long) D << 0))
 Helper for creating IP address.
 

Typedefs

typedef uint16_t in_port_t
 Port value.
 
typedef uint16_t sa_family_t
 Address family.
 
typedef uint32_t in_addr_t
 Internet address.
 
typedef uint32_t socklen_t
 Socket address length type definition.
 
typedef struct sockaddr SOCK_ADDR
 ELF's backward compatibility.
 
typedef struct sockaddr_in SOCKADDR_IN
 Windows-style.
 
typedef struct sockaddr SOCKADDR
 Windows-style.
 
typedef struct hostent HOSTENT
 Windows-style.
 

Enumerations

enum  SockProtocolID {
  IPPROTO_IP = 0 ,
  IPPROTO_TCP = 6 ,
  IPPROTO_UDP = 17 ,
  IPPROTO_MAX = 18
}
 Internet protocols. More...
 
enum  SockTypeID {
  SOCK_DGRAM = 0 ,
  SOCK_STREAM = 1
}
 Socket types. More...
 
enum  SockFamilyID {
  AF_UNSPEC = 0 ,
  AF_INET = 1 ,
  AF_MAX = 2
}
 Socket family types. More...
 
enum  SockShutdownTypeID {
  SHUT_RD = 0 ,
  SHUT_WR = 1 ,
  SHUT_RDWR = 2
}
 Socket shutdown types. More...
 

Functions

int async_gethostbyname (const char *name, void *response, int *req_id)
 Resolve domain name.
 
uint32_t str2ip (const char *ip)
 Convert the string IP address to the numeric format.
 
in_addr_t inet_addr (const char *ip)
 Convert the string IP address to the numeric format.
 
uint16_t htons (uint16_t p)
 Convert host to network order (unsigned short).
 
uint32_t htonl (uint32_t p)
 Convert host to network order (unsigned int).
 
static uint16_t ntohs (uint16_t p)
 Convert network to host order.
 
static uint32_t ntohl (uint32_t p)
 
int socket (int domain, int type, int protocol)
 
int connect (int socket, const struct sockaddr *address, socklen_t address_len)
 Connect socket to the address.
 
int bind (int socket, const struct sockaddr *address, socklen_t address_len)
 Bind socket to the address.
 
int shutdown (int socket, int how)
 Shutdown.
 
int recv (int socket, void *buf, int len, int flags)
 Receive data on a socket.
 
int send (int socket, const void *buf, int len, int flags)
 Send a message on a socket.
 
int available0 (int socket)
 Get available bytes for reading in the socket.
 
int * socklasterr (void)
 Pointer to the last socket error.
 
int closesocket (int socket)
 Close socket descriptor.
 
struct hostent * gethostbyname (const char *name)
 Resolve a host name synchronously.
 
int getsockname (int sock, SOCK_ADDR *addr, int *len)
 Read the local address of a socket.
 
int getpeername (int sock, SOCK_ADDR *addr, int *len)
 Read the remote address of a connected socket.
 
int setsockopt (int sock, int level, int option, const void *value, int len)
 Set a socket option.
 
int getsockopt (int sock, int level, int option, void *value, int *len)
 Read a socket option.
 
int ioctlsocket (int sock, int cmd, void *arg)
 Apply a socket control command; read socklasterr for errors.
 
int listen (int sock, int backlog)
 Start listening for incoming TCP connections.
 
int ENIP_GetNativeSocket (int sock, ENIP_SOCKET **native)
 Look up the native ENIP object for a socket descriptor.
 

Data Structure Documentation

◆ in_addr

struct in_addr

◆ sockaddr

struct sockaddr

◆ sockaddr_in

struct sockaddr_in

◆ hostent

struct hostent

Macro Definition Documentation

◆ INADDR_ANY

#define INADDR_ANY   (uint32_t) 0x00000000

Definitions of bits in internet address integers.

On subnets, the decomposition of addresses to host and net parts is done according to subnet mask, not the masks here.

Definition at line 34 of file socket.h.

◆ INADDR_BROADCAST

#define INADDR_BROADCAST   (uint32_t) 0xffffffff

Definition at line 35 of file socket.h.

◆ INET_ADDRSTRLEN

#define INET_ADDRSTRLEN   16

Maximum length of the IPv4 address.

Definition at line 37 of file socket.h.

◆ MAX_IPv4_STR_LEN

#define MAX_IPv4_STR_LEN   16

Maximum length of the IPv4 address.

Definition at line 38 of file socket.h.

◆ MAX_IPv6_STR_LEN

#define MAX_IPv6_STR_LEN   64

Maximum length of the IPv6 address.

Definition at line 39 of file socket.h.

◆ IP_ADDR

#define IP_ADDR ( A,
B,
C,
D )   (((unsigned long)A << 24) | ((unsigned long)B << 16) | ((unsigned long) C << 8) | ((unsigned long) D << 0))

Helper for creating IP address.

Definition at line 44 of file socket.h.

Typedef Documentation

◆ in_port_t

typedef uint16_t in_port_t

Port value.

Definition at line 18 of file socket.h.

◆ sa_family_t

typedef uint16_t sa_family_t

Address family.

Definition at line 19 of file socket.h.

◆ in_addr_t

typedef uint32_t in_addr_t

Internet address.

Definition at line 20 of file socket.h.

◆ socklen_t

typedef uint32_t socklen_t

Socket address length type definition.

Definition at line 21 of file socket.h.

◆ SOCK_ADDR

typedef struct sockaddr SOCK_ADDR

ELF's backward compatibility.

Definition at line 22 of file socket.h.

◆ SOCKADDR_IN

typedef struct sockaddr_in SOCKADDR_IN

Windows-style.

Definition at line 24 of file socket.h.

◆ SOCKADDR

typedef struct sockaddr SOCKADDR

Windows-style.

Definition at line 25 of file socket.h.

◆ HOSTENT

typedef struct hostent HOSTENT

Windows-style.

Definition at line 26 of file socket.h.

Enumeration Type Documentation

◆ SockProtocolID

Internet protocols.

Enumerator
IPPROTO_IP 

Dummy for IP.

IPPROTO_TCP 
IPPROTO_UDP 

TCP.

IPPROTO_MAX 

UDP.

Total count

Definition at line 50 of file socket.h.

◆ SockTypeID

enum SockTypeID

Socket types.

Enumerator
SOCK_DGRAM 

UDP.

SOCK_STREAM 

TCP.

Definition at line 60 of file socket.h.

◆ SockFamilyID

Socket family types.

Enumerator
AF_UNSPEC 

Unspecified.

AF_INET 

Internet IP Protocol.

AF_MAX 

Total count.

Definition at line 68 of file socket.h.

◆ SockShutdownTypeID

Socket shutdown types.

Enumerator
SHUT_RD 

shut down the reading side

SHUT_WR 

shut down the writing side

SHUT_RDWR 

shut down both sides

Definition at line 77 of file socket.h.

Function Documentation

◆ async_gethostbyname()

int async_gethostbyname ( const char * name,
void * response,
int * req_id )

Resolve domain name.

Parameters
namedomain name to be resolved
[out]responsepointer to the pointer of hostent
[out]req_idrequest ID or NULL
Returns
0 or socket error code (same as *socklasterr())
struct hostent *response;
int req_id;
int ret = async_gethostbyname("LOCALHOST", &response, &req_id);
int async_gethostbyname(const char *name, void *response, int *req_id)
Resolve domain name.
DNS response.
Definition socket.h:129
Note
response is void * for compatibility reasons - some old code still uses int **** for this function.

◆ str2ip()

uint32_t str2ip ( const char * ip)

Convert the string IP address to the numeric format.

Parameters
ipIP address, example: 127.0.0.1
Returns
IP address, example: 0x1B000001

◆ inet_addr()

in_addr_t inet_addr ( const char * ip)

Convert the string IP address to the numeric format.

Parameters
ipIP address, example: 127.0.0.1
Returns
IP address, example: 0x1B000001

◆ htons()

uint16_t htons ( uint16_t p)

Convert host to network order (unsigned short).

◆ htonl()

uint32_t htonl ( uint32_t p)

Convert host to network order (unsigned int).

◆ ntohs()

static uint16_t ntohs ( uint16_t p)
inlinestatic

Convert network to host order.

On this little-endian core the swap is symmetric, so these reuse the host-to-network SWIs above.

Definition at line 192 of file socket.h.

◆ ntohl()

static uint32_t ntohl ( uint32_t p)
inlinestatic

Definition at line 193 of file socket.h.

◆ socket()

int socket ( int domain,
int type,
int protocol )
Parameters
domainsocket domain, set to AF_INET
typesocket type, set to SOCK_STREAM
protocolset to 0 (default protocol)
Returns
socket descriptor number or -1 on error

◆ connect()

int connect ( int socket,
const struct sockaddr * address,
socklen_t address_len )

Connect socket to the address.

Parameters
socketsocket descriptor
addresssocket address, see sockaddr_in
address_lensize of the socket address
Returns
0 or error

◆ bind()

int bind ( int socket,
const struct sockaddr * address,
socklen_t address_len )

Bind socket to the address.

Parameters
socketsocket descriptor
addresssocket address, see sockaddr_in
address_lensize of the socket address
Returns
0 or error

◆ shutdown()

int shutdown ( int socket,
int how )

Shutdown.

Parameters
socketsocket descriptor
howspecifies the type of shutdown, see SockShutdownTypeID
Returns
0 on success, -1 on error (see socklasterr)

◆ recv()

int recv ( int socket,
void * buf,
int len,
int flags )

Receive data on a socket.

Parameters
socketsocket descriptor
[out]bufbuffer for received data
lenmaximum size for the receiving
flagsset to 0
Returns
count of the received bytes or -1 (error)

◆ send()

int send ( int socket,
const void * buf,
int len,
int flags )

Send a message on a socket.

Parameters
socketsocket descriptor
bufbuffer for sending
lensize of the data in buffer
flagsset to 0
Returns
count of the sended bytes or -1 (error)

◆ available0()

int available0 ( int socket)

Get available bytes for reading in the socket.

Parameters
socketsocket descriptor
Returns
bytes count

◆ socklasterr()

int * socklasterr ( void )

Pointer to the last socket error.

Returns
pointer to the error code
int sock_err = *socklasterr();
int * socklasterr(void)
Pointer to the last socket error.

◆ closesocket()

int closesocket ( int socket)

Close socket descriptor.

Parameters
socketsocket descriptor
Returns
0 on success, -1 on error (see socklasterr)

◆ gethostbyname()

struct hostent * gethostbyname ( const char * name)

Resolve a host name synchronously.

Parameters
namedomain name
Returns
firmware host entry, or NULL on error. Use async_gethostbyname for asynchronous lookup

◆ getsockname()

int getsockname ( int sock,
SOCK_ADDR * addr,
int * len )

Read the local address of a socket.

Parameters
socksocket descriptor
addroutput address
lenaddress-buffer size on entry, returned length on exit
Returns
0 on success, -1 on error

◆ getpeername()

int getpeername ( int sock,
SOCK_ADDR * addr,
int * len )

Read the remote address of a connected socket.

Parameters
socksocket descriptor
addroutput address
lenaddress-buffer size on entry, returned length on exit
Returns
0 on success, -1 on error

◆ setsockopt()

int setsockopt ( int sock,
int level,
int option,
const void * value,
int len )

Set a socket option.

Parameters
socksocket descriptor
leveloption level
optionoption identifier
valueoption data
lensize of option data in bytes
Returns
0 on success, -1 on error

◆ getsockopt()

int getsockopt ( int sock,
int level,
int option,
void * value,
int * len )

Read a socket option.

Parameters
socksocket descriptor
leveloption level
optionoption identifier
valueoutput option data
lenbuffer size on entry, returned length on exit
Returns
0 on success, -1 on error

◆ ioctlsocket()

int ioctlsocket ( int sock,
int cmd,
void * arg )

Apply a socket control command; read socklasterr for errors.

Parameters
socksocket descriptor
cmdcontrol command
argcommand argument storage
Returns
0 on success, -1 on error (see socklasterr)

◆ listen()

int listen ( int sock,
int backlog )

Start listening for incoming TCP connections.

Parameters
sockbound socket descriptor
backlogmust be 3, other values fail
Returns
0 on success, -1 on error

◆ ENIP_GetNativeSocket()

int ENIP_GetNativeSocket ( int sock,
ENIP_SOCKET ** native )

Look up the native ENIP object for a socket descriptor.

Parameters
socksocket descriptor
nativereceives the firmware-owned socket object on success
Returns
0 on success, native ENIP error code otherwise