1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/****************************************************************************
* net/socket/socket.h
*
* Copyright (C) 2007-2009, 2011-2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef _NET_SOCKET_SOCKET_H
#define _NET_SOCKET_SOCKET_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_NET
#include <sys/types.h>
#include <stdint.h>
#include <time.h>
#include <nuttx/net/net.h>
#include <nuttx/net/uip.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Definitions of 8-bit socket flags */
/* Bits 0-2: Socket state */
#define _SF_IDLE 0x00 /* - There is no socket activity */
#define _SF_ACCEPT 0x01 /* - Socket is waiting to accept a connection */
#define _SF_RECV 0x02 /* - Waiting for recv action to complete */
#define _SF_SEND 0x03 /* - Waiting for send action to complete */
#define _SF_MASK 0x03 /* - Mask to isolate the above actions */
#define _SF_NONBLOCK 0x08 /* Bit 3: Don't block if no data (TCP/READ only) */
#define _SF_LISTENING 0x10 /* Bit 4: SOCK_STREAM is listening */
#define _SF_BOUND 0x20 /* Bit 5: SOCK_STREAM is bound to an address */
/* Bits 6-7: Connection state */
#define _SF_CONNECTED 0x40 /* Bit 6: SOCK_STREAM is connected */
#define _SF_CLOSED 0x80 /* Bit 7: SOCK_STREAM was gracefully disconnected */
/* Connection state encoding:
*
* _SF_CONNECTED==1 && _SF_CLOSED==0 - the socket is connected
* _SF_CONNECTED==0 && _SF_CLOSED==1 - the socket was gracefully disconnected
* _SF_CONNECTED==0 && _SF_CLOSED==0 - the socket was rudely disconnected
*/
/* Macro to manage the socket state and flags */
#define _SS_SETSTATE(s,f) (((s) & ~_SF_MASK) | (f))
#define _SS_GETSTATE(s) ((s) & _SF_MASK)
#define _SS_ISBUSY(s) (_SS_GETSTATE(s) != _SF_IDLE)
#define _SS_ISNONBLOCK(s) (((s) & _SF_NONBLOCK) != 0)
#define _SS_ISLISTENING(s) (((s) & _SF_LISTENING) != 0)
#define _SS_ISBOUND(s) (((s) & _SF_CONNECTED) != 0)
#define _SS_ISCONNECTED(s) (((s) & _SF_CONNECTED) != 0)
#define _SS_ISCLOSED(s) (((s) & _SF_CLOSED) != 0)
/* This macro converts a socket option value into a bit setting */
#define _SO_BIT(o) (1 << (o))
/* These define bit positions for each socket option (see sys/socket.h) */
#define _SO_DEBUG _SO_BIT(SO_DEBUG)
#define _SO_ACCEPTCONN _SO_BIT(SO_ACCEPTCONN)
#define _SO_BROADCAST _SO_BIT(SO_BROADCAST)
#define _SO_REUSEADDR _SO_BIT(SO_REUSEADDR)
#define _SO_KEEPALIVE _SO_BIT(SO_KEEPALIVE)
#define _SO_LINGER _SO_BIT(SO_LINGER)
#define _SO_OOBINLINE _SO_BIT(SO_OOBINLINE)
#define _SO_SNDBUF _SO_BIT(SO_SNDBUF)
#define _SO_RCVBUF _SO_BIT(SO_RCVBUF)
#define _SO_ERROR _SO_BIT(SO_ERROR)
#define _SO_TYPE _SO_BIT(SO_TYPE)
#define _SO_DONTROUTE _SO_BIT(SO_DONTROUTE)
#define _SO_RCVLOWAT _SO_BIT(SO_RCVLOWAT)
#define _SO_RCVTIMEO _SO_BIT(SO_RCVTIMEO)
#define _SO_SNDLOWAT _SO_BIT(SO_SNDLOWAT)
#define _SO_SNDTIMEO _SO_BIT(SO_SNDTIMEO)
/* This is the larget option value */
#define _SO_MAXOPT (15)
/* Macros to set, test, clear options */
#define _SO_SETOPT(s,o) ((s) |= _SO_BIT(o))
#define _SO_CLROPT(s,o) ((s) &= ~_SO_BIT(o))
#define _SO_GETOPT(s,o) (((s) & _SO_BIT(o)) != 0)
/* These are macros that can be used to determine if socket option code is
* valid (in range) and supported by API.
*/
#define _SO_GETONLYSET (_SO_ACCEPTCONN|_SO_ERROR|_SO_TYPE)
#define _SO_GETONLY(o) ((_SO_BIT(o) & _SO_GETONLYSET) != 0)
#define _SO_GETVALID(o) (((unsigned int)(o)) <= _SO_MAXOPT)
#define _SO_SETVALID(o) ((((unsigned int)(o)) <= _SO_MAXOPT) && !_SO_GETONLY(o))
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Variables
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* net_sockets.c *************************************************************/
int sockfd_allocate(int minsd);
void sock_release(FAR struct socket *psock);
void sockfd_release(int sockfd);
FAR struct socket *sockfd_socket(int sockfd);
/* net_connect.c *************************************************************/
#ifdef CONFIG_NET_TCP
struct tcp_conn_s; /* Forward reference */
int net_startmonitor(FAR struct socket *psock);
void net_stopmonitor(FAR struct tcp_conn_s *conn);
void net_lostconnection(FAR struct socket *psock, uint16_t flags);
#endif
/* net_close.c ***************************************************************/
int psock_close(FAR struct socket *psock);
/* sockopt support ***********************************************************/
#if defined(CONFIG_NET_SOCKOPTS) && !defined(CONFIG_DISABLE_CLOCK)
int net_timeo(uint32_t start_time, socktimeo_t timeo);
#endif
/* send.c ********************************************************************/
ssize_t psock_send(FAR struct socket *psock, FAR const void *buf, size_t len,
int flags);
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* CONFIG_NET */
#endif /* _NET_SOCKET_SOCKET_H */
|