PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/net/ip/udp-socket.h

https://github.com/jianhuiz/contiki
C Header | 193 lines | 33 code | 14 blank | 146 comment | 0 complexity | 3392761fd99d9dc227912226d2a90bc8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (c) 2012-2014, Thingsquare, http://www.thingsquare.com/.
  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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holder nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  26. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  28. * OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef UDP_SOCKET_H
  32. #define UDP_SOCKET_H
  33. #include "net/ip/uip.h"
  34. struct udp_socket;
  35. /**
  36. * \brief A UDP socket callback function
  37. * \param c A pointer to the struct udp_socket that received the data
  38. * \param ptr An opaque pointer that was specified when the UDP socket was registered with udp_socket_register()
  39. * \param source_addr The IP address from which the datagram was sent
  40. * \param source_port The UDP port number, in host byte order, from which the datagram was sent
  41. * \param dest_addr The IP address that this datagram was sent to
  42. * \param dest_port The UDP port number, in host byte order, that the datagram was sent to
  43. * \param data A pointer to the data contents of the UDP datagram
  44. * \param datalen The length of the data being pointed to by the data pointer
  45. *
  46. * Each UDP socket has a callback function that is
  47. * registered as part of the call to
  48. * udp_socket_register(). The callback function gets
  49. * called every time a UDP packet is received.
  50. */
  51. typedef void (* udp_socket_input_callback_t)(struct udp_socket *c,
  52. void *ptr,
  53. const uip_ipaddr_t *source_addr,
  54. uint16_t source_port,
  55. const uip_ipaddr_t *dest_addr,
  56. uint16_t dest_port,
  57. const uint8_t *data,
  58. uint16_t datalen);
  59. struct udp_socket {
  60. udp_socket_input_callback_t input_callback;
  61. void *ptr;
  62. struct process *p;
  63. struct uip_udp_conn *udp_conn;
  64. };
  65. /**
  66. * \brief Register a UDP socket
  67. * \param c A pointer to the struct udp_socket that should be registered
  68. * \param ptr An opaque pointer that will be passed to callbacks
  69. * \param receive_callback A function pointer to the callback function that will be called when data arrives
  70. * \retval -1 The registration failed
  71. * \retval 1 The registration succeeded
  72. *
  73. * This function registers the UDP socket with the
  74. * system. A UDP socket must be registered before any data
  75. * can be sent or received over the socket.
  76. *
  77. * The caller must allocate memory for the struct
  78. * udp_socket that is to be registered.
  79. *
  80. * A UDP socket can begin to receive data by calling
  81. * udp_socket_bind().
  82. *
  83. */
  84. int udp_socket_register(struct udp_socket *c,
  85. void *ptr,
  86. udp_socket_input_callback_t receive_callback);
  87. /**
  88. * \brief Bind a UDP socket to a local port
  89. * \param c A pointer to the struct udp_socket that should be bound to a local port
  90. * \param local_port The UDP port number, in host byte order, to bind the UDP socket to
  91. * \retval -1 Binding the UDP socket to the local port failed
  92. * \retval 1 Binding the UDP socket to the local port succeeded
  93. *
  94. * This function binds the UDP socket to a local port so
  95. * that it will begin to receive data that arrives on the
  96. * specified port. A UDP socket will receive data
  97. * addressed to the specified port number on any IP
  98. * address of the host.
  99. *
  100. * A UDP socket that is bound to a local port will use
  101. * this port number as a source port in outgoing UDP
  102. * messages.
  103. *
  104. */
  105. int udp_socket_bind(struct udp_socket *c,
  106. uint16_t local_port);
  107. /**
  108. * \brief Bind a UDP socket to a remote address and port
  109. * \param c A pointer to the struct udp_socket that should be connected
  110. * \param remote_addr The IP address of the remote host, or NULL if the UDP socket should only be connected to a specific port
  111. * \param remote_port The UDP port number, in host byte order, to which the UDP socket should be connected
  112. * \retval -1 Connecting the UDP socket failed
  113. * \retval 1 Connecting the UDP socket succeeded
  114. *
  115. * This function connects the UDP socket to a specific
  116. * remote port and optional remote IP address. When a UDP
  117. * socket is connected to a remote port and address, it
  118. * will only receive packets that are sent from the remote
  119. * port and address. When sending data over a connected
  120. * UDP socket, the data will be sent to the connected
  121. * remote address.
  122. *
  123. * A UDP socket can be connected to a remote port, but not
  124. * a remote IP address, by providing a NULL parameter as
  125. * the remote_addr parameter. This lets the UDP socket
  126. * receive data from any IP address on the specified port.
  127. *
  128. */
  129. int udp_socket_connect(struct udp_socket *c,
  130. uip_ipaddr_t *remote_addr,
  131. uint16_t remote_port);
  132. /**
  133. * \brief Send data on a UDP socket
  134. * \param c A pointer to the struct udp_socket on which the data should be sent
  135. * \param data A pointer to the data that should be sent
  136. * \param datalen The length of the data to be sent
  137. * \return The number of bytes sent, or -1 if an error occurred
  138. *
  139. * This function sends data over a UDP socket. The UDP
  140. * socket must have been connected to a remote address and
  141. * port with udp_socket_connect().
  142. *
  143. */
  144. int udp_socket_send(struct udp_socket *c,
  145. const void *data, uint16_t datalen);
  146. /**
  147. * \brief Send data on a UDP socket to a specific address and port
  148. * \param c A pointer to the struct udp_socket on which the data should be sent
  149. * \param data A pointer to the data that should be sent
  150. * \param datalen The length of the data to be sent
  151. * \param addr The IP address to which the data should be sent
  152. * \param port The UDP port number, in host byte order, to which the data should be sent
  153. * \return The number of bytes sent, or -1 if an error occurred
  154. *
  155. * This function sends data over a UDP socket to a
  156. * specific address and port.
  157. *
  158. * The UDP socket does not have to be connected to use
  159. * this function.
  160. *
  161. */
  162. int udp_socket_sendto(struct udp_socket *c,
  163. const void *data, uint16_t datalen,
  164. const uip_ipaddr_t *addr, uint16_t port);
  165. /**
  166. * \brief Close a UDP socket
  167. * \param c A pointer to the struct udp_socket to be closed
  168. * \retval -1 If closing the UDP socket failed
  169. * \retval 1 If closing the UDP socket succeeded
  170. *
  171. * This function closes a UDP socket that has previously
  172. * been registered with udp_socket_register(). All
  173. * registered UDP sockets must be closed before exiting
  174. * the process that registered them, or undefined behavior
  175. * may occur.
  176. *
  177. */
  178. int udp_socket_close(struct udp_socket *c);
  179. #endif /* UDP_SOCKET_H */