Network Developers Kit API 3.61.01.01

socketndk.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2014-2019, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * */
32 /*
33 * ======== socket.h ========
34 *
35 * NDK socket APIs
36 *
37 */
38
39 #ifndef _SOCKET_NDK_H_
40 #define _SOCKET_NDK_H_
41
42 /* NDK_NOUSERAPIS is a temporary fix to enable NIMU drivers to avoid a POSIX
43 * dependency. User code typically doesn't throw it (and therefore
44 * has a POSIX dependency).
45 */
46 #ifndef NDK_NOUSERAPIS
47 /* use TI POSIX to get timeval, needed by fdSelect() */
48 #include <sys/time.h>
49 #endif
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 /* Network Support Macros for "Unix-like" functions */
56 #ifdef NDK_BIGENDIAN
57 #define NDK_htons(a) (a)
58 #define NDK_htonl(a) (a)
59 #define NDK_ntohl(a) (a)
60 #define NDK_ntohs(a) (a)
61 #else
62 #define NDK_htons(a) ( (((a)>>8)&0xffU) + (((a)<<8)&0xff00U) )
63 /*
64 * Fix warning when compiling for IAR (SDOCM00103001):
65 *
66 * Warning[Pe061]: integer operation result is out of range
67 *
68 * This macro has been updated to perform masking operations before shifting.
69 * In its previous form (which shifts THEN masks), the IAR compiler generated
70 * a warning because it did not like shaving off bits, as it is a (potential)
71 * accidental loss of data. Changing the code to mask first (purposefully
72 * losing the data) then shifting afterward fixes the warning.
73 *
74 * Note that the TI and GCC compilers never cared about this ...
75 *
76 */
77 #define NDK_htonl(a) ((((a) & 0xff000000) >> 24) | (((a) & 0x00ff0000) >> 8) | \
78 (((a) & 0x0000ff00) << 8) | (((a) & 0x000000ff) << 24) )
79 #define NDK_ntohl(a) NDK_htonl(a)
80 #define NDK_ntohs(a) NDK_htons(a)
81 #endif
82
83 /* OS and Sockets Types */
84 typedef void * SOCKET; /* OS Socket Type */
85
86 /*
87 * Select uses bit masks of file descriptors. These macros
88 * manipulate handle lists. NDK_FD_SETSIZE can be modified as
89 * needed.
90 *
91 */
92 #define NDK_FD_SETSIZE 16
93
94 #define _SYS_SELECT_H 1 // TODO: still needed for struct timeval conflict
95 typedef struct NDK_fd_set {
96 uint32_t count;
97 void *fd[NDK_FD_SETSIZE];
98 } NDK_fd_set;
99
100 extern void fdsetRemoveEntry( NDK_fd_set *pSet, void *hEntry );
101 extern uint32_t fdsetTestEntry( NDK_fd_set *pSet, void *hEntry );
102 extern void mmCopy( void* pDst, void* pSrc, uint32_t Size );
103
104 #define NDK_FD_SET(n, p) {if((p)->count<NDK_FD_SETSIZE){(p)->fd[(p)->count++]=(n);}}
105
106 #define NDK_FD_CLR(n, p) fdsetRemoveEntry( (p), (n) )
107
108 #define NDK_FD_ISSET(n, p) fdsetTestEntry( (p), (n) )
109
110 #define NDK_FD_COPY(f, t) mmCopy((t), (f), sizeof(NDK_fd_set))
111
112 #define NDK_FD_ZERO(p) ((p)->count=0)
113
114 /* Socket address families */
115 #define AF_UNSPEC 0
116 #define AF_TASK 1 /* Intertask Communication */
117 #define AF_INET 2 /* Internet: UDP, TCP, etc. */
118 #define AF_INET6 10 /* IPV6 */
119 #define AF_RAWETH 12 /* Raw Ethernet Protocol */
120
121 /* Socket Types */
122 #define SOCK_STREAM 1 /* stream socket */
123 #define SOCK_DGRAM 2 /* datagram socket */
124 #define SOCK_RAW 3 /* raw-protocol interface */
125 #define SOCK_STREAMNC 4 /* non-copy stream socket */
126 #define SOCK_RAWETH 5 /* non-copy raw eth socket */
127
128 /* Protocols */
129 #define IPPROTO_IP 0 /* IP Placeholder */
130 #define IPPROTO_ICMP 1 /* ICMP */
131 #define IPPROTO_IGMP 2 /* IGMP */
132 #define IPPROTO_TCP 6 /* TCP */
133 #define IPPROTO_UDP 17 /* UDP */
134 #define IPPROTO_IPV6 41 /* IPV6 */
135 #define IPPROTO_ICMPV6 58 /* ICMPV6 Header. */
136
137 /* Option flags per-socket. */
138 #define SO_DEBUG 0x0001 /* turn on debugging info recording */
139 #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
140 #define SO_REUSEADDR 0x0004 /* allow local address reuse */
141 #define SO_KEEPALIVE 0x0008 /* keep connections alive */
142 #define SO_DONTROUTE 0x0010
143 #define SO_BROADCAST 0x0020
144 #define SO_USELOOPBACK 0x0040 /* bypass hardware when possible */
145 #define SO_LINGER 0x0080 /* linger on close if data present */
146 #define SO_OOBINLINE 0x0100 /* leave received OOB data in line */
147 #define SO_REUSEPORT 0x0200 /* allow local address & port reuse */
148 #define SO_TIMESTAMP 0x0400 /* timestamp received dgram traffic */
149
150 /* Additional options (not flags) */
151 #define SO_SNDBUF 0x1001 /* send buffer size */
152 #define SO_RCVBUF 0x1002 /* receive buffer size */
153 #define SO_SNDLOWAT 0x1003 /* send low-water mark */
154 #define SO_RCVLOWAT 0x1004 /* receive low-water mark */
155 #define SO_SNDTIMEO 0x1005 /* send timeout */
156 #define SO_RCVTIMEO 0x1006 /* receive timeout */
157 #define SO_ERROR 0x1007 /* get error status and clear */
158 #define SO_TYPE 0x1008 /* get socket type */
159 #define SO_PRIVSTATE 0x1009 /* get/deny privileged state */
160 #define SO_IFDEVICE 0x100a /* broadcast IO device */
161 #define SO_BLOCKING 0x100b /* blocking is default */
162 #define SO_TXTIMESTAMP 0x100c /* register a call-out function */
163 /* to timestamp transmitted payload */
164 /* data. */
165 #define SO_PRIORITY 0x100d /* Use this to set the priority of the socket. */
166
167 #define SOL_SOCKET 0xFFFF /* Level used for above options */
168
169 /* Structure used for manipulating linger option. */
170 struct linger {
171 int l_onoff; /* option on/off */
172 int l_linger; /* linger time */
173 };
174
175 /* IP Level (IPPROTO_IP) Socket Options */
176 #define IP_OPTIONS 1 /* get/set IP Options (20 byte max) */
177 #define IP_HDRINCL 2 /* IP Header included with data (raw only) */
178 #define IP_TOS 3 /* IP tos & preced. */
179 #define IP_TTL 4 /* IP ttl */
180
181 /* Multicast Support as per RFC3678 */
182 #define IP_ADD_MEMBERSHIP 5 /* Join a Multicast group. */
183 #define IP_DROP_MEMBERSHIP 6 /* Leave a Multicast group. */
184
185 /* TCP Level (IPPROTO_TCP) Socket Options */
186 #define NDK_TCP_NODELAY 0x01 /* Don't delay send to combine packet data */
187 #define NDK_TCP_MAXSEG 0x02 /* Adjust MSS (down only) */
188 #define NDK_TCP_NOPUSH 0x04 /* Don't send just to finish a data block */
189 #define NDK_TCP_NOOPT 0x08 /* Don't use TCP options */
190 #define NDK_TCP_SACKPERMITTED 0x10 /* RFC 2018 - SACK is permitted */
191 #define NDK_TCP_MAXRTT 0x20 /* Set maximum RTT value used in calculation */
192
193 /* Read/Write Flags */
194 #define MSG_OOB 0x0001 /* Read OOB data (not supported) */
195 #define MSG_PEEK 0x0002 /* Get Rx data but leave in queue */
196 #define MSG_WAITALL 0x0004 /* Wait for data to fill buffers */
197 #define MSG_DONTWAIT 0x0008 /* Don't wait for resources */
198 #define MSG_DONTROUTE 0x0010 /* Won't route if not local to subnet */
199 #define MSG_NOSIGNAL 0x0020 /* Unused (fix for NDK-115) */
200
201 /* Generic Socket address storage data structure. */
202 struct sockaddr {
203 uint16_t sa_family; /* address family */
204 char sa_data[14]; /* socket data */
205 };
206
207 /* Structure used by kernel to store most */
208 /* addresses. */
209 struct in_addr {
210 uint32_t s_addr; /* 32 bit long IP address, net order */
211 };
212
213 /* Structure used for handling the Multicast socket options */
214 /* as defined by RFC3678 */
215 struct ip_mreq
216 {
217 struct in_addr imr_multiaddr; /* IP Address of the group */
218 struct in_addr imr_interface; /* IP Address of the interface */
219 };
220
221 /* AF_INET family (IPv4) Socket address data structure. */
222 struct sockaddr_in {
223 uint16_t sin_family; /* address family */
224 uint16_t sin_port; /* port */
225 struct in_addr sin_addr;
226 char sin_zero[8]; /* fixed length address value */
227 };
228
229 #define IPV6_UCAST_DEF_HOP_LIMIT 64
230 #define IPV6_MCAST_DEF_HOP_LIMIT 1
231
232 /* Socket options defined at IPPROTO_IPV6 level. */
233 #define IPV6_UNICAST_HOPS 1
234 #define IPV6_MULTICAST_IF 2
235 #define IPV6_MULTICAST_HOPS 3
236 #define IPV6_MULTICAST_LOOP 4
237 #define IPV6_JOIN_GROUP 5
238 #define IPV6_LEAVE_GROUP 6
239 #define IPV6_V6ONLY 7
240
241 /* Structure used by kernel to store IPv6 */
242 /* addresses. */
243 struct in6_addr {
244 union
245 {
246 unsigned char u6_addr8[16];
247 uint16_t u6_addr16[8];
248 uint32_t u6_addr32[4];
249 }in6_u;
250 #define s6_addr in6_u.u6_addr8
251 #define s6_addr16 in6_u.u6_addr16
252 #define s6_addr32 in6_u.u6_addr32
253 };
254
255 /* Structure used for handling the Multicast socket options */
256 /* as defined by RFC3678 for IPv6 */
257 struct ipv6_mreq
258 {
259 struct in6_addr ipv6mr_multiaddr; /* IP Address of the group */
260 uint32_t ipv6mr_interface; /* local interface */
261 };
262
263 /* AF_INET6 family (IPv6) Socket address data structure. */
264 struct sockaddr_in6 {
265 uint16_t sin6_family; /* address family */
266 uint16_t sin6_port; /* port */
267 uint32_t sin6_flowinfo; /* IPv6 flow information */
268 struct in6_addr sin6_addr; /* IPv6 address */
269 uint32_t sin6_scope_id; /* scope id */
270 };
271
272 /* Maximum queue length specifiable by listen. */
273 #define SOMAXCONN 8
274
275 /* Used by Shutdown() */
276 #define SHUT_RD 0 /* Read side of connection */
277 #define SHUT_WR 1 /* Write side of connection */
278 #define SHUT_RDWR 2 /* Both sides of connection */
279
280 /* Used by fdPoll() */
281 typedef struct _fdpollitem {
282 void *fd;
283 uint16_t eventsRequested;
284 uint16_t eventsDetected;
285 } FDPOLLITEM;
286
287 /* Errror Return Values */
288 #define INVALID_SOCKET (void *)(-1L) /* Used by socket() and accept() */
289 #define SOCKET_ERROR -1 /* Used by the rest */
290
291 /* Functions */
292
293 #ifndef NDK_NOUSERAPIS
294
295 /* File Descriptor Functions */
296 extern int fdOpenSession( void *hTask );
297 extern void fdCloseSession( void *hTask );
298 extern int fdClose( void *fd );
299 extern int fdShare( void *fd );
300 extern int fdError();
301
302 extern int fdSelect( int width, NDK_fd_set *readfds, NDK_fd_set *writefds,
303 NDK_fd_set *exceptfds, struct timeval *timeout );
304 extern void fdSelectAbort( void *hTask );
305
306 extern int fdStatus( SOCKET s, int request, int *results );
307 #define FDSTATUS_TYPE 0 /* get socket type */
308 #define FDSTATUS_TYPE_SOCKET 1
309 #define FDSTATUS_TYPE_PIPE 2
310 #define FDSTATUS_RECV 1 /* get socket rcv status */
311 #define FDSTATUS_SEND 2 /* get socket snd status */
312
313 extern int fdPoll( FDPOLLITEM items[], uint32_t itemcnt, int32_t timeout );
314 #define POLLINFTIM -1
315 #define POLLIN 0x01
316 #define POLLOUT 0x02
317 #define POLLPRI 0x04
318 #define POLLNVAL 0x08
319
320 /* Socket Oriented Functions */
321 extern SOCKET NDK_accept( SOCKET s, struct sockaddr *pName, int *plen );
322 extern int NDK_bind( SOCKET s, struct sockaddr *pName, int len );
323 extern int NDK_connect( SOCKET s, struct sockaddr *pName, int len );
324 extern int NDK_getpeername( SOCKET s, struct sockaddr *pName, int *plen );
325 extern int NDK_getsockname( SOCKET s, struct sockaddr *pName, int *plen );
326 extern int NDK_getsockopt( SOCKET s, int level, int op, void *pbuf, int *pbufsize );
327 extern int NDK_listen( SOCKET s, int maxcon );
328 extern int NDK_recv( SOCKET s, void *pbuf, int size, int flags );
329 extern int NDK_recvfrom( SOCKET s, void *pbuf, int size, int flags, struct sockaddr *pName, int *plen );
330 extern int NDK_recvnc( SOCKET s, void **ppbuf, int flags, void **pHandle );
331 extern int NDK_recvncfrom( SOCKET s, void **ppbuf, int flags,
332 struct sockaddr *pName, int *plen, void **pHandle );
333 extern void NDK_recvncfree( SOCKET Handle );
334 extern int NDK_send( SOCKET s, void *pbuf, int size, int flags );
335 extern int NDK_sendto( SOCKET s, void *pbuf, int size, int flags, struct sockaddr *pName, int len );
336 extern int NDK_setsockopt( SOCKET s, int level, int op, void *pbuf, int bufsize );
337 extern int NDK_shutdown( SOCKET s, int how );
338 extern SOCKET NDK_socket( int domain, int type, int protocol );
339
340 extern int NDK_getsendncbuff(SOCKET s, uint32_t bufSize, void** phBuf, void **phPkt);
341 extern int NDK_sendnc( SOCKET s, void *pbuf, int size, void *hPkt, int flags );
342 extern void NDK_sendncfree( SOCKET Handle );
343 extern int NDK_getSockCtx( SOCKET s);
344
345 /* Pipe Oriented Functions */
346 extern int NDK_pipe( void **pfd1, void **pfd2 );
347
348 /* Hook function prototypes */
349 #define CREATE_SKT_CTX_HOOK 0
350 #define CLOSE_SKT_CTX_HOOK 1
351 #define NETSTART_ERROR_HOOK 2
352
353 typedef struct _NetStartError {
354 int error;
355 } NetStartError_Obj;
356
357 typedef int (*NDK_HookFxn) (uintptr_t);
358 extern void NDK_registerHook(int type, NDK_HookFxn fxn);
359
360 /* Socket context defines */
361 #define NDK_NO_CTX -1
362
363 #endif /* NDK_NOUSERAPIS */
364
365 /* Call-out function prototype */
366 typedef void (*TimestampFxn)(unsigned char *pIpHdr);
367
368 #ifdef __cplusplus
369 }
370 #endif /* extern "C" */
371
372 #endif
_NetStartError::error
int error
Definition: socketndk.h:354
NDK_recv
int NDK_recv(SOCKET s, void *pbuf, int size, int flags)
NDK_sendto
int NDK_sendto(SOCKET s, void *pbuf, int size, int flags, struct sockaddr *pName, int len)
NDK_recvncfree
void NDK_recvncfree(SOCKET Handle)
NDK_getsendncbuff
int NDK_getsendncbuff(SOCKET s, uint32_t bufSize, void **phBuf, void **phPkt)
fdStatus
int fdStatus(SOCKET s, int request, int *results)
NDK_setsockopt
int NDK_setsockopt(SOCKET s, int level, int op, void *pbuf, int bufsize)
linger::l_onoff
int l_onoff
Definition: socketndk.h:171
_fdpollitem::fd
void * fd
Definition: socketndk.h:282
NDK_accept
SOCKET NDK_accept(SOCKET s, struct sockaddr *pName, int *plen)
sockaddr_in6::sin6_flowinfo
uint32_t sin6_flowinfo
Definition: socketndk.h:267
fdCloseSession
void fdCloseSession(void *hTask)
NDK_shutdown
int NDK_shutdown(SOCKET s, int how)
NetStartError_Obj
struct _NetStartError NetStartError_Obj
sockaddr_in
Definition: socketndk.h:222
fdSelectAbort
void fdSelectAbort(void *hTask)
fdSelect
int fdSelect(int width, NDK_fd_set *readfds, NDK_fd_set *writefds, NDK_fd_set *exceptfds, struct timeval *timeout)
ip_mreq
Definition: socketndk.h:215
NDK_listen
int NDK_listen(SOCKET s, int maxcon)
NDK_getsockname
int NDK_getsockname(SOCKET s, struct sockaddr *pName, int *plen)
NDK_pipe
int NDK_pipe(void **pfd1, void **pfd2)
ipv6_mreq::ipv6mr_interface
uint32_t ipv6mr_interface
Definition: socketndk.h:260
NDK_fd_set::count
uint32_t count
Definition: socketndk.h:96
sockaddr::sa_family
uint16_t sa_family
Definition: socketndk.h:203
NDK_socket
SOCKET NDK_socket(int domain, int type, int protocol)
NDK_sendnc
int NDK_sendnc(SOCKET s, void *pbuf, int size, void *hPkt, int flags)
NDK_getsockopt
int NDK_getsockopt(SOCKET s, int level, int op, void *pbuf, int *pbufsize)
_fdpollitem::eventsRequested
uint16_t eventsRequested
Definition: socketndk.h:283
sockaddr_in6::sin6_scope_id
uint32_t sin6_scope_id
Definition: socketndk.h:269
fdsetTestEntry
uint32_t fdsetTestEntry(NDK_fd_set *pSet, void *hEntry)
fdPoll
int fdPoll(FDPOLLITEM items[], uint32_t itemcnt, int32_t timeout)
NDK_sendncfree
void NDK_sendncfree(SOCKET Handle)
_fdpollitem::eventsDetected
uint16_t eventsDetected
Definition: socketndk.h:284
FDPOLLITEM
struct _fdpollitem FDPOLLITEM
NDK_getpeername
int NDK_getpeername(SOCKET s, struct sockaddr *pName, int *plen)
NDK_fd_set
struct NDK_fd_set NDK_fd_set
in6_addr
Definition: socketndk.h:243
SOCKET
void * SOCKET
Definition: socketndk.h:84
fdClose
int fdClose(void *fd)
NDK_connect
int NDK_connect(SOCKET s, struct sockaddr *pName, int len)
sockaddr
Definition: socketndk.h:202
sockaddr_in::sin_family
uint16_t sin_family
Definition: socketndk.h:223
fdOpenSession
int fdOpenSession(void *hTask)
in_addr
Definition: socketndk.h:209
sockaddr_in::sin_port
uint16_t sin_port
Definition: socketndk.h:224
mmCopy
void mmCopy(void *pDst, void *pSrc, uint32_t Size)
linger
Definition: socketndk.h:170
NDK_fd_set
Definition: socketndk.h:95
NDK_fd_set::fd
void * fd[16]
Definition: socketndk.h:97
fdShare
int fdShare(void *fd)
_NetStartError
Definition: socketndk.h:353
in_addr::s_addr
uint32_t s_addr
Definition: socketndk.h:210
TimestampFxn
void(* TimestampFxn)(unsigned char *pIpHdr)
Definition: socketndk.h:366
NDK_HookFxn
int(* NDK_HookFxn)(uintptr_t)
Definition: socketndk.h:357
ipv6_mreq
Definition: socketndk.h:257
NDK_recvnc
int NDK_recvnc(SOCKET s, void **ppbuf, int flags, void **pHandle)
sockaddr_in6::sin6_family
uint16_t sin6_family
Definition: socketndk.h:265
NDK_send
int NDK_send(SOCKET s, void *pbuf, int size, int flags)
fdsetRemoveEntry
void fdsetRemoveEntry(NDK_fd_set *pSet, void *hEntry)
linger::l_linger
int l_linger
Definition: socketndk.h:172
sockaddr_in6
Definition: socketndk.h:264
NDK_FD_SETSIZE
#define NDK_FD_SETSIZE
Definition: socketndk.h:92
NDK_recvfrom
int NDK_recvfrom(SOCKET s, void *pbuf, int size, int flags, struct sockaddr *pName, int *plen)
sockaddr_in6::sin6_port
uint16_t sin6_port
Definition: socketndk.h:266
NDK_bind
int NDK_bind(SOCKET s, struct sockaddr *pName, int len)
NDK_registerHook
void NDK_registerHook(int type, NDK_HookFxn fxn)
NDK_getSockCtx
int NDK_getSockCtx(SOCKET s)
NDK_recvncfrom
int NDK_recvncfrom(SOCKET s, void **ppbuf, int flags, struct sockaddr *pName, int *plen, void **pHandle)
_fdpollitem
Definition: socketndk.h:281
fdError
int fdError()

© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale