BSD socket API. More...
#include "core/net.h"
#include "core/bsd_socket.h"
#include "core/socket.h"
#include "core/socket_misc.h"
#include "debug.h"
#include "core/bsd_socket_options.h"
#include "core/bsd_socket_misc.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 | sendmsg (int_t s, struct msghdr *msg, int_t flags) |
Send a message. 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 | recvmsg (int_t s, struct msghdr *msg, int_t flags) |
Receive a message. 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 hostent * | gethostbyname (const char_t *name) |
Host name resolution. More... | |
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) 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_t * | inet_ntoa (struct in_addr in) |
Convert a binary IPv4 address to dot-decimal notation. More... | |
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) 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_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. 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-2023 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.
- Version
- 2.3.0
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()
Permit an incoming connection attempt on a socket.
- Parameters
-
[in] s Descriptor that identifies a socket in the listening state [out] addr Address of the connecting entity (optional) [in,out] addrlen Length 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 360 of file bsd_socket.c.
◆ bind()
Associate a local address with a socket.
- Parameters
-
[in] s Descriptor identifying an unbound socket [in] addr Local address to assign to the bound socket [in] addrlen Length in bytes of the address
- Returns
- If no error occurs, bind returns SOCKET_SUCCESS. Otherwise, it returns SOCKET_ERROR
Definition at line 107 of file bsd_socket.c.
◆ closesocket()
The closesocket function closes an existing socket.
- Parameters
-
[in] s Descriptor that identifies a socket
- Returns
- If no error occurs, closesocket returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR
Definition at line 2353 of file bsd_socket.c.
◆ connect()
Establish a connection to a specified socket.
- Parameters
-
[in] s Descriptor identifying an unconnected socket [in] addr Address to which the connection should be established [in] addrlen Length in bytes of the address
- Returns
- If no error occurs, connect returns SOCKET_SUCCESS. Otherwise, it returns SOCKET_ERROR
Definition at line 204 of file bsd_socket.c.
◆ fcntl()
Perform specific operation.
- Parameters
-
[in] s Descriptor that identifies a socket [in] cmd A command to perform on the socket [in,out] arg A pointer to a parameter
- Returns
- If no error occurs, setsockopt returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR
Definition at line 2242 of file bsd_socket.c.
◆ freeaddrinfo()
void freeaddrinfo | ( | struct addrinfo * | res | ) |
Free socket address structures.
- Parameters
-
[in] res Dynamically allocated list of socket address structures
Definition at line 2949 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] node Host name or numerical network address [in] service Service name or decimal number [in] hints Criteria for selecting the socket address structures [out] res Dynamically allocated list of socket address structures
- Returns
- On success, zero is returned. On error, a non-zero value is returned
Definition at line 2709 of file bsd_socket.c.
◆ gethostbyname()
Host name resolution.
- Parameters
-
[in] name Name of the host to resolve
- Returns
- Pointer to the hostent structure or a NULL if an error occurs
Definition at line 2612 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] name Name of the host to resolve [in] result Pointer to a hostent structure where the function can store the host entry [out] buf Pointer to a temporary work buffer [in] buflen Length of the temporary work buffer [out] h_errnop Pointer 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 2635 of file bsd_socket.c.
◆ gethostname()
Get system host name.
- Parameters
-
[out] name Output buffer where to store the system host name [in] len Length of the buffer, in bytes
- Returns
- On success, zero is returned. On error, -1 is returned
Definition at line 2571 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] addr Generic socket address structure [in] addrlen Length in bytes of the address [out] host Output buffer where to store the host name [in] hostlen Length of the host name buffer, in bytes [out] serv Output buffer where to store the service name [in] servlen Length of the service name buffer, in bytes [in] flags Set 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 2978 of file bsd_socket.c.
◆ getpeername()
Retrieves the address of the peer to which a socket is connected.
- Parameters
-
[in] s Descriptor identifying a socket [out] addr Address of the peer [in,out] addrlen Length in bytes of the address
- Returns
- If no error occurs, getpeername returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR
Definition at line 1552 of file bsd_socket.c.
◆ getsockname()
Retrieves the local name for a socket.
- Parameters
-
[in] s Descriptor identifying a socket [out] addr Address of the socket [in,out] addrlen Length in bytes of the address
- Returns
- If no error occurs, getsockname returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR
Definition at line 1456 of file bsd_socket.c.
◆ getsockopt()
The getsockopt function retrieves a socket option.
- Parameters
-
[in] s Descriptor that identifies a socket [in] level The level at which the option is defined [in] optname The socket option for which the value is to be retrieved [out] optval A pointer to the buffer in which the value for the requested option is to be returned [in,out] optlen The 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 1909 of file bsd_socket.c.
◆ inet_addr()
Convert a dot-decimal string into binary data in network byte order.
- Parameters
-
[in] cp NULL-terminated string representing the IPv4 address
- Returns
- Binary data in network byte order
Definition at line 3074 of file bsd_socket.c.
◆ inet_aton()
Convert a dot-decimal string into binary form.
- Parameters
-
[in] cp NULL-terminated string representing the IPv4 address [out] inp Binary data in network byte order
- Returns
- The function returns non-zero if the address is valid, zero if not
Definition at line 3108 of file bsd_socket.c.
◆ inet_ntoa()
Convert a binary IPv4 address to dot-decimal notation.
- Parameters
-
[in] in Binary representation of the IPv4 address
- Returns
- Pointer to the formatted string
Definition at line 3143 of file bsd_socket.c.
◆ inet_ntoa_r()
Convert a binary IPv4 address to dot-decimal notation (reentrant version)
- Parameters
-
[in] in Binary representation of the IPv4 address [out] buf Pointer to the buffer where to format the string [in] buflen Number of bytes available in the buffer
- Returns
- Pointer to the formatted string
Definition at line 3160 of file bsd_socket.c.
◆ inet_ntop()
Convert an IPv4 or IPv6 address from binary to text.
- Parameters
-
[in] af Address family [in] src Binary representation of the IP address [out] dst NULL-terminated string representing the IP address [in] size Number 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 3260 of file bsd_socket.c.
◆ inet_pton()
Convert an IPv4 or IPv6 address from text to binary form.
- Parameters
-
[in] af Address family [in] src NULL-terminated string representing the IP address [out] dst Binary 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 3188 of file bsd_socket.c.
◆ ioctlsocket()
Control the I/O mode of a socket.
- Parameters
-
[in] s Descriptor that identifies a socket [in] cmd A command to perform on the socket [in,out] arg A pointer to a parameter
- Returns
- If no error occurs, setsockopt returns SOCKET_SUCCESS Otherwise, it returns SOCKET_ERROR
Definition at line 2142 of file bsd_socket.c.
◆ listen()
Place a socket in the listening state.
Place a socket in a state in which it is listening for an incoming connection
- Parameters
-
[in] s Descriptor identifying a bound, unconnected socket [in] backlog Maximum length of the queue of pending connections
- Returns
- If no error occurs, listen returns SOCKET_SUCCESS. Otherwise, it returns SOCKET_ERROR
Definition at line 322 of file bsd_socket.c.
◆ recv()
Receive data from a connected socket.
- Parameters
-
[in] s Descriptor that identifies a connected socket [out] data Buffer where to store the incoming data [in] size Maximum number of bytes that can be received [in] flags Set 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 897 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] s Descriptor that identifies a socket [out] data Buffer where to store the incoming data [in] size Maximum number of bytes that can be received [in] flags Set of flags that influences the behavior of this function [out] addr Source address upon return (optional) [in,out] addrlen Length in bytes of the address (optional)
- Returns
- If no error occurs, recvfrom returns the number of bytes received. Otherwise, a value of SOCKET_ERROR is returned
Definition at line 969 of file bsd_socket.c.
◆ recvmsg()
Receive a message.
- Parameters
-
[in] s Descriptor that identifies a socket [in,out] msg Pointer to the structure describing the message [in] flags Set of flags that influences the behavior of this function
- Returns
- If no error occurs, recvmsg returns the number of bytes received. Otherwise, a value of SOCKET_ERROR is returned
Definition at line 1095 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] nfds Unused parameter included only for compatibility with Berkeley socket [in,out] readfds An optional pointer to a set of sockets to be checked for readability [in,out] writefds An optional pointer to a set of sockets to be checked for writability [in,out] exceptfds An optional pointer to a set of sockets to be checked for errors [in] timeout The 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 2395 of file bsd_socket.c.
◆ send()
Send data to a connected socket.
- Parameters
-
[in] s Descriptor that identifies a connected socket [in] data Pointer to a buffer containing the data to be transmitted [in] length Number of bytes to be transmitted [in] flags Set 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 459 of file bsd_socket.c.
◆ sendmsg()
Send a message.
- Parameters
-
[in] s Descriptor that identifies a socket [in] msg Pointer to the structure describing the message [in] flags Set of flags that influences the behavior of this function
- Returns
- If no error occurs, sendmsg 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 661 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] s Descriptor that identifies a socket [in] data Pointer to a buffer containing the data to be transmitted [in] length Number of bytes to be transmitted [in] flags Set of flags that influences the behavior of this function [in] addr Destination address [in] addrlen Length 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 535 of file bsd_socket.c.
◆ setsockopt()
The setsockopt function sets a socket option.
- Parameters
-
[in] s Descriptor that identifies a socket [in] level The level at which the option is defined [in] optname The socket option for which the value is to be set [in] optval A pointer to the buffer in which the value for the requested option is specified [in] optlen The 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 1652 of file bsd_socket.c.
◆ shutdown()
The shutdown function disables sends or receives on a socket.
- Parameters
-
[in] s Descriptor that identifies a socket [in] how A 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 2317 of file bsd_socket.c.
◆ socket()
Create a socket that is bound to a specific transport service provider.
- Parameters
-
[in] family Address family [in] type Type specification for the new socket [in] protocol Protocol to be used
- Returns
- On success, a file descriptor for the new socket is returned. On failure, SOCKET_ERROR is returned
Definition at line 65 of file bsd_socket.c.
Variable Documentation
◆ in6addr_any
const struct in6_addr in6addr_any |
Definition at line 1 of file bsd_socket.c.
◆ in6addr_loopback
const struct in6_addr in6addr_loopback |
Definition at line 1 of file bsd_socket.c.