UbixOS V2  2.0
api.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  *
36  */
37 #ifndef LWIP_HDR_API_H
38 #define LWIP_HDR_API_H
39 
40 #include "net/opt.h"
41 
42 #if LWIP_NETCONN || LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
43 /* Note: Netconn API is always available when sockets are enabled -
44  * sockets are implemented on top of them */
45 
46 #include "net/arch.h"
47 #include "net/netbuf.h"
48 #include "net/sys.h"
49 #include "net/ip_addr.h"
50 #include "net/err.h"
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 /* Throughout this file, IP addresses and port numbers are expected to be in
57  * the same byte order as in the corresponding pcb.
58  */
59 
60 /* Flags for netconn_write (u8_t) */
61 #define NETCONN_NOFLAG 0x00
62 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
63 #define NETCONN_COPY 0x01
64 #define NETCONN_MORE 0x02
65 #define NETCONN_DONTBLOCK 0x04
66 
67 /* Flags for struct netconn.flags (u8_t) */
69 #define NETCONN_FLAG_NON_BLOCKING 0x02
70 
71 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
72 
74 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
75 #if LWIP_IPV6
76 
79 #define NETCONN_FLAG_IPV6_V6ONLY 0x20
80 #endif /* LWIP_IPV6 */
81 
82 
83 /* Helpers to process several netconn_types by the same code */
84 #define NETCONNTYPE_GROUP(t) ((t)&0xF0)
85 #define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0)
86 #if LWIP_IPV6
87 #define NETCONN_TYPE_IPV6 0x08
88 #define NETCONNTYPE_ISIPV6(t) (((t)&NETCONN_TYPE_IPV6) != 0)
89 #define NETCONNTYPE_ISUDPLITE(t) (((t)&0xF3) == NETCONN_UDPLITE)
90 #define NETCONNTYPE_ISUDPNOCHKSUM(t) (((t)&0xF3) == NETCONN_UDPNOCHKSUM)
91 #else /* LWIP_IPV6 */
92 #define NETCONNTYPE_ISIPV6(t) (0)
93 #define NETCONNTYPE_ISUDPLITE(t) ((t) == NETCONN_UDPLITE)
94 #define NETCONNTYPE_ISUDPNOCHKSUM(t) ((t) == NETCONN_UDPNOCHKSUM)
95 #endif /* LWIP_IPV6 */
96 
100 enum netconn_type {
101  NETCONN_INVALID = 0,
103  NETCONN_TCP = 0x10,
104 #if LWIP_IPV6
105 
106  NETCONN_TCP_IPV6 = NETCONN_TCP | NETCONN_TYPE_IPV6 /* 0x18 */,
107 #endif /* LWIP_IPV6 */
108 
109  NETCONN_UDP = 0x20,
111  NETCONN_UDPLITE = 0x21,
113  NETCONN_UDPNOCHKSUM = 0x22,
114 
115 #if LWIP_IPV6
116 
117  NETCONN_UDP_IPV6 = NETCONN_UDP | NETCONN_TYPE_IPV6 /* 0x28 */,
119  NETCONN_UDPLITE_IPV6 = NETCONN_UDPLITE | NETCONN_TYPE_IPV6 /* 0x29 */,
121  NETCONN_UDPNOCHKSUM_IPV6 = NETCONN_UDPNOCHKSUM | NETCONN_TYPE_IPV6 /* 0x2a */,
122 #endif /* LWIP_IPV6 */
123 
125  NETCONN_RAW = 0x40
126 #if LWIP_IPV6
127 
128  , NETCONN_RAW_IPV6 = NETCONN_RAW | NETCONN_TYPE_IPV6 /* 0x48 */
129 #endif /* LWIP_IPV6 */
130 };
131 
134 enum netconn_state {
135  NETCONN_NONE,
136  NETCONN_WRITE,
137  NETCONN_LISTEN,
138  NETCONN_CONNECT,
139  NETCONN_CLOSE
140 };
141 
168 enum netconn_evt {
169  NETCONN_EVT_RCVPLUS,
170  NETCONN_EVT_RCVMINUS,
171  NETCONN_EVT_SENDPLUS,
172  NETCONN_EVT_SENDMINUS,
173  NETCONN_EVT_ERROR
174 };
175 
176 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
177 
178 enum netconn_igmp {
179  NETCONN_JOIN,
180  NETCONN_LEAVE
181 };
182 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
183 
184 #if LWIP_DNS
185 /* Used for netconn_gethostbyname_addrtype(), these should match the DNS_ADDRTYPE defines in dns.h */
186 #define NETCONN_DNS_DEFAULT NETCONN_DNS_IPV4_IPV6
187 #define NETCONN_DNS_IPV4 0
188 #define NETCONN_DNS_IPV6 1
189 #define NETCONN_DNS_IPV4_IPV6 2 /* try to resolve IPv4 first, try IPv6 if IPv4 fails only */
190 #define NETCONN_DNS_IPV6_IPV4 3 /* try to resolve IPv6 first, try IPv4 if IPv6 fails only */
191 #endif /* LWIP_DNS */
192 
193 /* forward-declare some structs to avoid to include their headers */
194 struct ip_pcb;
195 struct tcp_pcb;
196 struct udp_pcb;
197 struct raw_pcb;
198 struct netconn;
199 struct api_msg;
200 
202 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
203 
205 struct netconn {
207  enum netconn_type type;
209  enum netconn_state state;
211  union {
212  struct ip_pcb *ip;
213  struct tcp_pcb *tcp;
214  struct udp_pcb *udp;
215  struct raw_pcb *raw;
216  } pcb;
218  err_t last_err;
219 #if !LWIP_NETCONN_SEM_PER_THREAD
220 
221  sys_sem_t op_completed;
222 #endif
223 
225  sys_mbox_t recvmbox;
226 #if LWIP_TCP
227 
229  sys_mbox_t acceptmbox;
230 #endif /* LWIP_TCP */
231 
232 #if LWIP_SOCKET
233  int socket;
234 #endif /* LWIP_SOCKET */
235 #if LWIP_SO_SNDTIMEO
236 
238  s32_t send_timeout;
239 #endif /* LWIP_SO_RCVTIMEO */
240 #if LWIP_SO_RCVTIMEO
241 
243  int recv_timeout;
244 #endif /* LWIP_SO_RCVTIMEO */
245 #if LWIP_SO_RCVBUF
246 
248  int recv_bufsize;
252  int recv_avail;
253 #endif /* LWIP_SO_RCVBUF */
254 #if LWIP_SO_LINGER
255 
256  s16_t linger;
257 #endif /* LWIP_SO_LINGER */
258 
259  u8_t flags;
260 #if LWIP_TCP
261 
263  size_t write_offset;
267  struct api_msg *current_msg;
268 #endif /* LWIP_TCP */
269 
270  netconn_callback callback;
271 };
272 
274 #define API_EVENT(c,e,l) if (c->callback) { \
275  (*c->callback)(c, e, l); \
276  }
277 
279 #define NETCONN_SET_SAFE_ERR(conn, err) do { if ((conn) != NULL) { \
280  SYS_ARCH_DECL_PROTECT(netconn_set_safe_err_lev); \
281  SYS_ARCH_PROTECT(netconn_set_safe_err_lev); \
282  if (!ERR_IS_FATAL((conn)->last_err)) { \
283  (conn)->last_err = err; \
284  } \
285  SYS_ARCH_UNPROTECT(netconn_set_safe_err_lev); \
286 }} while(0);
287 
288 /* Network connection functions: */
289 
293 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
294 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
295 struct netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
296  netconn_callback callback);
297 err_t netconn_delete(struct netconn *conn);
299 #define netconn_type(conn) (conn->type)
300 
301 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
302  u16_t *port, u8_t local);
304 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
305 
306 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
307 
308 err_t netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port);
309 err_t netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port);
310 err_t netconn_disconnect (struct netconn *conn);
311 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
313 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
314 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
315 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
316 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
317 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
318  const ip_addr_t *addr, u16_t port);
319 err_t netconn_send(struct netconn *conn, struct netbuf *buf);
320 err_t netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
321  u8_t apiflags, size_t *bytes_written);
323 #define netconn_write(conn, dataptr, size, apiflags) \
324  netconn_write_partly(conn, dataptr, size, apiflags, NULL)
325 err_t netconn_close(struct netconn *conn);
326 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
327 
328 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
329 err_t netconn_join_leave_group(struct netconn *conn, const ip_addr_t *multiaddr,
330  const ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
331 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
332 #if LWIP_DNS
333 #if LWIP_IPV4 && LWIP_IPV6
334 err_t netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype);
335 #define netconn_gethostbyname(name, addr) netconn_gethostbyname_addrtype(name, addr, NETCONN_DNS_DEFAULT)
336 #else /* LWIP_IPV4 && LWIP_IPV6 */
337 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
338 #define netconn_gethostbyname_addrtype(name, addr, dns_addrtype) netconn_gethostbyname(name, addr)
339 #endif /* LWIP_IPV4 && LWIP_IPV6 */
340 #endif /* LWIP_DNS */
341 
342 #define netconn_err(conn) ((conn)->last_err)
343 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
344 
346 #define netconn_set_nonblocking(conn, val) do { if(val) { \
347  (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
348 } else { \
349  (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
350 
351 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
352 
353 #if LWIP_IPV6
354 
357 #define netconn_set_ipv6only(conn, val) do { if(val) { \
358  (conn)->flags |= NETCONN_FLAG_IPV6_V6ONLY; \
359 } else { \
360  (conn)->flags &= ~ NETCONN_FLAG_IPV6_V6ONLY; }} while(0)
361 
364 #define netconn_get_ipv6only(conn) (((conn)->flags & NETCONN_FLAG_IPV6_V6ONLY) != 0)
365 #endif /* LWIP_IPV6 */
366 
367 #if LWIP_SO_SNDTIMEO
368 
369 #define netconn_set_sendtimeout(conn, timeout) ((conn)->send_timeout = (timeout))
370 
371 #define netconn_get_sendtimeout(conn) ((conn)->send_timeout)
372 #endif /* LWIP_SO_SNDTIMEO */
373 #if LWIP_SO_RCVTIMEO
374 
375 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
376 
377 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
378 #endif /* LWIP_SO_RCVTIMEO */
379 #if LWIP_SO_RCVBUF
380 
381 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
382 
383 #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize)
384 #endif /* LWIP_SO_RCVBUF*/
385 
386 #if LWIP_NETCONN_SEM_PER_THREAD
387 void netconn_thread_init(void);
388 void netconn_thread_cleanup(void);
389 #else /* LWIP_NETCONN_SEM_PER_THREAD */
390 #define netconn_thread_init()
391 #define netconn_thread_cleanup()
392 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
393 
394 #ifdef __cplusplus
395 }
396 #endif
397 
398 #endif /* LWIP_NETCONN || LWIP_SOCKET */
399 
400 #endif /* LWIP_HDR_API_H */
sys.h
opt.h
s16_t
int16_t s16_t
Definition: arch.h:125
u16_t
uint16_t u16_t
Definition: arch.h:124
netbuf.h
sys_sem
Definition: sem.h:36
arch.h
s32_t
int32_t s32_t
Definition: arch.h:127
u8_t
uint8_t u8_t
Definition: arch.h:122
err.h
ip_addr_t
ip6_addr_t ip_addr_t
Definition: ip_addr.h:290
buf
Definition: buf.h:35
ip_pcb
Definition: ip.h:89
err_t
s8_t err_t
Definition: err.h:57
name
const char * name
Definition: pci.c:37
pbuf
Definition: pbuf.h:142
ip_addr.h
sys_mbox
Definition: sys_arch.h:22