diff options
Diffstat (limited to 'nuttx/net/socket/socket.c')
-rw-r--r-- | nuttx/net/socket/socket.c | 131 |
1 files changed, 75 insertions, 56 deletions
diff --git a/nuttx/net/socket/socket.c b/nuttx/net/socket/socket.c index 505dbfc0e..6dadf79d3 100644 --- a/nuttx/net/socket/socket.c +++ b/nuttx/net/socket/socket.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/socket/socket.c * - * Copyright (C) 2007-2009, 2012, 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2012, 2014-2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Redistribution and use in source and binary forms, with or without @@ -96,66 +96,85 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) { + bool ipdomain = false; int err; /* Only PF_INET, PF_INET6 or PF_PACKET domains supported */ - if ( -#if defined(CONFIG_NET_IPv6) - domain != PF_INET6 -#else - domain != PF_INET + switch (domain) + { +#ifdef CONFIG_NET_IPv4 + case PF_INET: + ipdomain = true; + break; #endif -#if defined(CONFIG_NET_PKT) - && domain != PF_PACKET + +#ifdef CONFIG_NET_IPv6 + case PF_INET6: + ipdomain = true; + break; #endif - ) - { + +#ifdef CONFIG_NET_PKT + case PF_PACKET: + break; +#endif + + default: err = EAFNOSUPPORT; goto errout; } /* Only SOCK_STREAM, SOCK_DGRAM and possible SOCK_RAW are supported */ - if ( -#if defined(CONFIG_NET_TCP) - (type == SOCK_STREAM && protocol != 0 && protocol != IPPROTO_TCP) || -#endif -#if defined(CONFIG_NET_UDP) - (type == SOCK_DGRAM && protocol != 0 && protocol != IPPROTO_UDP) || -#endif - ( -#if defined(CONFIG_NET_TCP) -#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_PKT) - type != SOCK_STREAM && -#else - type != SOCK_STREAM -#endif -#endif -#if defined(CONFIG_NET_UDP) -#if defined(CONFIG_NET_PKT) - type != SOCK_DGRAM && -#else - type != SOCK_DGRAM + switch (type) + { +#ifdef CONFIG_NET_TCP + case SOCK_STREAM: + if ((protocol != 0 && protocol != IPPROTO_TCP) || !ipdomain) + { + err = EPROTONOSUPPORT; + goto errout; + } + + break; #endif + +#ifdef CONFIG_NET_UDP + case SOCK_DGRAM: + if ((protocol != 0 && protocol != IPPROTO_UDP) || !ipdomain) + { + err = EPROTONOSUPPORT; + goto errout; + } + + break; #endif -#if defined(CONFIG_NET_PKT) - type != SOCK_RAW + +#ifdef CONFIG_NET_PKT + case SOCK_RAW: + if (ipdomain) + { + err = EPROTONOSUPPORT; + goto errout; + } + + break; #endif - ) - ) - { - err = EPROTONOSUPPORT; - goto errout; + + default: + err = EPROTONOSUPPORT; + goto errout; } /* Everything looks good. Initialize the socket structure */ /* Save the protocol type */ - psock->s_type = type; - psock->s_conn = NULL; + psock->s_domain = domain; + psock->s_type = type; + psock->s_conn = NULL; #ifdef CONFIG_NET_TCP_WRITE_BUFFERS - psock->s_sndcb = NULL; + psock->s_sndcb = NULL; #endif /* Allocate the appropriate connection structure. This reserves the @@ -166,19 +185,19 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) err = ENOMEM; /* Assume failure to allocate connection instance */ switch (type) { -#ifdef CONFIG_NET_PKT - case SOCK_RAW: +#ifdef CONFIG_NET_TCP + case SOCK_STREAM: { - /* Allocate the packet socket connection structure and save - * in the new socket instance. + /* Allocate the TCP connection structure and save in the new + * socket instance. */ - FAR struct pkt_conn_s *conn = pkt_alloc(); + FAR struct tcp_conn_s *conn = tcp_alloc(); if (!conn) { /* Failed to reserve a connection structure */ - goto errout; + goto errout; /* With err == ENFILE or ENOMEM */ } /* Set the reference count on the connection structure. This @@ -193,14 +212,14 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) break; #endif -#ifdef CONFIG_NET_TCP - case SOCK_STREAM: +#ifdef CONFIG_NET_UDP + case SOCK_DGRAM: { - /* Allocate the TCP connection structure and save in the new + /* Allocate the UDP connection structure and save in the new * socket instance. */ - FAR struct tcp_conn_s *conn = tcp_alloc(); + FAR struct udp_conn_s *conn = udp_alloc(); if (!conn) { /* Failed to reserve a connection structure */ @@ -220,19 +239,19 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) break; #endif -#ifdef CONFIG_NET_UDP - case SOCK_DGRAM: +#ifdef CONFIG_NET_PKT + case SOCK_RAW: { - /* Allocate the UDP connection structure and save in the new - * socket instance. + /* Allocate the packet socket connection structure and save + * in the new socket instance. */ - FAR struct udp_conn_s *conn = udp_alloc(); + FAR struct pkt_conn_s *conn = pkt_alloc(); if (!conn) { /* Failed to reserve a connection structure */ - goto errout; /* With err == ENFILE or ENOMEM */ + goto errout; } /* Set the reference count on the connection structure. This |