socket.c File Reference

Socket API. More...

#include "core/net.h"
#include "core/socket.h"
#include "core/socket_misc.h"
#include "core/raw_socket.h"
#include "core/udp.h"
#include "core/tcp.h"
#include "core/tcp_misc.h"
#include "dns/dns_client.h"
#include "mdns/mdns_client.h"
#include "netbios/nbns_client.h"
#include "llmnr/llmnr_client.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   SOCKET_TRACE_LEVEL
 

Functions

error_t socketInit (void)
 Socket related initialization. More...
 
SocketsocketOpen (uint_t type, uint_t protocol)
 Create a socket (UDP or TCP) More...
 
error_t socketSetTimeout (Socket *socket, systime_t timeout)
 Set timeout value for blocking operations. More...
 
error_t socketSetTtl (Socket *socket, uint8_t ttl)
 Set TTL value for unicast datagrams. More...
 
error_t socketSetMulticastTtl (Socket *socket, uint8_t ttl)
 Set TTL value for multicast datagrams. More...
 
error_t socketSetDscp (Socket *socket, uint8_t dscp)
 Set DSCP field. More...
 
error_t socketSetVlanPcp (Socket *socket, uint8_t pcp)
 Set VLAN priority. More...
 
error_t socketSetVlanDei (Socket *socket, bool_t dei)
 Set VLAN DEI flag. More...
 
error_t socketSetVmanPcp (Socket *socket, uint8_t pcp)
 Set VMAN priority. More...
 
error_t socketSetVmanDei (Socket *socket, bool_t dei)
 Set VMAN DEI flag. More...
 
error_t socketEnableBroadcast (Socket *socket, bool_t enabled)
 Enable reception of broadcast messages. More...
 
error_t socketJoinMulticastGroup (Socket *socket, const IpAddr *groupAddr)
 Join the specified host group. More...
 
error_t socketLeaveMulticastGroup (Socket *socket, const IpAddr *groupAddr)
 Leave the specified host group. More...
 
error_t socketSetMulticastSourceFilter (Socket *socket, const IpAddr *groupAddr, IpFilterMode filterMode, const IpAddr *sources, uint_t numSources)
 Set multicast source filter (full-state API) More...
 
error_t socketGetMulticastSourceFilter (Socket *socket, const IpAddr *groupAddr, IpFilterMode *filterMode, IpAddr *sources, uint_t *numSources)
 Get multicast source filter. More...
 
error_t socketAddMulticastSource (Socket *socket, const IpAddr *groupAddr, const IpAddr *srcAddr)
 Accept specific source for specific group (delta-based API) More...
 
error_t socketDropMulticastSource (Socket *socket, const IpAddr *groupAddr, const IpAddr *srcAddr)
 Drop specific source for specific group (delta-based API) More...
 
error_t socketBlockMulticastSource (Socket *socket, const IpAddr *groupAddr, const IpAddr *srcAddr)
 Block specific source for specific group (delta-based API) More...
 
error_t socketUnblockMulticastSource (Socket *socket, const IpAddr *groupAddr, const IpAddr *srcAddr)
 Unblock specific source for specific group (delta-based API) More...
 
error_t socketEnableKeepAlive (Socket *socket, bool_t enabled)
 Enable TCP keep-alive. More...
 
error_t socketSetKeepAliveParams (Socket *socket, systime_t idle, systime_t interval, uint_t maxProbes)
 Set TCP keep-alive parameters. More...
 
error_t socketSetMaxSegmentSize (Socket *socket, size_t mss)
 Specify the maximum segment size for outgoing TCP packets. More...
 
error_t socketSetTxBufferSize (Socket *socket, size_t size)
 Specify the size of the TCP send buffer. More...
 
error_t socketSetRxBufferSize (Socket *socket, size_t size)
 Specify the size of the TCP receive buffer. More...
 
error_t socketSetInterface (Socket *socket, NetInterface *interface)
 Bind a socket to a particular network interface. More...
 
NetInterfacesocketGetInterface (Socket *socket)
 Retrieve the underlying interface. More...
 
error_t socketBind (Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
 Associate a local address with a socket. More...
 
error_t socketConnect (Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
 Establish a connection to a specified socket. More...
 
error_t socketListen (Socket *socket, uint_t backlog)
 Place a socket in the listening state. More...
 
SocketsocketAccept (Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
 Permit an incoming connection attempt on a socket. More...
 
error_t socketSend (Socket *socket, const void *data, size_t length, size_t *written, uint_t flags)
 Send data to a connected socket. More...
 
error_t socketSendTo (Socket *socket, const IpAddr *destIpAddr, uint16_t destPort, const void *data, size_t length, size_t *written, uint_t flags)
 Send a datagram to a specific destination. More...
 
error_t socketSendMsg (Socket *socket, const SocketMsg *message, uint_t flags)
 Send a message to a connectionless socket. More...
 
error_t socketReceive (Socket *socket, void *data, size_t size, size_t *received, uint_t flags)
 Receive data from a connected socket. More...
 
error_t socketReceiveFrom (Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort, void *data, size_t size, size_t *received, uint_t flags)
 Receive a datagram from a connectionless socket. More...
 
error_t socketReceiveEx (Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort, IpAddr *destIpAddr, void *data, size_t size, size_t *received, uint_t flags)
 Receive a datagram. More...
 
error_t socketReceiveMsg (Socket *socket, SocketMsg *message, uint_t flags)
 Receive a message from a connectionless socket. More...
 
error_t socketGetLocalAddr (Socket *socket, IpAddr *localIpAddr, uint16_t *localPort)
 Retrieve the local address for a given socket. More...
 
error_t socketGetRemoteAddr (Socket *socket, IpAddr *remoteIpAddr, uint16_t *remotePort)
 Retrieve the address of the peer to which a socket is connected. More...
 
error_t socketShutdown (Socket *socket, uint_t how)
 Disable reception, transmission, or both. More...
 
void socketClose (Socket *socket)
 Close an existing socket. More...
 
error_t socketPoll (SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
 Wait for one of a set of sockets to become ready to perform I/O. More...
 
error_t getHostByName (NetInterface *interface, const char_t *name, IpAddr *ipAddr, uint_t flags)
 Resolve a host name into an IP address. More...
 

Variables

Socket socketTable [SOCKET_MAX_COUNT]
 
const SocketMsg SOCKET_DEFAULT_MSG
 

Detailed Description

Socket API.

License

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

Copyright (C) 2010-2024 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.4.4

Definition in file socket.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   SOCKET_TRACE_LEVEL

Definition at line 32 of file socket.c.

Function Documentation

◆ getHostByName()

error_t getHostByName ( NetInterface interface,
const char_t name,
IpAddr ipAddr,
uint_t  flags 
)

Resolve a host name into an IP address.

Parameters
[in]interfaceUnderlying network interface (optional parameter)
[in]nameName of the host to be resolved
[out]ipAddrIP address corresponding to the specified host name
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 2274 of file socket.c.

◆ socketAccept()

Socket* socketAccept ( Socket socket,
IpAddr clientIpAddr,
uint16_t *  clientPort 
)

Permit an incoming connection attempt on a socket.

Parameters
[in]socketHandle to a socket previously placed in a listening state
[out]clientIpAddrIP address of the client
[out]clientPortPort number used by the client
Returns
Handle to the socket in which the actual connection is made

Definition at line 1451 of file socket.c.

◆ socketAddMulticastSource()

error_t socketAddMulticastSource ( Socket socket,
const IpAddr groupAddr,
const IpAddr srcAddr 
)

Accept specific source for specific group (delta-based API)

Parameters
[in]socketHandle to a socket
[in]groupAddrIP address identifying a multicast group
[in]srcAddrSource IP address to accept
Returns
Error code

Definition at line 753 of file socket.c.

◆ socketBind()

error_t socketBind ( Socket socket,
const IpAddr localIpAddr,
uint16_t  localPort 
)

Associate a local address with a socket.

Parameters
[in]socketHandle to a socket
[in]localIpAddrLocal address to assign to the bound socket
[in]localPortLocal port number to assign to the bound socket
Returns
Error code

Definition at line 1316 of file socket.c.

◆ socketBlockMulticastSource()

error_t socketBlockMulticastSource ( Socket socket,
const IpAddr groupAddr,
const IpAddr srcAddr 
)

Block specific source for specific group (delta-based API)

Parameters
[in]socketHandle to a socket
[in]groupAddrIP address identifying a multicast group
[in]srcAddrSource IP address to block
Returns
Error code

Definition at line 917 of file socket.c.

◆ socketClose()

void socketClose ( Socket socket)

Close an existing socket.

Parameters
[in]socketHandle identifying the socket to close

Definition at line 2062 of file socket.c.

◆ socketConnect()

error_t socketConnect ( Socket socket,
const IpAddr remoteIpAddr,
uint16_t  remotePort 
)

Establish a connection to a specified socket.

Parameters
[in]socketHandle to an unconnected socket
[in]remoteIpAddrIP address of the remote host
[in]remotePortRemote port number that will be used to establish the connection
Returns
Error code

Definition at line 1349 of file socket.c.

◆ socketDropMulticastSource()

error_t socketDropMulticastSource ( Socket socket,
const IpAddr groupAddr,
const IpAddr srcAddr 
)

Drop specific source for specific group (delta-based API)

Parameters
[in]socketHandle to a socket
[in]groupAddrIP address identifying a multicast group
[in]srcAddrSource IP address to drop
Returns
Error code

Definition at line 840 of file socket.c.

◆ socketEnableBroadcast()

error_t socketEnableBroadcast ( Socket socket,
bool_t  enabled 
)

Enable reception of broadcast messages.

Parameters
[in]socketHandle to a socket
[in]enabledSpecifies whether broadcast messages should be accepted
Returns
Error code

Definition at line 392 of file socket.c.

◆ socketEnableKeepAlive()

error_t socketEnableKeepAlive ( Socket socket,
bool_t  enabled 
)

Enable TCP keep-alive.

Parameters
[in]socketHandle to a socket
[in]enabledSpecifies whether TCP keep-alive is enabled
Returns
Error code

Definition at line 1073 of file socket.c.

◆ socketGetInterface()

NetInterface* socketGetInterface ( Socket socket)

Retrieve the underlying interface.

Parameters
[in]socketHandle to a socket
Returns
Pointer to the underlying network interface

Definition at line 1293 of file socket.c.

◆ socketGetLocalAddr()

error_t socketGetLocalAddr ( Socket socket,
IpAddr localIpAddr,
uint16_t *  localPort 
)

Retrieve the local address for a given socket.

Parameters
[in]socketHandle that identifies a socket
[out]localIpAddrLocal IP address (optional)
[out]localPortLocal port number (optional)
Returns
Error code

Definition at line 1953 of file socket.c.

◆ socketGetMulticastSourceFilter()

error_t socketGetMulticastSourceFilter ( Socket socket,
const IpAddr groupAddr,
IpFilterMode filterMode,
IpAddr sources,
uint_t numSources 
)

Get multicast source filter.

Parameters
[in]socketHandle to a socket
[in]groupAddrIP address identifying a multicast group
[out]filterModeMulticast filter mode (include or exclude)
[out]sourcesIP addresses of sources to include or exclude depending on the filter mode
[out]numSourcesNumber of source addresses in the list
Returns
Error code

Definition at line 679 of file socket.c.

◆ socketGetRemoteAddr()

error_t socketGetRemoteAddr ( Socket socket,
IpAddr remoteIpAddr,
uint16_t *  remotePort 
)

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

Parameters
[in]socketHandle that identifies a socket
[out]remoteIpAddrIP address of the remote host (optional)
[out]remotePortRemote port number (optional)
Returns
Error code

Definition at line 1985 of file socket.c.

◆ socketInit()

error_t socketInit ( void  )

Socket related initialization.

Returns
Error code

Definition at line 85 of file socket.c.

◆ socketJoinMulticastGroup()

error_t socketJoinMulticastGroup ( Socket socket,
const IpAddr groupAddr 
)

Join the specified host group.

Parameters
[in]socketHandle to a socket
[in]groupAddrIP address identifying the host group to join
Returns
Error code

Definition at line 426 of file socket.c.

◆ socketLeaveMulticastGroup()

error_t socketLeaveMulticastGroup ( Socket socket,
const IpAddr groupAddr 
)

Leave the specified host group.

Parameters
[in]socketHandle to a socket
[in]groupAddrIP address identifying the host group to leave
Returns
Error code

Definition at line 494 of file socket.c.

◆ socketListen()

error_t socketListen ( Socket socket,
uint_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]socketSocket to place in the listening state
[in]backlogbacklog The maximum length of the pending connection queue. If this parameter is zero, then the default backlog value is used instead
Returns
Error code

Definition at line 1413 of file socket.c.

◆ socketOpen()

Socket* socketOpen ( uint_t  type,
uint_t  protocol 
)

Create a socket (UDP or TCP)

Parameters
[in]typeType specification for the new socket
[in]protocolProtocol to be used
Returns
Handle referencing the new socket

Definition at line 125 of file socket.c.

◆ socketPoll()

error_t socketPoll ( SocketEventDesc eventDesc,
uint_t  size,
OsEvent extEvent,
systime_t  timeout 
)

Wait for one of a set of sockets to become ready to perform I/O.

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

Parameters
[in,out]eventDescSet of entries specifying the events the user is interested in
[in]sizeNumber of entries in the descriptor set
[in]extEventExternal event that can abort the wait if necessary (optional)
[in]timeoutMaximum time to wait before returning
Returns
Error code

Definition at line 2149 of file socket.c.

◆ socketReceive()

error_t socketReceive ( Socket socket,
void *  data,
size_t  size,
size_t *  received,
uint_t  flags 
)

Receive data from a connected socket.

Parameters
[in]socketHandle that identifies a connected socket
[out]dataBuffer where to store the incoming data
[in]sizeMaximum number of bytes that can be received
[out]receivedNumber of bytes that have been received
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 1692 of file socket.c.

◆ socketReceiveEx()

error_t socketReceiveEx ( Socket socket,
IpAddr srcIpAddr,
uint16_t *  srcPort,
IpAddr destIpAddr,
void *  data,
size_t  size,
size_t *  received,
uint_t  flags 
)

Receive a datagram.

Parameters
[in]socketHandle that identifies a socket
[out]srcIpAddrSource IP address (optional)
[out]srcPortSource port number (optional)
[out]destIpAddrDestination IP address (optional)
[out]dataBuffer where to store the incoming data
[in]sizeMaximum number of bytes that can be received
[out]receivedNumber of bytes that have been received
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 1736 of file socket.c.

◆ socketReceiveFrom()

error_t socketReceiveFrom ( Socket socket,
IpAddr srcIpAddr,
uint16_t *  srcPort,
void *  data,
size_t  size,
size_t *  received,
uint_t  flags 
)

Receive a datagram from a connectionless socket.

Parameters
[in]socketHandle that identifies a socket
[out]srcIpAddrSource IP address (optional)
[out]srcPortSource port number (optional)
[out]dataBuffer where to store the incoming data
[in]sizeMaximum number of bytes that can be received
[out]receivedNumber of bytes that have been received
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 1714 of file socket.c.

◆ socketReceiveMsg()

error_t socketReceiveMsg ( Socket socket,
SocketMsg message,
uint_t  flags 
)

Receive a message from a connectionless socket.

Parameters
[in]socketHandle that identifies a socket
[in,out]messagePointer to the structure describing the message
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 1894 of file socket.c.

◆ socketSend()

error_t socketSend ( Socket socket,
const void *  data,
size_t  length,
size_t *  written,
uint_t  flags 
)

Send data to a connected socket.

Parameters
[in]socketHandle that identifies a connected socket
[in]dataPointer to a buffer containing the data to be transmitted
[in]lengthNumber of data bytes to send
[out]writtenActual number of bytes written (optional parameter)
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 1486 of file socket.c.

◆ socketSendMsg()

error_t socketSendMsg ( Socket socket,
const SocketMsg message,
uint_t  flags 
)

Send a message to a connectionless socket.

Parameters
[in]socketHandle that identifies a socket
[in]messagePointer to the structure describing the message
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 1634 of file socket.c.

◆ socketSendTo()

error_t socketSendTo ( Socket socket,
const IpAddr destIpAddr,
uint16_t  destPort,
const void *  data,
size_t  length,
size_t *  written,
uint_t  flags 
)

Send a datagram to a specific destination.

Parameters
[in]socketHandle that identifies a socket
[in]destIpAddrIP address of the target host
[in]destPortTarget port number
[in]dataPointer to a buffer containing the data to be transmitted
[in]lengthNumber of data bytes to send
[out]writtenActual number of bytes written (optional parameter)
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 1507 of file socket.c.

◆ socketSetDscp()

error_t socketSetDscp ( Socket socket,
uint8_t  dscp 
)

Set DSCP field.

Parameters
[in]socketHandle to a socket
[in]dscpDifferentiated services codepoint
Returns
Error code

Definition at line 223 of file socket.c.

◆ socketSetInterface()

error_t socketSetInterface ( Socket socket,
NetInterface interface 
)

Bind a socket to a particular network interface.

Parameters
[in]socketHandle to a socket
[in]interfaceNetwork interface to be used
Returns
Error code

Definition at line 1273 of file socket.c.

◆ socketSetKeepAliveParams()

error_t socketSetKeepAliveParams ( Socket socket,
systime_t  idle,
systime_t  interval,
uint_t  maxProbes 
)

Set TCP keep-alive parameters.

Parameters
[in]socketHandle to a socket
[in]idleTime interval between last data packet sent and first keep-alive probe
[in]intervalTime interval between subsequent keep-alive probes
[in]maxProbesNumber of unacknowledged keep-alive probes to send before considering the connection is dead
Returns
Error code

Definition at line 1122 of file socket.c.

◆ socketSetMaxSegmentSize()

error_t socketSetMaxSegmentSize ( Socket socket,
size_t  mss 
)

Specify the maximum segment size for outgoing TCP packets.

Parameters
[in]socketHandle to a socket
[in]mssMaximum segment size, in bytes
Returns
Error code

Definition at line 1164 of file socket.c.

◆ socketSetMulticastSourceFilter()

error_t socketSetMulticastSourceFilter ( Socket socket,
const IpAddr groupAddr,
IpFilterMode  filterMode,
const IpAddr sources,
uint_t  numSources 
)

Set multicast source filter (full-state API)

Parameters
[in]socketHandle to a socket
[in]groupAddrIP address identifying a multicast group
[in]filterModeMulticast filter mode (include or exclude)
[in]sourcesIP addresses of sources to include or exclude depending on the filter mode
[in]numSourcesNumber of source addresses in the list
Returns
Error code

Definition at line 562 of file socket.c.

◆ socketSetMulticastTtl()

error_t socketSetMulticastTtl ( Socket socket,
uint8_t  ttl 
)

Set TTL value for multicast datagrams.

Parameters
[in]socketHandle to a socket
[in]ttlTime-to-live value
Returns
Error code

Definition at line 198 of file socket.c.

◆ socketSetRxBufferSize()

error_t socketSetRxBufferSize ( Socket socket,
size_t  size 
)

Specify the size of the TCP receive buffer.

Parameters
[in]socketHandle to a socket
[in]sizeDesired buffer size, in bytes
Returns
Error code

Definition at line 1237 of file socket.c.

◆ socketSetTimeout()

error_t socketSetTimeout ( Socket socket,
systime_t  timeout 
)

Set timeout value for blocking operations.

Parameters
[in]socketHandle to a socket
[in]timeoutMaximum time to wait
Returns
Error code

Definition at line 148 of file socket.c.

◆ socketSetTtl()

error_t socketSetTtl ( Socket socket,
uint8_t  ttl 
)

Set TTL value for unicast datagrams.

Parameters
[in]socketHandle to a socket
[in]ttlTime-to-live value
Returns
Error code

Definition at line 173 of file socket.c.

◆ socketSetTxBufferSize()

error_t socketSetTxBufferSize ( Socket socket,
size_t  size 
)

Specify the size of the TCP send buffer.

Parameters
[in]socketHandle to a socket
[in]sizeDesired buffer size, in bytes
Returns
Error code

Definition at line 1201 of file socket.c.

◆ socketSetVlanDei()

error_t socketSetVlanDei ( Socket socket,
bool_t  dei 
)

Set VLAN DEI flag.

Parameters
[in]socketHandle to a socket
[in]deiDrop eligible indicator
Returns
Error code

Definition at line 289 of file socket.c.

◆ socketSetVlanPcp()

error_t socketSetVlanPcp ( Socket socket,
uint8_t  pcp 
)

Set VLAN priority.

Parameters
[in]socketHandle to a socket
[in]pcpVLAN priority value
Returns
Error code

Definition at line 252 of file socket.c.

◆ socketSetVmanDei()

error_t socketSetVmanDei ( Socket socket,
bool_t  dei 
)

Set VMAN DEI flag.

Parameters
[in]socketHandle to a socket
[in]deiDrop eligible indicator
Returns
Error code

Definition at line 359 of file socket.c.

◆ socketSetVmanPcp()

error_t socketSetVmanPcp ( Socket socket,
uint8_t  pcp 
)

Set VMAN priority.

Parameters
[in]socketHandle to a socket
[in]pcpVLAN priority value
Returns
Error code

Definition at line 322 of file socket.c.

◆ socketShutdown()

error_t socketShutdown ( Socket socket,
uint_t  how 
)

Disable reception, transmission, or both.

Note that socketShutdown() does not close the socket, and resources attached to the socket will not be freed until socketClose() is invoked

Parameters
[in]socketHandle to a socket
[in]howFlag that describes what types of operation will no longer be allowed
Returns
Error code

Definition at line 2020 of file socket.c.

◆ socketUnblockMulticastSource()

error_t socketUnblockMulticastSource ( Socket socket,
const IpAddr groupAddr,
const IpAddr srcAddr 
)

Unblock specific source for specific group (delta-based API)

Parameters
[in]socketHandle to a socket
[in]groupAddrIP address identifying a multicast group
[in]srcAddrSource IP address to unblock
Returns
Error code

Definition at line 1004 of file socket.c.

Variable Documentation

◆ SOCKET_DEFAULT_MSG

const SocketMsg SOCKET_DEFAULT_MSG
Initial value:
=
{
NULL,
0,
0,
0,
0,
NULL,
{0},
0,
{0},
0,
{{{0}}},
{{{0}}},
0,
0,
-1,
{0},
}

Definition at line 52 of file socket.c.

◆ socketTable

Socket socketTable[SOCKET_MAX_COUNT]

Definition at line 49 of file socket.c.

#define IP_DEFAULT_DF
Definition: ip.h:40