bsd_socket.c File Reference

BSD socket API. More...

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

Go to the source code of this file.

Macros

#define TRACE_LEVEL   BSD_SOCKET_TRACE_LEVEL
 

Functions

int_t socketTranslateErrorCode (error_t error)
 Translate error code. More...
 
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 sockaddr *addr, socklen_t addrlen)
 Associate a local address with a socket. More...
 
int_t connect (int_t s, const 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, 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 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, sockaddr *addr, socklen_t *addrlen)
 Receive a datagram. More...
 
int_t getsockname (int_t s, sockaddr *addr, socklen_t *addrlen)
 Retrieves the local name for a socket. More...
 
int_t getpeername (int_t s, 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 timeval *timeout)
 Determine the status of one or more sockets. More...
 
void selectFdZero (fd_set *fds)
 Initializes a descriptor set. More...
 
void selectFdSet (fd_set *fds, int_t s)
 Add a descriptor to an existing set. More...
 
void selectFdClr (fd_set *fds, int_t s)
 Remove a descriptor from an existing set. More...
 
int_t selectFdIsSet (fd_set *fds, int_t s)
 Check whether a descriptor is set. More...
 
int_t gethostbyname (const char_t *name, hostent *info)
 Retrieve host address corresponding to a host name. 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, in_addr *inp)
 Convert a dot-decimal string into binary form. More...
 
const char_tinet_ntoa (in_addr in, char_t *cp)
 Convert a binary IPv4 address to dot-decimal notation. 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 in6_addr in6addr_any
 
const in6_addr in6addr_loopback
 

Detailed Description

BSD socket API.

License

Copyright (C) 2010-2018 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
1.9.0

Definition in file bsd_socket.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   BSD_SOCKET_TRACE_LEVEL

Definition at line 30 of file bsd_socket.c.

Function Documentation

◆ accept()

int_t accept ( int_t  s,
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 389 of file bsd_socket.c.

◆ bind()

int_t bind ( int_t  s,
const 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 148 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 1494 of file bsd_socket.c.

◆ connect()

int_t connect ( int_t  s,
const 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 249 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 1383 of file bsd_socket.c.

◆ gethostbyname()

int_t gethostbyname ( const char_t name,
hostent info 
)

Retrieve host address corresponding to a host name.

Parameters
[in]nameName of the host to resolve
[out]infoAddress of the specified host
Returns
If no error occurs, gethostbyname returns 0. Otherwise it returns an appropriate error code

Definition at line 1801 of file bsd_socket.c.

◆ getpeername()

int_t getpeername ( int_t  s,
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 881 of file bsd_socket.c.

◆ getsockname()

int_t getsockname ( int_t  s,
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 789 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 1125 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 1857 of file bsd_socket.c.

◆ inet_aton()

int_t inet_aton ( const char_t cp,
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 1891 of file bsd_socket.c.

◆ inet_ntoa()

const char_t* inet_ntoa ( in_addr  in,
char_t cp 
)

Convert a binary IPv4 address to dot-decimal notation.

Parameters
[in]inBinary representation of the IPv4 address
[out]cpPointer to the buffer where to format the string
Returns
Pointer to the formatted string

Definition at line 1927 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 2022 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 1950 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 1306 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 351 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 645 of file bsd_socket.c.

◆ recvfrom()

int_t recvfrom ( int_t  s,
void *  data,
size_t  size,
int_t  flags,
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 694 of file bsd_socket.c.

◆ select()

int_t select ( int_t  nfds,
fd_set readfds,
fd_set writefds,
fd_set exceptfds,
const 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 1532 of file bsd_socket.c.

◆ selectFdClr()

void selectFdClr ( fd_set fds,
int_t  s 
)

Remove a descriptor from an existing set.

Parameters
[in]fdsPointer to a descriptor set
[in]sDescriptor that identifies the socket to remove

Definition at line 1744 of file bsd_socket.c.

◆ selectFdIsSet()

int_t selectFdIsSet ( fd_set fds,
int_t  s 
)

Check whether a descriptor is set.

Parameters
[in]fdsPointer to a descriptor set
[in]sDescriptor that identifies the socket to test
Returns
Nonzero if s is a member of the set. Otherwise, zero

Definition at line 1776 of file bsd_socket.c.

◆ selectFdSet()

void selectFdSet ( fd_set fds,
int_t  s 
)

Add a descriptor to an existing set.

Parameters
[in]fdsPointer to a descriptor set
[in]sDescriptor that identifies the socket to add

Definition at line 1715 of file bsd_socket.c.

◆ selectFdZero()

void selectFdZero ( fd_set fds)

Initializes a descriptor set.

Parameters
[in]fdsPointer to a descriptor set

Definition at line 1702 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 481 of file bsd_socket.c.

◆ sendto()

int_t sendto ( int_t  s,
const void *  data,
size_t  length,
int_t  flags,
const 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 540 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 975 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 1458 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 106 of file bsd_socket.c.

◆ socketTranslateErrorCode()

int_t socketTranslateErrorCode ( error_t  error)

Translate error code.

Parameters
[in]errorError code to be translated
Returns
BSD error code

Definition at line 56 of file bsd_socket.c.

Variable Documentation

◆ in6addr_any

const 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 43 of file bsd_socket.c.

◆ in6addr_loopback

const 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 46 of file bsd_socket.c.