SimpleLink CC3120/CC3220 Host Driver  Version 2.0.1.22
Simplifies the implementation of Internet connectivity
BSD_Socket

Controls standard client/server sockets programming options and capabilities. More...

Functions

struct hostentgethostbyname (const char *name)
 Get host IP by name
Obtain the IP Address of machine on network, by machine name. More...
 
static int select (int nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds, struct timeval *timeout)
 Monitor socket activity. More...
 
int socket (int Domain, int Type, int Protocol)
 Create an endpoint for communication. More...
 
static int close (int sd)
 Gracefully close socket. More...
 
int accept (int sd, sockaddr *addr, socklen_t *addrlen)
 Accept a connection on a socket. More...
 
int bind (int sd, const sockaddr *addr, socklen_t addrlen)
 Assign a name to a socket. More...
 
int listen (int sd, int backlog)
 Listen for connections on a socket. More...
 
int connect (int sd, const sockaddr *addr, socklen_t addrlen)
 Initiate a connection on a socket. More...
 
int setsockopt (int sd, int level, int optname, const void *optval, socklen_t optlen)
 Set socket options-. More...
 
int getsockopt (int sd, int level, int optname, void *optval, socklen_t *optlen)
 Get socket options. More...
 
ssize_t recv (int sd, void *pBuf, size_t Len, int flags)
 Read data from TCP socket. More...
 
ssize_t recvfrom (int sd, void *buf, _i16 Len, _i16 flags, sockaddr *from, socklen_t *fromlen)
 Read data from socket. More...
 
ssize_t send (int sd, const void *pBuf, _i16 Len, _i16 flags)
 Write data to TCP socket. More...
 
ssize_t sendto (int sd, const void *pBuf, size_t Len, int flags, const sockaddr *to, socklen_t tolen)
 Write data to socket. More...
 

Typedefs

typedef struct hostent hostent_t
 

Detailed Description

Controls standard client/server sockets programming options and capabilities.

Function Documentation

§ accept()

int accept ( int  sd,
sockaddr *  addr,
socklen_t *  addrlen 
)

Accept a connection on a socket.

This function is used with connection-based socket types (SOCK_STREAM).
It extracts the first connection request on the queue of pending connections, creates a new connected socket, and returns a new file descriptor referring to that socket.
The newly created socket is not in the listening state. The original socket sd is unaffected by this call.
The argument sd is a socket that has been created with socket(), bound to a local address with bind(), and is listening for connections after a listen(). The argument addr is a pointer to a sockaddr structure. This structure is filled in with the address of the peer socket, as known to the communications layer. The exact format of the address returned addr is determined by the socket's address family.
The addrlen argument is a value-result argument: it should initially contain the size of the structure pointed to by addr, on return it will contain the actual length (in bytes) of the address returned.

Parameters
[in]sdSocket descriptor (handle)
[out]addrThe argument addr is a pointer to a sockaddr structure. This structure is filled in with the address of the peer socket, as known to the communications layer. The exact format of the address returned addr is determined by the socket's address
sockaddr:
- code for the address format. On this version only AF_INET is supported.
- socket address, the length depends on the code format
[out]addrlenThe addrlen argument is a value-result argument: it should initially contain the size of the structure pointed to by addr
Returns
On success, a socket handle.
On a non-blocking accept a possible retrun is -1 and errno is set to EAGAIN.
On failure, errno is set and -1 is returned.
ENOMEM may be return in case there are no resources in the system In this case try again later or increase MAX_CONCURRENT_ACTIONS.
See also
socket bind listen
Note
Belongs to server_side
Warning

Definition at line 130 of file socket.c.

131 {
132  int RetVal = (int)sl_Accept(sd, addr, addrlen);
133  return _SlDrvSetErrno(RetVal);
134 }
_i16 sl_Accept(_i16 sd, SlSockAddr_t *addr, SlSocklen_t *addrlen)
Accept a connection on a socket.
Definition: sl_socket.c:708

§ bind()

int bind ( int  sd,
const sockaddr *  addr,
socklen_t  addrlen 
)

Assign a name to a socket.

This function gives the socket the local address addr. addr is addrlen bytes long. Traditionally, this is called When a socket is created with socket, it exists in a name space (address family) but has no name assigned. It is necessary to assign a local address before a SOCK_STREAM socket may receive connections.

Parameters
[in]sdSocket descriptor (handle)
[in]addrSpecifies the destination addrs
sockaddr:
- code for the address format. On this version only AF_INET is supported.
- socket address, the length depends on the code format.
[in]addrlenContains the size of the structure pointed to by addr
Returns
Zero on success, or -1 on failure and sets errno to the corresponding BDS error code.
See also
socket accept listen
Note
belongs to basic_api
Warning

Definition at line 63 of file socket.c.

64 {
65  int RetVal = (int)sl_Bind(sd, addr, addrlen);
66  return _SlDrvSetErrno(RetVal);
67 }
_i16 sl_Bind(_i16 sd, const SlSockAddr_t *addr, _i16 addrlen)
Assign a name to a socket.
Definition: sl_socket.c:247

§ close()

static int close ( int  sd)
inlinestatic

Gracefully close socket.

This function causes the system to release resources allocated to a socket.
In case of TCP, the connection is terminated.

Parameters
[in]sdSocket handle (received in socket)
Returns
Zero on success, or negative error code on failure
See also
socket
Note
belongs to ext_api
Warning

Definition at line 162 of file socket.h.

163 {
164  int RetVal = (int)sl_Close((_i16)sd);
165  return _SlDrvSetErrno(RetVal);
166 }
_i16 sl_Close(_i16 sd)
Gracefully close socket.
Definition: sl_socket.c:187

§ connect()

int connect ( int  sd,
const sockaddr *  addr,
socklen_t  addrlen 
)

Initiate a connection on a socket.

Function connects the socket referred to by the socket descriptor sd, to the address specified by addr. The addrlen argument specifies the size of addr. The format of the address in addr is determined by the address space of the socket. If it is of type SOCK_DGRAM, this call specifies the peer with which the socket is to be associated; this address is that to which datagrams are to be sent, and the only address from which datagrams are to be received. If the socket is of type SOCK_STREAM, this call attempts to make a connection to another socket. The other socket is specified by address, which is an address in the communications space of the socket.

Parameters
[in]sdSocket descriptor (handle)
[in]addrSpecifies the destination addr
sockaddr:
- code for the address format. On this version only AF_INET is supported.
- socket address, the length depends on the code format
[in]addrlenContains the size of the structure pointed to by addr
Returns
On success, a socket handle.
On a non-blocking connect a possible negative value is EALREADY. On failure, -1 is returned and sets errno to the corresponding BDS error code.
ENOMEM may be return in case there are no resources in the system In this case try again later or increase MAX_CONCURRENT_ACTIONS
See also
sl_Socket
Note
belongs to client_side
Warning

Definition at line 97 of file socket.c.

98 {
99  int RetVal = (int)sl_Connect(sd, addr, addrlen);
100  return _SlDrvSetErrno(RetVal);
101 }
_i16 sl_Connect(_i16 sd, const SlSockAddr_t *addr, _i16 addrlen)
Initiate a connection on a socket.
Definition: sl_socket.c:464

§ gethostbyname()

struct hostent* gethostbyname ( const char *  name)

Get host IP by name
Obtain the IP Address of machine on network, by machine name.

Parameters
[in]constchar *name Host name
Returns
Struct hostent containing the answer on success or NULL failure.
See also
sl_NetAppDnsGetHostByName
Note
Note: The function isn't reentrant! It utilize a static hostent struct which holds the answer for the DNS query. Calling this function form several threads may result in invalid answers. A user interested in a reentrant function which resolves IP address by name, can use the SimpleLink API: 'sl_NetAppDnsGetHostByName'. Another option is to protect this call with a lock and copy the returned hostent struct to a user buffer, before unlocking.
Warning
The parameter 'name' is assumed to be allocated by the user, and it's the user's Responsibility to maintain it's validity. This field is copied (not deep copied) to the struct returned by this function.
Example
  • Getting host by name:
    struct sockaddr_in sa;
    struct hostent *host_addr;
    host_addr = gethostbyname("www.cloudprovider.com");
    if(!host_addr)
    {
    sa.sin_family = host_addr->h_addrtype;
    memcpy(&sa.sin_addr.s_addr, host_addr->h_addr_list, host_addr->h_length);
    sa.sin_port = htons(80);
    }
    connect(Socketfd, (struct sockaddr *)&sa, sizeof(sa));

Definition at line 50 of file netdb.c.

51 {
52  static struct hostent Hostent;
53  static char* AddrArray[2];
54  static char Addr[IPv6_ADDR_LEN];
55 
56  int RetVal = 0;
57 
58  /* Clear the reused buffer */
59  _SlDrvMemZero(&Hostent, sizeof(struct hostent));
60  _SlDrvMemZero(&AddrArray, sizeof(AddrArray));
61  _SlDrvMemZero(&Addr, sizeof(Addr));
62 
63  /* Set the host name */
64  Hostent.h_name = (char*)name;
65  /* Query DNS for IPv4 address. */
66  RetVal = sl_NetAppDnsGetHostByName((signed char*)name , (_u16)strlen(name), (unsigned long*)(&Addr), AF_INET);
67 
68  if(RetVal < 0)
69  {
70  /* If call fails, try again for IPv6. */
71  RetVal = sl_NetAppDnsGetHostByName((signed char*)name , (_u16)strlen(name), (unsigned long*)(&Addr), AF_INET6);
72  if(RetVal < 0)
73  {
74  /* if the request failed twice, there's an error - return NULL. */
75  return NULL;
76  }
77  else
78  {
79  /* fill the answer fields */
80  Hostent.h_addrtype = AF_INET6 ;
81  Hostent.h_length = IPv6_ADDR_LEN;
82  }
83  }
84  else
85  {
86  /* fill the answer fields */
87  Hostent.h_addrtype = AF_INET ;
88  Hostent.h_length = IPv4_ADDR_LEN;
89  }
90 
91  AddrArray[0] = &Addr[0];
92  Hostent.h_addr_list = AddrArray;
93 
94  /* Return the address of the reused buffer */
95  return (&Hostent);
96 }
_i16 sl_NetAppDnsGetHostByName(_i8 *pHostName, const _u16 NameLen, _u32 *OutIpAddr, const _u8 Family)
Get host IP by name Obtain the IP Address of machine on network, by machine name. ...
Definition: netapp.c:856
Definition: netdb.h:62

§ getsockopt()

int getsockopt ( int  sd,
int  level,
int  optname,
void *  optval,
socklen_t *  optlen 
)

Get socket options.

This function manipulate the options associated with a socket. Options may exist at multiple protocol levels; they are always present at the uppermost socket level.
When manipulating socket options the level at which the option resides and the name of the option must be specified. To manipulate options at the socket level, level is specified as SOL_SOCKET. To manipulate options at any other level the protocol number of the appropriate proto- col controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP;
The parameters optval and optlen are used to access optval - ues for setsockopt(). For getsockopt() they identify a buffer in which the value for the requested option(s) are to be returned. For getsockopt(), optlen is a value-result parameter, initially containing the size of the buffer pointed to by option_value, and modified on return to indicate the actual size of the value returned. If no option value is to be supplied or returned, option_value may be NULL.

Parameters
[in]sdSocket handle
[in]levelDefines the protocol level for this option
[in]optnamedefines the option name to interrogate
[out]optvalSpecifies a value for the option
[out]optlenSpecifies the length of the option value
Returns
Zero on success, or -1 on failure and sets errno to the corresponding BDS error code.
See also
setsockopt Belongs to ext_api
Warning

Definition at line 184 of file socket.c.

185 {
186  switch(optname)
187  {
188  /* This option (TCP_NODELAY) is always set by the NWP, hence we always return true */
189  case TCP_NODELAY:
190  if(optval)
191  {
192  (*(_u32*)optval) = TRUE;
193  return 0;
194  }
195  /* These sock opts Aren't supported by the cc31xx\cc32xx network stack,
196  hence, we silently ignore them and set errno to EINVAL (invalid argument).
197  This is made in order to not break of "off-the-shelf" BSD code. */
198  case SO_BROADCAST:
199  case SO_REUSEADDR:
200  case SO_SNDBUF:
201  return _SlDrvSetErrno(EINVAL);
202  default:
203  break;
204  }
205 
206  int RetVal = (int)sl_GetSockOpt(sd, level, optname, optval, optlen);
207  return _SlDrvSetErrno(RetVal);
208 }
_i16 sl_GetSockOpt(_i16 sd, _i16 level, _i16 optname, void *optval, SlSocklen_t *optlen)
Get socket options.
Definition: sl_socket.c:986

§ listen()

int listen ( int  sd,
int  backlog 
)

Listen for connections on a socket.

The willingness to accept incoming connections and a queue limit for incoming connections are specified with listen(), and then the connections are accepted with accept. The listen() call applies only to sockets of type SOCK_STREAM The backlog parameter defines the maximum length the queue of pending connections may grow to.

Parameters
[in]sdSocket descriptor (handle)
[in]backlogSpecifies the listen queue depth.
Returns
Zero on success, or -1 on failure and sets errno to the corresponding BDS error code.
See also
socket accept bind
Note
Belongs to server_side
Warning

Definition at line 119 of file socket.c.

120 {
121  int RetVal = (int)sl_Listen(sd, backlog);
122  return _SlDrvSetErrno(RetVal);
123 }
_i16 sl_Listen(_i16 sd, _i16 backlog)
Listen for connections on a socket.
Definition: sl_socket.c:672

§ recv()

ssize_t recv ( int  sd,
void *  pBuf,
size_t  Len,
int  flags 
)

Read data from TCP socket.

Function receives a message from a connection-mode socket

Parameters
[in]sdSocket handle
[out]bufPoints to the buffer where the message should be stored.
[in]lenSpecifies the length in bytes of the buffer pointed to by the buffer argument. Range: 1-16000 bytes
[in]flagsSpecifies the type of message reception. On this version, this parameter is not supported.
Returns
Return the number of bytes received, or a -1 if an error occurred. Errno is set accordingly.
Using a non-blocking recv a possible errno value is EAGAIN.
errno may be set to ENOMEM in case there are no resources in the system In this case try again later or increase MAX_CONCURRENT_ACTIONS
See also
sl_RecvFrom
Note
Belongs to recv_api
Warning
Examples
  • Receiving data using TCP socket:
    sockaddr_in Addr;
    sockaddr_in LocalAddr;
    int AddrSize = sizeof(socklen_t);
    int SockID, newSockID;
    int Status;
    char Buf[RECV_BUF_LEN];
    LocalAddr.sin_family = AF_INET;
    LocalAddr.sin_port = htons(5001);
    LocalAddr.sin_addr.s_addr = 0;
    Addr.sin_family = AF_INET;
    Addr.sin_port = htons(5001);
    Addr.sin_addr.s_addr = htonl(SL_IPV4_VAL(10,1,1,200));
    SockID = socket(AF_INET, SOCK_STREAM, 0);
    Status = bind(SockID, (sockaddr *)&LocalAddr, AddrSize);
    Status = listen(SockID, 0);
    newSockID = accept(SockID, (sockaddr *)&Addr, (socklen_t*) &AddrSize);
    Status = recv(newSockID, Buf, 1460, 0);

Definition at line 141 of file socket.c.

142 {
143  ssize_t RetVal = (ssize_t)sl_Recv(sd, pBuf, (_i16)Len, flags);
144  return (ssize_t)(_SlDrvSetErrno(RetVal));
145 }
_i16 sl_Recv(_i16 sd, void *buf, _i16 len, _i16 flags)
Read data from TCP socket.
Definition: sl_socket.c:892

§ recvfrom()

ssize_t recvfrom ( int  sd,
void *  buf,
_i16  Len,
_i16  flags,
sockaddr *  from,
socklen_t *  fromlen 
)

Read data from socket.

Function receives a message from a connection-mode or connectionless-mode socket

Parameters
[in]sdSocket handle
[out]bufPoints to the buffer where the message should be stored.
[in]lenSpecifies the length in bytes of the buffer pointed to by the buffer argument. Range: 1-16000 bytes
[in]flagsSpecifies the type of message reception. On this version, this parameter is not supported.
[in]fromPointer to an address structure indicating the source address.
sockaddr:
- code for the address format. On this version only AF_INET is supported.
- socket address, the length depends on the code format
[in]fromlenSource address structure size. This parameter MUST be set to the size of the structure pointed to by addr.
Returns
Return the number of bytes received, or a -1 if an error occurred. Errno is set accordingly.
Using a non-blocking recv a possible errno value is EAGAIN.
errno will be set to EINVAL if fromlen has incorrect length.
errno may be set to ENOMEM in case there are no resources in the system In this case try again later or increase MAX_CONCURRENT_ACTIONS
See also
recv
Note
Belongs to recv_api
Warning
Example
  • Receiving data:
    sockaddr_in Addr;
    sockaddr_in LocalAddr;
    int AddrSize = sizeof(socklen_t);
    int SockID, newSockID;
    int Status;
    char Buf[RECV_BUF_LEN];
    LocalAddr.sin_family = AF_INET;
    LocalAddr.sin_port = htons(5001);
    LocalAddr.sin_addr.s_addr = 0;
    SockID = socket(AF_INET, SOCK_STREAM, 0);
    Status = bind(SockID, (sockaddr *)&LocalAddr, AddrSize);
    Status = recvfrom(SockID, Buf, 1472, 0, (sockaddr_in *)&Addr, (socklen_t*)&AddrSize);

Definition at line 86 of file socket.c.

87 {
88  ssize_t RetVal = (ssize_t)sl_RecvFrom(sd, buf, Len, flags, from, fromlen);
89  return (ssize_t)(_SlDrvSetErrno(RetVal));
90 }
_i16 sl_RecvFrom(_i16 sd, void *buf, _i16 len, _i16 flags, SlSockAddr_t *from, SlSocklen_t *fromlen)
Read data from socket.
Definition: sl_socket.c:375

§ select()

static int select ( int  nfds,
fd_set *  readsds,
fd_set *  writesds,
fd_set *  exceptsds,
struct timeval *  timeout 
)
inlinestatic

Monitor socket activity.

Select allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation.

Parameters
[in]nfdsThe highest-numbered file descriptor in any of the three sets, plus 1.
[out]readsdsSocket descriptors list for read monitoring and accept monitoring
[out]writesdsSocket descriptors list for connect monitoring only, write monitoring is not supported
[out]exceptsdsSocket descriptors list for exception monitoring, not supported.
[in]timeoutIs an upper bound on the amount of time elapsed before select() returns. Null or above 0xffff seconds means infinity timeout. The minimum timeout is 10 milliseconds, less than 10 milliseconds will be set automatically to 10 milliseconds. Max microseconds supported is 0xfffc00. In trigger mode the timout fields must be set to zero.
Returns
On success, select() returns the number of file descriptors contained in the three returned descriptor sets (that is, the total number of bits that are set in readfds, writefds, exceptfds) which may be zero if the timeout expires before anything interesting happens.
On error, a negative value is returned. readsds - return the sockets on which read request will return without delay with valid data.
writesds - return the sockets on which write request will return without delay.
exceptsds - return the sockets closed recently. (not supported)
ENOMEM may be return in case there are no resources in the system In this case try again later or increase MAX_CONCURRENT_ACTIONS
See also
sl_Socket
Note
If the timeout value set to less than 10ms it will automatically set to 10ms to prevent overload of the system
Belongs to basic_api

Several threads can call select at the same time.Calling this API while the same command is called from another thread, may result in one of the following scenarios:

  1. The command will be executed alongside other select callers (success).
  2. The command will wait (internal) until the previous select finish, and then be executed.
  3. There are not enough resources and SL_POOL_IS_EMPTY error will return. In this case, ENOMEM can be increased (result in memory increase) or try again later to issue the command.

In case all the user sockets are open, select will exhibit the behaviour mentioned in (2) This is due to the fact select supports multiple callers by utilizing one user socket internally. User who wish to ensure multiple select calls at any given time, must reserve one socket out of the 16 given.

Warning
Example
  • Monitoring two sockets:
int socketFD1;
int socketFD2;
fd_set readfds;
struct timeval tv;
// go and connect to both servers
socketFD1 = socket(...);
socketFD2 = socket(...);
connect(socketFD1, ...)...
connect(socketFD2, ...)...
// clear the set ahead of time
FD_ZERO(&readfds);
// add our descriptors to the set
FD_SET(socketFD1, &readfds);
FD_SET(socketFD2, &readfds);
// since we opened socketFD2 second, it's the "greater", so we use that for
// the nfds param in select();
n = socketFD2 + 1;
// wait until either socket has data ready to be recv() (we set timeout to 10.5 Sec)
tv.tv_sec = 10;
tv.tv_usec = 500000;
retVal = select(n, &readfds, NULL, NULL, &tv);
if (retVal == -1)
{
printf("error: %d in select().\n", errno);
}
else if (retVal == 0)
{
printf("Timeout occurred! No data after 10.5 seconds.\n");
}
else
{
// one or both of the descriptors have data: go ahead and read it.
if (FD_ISSET(socketFD1, &readfds))
{
recv(socketFD1, buf1, sizeof buf1, 0);
}
if (FD_ISSET(socketFD2, &readfds))
{
recv(socketFD2, buf2, sizeof buf2, 0);
}
}

Definition at line 185 of file select.h.

186 {
187  int RetVal = (int)sl_Select(nfds, readsds, writesds, exceptsds, timeout);
188  return _SlDrvSetErrno(RetVal);
189 }
_i16 sl_Select(_i16 nfds, SlFdSet_t *readsds, SlFdSet_t *writesds, SlFdSet_t *exceptsds, struct SlTimeval_t *timeout)
Monitor socket activity.
Definition: sl_socket.c:1728

§ send()

ssize_t send ( int  sd,
const void *  pBuf,
_i16  Len,
_i16  flags 
)

Write data to TCP socket.

This function is used to transmit a message to another socket. Returns immediately after sending data to device. In case of TCP failure an async event SL_SOCKET_TX_FAILED_EVENT is going to be received.

Parameters
[in]sdSocket handle
[in]bufPoints to a buffer containing the message to be sent
[in]lenMessage size in bytes. Range: 1-1460 bytes
[in]flagsSpecifies the type of message transmission. On this version, this parameter is not supported for TCP.
Returns
Zero on success, or -1 on failure and sets errno to the corresponding BDS error code.
See also
sl_SendTo
Note
Belongs to send_api
Warning
Example
  • Sending data:
    sockaddr_in Addr;
    int AddrSize = sizeof(socklen_t);
    int SockID;
    int Status;
    char Buf[SEND_BUF_LEN];
    Addr.sin_family = AF_INET;
    Addr.sin_port = htons(5001);
    Addr.sin_addr.s_addr = htonl(SL_IPV4_VAL(10,1,1,200));
    SockID = socket(AF_INET, SOCK_STREAM, 0);
    Status = connect(SockID, (sockaddr_in*)&Addr, AddrSize);
    Status = send(SockID, Buf, 1460, 0 );

Definition at line 108 of file socket.c.

109 {
110  ssize_t RetVal = (ssize_t)sl_Send(sd, pBuf, Len, flags);
111  return (ssize_t)(_SlDrvSetErrno(RetVal));
112 }
_i16 sl_Send(_i16 sd, const void *buf, _i16 len, _i16 flags)
Write data to TCP socket.
Definition: sl_socket.c:606

§ sendto()

ssize_t sendto ( int  sd,
const void *  pBuf,
size_t  Len,
int  flags,
const sockaddr *  to,
socklen_t  tolen 
)

Write data to socket.

This function is used to transmit a message to another socket (connection less socket SOCK_DGRAM, SOCK_RAW).
Returns immediately after sending data to device.
In case of transmission failure an async event SL_SOCKET_TX_FAILED_EVENT is going to be received.

Parameters
[in]sdSocket handle
[in]bufPoints to a buffer containing the message to be sent
[in]lenmessage size in bytes. Range: 1-1460 bytes
[in]flagsSpecifies the type of message transmission. On this version, this parameter is not supported
[in]toPointer to an address structure indicating the destination address.
sockaddr:
- code for the address format. On this version only AF_INET is supported.
- socket address, the length depends on the code format
[in]tolenDestination address structure size
Returns
Zero on success, or -1 on failure and sets errno to the corresponding BDS error code.
See also
sl_Send
Note
Belongs to send_api
Warning
Example
  • Sending data:
    sockaddr_in Addr;
    int AddrSize = sizeof(socklen_t);
    int SockID;
    int Status;
    char Buf[SEND_BUF_LEN];
    Addr.sin_family = AF_INET;
    Addr.sin_port = htons(5001);
    Addr.sin_addr.s_addr = htonl(SL_IPV4_VAL(10,1,1,200));
    SockID = socket(AF_INET, SOCK_DGRAM, 0);
    Status = sendto(SockID, Buf, 1472, 0, (sockaddr_in *)&Addr, AddrSize);

Definition at line 75 of file socket.c.

76 {
77  ssize_t RetVal = (ssize_t)sl_SendTo(sd, pBuf, (_i16)Len, flags, to, tolen);
78  return (ssize_t)(_SlDrvSetErrno(RetVal));
79 }
_i16 sl_SendTo(_i16 sd, const void *buf, _i16 len, _i16 flags, const SlSockAddr_t *to, SlSocklen_t tolen)
Write data to socket.
Definition: sl_socket.c:296

§ setsockopt()

int setsockopt ( int  sd,
int  level,
int  optname,
const void *  optval,
socklen_t  optlen 
)

Set socket options-.

This function manipulate the options associated with a socket.
Options may exist at multiple protocol levels; they are always present at the uppermost socket level.
When manipulating socket options the level at which the option resides and the name of the option must be specified. To manipulate options at the socket level, level is specified as SOL_SOCKET. To manipulate options at any other level the protocol number of the appropriate proto- col controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP;
The parameters optval and optlen are used to access optval - ues for setsockopt(). For getsockopt() they identify a buffer in which the value for the requested option(s) are to be returned. For getsockopt(), optlen is a value-result parameter, initially containing the size of the buffer pointed to by option_value, and modified on return to indicate the actual size of the value returned. If no option value is to be supplied or returned, option_value may be NULL.

Parameters
[in]sdSocket handle
[in]levelDefines the protocol level for this option
  • SOL_SOCKET Socket level configurations (L4, transport layer)
  • IPPROTO_IP IP level configurations (L3, network layer)
[in]optnameDefines the option name to interrogate
  • SOL_SOCKET
  • SO_KEEPALIVE
    Enable/Disable periodic keep alive. Keeps TCP connections active by enabling the periodic transmission of messages
    Timeout is 5 minutes.
    Default: Enabled
    This options takes SlSockKeepalive_t struct as parameter
    • SO_KEEPALIVETIME
      Set keep alive timeout. Value is in seconds
      Default: 5 minutes
    • SO_RX_NO_IP_BOUNDARY
      Enable/Disable rx ip boundary. In connectionless socket (udp/raw), unread data is dropped (when recvfrom len parameter < data size), Enable this option in order to read the left data on the next recvfrom iteration Default: Disabled, IP boundary kept,
      This options takes SlSockRxNoIpBoundary_t struct as parameter
    • SO_RCVTIMEO
      Sets the timeout value that specifies the maximum amount of time an input function waits until it completes.
      Default: No timeout
      This options takes SlTimeval_t struct as parameter
    • SO_RCVBUF
      Sets tcp max recv window size.
      This options takes SlSockWinsize_t struct as parameter
    • SO_NONBLOCKING
      Sets socket to non-blocking operation Impacts: connect, accept, send, sendto, recv and recvfrom.
      Default: Blocking. This options takes SlSockNonblocking_t struct as parameter
  • IPPROTO_IP
    • IP_MULTICAST_TTL
      Set the time-to-live value of outgoing multicast packets for this socket.
      This options takes _u8 as parameter
    • IP_ADD_MEMBERSHIP
      UDP socket, Join a multicast group.
      This options takes SlSockIpMreq_t struct as parameter
    • IP_DROP_MEMBERSHIP
      UDP socket, Leave a multicast group
      This options takes SlSockIpMreq_t struct as parameter
    • SO_LINGER
      Socket lingers on close pending remaining send/receive packetst
[in]optvalSpecifies a value for the option
[in]optlenSpecifies the length of the option value
Returns
Zero on success, or -1 on failure and sets errno to the corresponding BDS error code.
Persistent
All params are Non- Persistent
See also
getsockopt
Note
Belongs to basic_api
Warning

Definition at line 152 of file socket.c.

153 {
154  switch(optname)
155  {
156  /* This option (TCP_NODELAY) is always set by the NWP, setting it to true always success. */
157  case TCP_NODELAY:
158  if(optval)
159  {
160  /* if user wish to have TCP_NODELAY = FALSE, we return EINVAL and failure,
161  in the cases below. */
162  if(*(_u32*)optval){ return 0; }
163  }
164  /* These sock opts Aren't supported by the cc31xx\cc32xx network stack,
165  hence, we silently ignore them and set errno to EINVAL (invalid argument).
166  This is made in order to not break of "off-the-shelf" BSD code. */
167  case SO_BROADCAST:
168  case SO_REUSEADDR:
169  case SO_SNDBUF:
170  return _SlDrvSetErrno(EINVAL);
171  default:
172  break;
173  }
174 
175  int RetVal = (int)sl_SetSockOpt(sd, level, optname, optval, optlen);
176  return _SlDrvSetErrno(RetVal);
177 }
_i16 sl_SetSockOpt(_i16 sd, _i16 level, _i16 optname, const void *optval, SlSocklen_t optlen)
Set socket options-.
Definition: sl_socket.c:943

§ socket()

int socket ( int  Domain,
int  Type,
int  Protocol 
)

Create an endpoint for communication.

The socket function creates a new socket of a certain socket type, identified by an integer number, and allocates system resources to it.
This function is called by the application layer to obtain a socket handle.

Parameters
[in]DomainSpecifies the protocol family of the created socket. For example:
  • AF_INET for network protocol IPv4
  • AF_INET6 for network protocol IPv6
[in]Typespecifies the communication semantic, one of:
  • SOCK_STREAM (reliable stream-oriented service or Stream Sockets)
  • SOCK_DGRAM (datagram service or Datagram Sockets)
  • SOCK_RAW (raw protocols atop the network layer)
[in]Protocolspecifies a particular transport to be used with the socket.
The most common are
  • IPPROTO_TCP
  • IPPROTO_UDP The value 0 may be used to select a default protocol from the selected domain and type
Returns
On success, socket handle that is used for consequent socket operations.
A successful return code should be a positive number (int16)
On error, a negative (int16) value will be returned specifying the error code.
  • EAFNOSUPPORT - illegal domain parameter
  • EPROTOTYPE - illegal type parameter
  • EACCES - permission denied
  • ENSOCK - exceeded maximal number of socket
  • ENOMEM - memory allocation error
  • EINVAL - error in socket configuration
  • EPROTONOSUPPORT - illegal protocol parameter
  • EOPNOTSUPP - illegal combination of protocol and type parameters
See also
close
Note
belongs to basic_api
Warning

Definition at line 51 of file socket.c.

52 {
53  int RetVal = (int)sl_Socket(Domain, Type, Protocol);
54  return _SlDrvSetErrno(RetVal);
55 }
_i16 sl_Socket(_i16 Domain, _i16 Type, _i16 Protocol)
Create an endpoint for communication.
Definition: sl_socket.c:143

Data Structure Documentation

§ hostent

struct hostent

Definition at line 62 of file netdb.h.

Data Fields
char ** h_addr_list
int h_addrtype
char ** h_aliases
int h_length
char * h_name