PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/VBox/Devices/Network/slirp/socket.h

https://bitbucket.org/diagiman/vbox-trunk
C Header | 204 lines | 105 code | 28 blank | 71 comment | 10 complexity | da53b2e7032ff578bb1d8f535bdfdb85 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, LGPL-2.1
  1. /* $Id$ */
  2. /** @file
  3. * NAT - socket handling (declarations/defines).
  4. */
  5. /*
  6. * Copyright (C) 2006-2010 Oracle Corporation
  7. *
  8. * This file is part of VirtualBox Open Source Edition (OSE), as
  9. * available from http://www.virtualbox.org. This file is free software;
  10. * you can redistribute it and/or modify it under the terms of the GNU
  11. * General Public License (GPL) as published by the Free Software
  12. * Foundation, in version 2 as it comes in the "COPYING" file of the
  13. * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
  14. * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
  15. */
  16. /*
  17. * This code is based on:
  18. *
  19. * Copyright (c) 1995 Danny Gasparovski.
  20. *
  21. * Please read the file COPYRIGHT for the
  22. * terms and conditions of the copyright.
  23. */
  24. /* MINE */
  25. #ifndef _SLIRP_SOCKET_H_
  26. #define _SLIRP_SOCKET_H_
  27. #define SO_EXPIRE 240000
  28. #define SO_EXPIREFAST 10000
  29. /*
  30. * Our socket structure
  31. */
  32. struct socket
  33. {
  34. struct socket *so_next;
  35. struct socket *so_prev; /* For a linked list of sockets */
  36. #if !defined(RT_OS_WINDOWS)
  37. int s; /* The actual socket */
  38. #else
  39. union {
  40. int s;
  41. HANDLE sh;
  42. };
  43. uint64_t so_icmp_id; /* XXX: hack */
  44. uint64_t so_icmp_seq; /* XXX: hack */
  45. #endif
  46. /* XXX union these with not-yet-used sbuf params */
  47. struct mbuf *so_m; /* Pointer to the original SYN packet,
  48. * for non-blocking connect()'s, and
  49. * PING reply's */
  50. struct tcpiphdr *so_ti; /* Pointer to the original ti within
  51. * so_mconn, for non-blocking connections */
  52. int so_urgc;
  53. struct in_addr so_faddr; /* foreign host table entry */
  54. struct in_addr so_laddr; /* local host table entry */
  55. u_int16_t so_fport; /* foreign port */
  56. u_int16_t so_lport; /* local port */
  57. u_int16_t so_hlport; /* host local port */
  58. struct in_addr so_hladdr; /* local host addr */
  59. u_int8_t so_iptos; /* Type of service */
  60. u_char so_type; /* Type of socket, UDP or TCP */
  61. int so_state; /* internal state flags SS_*, below */
  62. struct tcpcb *so_tcpcb; /* pointer to TCP protocol control block */
  63. u_int so_expire; /* When the socket will expire */
  64. int so_queued; /* Number of packets queued from this socket */
  65. int so_nqueued; /* Number of packets queued in a row
  66. * Used to determine when to "downgrade" a session
  67. * from fastq to batchq */
  68. struct sbuf so_rcv; /* Receive buffer */
  69. struct sbuf so_snd; /* Send buffer */
  70. #ifndef RT_OS_WINDOWS
  71. int so_poll_index;
  72. #endif /* !RT_OS_WINDOWS */
  73. /*
  74. * FD_CLOSE/POLLHUP event has been occurred on socket
  75. */
  76. int so_close;
  77. void (* so_timeout)(PNATState pData, struct socket *so, void *arg);
  78. void *so_timeout_arg;
  79. #ifdef VBOX_WITH_NAT_SERVICE
  80. /* storage of source ether address */
  81. unsigned char so_ethaddr[6];
  82. #endif
  83. /* required for port-forwarding */
  84. struct libalias *so_la;
  85. /* libalias might attach the socket and we want to notify libalias we're freeing it */
  86. void *so_pvLnk;
  87. #ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
  88. struct socket *so_cloneOf; /* pointer to master instance */
  89. int so_cCloneCounter; /* number of clones */
  90. #endif
  91. /** These flags (''fUnderPolling'' and ''fShouldBeRemoved'') introduced to
  92. * to let polling routine gain control over freeing socket whatever level of
  93. * TCP/IP initiated socket releasing.
  94. * So polling routine when start processing socket alter it's state to
  95. * ''fUnderPolling'' to 1, and clean (set to 0) when it finish.
  96. * When polling routine calls functions it should be ensure on return,
  97. * whether ''fShouldBeRemoved'' set or not, and depending on state call
  98. * ''sofree'' or continue socket processing.
  99. * On ''fShouldBeRemoved'' equal to 1, polling routine should call ''sofree'',
  100. * clearing ''fUnderPolling'' to do real freeng of the socket and removing from
  101. * the queue.
  102. * @todo: perhaps, to simplefy the things we need some helper function.
  103. * @note: it's used like a bool, I use 'int' to avoid compiler warnings
  104. * appearing if [-Wc++-compat] used.
  105. */
  106. int fUnderPolling;
  107. /** This flag used by ''sofree'' function in following manner
  108. *
  109. * fUnderPolling = 1, then we don't remove socket from the queue, just
  110. * alter value ''fShouldBeRemoved'' to 1, else we do removal.
  111. */
  112. int fShouldBeRemoved;
  113. };
  114. /* this function inform libalias about socket close */
  115. void slirpDeleteLinkSocket(void *pvLnk);
  116. # define SOCKET_LOCK(so) do {} while (0)
  117. # define SOCKET_UNLOCK(so) do {} while (0)
  118. # define SOCKET_LOCK_CREATE(so) do {} while (0)
  119. # define SOCKET_LOCK_DESTROY(so) do {} while (0)
  120. /*
  121. * Socket state bits. (peer means the host on the Internet,
  122. * local host means the host on the other end of the modem)
  123. */
  124. #define SS_NOFDREF 0x001 /* No fd reference */
  125. #define SS_ISFCONNECTING 0x002 /* Socket is connecting to peer (non-blocking connect()'s) */
  126. #define SS_ISFCONNECTED 0x004 /* Socket is connected to peer */
  127. #define SS_FCANTRCVMORE 0x008 /* Socket can't receive more from peer (for half-closes) */
  128. #define SS_FCANTSENDMORE 0x010 /* Socket can't send more to peer (for half-closes) */
  129. /* #define SS_ISFDISCONNECTED 0x020*/ /* Socket has disconnected from peer, in 2MSL state */
  130. #define SS_FWDRAIN 0x040 /* We received a FIN, drain data and set SS_FCANTSENDMORE */
  131. /* #define SS_CTL 0x080 */
  132. #define SS_FACCEPTCONN 0x100 /* Socket is accepting connections from a host on the internet */
  133. #define SS_FACCEPTONCE 0x200 /* If set, the SS_FACCEPTCONN socket will die after one accept */
  134. extern struct socket tcb;
  135. #if defined(DECLARE_IOVEC) && !defined(HAVE_READV)
  136. struct iovec
  137. {
  138. char *iov_base;
  139. size_t iov_len;
  140. };
  141. #endif
  142. void so_init (void);
  143. struct socket * solookup (struct socket *, struct in_addr, u_int, struct in_addr, u_int);
  144. struct socket * socreate (void);
  145. void sofree (PNATState, struct socket *);
  146. int soread (PNATState, struct socket *);
  147. void sorecvoob (PNATState, struct socket *);
  148. int sosendoob (struct socket *);
  149. int sowrite (PNATState, struct socket *);
  150. void sorecvfrom (PNATState, struct socket *);
  151. int sosendto (PNATState, struct socket *, struct mbuf *);
  152. struct socket * solisten (PNATState, u_int32_t, u_int, u_int32_t, u_int, int);
  153. void sorwakeup (struct socket *);
  154. void sowwakeup (struct socket *);
  155. void soisfconnecting (register struct socket *);
  156. void soisfconnected (register struct socket *);
  157. void sofcantrcvmore (struct socket *);
  158. void sofcantsendmore (struct socket *);
  159. void soisfdisconnected (struct socket *);
  160. void sofwdrain (struct socket *);
  161. /**
  162. * Creates copy of UDP socket with specified addr
  163. * fBindSocket - in case we want bind a real socket.
  164. * @return copy of the socket with f_addr equal to u32ForeignAddr
  165. */
  166. #ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
  167. struct socket * soCloneUDPSocketWithForegnAddr(PNATState pData, bool fBindSocket, struct socket *pSo, uint32_t u32ForeignAddr);
  168. struct socket *soLookUpClonedUDPSocket(PNATState pData, const struct socket *pcSo, uint32_t u32ForeignAddress);
  169. #endif
  170. static inline int soIgnorableErrorCode(int iErrorCode)
  171. {
  172. return ( iErrorCode == EINPROGRESS
  173. || iErrorCode == EAGAIN
  174. || iErrorCode == EWOULDBLOCK);
  175. }
  176. #endif /* _SOCKET_H_ */