* Allocate a new socket for a given netconn. * * @param newconn the netconn for which to allocate a socket * @param accepted 1 if socket has been created by accept(), * 0 if socket has been created by socket() * @return the index of the new socket; -1 on error */
| 252 | * @return the index of the new socket; -1 on error |
| 253 | */ |
| 254 | static int |
| 255 | alloc_socket(struct netconn *newconn, int accepted) |
| 256 | { |
| 257 | int i; |
| 258 | SYS_ARCH_DECL_PROTECT(lev); |
| 259 | |
| 260 | /* allocate a new socket identifier */ |
| 261 | for (i = 0; i < NUM_SOCKETS; ++i) { |
| 262 | /* Protect socket array */ |
| 263 | SYS_ARCH_PROTECT(lev); |
| 264 | if (!sockets[i].conn) { |
| 265 | sockets[i].conn = newconn; |
| 266 | /* The socket is not yet known to anyone, so no need to protect |
| 267 | after having marked it as used. */ |
| 268 | SYS_ARCH_UNPROTECT(lev); |
| 269 | sockets[i].lastdata = NULL; |
| 270 | sockets[i].lastoffset = 0; |
| 271 | sockets[i].rcvevent = 0; |
| 272 | /* TCP sendbuf is empty, but the socket is not yet writable until connected |
| 273 | * (unless it has been created by accept()). */ |
| 274 | sockets[i].sendevent = (newconn->type == NETCONN_TCP ? (accepted != 0) : 1); |
| 275 | sockets[i].errevent = 0; |
| 276 | sockets[i].err = 0; |
| 277 | sockets[i].select_waiting = 0; |
| 278 | return i; |
| 279 | } |
| 280 | SYS_ARCH_UNPROTECT(lev); |
| 281 | } |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | /** Free a socket. The socket's netconn must have been |
| 286 | * delete before! |
no outgoing calls
no test coverage detected