bsd_socket.c File Reference

BSD socket API. More...

#include <string.h>
#include "core/net.h"
#include "core/bsd_socket.h"
#include "core/bsd_socket_misc.h"
#include "core/socket.h"
#include "core/socket_misc.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   BSD_SOCKET_TRACE_LEVEL
 

Functions

int_t socket (int_t family, int_t type, int_t protocol)
 Create a socket that is bound to a specific transport service provider. More...
 
int_t bind (int_t s, const struct sockaddr *addr, socklen_t addrlen)
 Associate a local address with a socket. More...
 
int_t connect (int_t s, const struct sockaddr *addr, socklen_t addrlen)
 Establish a connection to a specified socket. More...
 
int_t listen (int_t s, int_t backlog)
 Place a socket in the listening state. More...
 
int_t accept (int_t s, struct sockaddr *addr, socklen_t *addrlen)
 Permit an incoming connection attempt on a socket. More...
 
int_t send (int_t s, const void *data, size_t length, int_t flags)
 Send data to a connected socket. More...
 
int_t sendto (int_t s, const void *data, size_t length, int_t flags, const struct sockaddr *addr, socklen_t addrlen)
 Send a datagram to a specific destination. More...
 
int_t recv (int_t s, void *data, size_t size, int_t flags)
 Receive data from a connected socket. More...
 
int_t recvfrom (int_t s, void *data, size_t size, int_t flags, struct sockaddr *addr, socklen_t *addrlen)
 Receive a datagram. More...
 
int_t getsockname (int_t s, struct sockaddr *addr, socklen_t *addrlen)
 Retrieves the local name for a socket. More...
 
int_t getpeername (int_t s, struct sockaddr *addr, socklen_t *addrlen)
 Retrieves the address of the peer to which a socket is connected. More...
 
int_t setsockopt (int_t s, int_t level, int_t optname, const void *optval, socklen_t optlen)
 The setsockopt function sets a socket option. More...
 
int_t getsockopt (int_t s, int_t level, int_t optname, void *optval, socklen_t *optlen)
 The getsockopt function retrieves a socket option. More...
 
int_t ioctlsocket (int_t s, uint32_t cmd, void *arg)
 Control the I/O mode of a socket. More...
 
int_t fcntl (int_t s, int_t cmd, void *arg)
 Perform specific operation. More...
 
int_t shutdown (int_t s, int_t how)
 The shutdown function disables sends or receives on a socket. More...
 
int_t closesocket (int_t s)
 The closesocket function closes an existing socket. More...
 
int_t select (int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
 Determine the status of one or more sockets. More...
 
int_t gethostname (char_t *name, size_t len)
 Get system host name. More...
 
struct hostentgethostbyname (const char_t *name)
 Host name resolution. More...
 
struct hostentgethostbyname_r (const char_t *name, struct hostent *result, char_t *buf, size_t buflen, int_t *h_errnop)
 Host name resolution (reentrant version) More...
 
int_t getaddrinfo (const char_t *node, const char_t *service, const struct addrinfo *hints, struct addrinfo **res)
 Convert host and service names to socket address. More...
 
void freeaddrinfo (struct addrinfo *res)
 Free socket address structures. More...
 
int_t getnameinfo (const struct sockaddr *addr, socklen_t addrlen, char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags)
 Convert a socket address to a corresponding host and service. More...
 
in_addr_t inet_addr (const char_t *cp)
 Convert a dot-decimal string into binary data in network byte order. More...
 
int_t inet_aton (const char_t *cp, struct in_addr *inp)
 Convert a dot-decimal string into binary form. More...
 
const char_tinet_ntoa (struct in_addr in)
 Convert a binary IPv4 address to dot-decimal notation. More...
 
const char_tinet_ntoa_r (struct in_addr in, char_t *buf, socklen_t buflen)
 Convert a binary IPv4 address to dot-decimal notation (reentrant version) More...
 
int_t inet_pton (int_t af, const char_t *src, void *dst)
 Convert an IPv4 or IPv6 address from text to binary form. More...
 
const char_tinet_ntop (int_t af, const void *src, char_t *dst, socklen_t size)
 Convert an IPv4 or IPv6 address from binary to text. More...
 

Variables

const struct in6_addr in6addr_any
 
const struct in6_addr in6addr_loopback
 

Detailed Description

BSD socket API.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2022 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.1.8

Definition in file bsd_socket.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   BSD_SOCKET_TRACE_LEVEL

Definition at line 32 of file bsd_socket.c.

Function Documentation

◆ accept()

int_t accept ( int_t  s,
struct sockaddr addr,
socklen_t addrlen 
)

Permit an incoming connection attempt on a socket.

Parameters
[in]sDescriptor that identifies a socket in the listening state
[out]addrAddress of the connecting entity (optional)
[in,out]addrlenLength in bytes of the address (optional)
Returns
If no error occurs, accept returns a descriptor for the new socket. Otherwise, it returns SOCKET_ERROR

Definition at line 370 of file bsd_socket.c.

◆ bind()

int_t bind ( int_t  s,
const struct sockaddr addr,
socklen_t  addrlen 
)

Associate a local address with a socket.

Parameters
[in]sDescriptor identifying an unbound socket
[in]addrLocal address to assign to the bound socket
[in]addrlenLength in bytes of the address
Returns
If no error occurs, bind returns SOCKET_SUCCESS. Otherwise, it returns SOCKET_ERROR

Definition at line 105 of file bsd_socket.c.

◆ closesocket()

int_t closesocket ( int_t  s)

The closesocket function closes an existing socket.

Parameters
[in]sDescriptor that identifies a socket
Returns
If no error occurs, closesocket returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR

Definition at line 1823 of file bsd_socket.c.

◆ connect()

int_t connect ( int_t  s,
const struct sockaddr addr,
socklen_t  addrlen 
)

Establish a connection to a specified socket.

Parameters
[in]sDescriptor identifying an unconnected socket
[in]addrAddress to which the connection should be established
[in]addrlenLength in bytes of the address
Returns
If no error occurs, connect returns SOCKET_SUCCESS. Otherwise, it returns SOCKET_ERROR

Definition at line 208 of file bsd_socket.c.

◆ fcntl()

int_t fcntl ( int_t  s,
int_t  cmd,
void *  arg 
)

Perform specific operation.

Parameters
[in]sDescriptor that identifies a socket
[in]cmdA command to perform on the socket
[in,out]argA pointer to a parameter
Returns
If no error occurs, setsockopt returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR

Definition at line 1712 of file bsd_socket.c.

◆ freeaddrinfo()

void freeaddrinfo ( struct addrinfo res)

Free socket address structures.

Parameters
[in]resDynamically allocated list of socket address structures

Definition at line 2417 of file bsd_socket.c.

◆ getaddrinfo()

int_t getaddrinfo ( const char_t node,
const char_t service,
const struct addrinfo hints,
struct addrinfo **  res 
)

Convert host and service names to socket address.

Parameters
[in]nodeHost name or numerical network address
[in]serviceService name or decimal number
[in]hintsCriteria for selecting the socket address structures
[out]resDynamically allocated list of socket address structures
Returns
On success, zero is returned. On error, a non-zero value is returned

Definition at line 2179 of file bsd_socket.c.

◆ gethostbyname()

struct hostent* gethostbyname ( const char_t name)

Host name resolution.

Parameters
[in]nameName of the host to resolve
Returns
Pointer to the hostent structure or a NULL if an error occurs

Definition at line 2082 of file bsd_socket.c.

◆ gethostbyname_r()

struct hostent* gethostbyname_r ( const char_t name,
struct hostent result,
char_t buf,
size_t  buflen,
int_t h_errnop 
)

Host name resolution (reentrant version)

Parameters
[in]nameName of the host to resolve
[in]resultPointer to a hostent structure where the function can store the host entry
[out]bufPointer to a temporary work buffer
[in]buflenLength of the temporary work buffer
[out]h_errnopPointer to a location where the function can store an h_errno value if an error occurs
Returns
Pointer to the hostent structure or a NULL if an error occurs

Definition at line 2105 of file bsd_socket.c.

◆ gethostname()

int_t gethostname ( char_t name,
size_t  len 
)

Get system host name.

Parameters
[out]nameOutput buffer where to store the system host name
[in]lenLength of the buffer, in bytes
Returns
On success, zero is returned. On error, -1 is returned

Definition at line 2041 of file bsd_socket.c.

◆ getnameinfo()

int_t getnameinfo ( const struct sockaddr addr,
socklen_t  addrlen,
char_t host,
size_t  hostlen,
char_t serv,
size_t  servlen,
int  flags 
)

Convert a socket address to a corresponding host and service.

Parameters
[in]addrGeneric socket address structure
[in]addrlenLength in bytes of the address
[out]hostOutput buffer where to store the host name
[in]hostlenLength of the host name buffer, in bytes
[out]servOutput buffer where to store the service name
[in]servlenLength of the service name buffer, in bytes
[in]flagsSet of flags that influences the behavior of this function
Returns
On success, zero is returned. On error, a non-zero value is returned

Definition at line 2446 of file bsd_socket.c.

◆ getpeername()

int_t getpeername ( int_t  s,
struct sockaddr addr,
socklen_t addrlen 
)

Retrieves the address of the peer to which a socket is connected.

Parameters
[in]sDescriptor identifying a socket
[out]addrAddress of the peer
[in,out]addrlenLength in bytes of the address
Returns
If no error occurs, getpeername returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR

Definition at line 877 of file bsd_socket.c.

◆ getsockname()

int_t getsockname ( int_t  s,
struct sockaddr addr,
socklen_t addrlen 
)

Retrieves the local name for a socket.

Parameters
[in]sDescriptor identifying a socket
[out]addrAddress of the socket
[in,out]addrlenLength in bytes of the address
Returns
If no error occurs, getsockname returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR

Definition at line 783 of file bsd_socket.c.

◆ getsockopt()

int_t getsockopt ( int_t  s,
int_t  level,
int_t  optname,
void *  optval,
socklen_t optlen 
)

The getsockopt function retrieves a socket option.

Parameters
[in]sDescriptor that identifies a socket
[in]levelThe level at which the option is defined
[in]optnameThe socket option for which the value is to be retrieved
[out]optvalA pointer to the buffer in which the value for the requested option is to be returned
[in,out]optlenThe size, in bytes, of the buffer pointed to by the optval parameter
Returns
If no error occurs, getsockopt returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR

Definition at line 1278 of file bsd_socket.c.

◆ inet_addr()

in_addr_t inet_addr ( const char_t cp)

Convert a dot-decimal string into binary data in network byte order.

Parameters
[in]cpNULL-terminated string representing the IPv4 address
Returns
Binary data in network byte order

Definition at line 2542 of file bsd_socket.c.

◆ inet_aton()

int_t inet_aton ( const char_t cp,
struct in_addr inp 
)

Convert a dot-decimal string into binary form.

Parameters
[in]cpNULL-terminated string representing the IPv4 address
[out]inpBinary data in network byte order
Returns
The function returns non-zero if the address is valid, zero if not

Definition at line 2576 of file bsd_socket.c.

◆ inet_ntoa()

const char_t* inet_ntoa ( struct in_addr  in)

Convert a binary IPv4 address to dot-decimal notation.

Parameters
[in]inBinary representation of the IPv4 address
Returns
Pointer to the formatted string

Definition at line 2611 of file bsd_socket.c.

◆ inet_ntoa_r()

const char_t* inet_ntoa_r ( struct in_addr  in,
char_t buf,
socklen_t  buflen 
)

Convert a binary IPv4 address to dot-decimal notation (reentrant version)

Parameters
[in]inBinary representation of the IPv4 address
[out]bufPointer to the buffer where to format the string
[in]buflenNumber of bytes available in the buffer
Returns
Pointer to the formatted string

Definition at line 2628 of file bsd_socket.c.

◆ inet_ntop()

const char_t* inet_ntop ( int_t  af,
const void *  src,
char_t dst,
socklen_t  size 
)

Convert an IPv4 or IPv6 address from binary to text.

Parameters
[in]afAddress family
[in]srcBinary representation of the IP address
[out]dstNULL-terminated string representing the IP address
[in]sizeNumber of bytes available in the buffer
Returns
On success, the function returns a pointer to the formatted string. NULL is returned if there was an error

Definition at line 2728 of file bsd_socket.c.

◆ inet_pton()

int_t inet_pton ( int_t  af,
const char_t src,
void *  dst 
)

Convert an IPv4 or IPv6 address from text to binary form.

Parameters
[in]afAddress family
[in]srcNULL-terminated string representing the IP address
[out]dstBinary representation of the IP address
Returns
The function returns 1 on success. 0 is returned if the address is not valid. If the address family is not valid, -1 is returned

Definition at line 2656 of file bsd_socket.c.

◆ ioctlsocket()

int_t ioctlsocket ( int_t  s,
uint32_t  cmd,
void *  arg 
)

Control the I/O mode of a socket.

Parameters
[in]sDescriptor that identifies a socket
[in]cmdA command to perform on the socket
[in,out]argA pointer to a parameter
Returns
If no error occurs, setsockopt returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR

Definition at line 1612 of file bsd_socket.c.

◆ listen()

int_t listen ( int_t  s,
int_t  backlog 
)

Place a socket in the listening state.

Place a socket in a state in which it is listening for an incoming connection

Parameters
[in]sDescriptor identifying a bound, unconnected socket
[in]backlogMaximum length of the queue of pending connections
Returns
If no error occurs, listen returns SOCKET_SUCCESS. Otherwise, it returns SOCKET_ERROR

Definition at line 332 of file bsd_socket.c.

◆ recv()

int_t recv ( int_t  s,
void *  data,
size_t  size,
int_t  flags 
)

Receive data from a connected socket.

Parameters
[in]sDescriptor that identifies a connected socket
[out]dataBuffer where to store the incoming data
[in]sizeMaximum number of bytes that can be received
[in]flagsSet of flags that influences the behavior of this function
Returns
If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned

Definition at line 634 of file bsd_socket.c.

◆ recvfrom()

int_t recvfrom ( int_t  s,
void *  data,
size_t  size,
int_t  flags,
struct sockaddr addr,
socklen_t addrlen 
)

Receive a datagram.

Parameters
[in]sDescriptor that identifies a socket
[out]dataBuffer where to store the incoming data
[in]sizeMaximum number of bytes that can be received
[in]flagsSet of flags that influences the behavior of this function
[out]addrSource address upon return (optional)
[in,out]addrlenLength in bytes of the address (optional)
Returns
If no error occurs, recvfrom returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned

Definition at line 683 of file bsd_socket.c.

◆ select()

int_t select ( int_t  nfds,
fd_set readfds,
fd_set writefds,
fd_set exceptfds,
const struct timeval timeout 
)

Determine the status of one or more sockets.

The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O

Parameters
[in]nfdsUnused parameter included only for compatibility with Berkeley socket
[in,out]readfdsAn optional pointer to a set of sockets to be checked for readability
[in,out]writefdsAn optional pointer to a set of sockets to be checked for writability
[in,out]exceptfdsAn optional pointer to a set of sockets to be checked for errors
[in]timeoutThe maximum time for select to wait. Set the timeout parameter to null for blocking operations
Returns
The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred

Definition at line 1865 of file bsd_socket.c.

◆ send()

int_t send ( int_t  s,
const void *  data,
size_t  length,
int_t  flags 
)

Send data to a connected socket.

Parameters
[in]sDescriptor that identifies a connected socket
[in]dataPointer to a buffer containing the data to be transmitted
[in]lengthNumber of bytes to be transmitted
[in]flagsSet of flags that influences the behavior of this function
Returns
If no error occurs, send returns the total number of bytes sent, which can be less than the number requested to be sent in the length parameter. Otherwise, a value of SOCKET_ERROR is returned

Definition at line 467 of file bsd_socket.c.

◆ sendto()

int_t sendto ( int_t  s,
const void *  data,
size_t  length,
int_t  flags,
const struct sockaddr addr,
socklen_t  addrlen 
)

Send a datagram to a specific destination.

Parameters
[in]sDescriptor that identifies a socket
[in]dataPointer to a buffer containing the data to be transmitted
[in]lengthNumber of bytes to be transmitted
[in]flagsSet of flags that influences the behavior of this function
[in]addrDestination address
[in]addrlenLength in bytes of the destination address
Returns
If no error occurs, sendto returns the total number of bytes sent, which can be less than the number requested to be sent in the length parameter. Otherwise, a value of SOCKET_ERROR is returned

Definition at line 526 of file bsd_socket.c.

◆ setsockopt()

int_t setsockopt ( int_t  s,
int_t  level,
int_t  optname,
const void *  optval,
socklen_t  optlen 
)

The setsockopt function sets a socket option.

Parameters
[in]sDescriptor that identifies a socket
[in]levelThe level at which the option is defined
[in]optnameThe socket option for which the value is to be set
[in]optvalA pointer to the buffer in which the value for the requested option is specified
[in]optlenThe size, in bytes, of the buffer pointed to by the optval parameter
Returns
If no error occurs, setsockopt returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR

Definition at line 973 of file bsd_socket.c.

◆ shutdown()

int_t shutdown ( int_t  s,
int_t  how 
)

The shutdown function disables sends or receives on a socket.

Parameters
[in]sDescriptor that identifies a socket
[in]howA flag that describes what types of operation will no longer be allowed
Returns
If no error occurs, shutdown returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR

Definition at line 1787 of file bsd_socket.c.

◆ socket()

int_t socket ( int_t  family,
int_t  type,
int_t  protocol 
)

Create a socket that is bound to a specific transport service provider.

Parameters
[in]familyAddress family
[in]typeType specification for the new socket
[in]protocolProtocol to be used
Returns
On success, a file descriptor for the new socket is returned. On failure, SOCKET_ERROR is returned

Definition at line 63 of file bsd_socket.c.

Variable Documentation

◆ in6addr_any

const struct in6_addr in6addr_any
Initial value:
=
{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}

Definition at line 1 of file bsd_socket.c.

◆ in6addr_loopback

const struct in6_addr in6addr_loopback
Initial value:
=
{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}

Definition at line 1 of file bsd_socket.c.