api_msg.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. */
  32. #ifndef __LWIP_API_MSG_H__
  33. #define __LWIP_API_MSG_H__
  34. #include "lwip/opt.h"
  35. #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
  36. #include <stddef.h> /* for size_t */
  37. #include "lwip/ip_addr.h"
  38. #include "lwip/err.h"
  39. #include "lwip/sys.h"
  40. #include "lwip/igmp.h"
  41. #include "lwip/api.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /* For the netconn API, these values are use as a bitmask! */
  46. #define NETCONN_SHUT_RD 1
  47. #define NETCONN_SHUT_WR 2
  48. #define NETCONN_SHUT_RDWR (NETCONN_SHUT_RD | NETCONN_SHUT_WR)
  49. /* IP addresses and port numbers are expected to be in
  50. * the same byte order as in the corresponding pcb.
  51. */
  52. /** This struct includes everything that is necessary to execute a function
  53. for a netconn in another thread context (mainly used to process netconns
  54. in the tcpip_thread context to be thread safe). */
  55. struct api_msg_msg {
  56. /** The netconn which to process - always needed: it includes the semaphore
  57. which is used to block the application thread until the function finished. */
  58. struct netconn *conn;
  59. /** The return value of the function executed in tcpip_thread. */
  60. err_t err;
  61. /** Depending on the executed function, one of these union members is used */
  62. union {
  63. /** used for do_send */
  64. struct netbuf *b;
  65. /** used for do_newconn */
  66. struct {
  67. u8_t proto;
  68. } n;
  69. /** used for do_bind and do_connect */
  70. struct {
  71. ip_addr_t *ipaddr;
  72. u16_t port;
  73. } bc;
  74. /** used for do_getaddr */
  75. struct {
  76. ip_addr_t *ipaddr;
  77. u16_t *port;
  78. u8_t local;
  79. } ad;
  80. /** used for do_write */
  81. struct {
  82. const void *dataptr;
  83. size_t len;
  84. u8_t apiflags;
  85. } w;
  86. /** used for do_recv */
  87. struct {
  88. u32_t len;
  89. } r;
  90. /** used for do_close (/shutdown) */
  91. struct {
  92. u8_t shut;
  93. } sd;
  94. #if LWIP_IGMP
  95. /** used for do_join_leave_group */
  96. struct {
  97. ip_addr_t *multiaddr;
  98. ip_addr_t *netif_addr;
  99. enum netconn_igmp join_or_leave;
  100. } jl;
  101. #endif /* LWIP_IGMP */
  102. #if TCP_LISTEN_BACKLOG
  103. struct {
  104. u8_t backlog;
  105. } lb;
  106. #endif /* TCP_LISTEN_BACKLOG */
  107. } msg;
  108. };
  109. /** This struct contains a function to execute in another thread context and
  110. a struct api_msg_msg that serves as an argument for this function.
  111. This is passed to tcpip_apimsg to execute functions in tcpip_thread context. */
  112. struct api_msg {
  113. /** function to execute in tcpip_thread context */
  114. void (* function)(struct api_msg_msg *msg);
  115. /** arguments for this function */
  116. struct api_msg_msg msg;
  117. };
  118. #if LWIP_DNS
  119. /** As do_gethostbyname requires more arguments but doesn't require a netconn,
  120. it has its own struct (to avoid struct api_msg getting bigger than necessary).
  121. do_gethostbyname must be called using tcpip_callback instead of tcpip_apimsg
  122. (see netconn_gethostbyname). */
  123. struct dns_api_msg {
  124. /** Hostname to query or dotted IP address string */
  125. const char *name;
  126. /** Rhe resolved address is stored here */
  127. ip_addr_t *addr;
  128. /** This semaphore is posted when the name is resolved, the application thread
  129. should wait on it. */
  130. sys_sem_t *sem;
  131. /** Errors are given back here */
  132. err_t *err;
  133. };
  134. #endif /* LWIP_DNS */
  135. void do_newconn ( struct api_msg_msg *msg);
  136. void do_delconn ( struct api_msg_msg *msg);
  137. void do_bind ( struct api_msg_msg *msg);
  138. void do_connect ( struct api_msg_msg *msg);
  139. void do_disconnect ( struct api_msg_msg *msg);
  140. void do_listen ( struct api_msg_msg *msg);
  141. void do_send ( struct api_msg_msg *msg);
  142. void do_recv ( struct api_msg_msg *msg);
  143. void do_write ( struct api_msg_msg *msg);
  144. void do_getaddr ( struct api_msg_msg *msg);
  145. void do_close ( struct api_msg_msg *msg);
  146. void do_shutdown ( struct api_msg_msg *msg);
  147. #if LWIP_IGMP
  148. void do_join_leave_group( struct api_msg_msg *msg);
  149. #endif /* LWIP_IGMP */
  150. #if LWIP_DNS
  151. void do_gethostbyname(void *arg);
  152. #endif /* LWIP_DNS */
  153. struct netconn* netconn_alloc(enum netconn_type t, netconn_callback callback);
  154. void netconn_free(struct netconn *conn);
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158. #endif /* LWIP_NETCONN */
  159. #endif /* __LWIP_API_MSG_H__ */