socket.c File Reference

Socket API. More...

#include <string.h>
#include "core/net.h"
#include "core/socket.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 "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 socketSetTxBufferSize (Socket *socket, size_t size)
 Specify the size of the send buffer. More...
 
error_t socketSetRxBufferSize (Socket *socket, size_t size)
 Specify the size of the receive buffer. More...
 
error_t socketBindToInterface (Socket *socket, NetInterface *interface)
 Bind a socket to a particular network 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 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 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 socketRegisterEvents (Socket *socket, OsEvent *event, uint_t eventMask)
 Subscribe to the specified socket events. More...
 
error_t socketUnregisterEvents (Socket *socket)
 Unsubscribe previously registered events. More...
 
error_t socketGetEvents (Socket *socket, uint_t *eventFlags)
 Retrieve event flags for a specified socket. 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]
 

Detailed Description

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 socket.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   SOCKET_TRACE_LEVEL

Definition at line 30 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 1049 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 457 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 331 of file socket.c.

◆ socketBindToInterface()

error_t socketBindToInterface ( 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 309 of file socket.c.

◆ socketClose()

void socketClose ( Socket socket)

Close an existing socket.

Parameters
[in]socketHandle identifying the socket to close

Definition at line 797 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 357 of file socket.c.

◆ socketGetEvents()

error_t socketGetEvents ( Socket socket,
uint_t eventFlags 
)

Retrieve event flags for a specified socket.

Parameters
[in]socketHandle that identifies a socket
[out]eventFlagsLogic OR of events in the signaled state
Returns
Error code

Definition at line 1016 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 703 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 730 of file socket.c.

◆ socketInit()

error_t socketInit ( void  )

Socket related initialization.

Returns
Error code

Definition at line 54 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 420 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 92 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.

The socketPoll 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 857 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 584 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 625 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 604 of file socket.c.

◆ socketRegisterEvents()

error_t socketRegisterEvents ( Socket socket,
OsEvent event,
uint_t  eventMask 
)

Subscribe to the specified socket events.

Parameters
[in]socketHandle that identifies a socket
[in]eventEvent object used to receive notifications
[in]eventMaskLogic OR of the requested socket events
Returns
Error code

Definition at line 936 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 490 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 511 of file socket.c.

◆ socketSetRxBufferSize()

error_t socketSetRxBufferSize ( Socket socket,
size_t  size 
)

Specify the size of the receive buffer.

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

Definition at line 275 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 216 of file socket.c.

◆ socketSetTxBufferSize()

error_t socketSetTxBufferSize ( Socket socket,
size_t  size 
)

Specify the size of the send buffer.

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

Definition at line 241 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 760 of file socket.c.

◆ socketUnregisterEvents()

error_t socketUnregisterEvents ( Socket socket)

Unsubscribe previously registered events.

Parameters
[in]socketHandle that identifies a socket
Returns
Error code

Definition at line 990 of file socket.c.

Variable Documentation

◆ socketTable

Socket socketTable[SOCKET_MAX_COUNT]

Definition at line 46 of file socket.c.