PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/FNET/fnet_stack/stack/fnet_socket_prv.h

https://gitlab.com/fuggles/ucos
C Header | 206 lines | 106 code | 28 blank | 72 comment | 0 complexity | 10a2d8969baf92c7c81e919e8631d614 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /**************************************************************************
  2. *
  3. * Copyright 2011-2015 by Andrey Butok. FNET Community.
  4. * Copyright 2008-2010 by Andrey Butok. Freescale Semiconductor, Inc.
  5. *
  6. ***************************************************************************
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License Version 3
  9. * or later (the "LGPL").
  10. *
  11. * As a special exception, the copyright holders of the FNET project give you
  12. * permission to link the FNET sources with independent modules to produce an
  13. * executable, regardless of the license terms of these independent modules,
  14. * and to copy and distribute the resulting executable under terms of your
  15. * choice, provided that you also meet, for each linked independent module,
  16. * the terms and conditions of the license of that module.
  17. * An independent module is a module which is not derived from or based
  18. * on this library.
  19. * If you modify the FNET sources, you may extend this exception
  20. * to your version of the FNET sources, but you are not obligated
  21. * to do so. If you do not wish to do so, delete this
  22. * exception statement from your version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * and the GNU Lesser General Public License along with this program.
  30. * If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. **********************************************************************/ /*!
  33. *
  34. * @file fnet_socket_prv.h
  35. *
  36. * @author Andrey Butok
  37. *
  38. * @brief Private. Socket API.
  39. *
  40. ***************************************************************************/
  41. #ifndef _FNET_SOCKET_PRV_H_
  42. #define _FNET_SOCKET_PRV_H_
  43. #include "fnet_error.h"
  44. #include "fnet_socket.h"
  45. #if FNET_CFG_TCP
  46. #include "fnet_tcp.h"
  47. #endif
  48. #include "fnet_netbuf.h"
  49. #include "fnet_ip_prv.h"
  50. #include "fnet_ip6_prv.h"
  51. /************************************************************************
  52. * Definitions.
  53. *************************************************************************/
  54. /* A allocated port will always be
  55. * FNET_SOCKET_PORT_RESERVED < local_port <= FNET_SOCKET_PORT_USERRESERVED (ephemeral port)
  56. */
  57. #define FNET_SOCKET_PORT_RESERVED (1024) /* In host byte order.*/
  58. #define FNET_SOCKET_PORT_USERRESERVED (5000) /* In host byte order.*/
  59. #define FNET_SOCKET_DESC_RESERVED (-1) /* The descriptor is reserved.*/
  60. extern int fnet_enabled;
  61. /**************************************************************************/ /*!
  62. * @internal
  63. * @brief Structure of socket buffer.
  64. ******************************************************************************/
  65. typedef struct
  66. {
  67. unsigned long count; /**< Aactual chars in buffer.*/
  68. unsigned long count_max; /**< Max actual char count (9*1024).*/
  69. fnet_netbuf_t *net_buf_chain; /**< The net_buf chain.*/
  70. int is_shutdown; /**< The socket has been shut down for read/write.*/
  71. } fnet_socket_buffer_t;
  72. /**************************************************************************/ /*!
  73. * @internal
  74. * @brief Structure contains parameter of receive datagram
  75. * (only for SOCK_DGRAM).
  76. ******************************************************************************/
  77. typedef struct
  78. {
  79. struct sockaddr addr_s;
  80. } fnet_socket_buffer_addr_t;
  81. /**************************************************************************/ /*!
  82. * @internal
  83. * @brief Socket options.
  84. ******************************************************************************/
  85. typedef struct
  86. {
  87. #if FNET_CFG_IP4
  88. fnet_ip_sockopt_t ip_opt; /**< IP options.*/
  89. #endif
  90. #if FNET_CFG_IP6
  91. fnet_ip6_sockopt_t ip6_opt; /**< IP options.*/
  92. #endif
  93. #if FNET_CFG_TCP
  94. fnet_tcp_sockopt_t tcp_opt; /**< TCP options.*/
  95. #endif
  96. int error; /**< Socket last error.*/
  97. int local_error; /**< Socket local error (ICMP, on timeout).*/
  98. int flags; /**< Socket flags.*/
  99. int linger_ticks; /**< Lingers on close if unsent data is present (in timer ticks).*/
  100. } fnet_socket_option_t;
  101. /************************************************************************
  102. * Structure per socket.
  103. *************************************************************************/
  104. typedef struct _socket
  105. {
  106. struct _socket *next; /**< Pointer to the next socket structure.*/
  107. struct _socket *prev; /**< Pointer to the previous socket structure.*/
  108. SOCKET descriptor; /**< Socket descriptor.*/
  109. fnet_socket_state_t state; /**< Socket state.*/
  110. int protocol_number; /**< Protocol number.*/
  111. struct fnet_prot_if *protocol_interface; /**< Points to protocol specific functions (interface).*/
  112. void *protocol_control; /**< Points to protocol control structure (optional).*/
  113. /* For sockets with SO_ACCEPTCONN.*/
  114. struct _socket *partial_con; /**< Queue of partial connections.*/
  115. int partial_con_len; /**< Number of connections on partial_con.*/
  116. struct _socket *incoming_con; /**< Queue of incoming connections.*/
  117. int incoming_con_len; /**< Number of connections on incoming_con.*/
  118. int con_limit; /**< Max number queued connections (specified by "listen").*/
  119. struct _socket *head_con; /**< Back pointer to accept socket.*/
  120. fnet_socket_buffer_t receive_buffer; /**< Socket buffer for incoming data.*/
  121. fnet_socket_buffer_t send_buffer; /**< Socket buffer for outgoing data.*/
  122. /* Common protocol params.*/
  123. struct sockaddr foreign_addr; /**< Foreign socket address.*/
  124. struct sockaddr local_addr; /**< Lockal socket address.*/
  125. fnet_socket_option_t options; /**< Collection of socket options.*/
  126. #if FNET_CFG_MULTICAST
  127. /* Multicast params.*/
  128. #if FNET_CFG_IP4
  129. fnet_ip4_multicast_list_entry_t *ip4_multicast_entry[FNET_CFG_MULTICAST_SOCKET_MAX];
  130. #endif
  131. #if FNET_CFG_IP6
  132. fnet_ip6_multicast_list_entry_t *ip6_multicast_entry[FNET_CFG_MULTICAST_SOCKET_MAX];
  133. #endif
  134. #endif /* FNET_CFG_MULTICAST */
  135. } fnet_socket_t;
  136. /**************************************************************************/ /*!
  137. * @internal
  138. * @brief Transport Protocol interface general API structure.
  139. ******************************************************************************/
  140. typedef struct fnet_socket_prot_if
  141. {
  142. int con_req; /* Flag that protocol is connection oriented.*/
  143. int (*prot_attach)(fnet_socket_t *sk); /* Protocol "attach" function. */
  144. int (*prot_detach)(fnet_socket_t *sk); /* Protocol "detach" function. */
  145. int (*prot_connect)(fnet_socket_t *sk, struct sockaddr *foreign_addr); /* Protocol "connect" function. */
  146. fnet_socket_t *( *prot_accept)(fnet_socket_t * sk); /* Protocol "accept" function. */
  147. int (*prot_rcv)(fnet_socket_t *sk, char *buf, int len, int flags, struct sockaddr *foreign_addr ); /* Protocol "receive" function. */
  148. int (*prot_snd)(fnet_socket_t *sk, char *buf, int len, int flags, const struct sockaddr *foreign_addr ); /* Protocol "send" function. */
  149. int (*prot_shutdown)(fnet_socket_t *sk, int how); /* Protocol "shutdown" function. */
  150. int (*prot_setsockopt)(fnet_socket_t *sk, int level, int optname, char *optval, unsigned int optlen); /* Protocol "setsockopt" function. */
  151. int (*prot_getsockopt)(fnet_socket_t *sk, int level, int optname, char *optval, unsigned int *optlen); /* Protocol "getsockopt" function. */
  152. int (*prot_listen)(fnet_socket_t *sk, int backlog); /* Protocol "listen" function.*/
  153. } fnet_socket_prot_if_t;
  154. /************************************************************************
  155. * Function Prototypes
  156. *************************************************************************/
  157. void fnet_socket_init( void );
  158. void fnet_socket_list_add( fnet_socket_t ** head, fnet_socket_t *s );
  159. void fnet_socket_list_del( fnet_socket_t ** head, fnet_socket_t *s );
  160. void fnet_socket_set_error( fnet_socket_t *sock, int error );
  161. fnet_socket_t *fnet_socket_lookup( fnet_socket_t *head, struct sockaddr *local_addr, struct sockaddr *foreign_addr, int protocol_number);
  162. unsigned short fnet_socket_get_uniqueport(fnet_socket_t *head, struct sockaddr *local_addr);
  163. int fnet_socket_conflict( fnet_socket_t *head, const struct sockaddr *local_addr,
  164. const struct sockaddr *foreign_addr /*optional*/, int wildcard );
  165. fnet_socket_t *fnet_socket_copy( fnet_socket_t *sock );
  166. void fnet_socket_release( fnet_socket_t ** head, fnet_socket_t *sock );
  167. int fnet_socket_buffer_append_address( fnet_socket_buffer_t *sb, fnet_netbuf_t *nb, struct sockaddr *addr);
  168. int fnet_socket_buffer_append_record( fnet_socket_buffer_t *sb, fnet_netbuf_t *nb );
  169. int fnet_socket_buffer_read_address( fnet_socket_buffer_t *sb, char *buf, int len, struct sockaddr *foreign_addr, int remove );
  170. int fnet_socket_buffer_read_record( fnet_socket_buffer_t *sb, char *buf, int len, int remove );
  171. void fnet_socket_buffer_release( fnet_socket_buffer_t *sb );
  172. int fnet_ip_setsockopt( fnet_socket_t *sock, int level, int optname, char *optval, unsigned int optlen );
  173. int fnet_ip_getsockopt( fnet_socket_t *sock, int level, int optname, char *optval, unsigned int *optlen );
  174. int fnet_socket_addr_is_broadcast(const struct sockaddr *addr, fnet_netif_t *netif);
  175. void fnet_socket_ip_addr_copy(const struct sockaddr *from_addr, struct sockaddr *to_addr);
  176. void fnet_socket_addr_copy(const struct sockaddr *from_addr, struct sockaddr *to_addr);
  177. fnet_netif_t *fnet_socket_addr_route(const struct sockaddr *dest_addr);
  178. #endif /* _FNET_SOCKET_PRV_H_ */