1 | /* $Id: socket.h 39299 2011-11-15 06:59:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - socket handling (declarations/defines).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
22 | *
|
---|
23 | * Please read the file COPYRIGHT for the
|
---|
24 | * terms and conditions of the copyright.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /* MINE */
|
---|
28 |
|
---|
29 | #ifndef _SLIRP_SOCKET_H_
|
---|
30 | #define _SLIRP_SOCKET_H_
|
---|
31 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
32 | #include <iprt/critsect.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #define SO_EXPIRE 240000
|
---|
36 | #define SO_EXPIREFAST 10000
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * Our socket structure
|
---|
40 | */
|
---|
41 |
|
---|
42 | struct socket
|
---|
43 | {
|
---|
44 | struct socket *so_next;
|
---|
45 | struct socket *so_prev; /* For a linked list of sockets */
|
---|
46 |
|
---|
47 | #if !defined(RT_OS_WINDOWS)
|
---|
48 | int s; /* The actual socket */
|
---|
49 | #else
|
---|
50 | union {
|
---|
51 | int s;
|
---|
52 | HANDLE sh;
|
---|
53 | };
|
---|
54 | uint64_t so_icmp_id; /* XXX: hack */
|
---|
55 | uint64_t so_icmp_seq; /* XXX: hack */
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | /* XXX union these with not-yet-used sbuf params */
|
---|
59 | struct mbuf *so_m; /* Pointer to the original SYN packet,
|
---|
60 | * for non-blocking connect()'s, and
|
---|
61 | * PING reply's */
|
---|
62 | struct tcpiphdr *so_ti; /* Pointer to the original ti within
|
---|
63 | * so_mconn, for non-blocking connections */
|
---|
64 | int so_urgc;
|
---|
65 | struct in_addr so_faddr; /* foreign host table entry */
|
---|
66 | struct in_addr so_laddr; /* local host table entry */
|
---|
67 | u_int16_t so_fport; /* foreign port */
|
---|
68 | u_int16_t so_lport; /* local port */
|
---|
69 | u_int16_t so_hlport; /* host local port */
|
---|
70 | struct in_addr so_hladdr; /* local host addr */
|
---|
71 |
|
---|
72 | u_int8_t so_iptos; /* Type of service */
|
---|
73 |
|
---|
74 | u_char so_type; /* Type of socket, UDP or TCP */
|
---|
75 | int so_state; /* internal state flags SS_*, below */
|
---|
76 |
|
---|
77 | struct tcpcb *so_tcpcb; /* pointer to TCP protocol control block */
|
---|
78 | u_int so_expire; /* When the socket will expire */
|
---|
79 |
|
---|
80 | int so_queued; /* Number of packets queued from this socket */
|
---|
81 | int so_nqueued; /* Number of packets queued in a row
|
---|
82 | * Used to determine when to "downgrade" a session
|
---|
83 | * from fastq to batchq */
|
---|
84 |
|
---|
85 | struct sbuf so_rcv; /* Receive buffer */
|
---|
86 | struct sbuf so_snd; /* Send buffer */
|
---|
87 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
88 | RTCRITSECT so_mutex;
|
---|
89 | int so_deleted;
|
---|
90 | #endif
|
---|
91 | #ifndef RT_OS_WINDOWS
|
---|
92 | int so_poll_index;
|
---|
93 | #endif /* !RT_OS_WINDOWS */
|
---|
94 | /*
|
---|
95 | * FD_CLOSE/POLLHUP event has been occurred on socket
|
---|
96 | */
|
---|
97 | int so_close;
|
---|
98 |
|
---|
99 | void (* so_timeout)(PNATState pData, struct socket *so, void *arg);
|
---|
100 | void *so_timeout_arg;
|
---|
101 |
|
---|
102 | #ifdef VBOX_WITH_NAT_SERVICE
|
---|
103 | /* storage of source ether address */
|
---|
104 | unsigned char so_ethaddr[6];
|
---|
105 | #endif
|
---|
106 | /* required for port-forwarding */
|
---|
107 | struct libalias *so_la;
|
---|
108 | #ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
|
---|
109 | struct socket *so_cloneOf; /* pointer to master instance */
|
---|
110 | int so_cCloneCounter; /* number of clones */
|
---|
111 | #endif
|
---|
112 | };
|
---|
113 |
|
---|
114 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
115 | # define SOCKET_LOCK(so) \
|
---|
116 | do { \
|
---|
117 | int rc; \
|
---|
118 | /* Assert(strcmp(RTThreadSelfName(), "EMT") != 0); */ \
|
---|
119 | Log2(("lock:%s:%d L on %R[natsock]\n", __FUNCTION__, __LINE__, (so))); \
|
---|
120 | Assert(!RTCritSectIsOwner(&(so)->so_mutex)); \
|
---|
121 | rc = RTCritSectEnter(&(so)->so_mutex); \
|
---|
122 | AssertRC(rc); \
|
---|
123 | } while (0)
|
---|
124 | # define SOCKET_UNLOCK(so) \
|
---|
125 | do { \
|
---|
126 | int rc; \
|
---|
127 | if ((so) != NULL) Log2(("lock:%s:%d U on %R[natsock]\n", __FUNCTION__, __LINE__, (so))); \
|
---|
128 | rc = RTCritSectLeave(&(so)->so_mutex); \
|
---|
129 | Assert(rc); \
|
---|
130 | } while (0)
|
---|
131 | # define SOCKET_LOCK_CREATE(so) \
|
---|
132 | do { \
|
---|
133 | int rc; \
|
---|
134 | rc = RTCritSectInit(&(so)->so_mutex); \
|
---|
135 | AssertRC(rc); \
|
---|
136 | } while (0)
|
---|
137 | # define SOCKET_LOCK_DESTROY(so) \
|
---|
138 | do { \
|
---|
139 | int rc = RTCritSectDelete(&(so)->so_mutex); \
|
---|
140 | AssertRC(rc); \
|
---|
141 | } while (0)
|
---|
142 | #else
|
---|
143 | # define SOCKET_LOCK(so) do {} while (0)
|
---|
144 | # define SOCKET_UNLOCK(so) do {} while (0)
|
---|
145 | # define SOCKET_LOCK_CREATE(so) do {} while (0)
|
---|
146 | # define SOCKET_LOCK_DESTROY(so) do {} while (0)
|
---|
147 | #endif
|
---|
148 | /*
|
---|
149 | * Socket state bits. (peer means the host on the Internet,
|
---|
150 | * local host means the host on the other end of the modem)
|
---|
151 | */
|
---|
152 | #define SS_NOFDREF 0x001 /* No fd reference */
|
---|
153 |
|
---|
154 | #define SS_ISFCONNECTING 0x002 /* Socket is connecting to peer (non-blocking connect()'s) */
|
---|
155 | #define SS_ISFCONNECTED 0x004 /* Socket is connected to peer */
|
---|
156 | #define SS_FCANTRCVMORE 0x008 /* Socket can't receive more from peer (for half-closes) */
|
---|
157 | #define SS_FCANTSENDMORE 0x010 /* Socket can't send more to peer (for half-closes) */
|
---|
158 | /* #define SS_ISFDISCONNECTED 0x020*/ /* Socket has disconnected from peer, in 2MSL state */
|
---|
159 | #define SS_FWDRAIN 0x040 /* We received a FIN, drain data and set SS_FCANTSENDMORE */
|
---|
160 |
|
---|
161 | /* #define SS_CTL 0x080 */
|
---|
162 | #define SS_FACCEPTCONN 0x100 /* Socket is accepting connections from a host on the internet */
|
---|
163 | #define SS_FACCEPTONCE 0x200 /* If set, the SS_FACCEPTCONN socket will die after one accept */
|
---|
164 |
|
---|
165 | extern struct socket tcb;
|
---|
166 |
|
---|
167 | #if defined(DECLARE_IOVEC) && !defined(HAVE_READV)
|
---|
168 | struct iovec
|
---|
169 | {
|
---|
170 | char *iov_base;
|
---|
171 | size_t iov_len;
|
---|
172 | };
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | void so_init (void);
|
---|
176 | struct socket * solookup (struct socket *, struct in_addr, u_int, struct in_addr, u_int);
|
---|
177 | struct socket * socreate (void);
|
---|
178 | void sofree (PNATState, struct socket *);
|
---|
179 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
180 | void soread_queue (PNATState, struct socket *, int *);
|
---|
181 | #endif
|
---|
182 | int soread (PNATState, struct socket *);
|
---|
183 | void sorecvoob (PNATState, struct socket *);
|
---|
184 | int sosendoob (struct socket *);
|
---|
185 | int sowrite (PNATState, struct socket *);
|
---|
186 | void sorecvfrom (PNATState, struct socket *);
|
---|
187 | int sosendto (PNATState, struct socket *, struct mbuf *);
|
---|
188 | struct socket * solisten (PNATState, u_int32_t, u_int, u_int32_t, u_int, int);
|
---|
189 | void sorwakeup (struct socket *);
|
---|
190 | void sowwakeup (struct socket *);
|
---|
191 | void soisfconnecting (register struct socket *);
|
---|
192 | void soisfconnected (register struct socket *);
|
---|
193 | void sofcantrcvmore (struct socket *);
|
---|
194 | void sofcantsendmore (struct socket *);
|
---|
195 | void soisfdisconnected (struct socket *);
|
---|
196 | void sofwdrain (struct socket *);
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Creates copy of UDP socket with specified addr
|
---|
200 | * fBindSocket - in case we want bind a real socket.
|
---|
201 | * @return copy of the socket with f_addr equal to u32ForeignAddr
|
---|
202 | */
|
---|
203 | #ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
|
---|
204 | struct socket * soCloneUDPSocketWithForegnAddr(PNATState pData, bool fBindSocket, struct socket *pSo, uint32_t u32ForeignAddr);
|
---|
205 | struct socket *soLookUpClonedUDPSocket(PNATState pData, const struct socket *pcSo, uint32_t u32ForeignAddress);
|
---|
206 | #endif
|
---|
207 |
|
---|
208 | #endif /* _SOCKET_H_ */
|
---|