bsd_socket_misc.c
Go to the documentation of this file.
1 /**
2  * @file bsd_socket_misc.c
3  * @brief Helper function for BSD socket API
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2022 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.2.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL BSD_SOCKET_TRACE_LEVEL
33 
34 //Dependencies
35 #include <string.h>
36 #include "core/net.h"
37 #include "core/bsd_socket.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (BSD_SOCKET_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Initializes a descriptor set
46  * @param[in] fds Pointer to a descriptor set
47  **/
48 
49 void socketFdZero(fd_set *fds)
50 {
51  //Reset the descriptor count
52  fds->fd_count = 0;
53 }
54 
55 
56 /**
57  * @brief Add a descriptor to an existing set
58  * @param[in] fds Pointer to a descriptor set
59  * @param[in] s Descriptor that identifies the socket to add
60  **/
61 
63 {
64  int_t i;
65 
66  //Loop through descriptors
67  for(i = 0; i < fds->fd_count; i++)
68  {
69  //The specified descriptor is already set?
70  if(fds->fd_array[i] == s)
71  return;
72  }
73 
74  //Ensure the descriptor set is not full
75  if(i < FD_SETSIZE)
76  {
77  //The specified descriptor can be safely added
78  fds->fd_array[i] = s;
79  //Adjust the size of the descriptor set
80  fds->fd_count++;
81  }
82 }
83 
84 
85 /**
86  * @brief Remove a descriptor from an existing set
87  * @param[in] fds Pointer to a descriptor set
88  * @param[in] s Descriptor that identifies the socket to remove
89  **/
90 
92 {
93  int_t i;
94  int_t j;
95 
96  //Loop through descriptors
97  for(i = 0; i < fds->fd_count; i++)
98  {
99  //Specified descriptor found?
100  if(fds->fd_array[i] == s)
101  {
102  //Adjust the size of the descriptor set
103  fds->fd_count--;
104 
105  //Remove the entry from the descriptor set
106  for(j = i; j < fds->fd_count; j++)
107  {
108  fds->fd_array[j] = fds->fd_array[j + 1];
109  }
110 
111  //Return immediately
112  return;
113  }
114  }
115 }
116 
117 
118 /**
119  * @brief Check whether a descriptor is set
120  * @param[in] fds Pointer to a descriptor set
121  * @param[in] s Descriptor that identifies the socket to test
122  * @return Nonzero if s is a member of the set. Otherwise, zero
123  **/
124 
126 {
127  int_t i;
128 
129  //Loop through descriptors
130  for(i = 0; i < fds->fd_count; i++)
131  {
132  //Check whether the specified descriptor is set
133  if(fds->fd_array[i] == s)
134  {
135  return TRUE;
136  }
137  }
138 
139  //The specified descriptor is not currently set
140  return FALSE;
141 }
142 
143 
144 /**
145  * @brief Set BSD error code
146  * @param[in] socket Handle that identifies a socket
147  * @param[in] errnoCode BSD error code
148  **/
149 
151 {
152  //Valid socket handle?
153  if(socket != NULL)
154  {
155  //Save error code
156  socket->errnoCode = errnoCode;
157  }
158 
159  //Save the code of the last error
160  BSD_SOCKET_SET_ERRNO(errnoCode);
161 }
162 
163 
164 /**
165  * @brief Translate error code
166  * @param[in] socket Handle that identifies a socket
167  * @param[in] errorCode Error code
168  **/
169 
171 {
172  uint_t errnoCode;
173 
174  //Translate error code
175  switch(errorCode)
176  {
177  case NO_ERROR:
178  errnoCode = 0;
179  break;
180 
181  case ERROR_TIMEOUT:
182  errnoCode = EWOULDBLOCK;
183  break;
184 
186  errnoCode = EINVAL;
187  break;
188 
190  errnoCode = ECONNRESET;
191  break;
192 
194  errnoCode = EISCONN;
195  break;
196 
197  case ERROR_NOT_CONNECTED:
198  errnoCode = ENOTCONN;
199  break;
200 
202  errnoCode = ESHUTDOWN;
203  break;
204 
206  errnoCode = ECONNREFUSED;
207  break;
208 
209  default:
210  errnoCode = EFAULT;
211  break;
212  }
213 
214  //Valid socket handle?
215  if(socket != NULL)
216  {
217  //Save error code
218  socket->errnoCode = errnoCode;
219  }
220 
221  //Save the code of the last error
222  BSD_SOCKET_SET_ERRNO(errnoCode);
223 }
224 
225 #endif
#define ESHUTDOWN
Definition: bsd_socket.h:224
signed int int_t
Definition: compiler_port.h:44
void socketFdClr(fd_set *fds, int_t s)
Remove a descriptor from an existing set.
#define EISCONN
Definition: bsd_socket.h:222
Set of sockets.
Definition: bsd_socket.h:324
#define TRUE
Definition: os_port.h:50
@ ERROR_ALREADY_CONNECTED
Definition: error.h:81
@ ERROR_CONNECTION_RESET
Definition: error.h:78
void socketFdSet(fd_set *fds, int_t s)
Add a descriptor to an existing set.
#define EINVAL
Definition: bsd_socket.h:216
uint16_t errorCode
Definition: tftp_common.h:136
#define BSD_SOCKET_SET_ERRNO(e)
Definition: bsd_socket.h:50
#define EWOULDBLOCK
Definition: bsd_socket.h:214
#define FALSE
Definition: os_port.h:46
void socketTranslateErrorCode(Socket *socket, error_t errorCode)
Translate error code.
int_t fd_count
Definition: bsd_socket.h:325
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:63
void socketSetErrnoCode(Socket *socket, uint_t errnoCode)
Set BSD error code.
#define ENOTCONN
Definition: bsd_socket.h:223
BSD socket API.
int_t fd_array[FD_SETSIZE]
Definition: bsd_socket.h:326
void socketFdZero(fd_set *fds)
Initializes a descriptor set.
@ ERROR_CONNECTION_CLOSING
Definition: error.h:77
#define EFAULT
Definition: bsd_socket.h:215
@ ERROR_TIMEOUT
Definition: error.h:94
@ ERROR_NOT_CONNECTED
Definition: error.h:79
#define ECONNREFUSED
Definition: bsd_socket.h:225
#define Socket
Definition: socket.h:36
int_t socketFdIsSet(fd_set *fds, int_t s)
Check whether a descriptor is set.
#define ECONNRESET
Definition: bsd_socket.h:221
uint8_t s
@ ERROR_CONNECTION_FAILED
Definition: error.h:75
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
#define FD_SETSIZE
Definition: bsd_socket.h:43
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.