Socket interface: Added close() interface.

This commit is contained in:
Gregory Nutt
2017-07-13 12:15:15 -06:00
parent 0c840ed22f
commit 833ee2275d
8 changed files with 760 additions and 628 deletions

View File

@@ -118,6 +118,7 @@ struct sock_intf_s
CODE ssize_t (*si_recvfrom)(FAR struct socket *psock, FAR void *buf,
size_t len, int flags, FAR struct sockaddr *from,
FAR socklen_t *fromlen);
CODE int (*si_close)(FAR struct socket *psock);
};
/* This is the internal representation of a socket reference by a file

View File

@@ -69,6 +69,7 @@ static ssize_t local_send(FAR struct socket *psock, FAR const void *buf,
static ssize_t local_sendto(FAR struct socket *psock, FAR const void *buf,
size_t len, int flags, FAR const struct sockaddr *to,
socklen_t tolen);
static int local_close(FAR struct socket *psock);
/****************************************************************************
* Public Data
@@ -83,7 +84,8 @@ const struct sock_intf_s g_local_sockif =
local_accept, /* si_accept */
local_send, /* si_send */
local_sendto, /* si_sendto */
local_recvfrom /* si_recvfrom */
local_recvfrom, /* si_recvfrom */
local_close /* si_close */
};
/****************************************************************************
@@ -444,6 +446,63 @@ ssize_t local_sendto(FAR struct socket *psock, FAR const void *buf,
return nsent;
}
/****************************************************************************
* Name: local_close
*
* Description:
* Performs the close operation on a local, Unix socket instance
*
* Parameters:
* psock Socket instance
*
* Returned Value:
* 0 on success; a negated errno value is returned on any failure.
*
* Assumptions:
*
****************************************************************************/
static int local_close(FAR struct socket *psock)
{
/* Perform some pre-close operations for the local address type */
switch (psock->s_type)
{
#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
#ifdef CONFIG_NET_TCP
case SOCK_STREAM:
#endif
#ifdef CONFIG_NET_UDP
case SOCK_DGRAM:
#endif
{
FAR struct local_conn_s *conn = psock->s_conn;
/* Is this the last reference to the connection structure (there
* could be more if the socket was dup'ed).
*/
if (conn->lc_crefs <= 1)
{
conn->lc_crefs = 0;
local_release(conn);
}
else
{
/* No.. Just decrement the reference count */
conn->lc_crefs--;
}
return OK;
}
#endif /* CONFIG_NET_TCP || CONFIG_NET_UDP*/
default:
return -EBADF;
}
}
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@@ -68,6 +68,7 @@ static ssize_t pkt_send(FAR struct socket *psock, FAR const void *buf,
static ssize_t pkt_sendto(FAR struct socket *psock, FAR const void *buf,
size_t len, int flags, FAR const struct sockaddr *to,
socklen_t tolen);
static int pkt_close(FAR struct socket *psock);
/****************************************************************************
* Public Data
@@ -82,7 +83,8 @@ const struct sock_intf_s g_pkt_sockif =
pkt_accept, /* si_accept */
pkt_send, /* si_send */
pkt_sendto, /* si_sendto */
pkt_recvfrom /* si_recvfrom */
pkt_recvfrom, /* si_recvfrom */
pkt_close /* si_close */
};
/****************************************************************************
@@ -425,6 +427,59 @@ static ssize_t pkt_sendto(FAR struct socket *psock, FAR const void *buf,
return -EAFNOSUPPORT;
}
/****************************************************************************
* Name: pkt_close
*
* Description:
* Performs the close operation on a raw packet socket instance
*
* Parameters:
* psock Socket instance
*
* Returned Value:
* 0 on success; a negated errno value is returned on any failure.
*
* Assumptions:
*
****************************************************************************/
static int pkt_close(FAR struct socket *psock)
{
/* Perform some pre-close operations for the raw packet address type */
switch (psock->s_type)
{
case SOCK_RAW:
{
FAR struct pkt_conn_s *conn = psock->s_conn;
/* Is this the last reference to the connection structure (there
* could be more if the socket was dup'ed).
*/
if (conn->crefs <= 1)
{
/* Yes... free the connection structure */
conn->crefs = 0; /* No more references on the connection */
pkt_free(psock->s_conn); /* Free network resources */
}
else
{
/* No.. Just decrement the reference count */
conn->crefs--;
}
return OK;
}
#endif
default:
return -EBADF;
}
}
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@@ -44,9 +44,9 @@ SOCK_CSRCS += net_dupsd2.c net_clone.c net_poll.c net_vfcntl.c
SOCK_CSRCS += net_sockif.c
ifeq ($(CONFIG_NET_IPv4),y)
SOCK_CSRCS += inet_sockif.c inet_recvfrom.c inet_connect.c
SOCK_CSRCS += inet_sockif.c inet_recvfrom.c inet_connect.c inet_close.c
else ifeq ($(CONFIG_NET_IPv6),y)
SOCK_CSRCS += inet_sockif.c inet_recvfrom.c inet_connect.c
SOCK_CSRCS += inet_sockif.c inet_recvfrom.c inet_connect.c inet_close.c
endif
# TCP/IP support

610
net/socket/inet_close.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -83,7 +83,8 @@ const struct sock_intf_s g_inet_sockif =
inet_accept, /* si_accept */
inet_send, /* si_send */
inet_sendto, /* si_sendto */
inet_recvfrom /* si_recvfrom */
inet_recvfrom, /* si_recvfrom */
inet_close /* si_close */
};
/****************************************************************************

File diff suppressed because it is too large Load Diff

View File

@@ -506,6 +506,24 @@ ssize_t inet_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
int flags, FAR struct sockaddr *from,
FAR socklen_t *fromlen);
/****************************************************************************
* Name: inet_close
*
* Description:
* Performs the close operation on an AF_INET or AF_INET6 socket instance
*
* Parameters:
* psock Socket instance
*
* Returned Value:
* 0 on success; -1 on error with errno set appropriately.
*
* Assumptions:
*
****************************************************************************/
int inet_close(FAR struct socket *psock);
#undef EXTERN
#if defined(__cplusplus)
}