From 279136fd239ad7bb378b0dae01fe8f109e463c4f Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 1 Sep 2007 20:56:19 +0000 Subject: Adding socket(), bind() logic git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@319 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/net/net_sockets.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'nuttx/net/net_sockets.c') diff --git a/nuttx/net/net_sockets.c b/nuttx/net/net_sockets.c index fb4927e72..d2be221a8 100644 --- a/nuttx/net/net_sockets.c +++ b/nuttx/net/net_sockets.c @@ -169,3 +169,86 @@ int net_releaselist(FAR struct socketlist *list) return OK; } +int sockfd_allocate(void) +{ + FAR struct socketlist *list; + int i; + + /* Get the socket list for this task/thread */ + + list = sched_getsockets(); + if (list) + { + /* Search for a socket structure with no references */ + + _net_semtake(list); + for (i = 0; i < CONFIG_NSOCKET_DESCRIPTORS; i++) + { + /* Are there references on this socket? */ + if (!list->sl_sockets[i].s_crefs) + { + /* No take the reference and return the index + an offset + * as the socket descriptor. + */ + memset(&list->sl_sockets[i], 0, sizeof(struct socket)); + list->sl_sockets[i].s_crefs = 1; + _net_semgive(list); + return i + __SOCKFD_OFFSET; + } + } + _net_semgive(list); + } + return ERROR; +} + +void sockfd_release(int sockfd) +{ + FAR struct socket *psock; + + /* Get the socket structure for this sockfd */ + + psock = sockfd_socket(sockfd); + if (psock) + { + /* Take the list semaphore so that there will be no accesses + * to this socket structure. + */ + + FAR struct socketlist *list = sched_getsockets(); + if (list) + { + /* Decrement the count if there the socket will persist + * after this. + */ + + _net_semtake(list); + if (psock && psock->s_crefs > 1) + { + psock->s_crefs--; + } + else + { + /* The socket will not persist... reset it */ + + memset(psock, 0, sizeof(struct socket)); + } + _net_semgive(list); + } + } +} + +FAR struct socket *sockfd_socket(int sockfd) +{ + FAR struct socketlist *list; + int ndx = sockfd - __SOCKFD_OFFSET; + + if (ndx >=0 && ndx < CONFIG_NSOCKET_DESCRIPTORS) + { + list = sched_getsockets(); + if (list) + { + return &list->sl_sockets[ndx]; + } + } + return NULL; +} \ No newline at end of file -- cgit v1.2.3