CC3000  1.14
 All Classes Functions Groups
socket.h
1 /*****************************************************************************
2 *
3 * socket.h - CC3000 Host Driver Implementation.
4 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 *****************************************************************************/
35 #ifndef __SOCKET_H__
36 #define __SOCKET_H__
37 
38 
39 //*****************************************************************************
40 //
43 //
44 //*****************************************************************************
45 
46 
47 //*****************************************************************************
48 //
49 // If building with a C++ compiler, make all of the definitions in this header
50 // have a C binding.
51 //
52 //*****************************************************************************
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 #define HOSTNAME_MAX_LENGTH (230) // 230 bytes + header shouldn't exceed 8 bit value
58 
59 //--------- Address Families --------
60 
61 #define AF_INET 2
62 #define AF_INET6 23
63 
64 //------------ Socket Types ------------
65 
66 #define SOCK_STREAM 1
67 #define SOCK_DGRAM 2
68 #define SOCK_RAW 3 // Raw sockets allow new IPv4 protocols to be implemented in user space. A raw socket receives or sends the raw datagram not including link level headers
69 #define SOCK_RDM 4
70 #define SOCK_SEQPACKET 5
71 
72 //----------- Socket Protocol ----------
73 
74 #define IPPROTO_IP 0 // dummy for IP
75 #define IPPROTO_ICMP 1 // control message protocol
76 #define IPPROTO_IPV4 IPPROTO_IP // IP inside IP
77 #define IPPROTO_TCP 6 // tcp
78 #define IPPROTO_UDP 17 // user datagram protocol
79 #define IPPROTO_IPV6 41 // IPv6 in IPv6
80 #define IPPROTO_NONE 59 // No next header
81 #define IPPROTO_RAW 255 // raw IP packet
82 #define IPPROTO_MAX 256
83 
84 //----------- Socket retunr codes -----------
85 
86 #define SOC_ERROR (-1) // error
87 #define SOC_IN_PROGRESS (-2) // socket in progress
88 
89 //----------- Socket Options -----------
90 #define SOL_SOCKET 0xffff // socket level
91 #define SOCKOPT_RECV_NONBLOCK 0 // recv non block mode, set SOCK_ON or SOCK_OFF (default block mode)
92 #define SOCKOPT_RECV_TIMEOUT 1 // optname to configure recv and recvfromtimeout
93 #define SOCKOPT_ACCEPT_NONBLOCK 2 // accept non block mode, set SOCK_ON or SOCK_OFF (default block mode)
94 #define SOCK_ON 0 // socket non-blocking mode is enabled
95 #define SOCK_OFF 1 // socket blocking mode is enabled
96 
97 #define MAX_PACKET_SIZE 1500
98 #define MAX_LISTEN_QUEUE 4
99 
100 #define IOCTL_SOCKET_EVENTMASK
101 
102 #define ENOBUFS 55 // No buffer space available
103 
104 #define __FD_SETSIZE 32
105 
106 #define ASIC_ADDR_LEN 8
107 
108 #define NO_QUERY_RECIVED -3
109 
110 typedef struct _in_addr_t
111 {
112  UINT32 s_addr; // load with inet_aton()
113 } in_addr;
114 
115 typedef struct _sockaddr_t
116 {
117  UINT16 sa_family;
118  UINT8 sa_data[14];
119 } sockaddr;
120 
121 typedef struct _sockaddr_in_t
122 {
123  INT16 sin_family; // e.g. AF_INET
124  UINT16 sin_port; // e.g. htons(3490)
125  in_addr sin_addr; // see struct in_addr, below
126  CHAR sin_zero[8]; // zero this if you want to
127 } sockaddr_in;
128 
129 typedef UINT32 socklen_t;
130 
131 // The fd_set member is required to be an array of INT32s.
132 typedef INT32 __fd_mask;
133 
134 // It's easier to assume 8-bit bytes than to get CHAR_BIT.
135 #define __NFDBITS (8 * sizeof (__fd_mask))
136 #define __FDELT(d) ((d) / __NFDBITS)
137 #define __FDMASK(d) ((__fd_mask) 1 << ((d) % __NFDBITS))
138 
139 // fd_set for select and pselect.
140 typedef struct
141 {
142  __fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
143 #define __FDS_BITS(set) ((set)->fds_bits)
144 } fd_set;
145 
146 // We don't use `memset' because this would require a prototype and
147 // the array isn't too big.
148 #define __FD_ZERO(set) \
149  do { \
150  UINT16 __i; \
151  fd_set *__arr = (set); \
152  for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) \
153  __FDS_BITS (__arr)[__i] = 0; \
154  } while (0)
155 #define __FD_SET(d, set) (__FDS_BITS (set)[__FDELT (d)] |= __FDMASK (d))
156 #define __FD_CLR(d, set) (__FDS_BITS (set)[__FDELT (d)] &= ~__FDMASK (d))
157 #define __FD_ISSET(d, set) (__FDS_BITS (set)[__FDELT (d)] & __FDMASK (d))
158 
159 // Access macros for 'fd_set'.
160 #define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp)
161 #define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp)
162 #define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp)
163 #define FD_ZERO(fdsetp) __FD_ZERO (fdsetp)
164 
165 //Use in case of Big Endian only
166 
167 #define htonl(A) ((((UINT32)(A) & 0xff000000) >> 24) | \
168  (((UINT32)(A) & 0x00ff0000) >> 8) | \
169  (((UINT32)(A) & 0x0000ff00) << 8) | \
170  (((UINT32)(A) & 0x000000ff) << 24))
171 
172 #define ntohl htonl
173 
174 //Use in case of Big Endian only
175 #define htons(A) ((((UINT32)(A) & 0xff00) >> 8) | \
176  (((UINT32)(A) & 0x00ff) << 8))
177 
178 
179 #define ntohs htons
180 
181 // mDNS port - 5353 mDNS multicast address - 224.0.0.251
182 #define SET_mDNS_ADD(sockaddr) sockaddr.sa_data[0] = 0x14; \
183  sockaddr.sa_data[1] = 0xe9; \
184  sockaddr.sa_data[2] = 0xe0; \
185  sockaddr.sa_data[3] = 0x0; \
186  sockaddr.sa_data[4] = 0x0; \
187  sockaddr.sa_data[5] = 0xfb;
188 
189 
190 //*****************************************************************************
191 //
192 // Prototypes for the APIs.
193 //
194 //*****************************************************************************
195 
196 //*****************************************************************************
197 //
215 //
216 //*****************************************************************************
217 extern INT32 socket(INT32 domain, INT32 type, INT32 protocol);
218 
219 //*****************************************************************************
220 //
228 //
229 //*****************************************************************************
230 extern INT32 closesocket(INT32 sd);
231 
232 //*****************************************************************************
233 //
274 //
275 //*****************************************************************************
276 extern INT32 accept(INT32 sd, sockaddr *addr, socklen_t *addrlen);
277 
278 //*****************************************************************************
279 //
298 //
299 //*****************************************************************************
300 extern INT32 bind(INT32 sd, const sockaddr *addr, INT32 addrlen);
301 
302 //*****************************************************************************
303 //
322 //
323 //*****************************************************************************
324 extern INT32 listen(INT32 sd, INT32 backlog);
325 
326 //*****************************************************************************
327 //
342 //
343 //*****************************************************************************
344 #ifndef CC3000_TINY_DRIVER
345 extern INT16 gethostbyname(CHAR * hostname, UINT16 usNameLen, UINT32* out_ip_addr);
346 #endif
347 
348 
349 //*****************************************************************************
350 //
375 //
376 //*****************************************************************************
377 extern INT32 connect(INT32 sd, const sockaddr *addr, INT32 addrlen);
378 
379 //*****************************************************************************
380 //
414 //
415 //*****************************************************************************
416 extern INT16 select(INT32 nfds, fd_set *readsds, fd_set *writesds,
417  fd_set *exceptsds, struct timeval *timeout);
418 
419 //*****************************************************************************
420 //
469 //
470 //*****************************************************************************
471 #ifndef CC3000_TINY_DRIVER
472 extern INT16 setsockopt(INT32 sd, INT32 level, INT32 optname, const void *optval,
473  socklen_t optlen);
474 #endif
475 //*****************************************************************************
476 //
526 //
527 //*****************************************************************************
528 extern INT16 getsockopt(INT32 sd, INT32 level, INT32 optname, void *optval,
529  socklen_t *optlen);
530 
531 //*****************************************************************************
532 //
550 //
551 //*****************************************************************************
552 extern INT16 recv(INT32 sd, void *buf, INT32 len, INT32 flags);
553 
554 //*****************************************************************************
555 //
580 //
581 //*****************************************************************************
582 extern INT16 recvfrom(INT32 sd, void *buf, INT32 len, INT32 flags, sockaddr *from,
583  socklen_t *fromlen);
584 
585 //*****************************************************************************
586 //
604 //
605 //*****************************************************************************
606 
607 extern INT16 send(INT32 sd, const void *buf, INT32 len, INT32 flags);
608 
609 //*****************************************************************************
610 //
632 //
633 //*****************************************************************************
634 
635 extern INT16 sendto(INT32 sd, const void *buf, INT32 len, INT32 flags,
636  const sockaddr *to, socklen_t tolen);
637 
638 //*****************************************************************************
639 //
652 //
653 //*****************************************************************************
654 extern INT16 mdnsAdvertiser(UINT16 mdnsEnabled, CHAR * deviceServiceName, UINT16 deviceServiceNameLength);
655 
656 //*****************************************************************************
657 //
665 //
666 //*****************************************************************************
667 extern UINT16 getmssvalue (INT32 sd);
668 
669 //*****************************************************************************
670 //
671 // Close the Doxygen group.
673 //
674 //*****************************************************************************
675 
676 
677 //*****************************************************************************
678 //
679 // Mark the end of the C bindings section for C++ compilers.
680 //
681 //*****************************************************************************
682 #ifdef __cplusplus
683 }
684 #endif // __cplusplus
685 
686 #endif // __SOCKET_H__
Definition: socket.h:140
INT32 connect(INT32 sd, const sockaddr *addr, INT32 addrlen)
initiate a connection on a socket Function connects the socket referred to by the socket descriptor s...
Definition: socket.c:526
INT32 listen(INT32 sd, INT32 backlog)
listen for connections on a socket The willingness to accept incoming connections and a queue limit f...
Definition: socket.c:416
INT16 mdnsAdvertiser(UINT16 mdnsEnabled, CHAR *deviceServiceName, UINT16 deviceServiceNameLength)
Set CC3000 in mDNS advertiser mode in order to advertise itself.
Definition: socket.c:1140
Definition: socket.h:110
Definition: socket.h:115
INT32 closesocket(INT32 sd)
The socket function closes a created socket.
Definition: socket.c:229
INT16 select(INT32 nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds, struct timeval *timeout)
Monitor socket activity Select allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation.
Definition: socket.c:593
INT16 recv(INT32 sd, void *buf, INT32 len, INT32 flags)
function receives a message from a connection-mode socket
Definition: socket.c:921
INT16 setsockopt(INT32 sd, INT32 level, INT32 optname, const void *optval, socklen_t optlen)
set socket options This function manipulate the options associated with a socket. Options may exist a...
Definition: socket.c:723
INT16 getsockopt(INT32 sd, INT32 level, INT32 optname, void *optval, socklen_t *optlen)
set socket options This function manipulate the options associated with a socket. Options may exist a...
Definition: socket.c:812
Definition: cc3000_common.h:172
INT32 accept(INT32 sd, sockaddr *addr, socklen_t *addrlen)
accept a connection on a socket: This function is used with connection-based socket types (SOCK_STREA...
Definition: socket.c:301
INT16 gethostbyname(CHAR *hostname, UINT16 usNameLen, UINT32 *out_ip_addr)
Get host IP by name. Obtain the IP Address of machine on network, by its name.
Definition: socket.c:460
INT32 bind(INT32 sd, const sockaddr *addr, INT32 addrlen)
assign a name to a socket This function gives the socket the local address addr. addr is addrlen byte...
Definition: socket.c:364
Definition: socket.h:121
UINT16 getmssvalue(INT32 sd)
Returns the MSS value of a TCP connection according to the socket descriptor.
Definition: socket.c:1447
INT16 sendto(INT32 sd, const void *buf, INT32 len, INT32 flags, const sockaddr *to, socklen_t tolen)
Write data to TCP socket This function is used to transmit a message to another socket.
Definition: socket.c:1116
INT16 recvfrom(INT32 sd, void *buf, INT32 len, INT32 flags, sockaddr *from, socklen_t *fromlen)
read data from socket function receives a message from a connection-mode or connectionless-mode socke...
Definition: socket.c:954
INT16 send(INT32 sd, const void *buf, INT32 len, INT32 flags)
Write data to TCP socket This function is used to transmit a message to another socket.
Definition: socket.c:1085
INT32 socket(INT32 domain, INT32 type, INT32 protocol)
create an endpoint for communication The socket function creates a socket that is bound to a specific...
Definition: socket.c:189