1 | #ifndef __COMMON_H__
|
---|
2 | #define __COMMON_H__
|
---|
3 |
|
---|
4 | #ifdef RT_OS_WINDOWS
|
---|
5 | # include <winsock2.h>
|
---|
6 | typedef int socklen_t;
|
---|
7 | #endif
|
---|
8 | #ifdef RT_OS_OS2 /* temporary workaround, see ticket #127 */
|
---|
9 | # define mbstat mbstat_os2
|
---|
10 | # include <sys/socket.h>
|
---|
11 | # undef mbstat
|
---|
12 | typedef int socklen_t;
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | #define CONFIG_QEMU
|
---|
16 |
|
---|
17 | #ifdef DEBUG
|
---|
18 | # undef DEBUG
|
---|
19 | # define DEBUG 1
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #ifndef CONFIG_QEMU
|
---|
23 | #include "version.h"
|
---|
24 | #endif
|
---|
25 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
26 | #include <VBox/log.h>
|
---|
27 | #include <iprt/mem.h>
|
---|
28 | #ifdef RT_OS_WINDOWS
|
---|
29 | # include <windows.h>
|
---|
30 | # include <io.h>
|
---|
31 | #endif
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/dir.h>
|
---|
35 | #include <VBox/types.h>
|
---|
36 |
|
---|
37 | # define malloc(a) RTMemAllocZ(a)
|
---|
38 | # define free(a) RTMemFree(a)
|
---|
39 | # define realloc(a,b) RTMemRealloc(a, b)
|
---|
40 |
|
---|
41 | #include "slirp_config.h"
|
---|
42 |
|
---|
43 | #ifdef _WIN32
|
---|
44 |
|
---|
45 | #ifndef _MSC_VER
|
---|
46 | # include <inttypes.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | typedef uint8_t u_int8_t;
|
---|
50 | typedef uint16_t u_int16_t;
|
---|
51 | typedef uint32_t u_int32_t;
|
---|
52 | typedef uint64_t u_int64_t;
|
---|
53 | typedef char *caddr_t;
|
---|
54 |
|
---|
55 | # include <sys/timeb.h>
|
---|
56 | # include <iphlpapi.h>
|
---|
57 |
|
---|
58 | # define EWOULDBLOCK WSAEWOULDBLOCK
|
---|
59 | # define EINPROGRESS WSAEINPROGRESS
|
---|
60 | # define ENOTCONN WSAENOTCONN
|
---|
61 | # define EHOSTUNREACH WSAEHOSTUNREACH
|
---|
62 | # define ENETUNREACH WSAENETUNREACH
|
---|
63 | # define ECONNREFUSED WSAECONNREFUSED
|
---|
64 | #else
|
---|
65 | # define ioctlsocket ioctl
|
---|
66 | # define closesocket(s) close(s)
|
---|
67 | # define O_BINARY 0
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | #include <sys/types.h>
|
---|
71 | #ifdef HAVE_SYS_BITYPES_H
|
---|
72 | # include <sys/bitypes.h>
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | #ifdef _MSC_VER
|
---|
76 | #include <time.h>
|
---|
77 | #else /* !_MSC_VER */
|
---|
78 | #include <sys/time.h>
|
---|
79 | #endif /* !_MSC_VER */
|
---|
80 |
|
---|
81 | #ifdef NEED_TYPEDEFS
|
---|
82 | typedef char int8_t;
|
---|
83 | typedef unsigned char u_int8_t;
|
---|
84 |
|
---|
85 | # if SIZEOF_SHORT == 2
|
---|
86 | typedef short int16_t;
|
---|
87 | typedef unsigned short u_int16_t;
|
---|
88 | # else
|
---|
89 | # if SIZEOF_INT == 2
|
---|
90 | typedef int int16_t;
|
---|
91 | typedef unsigned int u_int16_t;
|
---|
92 | # else
|
---|
93 | #error Cannot find a type with sizeof() == 2
|
---|
94 | # endif
|
---|
95 | # endif
|
---|
96 |
|
---|
97 | # if SIZEOF_SHORT == 4
|
---|
98 | typedef short int32_t;
|
---|
99 | typedef unsigned short u_int32_t;
|
---|
100 | # else
|
---|
101 | # if SIZEOF_INT == 4
|
---|
102 | typedef int int32_t;
|
---|
103 | typedef unsigned int u_int32_t;
|
---|
104 | # else
|
---|
105 | #error Cannot find a type with sizeof() == 4
|
---|
106 | # endif
|
---|
107 | # endif
|
---|
108 | #endif /* NEED_TYPEDEFS */
|
---|
109 |
|
---|
110 | #ifdef HAVE_UNISTD_H
|
---|
111 | # include <unistd.h>
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | #ifdef HAVE_STDLIB_H
|
---|
115 | # include <stdlib.h>
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | #include <stdio.h>
|
---|
119 | #include <errno.h>
|
---|
120 |
|
---|
121 | #ifndef HAVE_MEMMOVE
|
---|
122 | #define memmove(x, y, z) bcopy(y, x, z)
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | #if TIME_WITH_SYS_TIME
|
---|
126 | # include <sys/time.h>
|
---|
127 | # include <time.h>
|
---|
128 | #else
|
---|
129 | # if HAVE_SYS_TIME_H
|
---|
130 | # include <sys/time.h>
|
---|
131 | # else
|
---|
132 | # include <time.h>
|
---|
133 | # endif
|
---|
134 | #endif
|
---|
135 |
|
---|
136 | #ifdef HAVE_STRING_H
|
---|
137 | # include <string.h>
|
---|
138 | #else
|
---|
139 | # include <strings.h>
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | #ifndef _WIN32
|
---|
143 | #include <sys/uio.h>
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | #ifndef _P
|
---|
147 | #ifndef NO_PROTOTYPES
|
---|
148 | # define _P(x) x
|
---|
149 | #else
|
---|
150 | # define _P(x) ()
|
---|
151 | #endif
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | #ifndef _WIN32
|
---|
155 | #include <netinet/in.h>
|
---|
156 | #include <arpa/inet.h>
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | #ifdef GETTIMEOFDAY_ONE_ARG
|
---|
160 | #define gettimeofday(x, y) gettimeofday(x)
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | /* Systems lacking strdup() definition in <string.h>. */
|
---|
164 | #if defined(ultrix)
|
---|
165 | char *strdup _P((const char *));
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | /* Systems lacking malloc() definition in <stdlib.h>. */
|
---|
169 | #if defined(ultrix) || defined(hcx)
|
---|
170 | void *malloc _P((size_t arg));
|
---|
171 | void free _P((void *ptr));
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | #ifndef HAVE_INET_ATON
|
---|
175 | int inet_aton _P((const char *cp, struct in_addr *ia));
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | #include <fcntl.h>
|
---|
179 | #ifndef NO_UNIX_SOCKETS
|
---|
180 | #include <sys/un.h>
|
---|
181 | #endif
|
---|
182 | #include <signal.h>
|
---|
183 | #ifdef HAVE_SYS_SIGNAL_H
|
---|
184 | # include <sys/signal.h>
|
---|
185 | #endif
|
---|
186 | #ifndef _WIN32
|
---|
187 | #include <sys/socket.h>
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | #if defined(HAVE_SYS_IOCTL_H)
|
---|
191 | # include <sys/ioctl.h>
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | #ifdef HAVE_SYS_SELECT_H
|
---|
195 | # include <sys/select.h>
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | #ifdef HAVE_SYS_WAIT_H
|
---|
199 | # include <sys/wait.h>
|
---|
200 | #endif
|
---|
201 |
|
---|
202 | #ifdef HAVE_SYS_FILIO_H
|
---|
203 | # include <sys/filio.h>
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | #ifdef USE_PPP
|
---|
207 | #include <ppp/slirppp.h>
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | #if defined(__STDC__) || defined(_MSC_VER)
|
---|
211 | #include <stdarg.h>
|
---|
212 | #else
|
---|
213 | #include <varargs.h>
|
---|
214 | #endif
|
---|
215 |
|
---|
216 | #include <sys/stat.h>
|
---|
217 |
|
---|
218 | #if 1 /* ndef _MSC_VER */
|
---|
219 | /* Avoid conflicting with the libc insque() and remque(), which
|
---|
220 | have different prototypes. */
|
---|
221 | #define insque slirp_insque
|
---|
222 | #define remque slirp_remque
|
---|
223 | #endif /* !_MSC_VER */
|
---|
224 |
|
---|
225 | #ifdef HAVE_SYS_STROPTS_H
|
---|
226 | #include <sys/stropts.h>
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | #include "libslirp.h"
|
---|
230 |
|
---|
231 | #include "debug.h"
|
---|
232 |
|
---|
233 | #include "ip.h"
|
---|
234 | #include "tcp.h"
|
---|
235 | #include "tcp_timer.h"
|
---|
236 | #include "tcp_var.h"
|
---|
237 | #include "tcpip.h"
|
---|
238 | #include "udp.h"
|
---|
239 | #include "icmp_var.h"
|
---|
240 | #include "mbuf.h"
|
---|
241 | #include "sbuf.h"
|
---|
242 | #include "socket.h"
|
---|
243 | #include "if.h"
|
---|
244 | #include "main.h"
|
---|
245 | #include "misc.h"
|
---|
246 | #include "ctl.h"
|
---|
247 | #ifdef USE_PPP
|
---|
248 | #include "ppp/pppd.h"
|
---|
249 | #include "ppp/ppp.h"
|
---|
250 | #endif
|
---|
251 |
|
---|
252 | #include "bootp.h"
|
---|
253 | #include "tftp.h"
|
---|
254 |
|
---|
255 | #include "slirp_state.h"
|
---|
256 |
|
---|
257 | #ifndef NULL
|
---|
258 | #define NULL (void *)0
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | void if_start _P((PNATState));
|
---|
262 |
|
---|
263 | #ifdef BAD_SPRINTF
|
---|
264 | # define vsprintf vsprintf_len
|
---|
265 | # define sprintf sprintf_len
|
---|
266 | extern int vsprintf_len _P((char *, const char *, va_list));
|
---|
267 | extern int sprintf_len _P((char *, const char *, ...));
|
---|
268 | #endif
|
---|
269 |
|
---|
270 | #ifdef DECLARE_SPRINTF
|
---|
271 | # ifndef BAD_SPRINTF
|
---|
272 | extern int vsprintf _P((char *, const char *, va_list));
|
---|
273 | # endif
|
---|
274 | extern int vfprintf _P((FILE *, const char *, va_list));
|
---|
275 | #endif
|
---|
276 |
|
---|
277 | #ifndef HAVE_STRERROR
|
---|
278 | extern char *strerror _P((int error));
|
---|
279 | #endif
|
---|
280 |
|
---|
281 | #ifndef HAVE_INDEX
|
---|
282 | char *index _P((const char *, int));
|
---|
283 | #endif
|
---|
284 |
|
---|
285 | #ifndef HAVE_GETHOSTID
|
---|
286 | long gethostid _P((void));
|
---|
287 | #endif
|
---|
288 |
|
---|
289 | DECLINLINE(void) lprint (const char *pszFormat, ...)
|
---|
290 | {
|
---|
291 | #ifdef LOG_ENABLED
|
---|
292 | va_list args;
|
---|
293 | va_start(args, pszFormat);
|
---|
294 | #if 1 /* nearly only for stats which we always want. */
|
---|
295 | RTLogPrintfV(pszFormat, args);
|
---|
296 | #else
|
---|
297 | Log(("%N", pszFormat, &args));
|
---|
298 | #endif
|
---|
299 | va_end(args);
|
---|
300 | #endif
|
---|
301 | }
|
---|
302 |
|
---|
303 | extern int do_echo;
|
---|
304 |
|
---|
305 | #if SIZEOF_CHAR_P == 4
|
---|
306 | # define insque_32 insque
|
---|
307 | # define remque_32 remque
|
---|
308 | #else
|
---|
309 | extern void insque_32 _P((PNATState, void *, void *));
|
---|
310 | extern void remque_32 _P((PNATState, void *));
|
---|
311 | #endif
|
---|
312 |
|
---|
313 | #ifndef _WIN32
|
---|
314 | #include <netdb.h>
|
---|
315 | #endif
|
---|
316 |
|
---|
317 | #define DEFAULT_BAUD 115200
|
---|
318 |
|
---|
319 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr);
|
---|
320 |
|
---|
321 | /* cksum.c */
|
---|
322 | int cksum(struct mbuf *m, int len);
|
---|
323 |
|
---|
324 | /* if.c */
|
---|
325 | void if_init _P((PNATState));
|
---|
326 | void if_output _P((PNATState, struct socket *, struct mbuf *));
|
---|
327 |
|
---|
328 | /* ip_input.c */
|
---|
329 | void ip_init _P((PNATState));
|
---|
330 | void ip_input _P((PNATState, struct mbuf *));
|
---|
331 | struct ip * ip_reass _P((PNATState, register struct ipasfrag *, register struct ipq_t *));
|
---|
332 | void ip_freef _P((PNATState, struct ipq_t *));
|
---|
333 | void ip_enq _P((PNATState, register struct ipasfrag *, register struct ipasfrag *));
|
---|
334 | void ip_deq _P((PNATState, register struct ipasfrag *));
|
---|
335 | void ip_slowtimo _P((PNATState));
|
---|
336 | void ip_stripoptions _P((register struct mbuf *, struct mbuf *));
|
---|
337 |
|
---|
338 | /* ip_output.c */
|
---|
339 | int ip_output _P((PNATState, struct socket *, struct mbuf *));
|
---|
340 |
|
---|
341 | /* tcp_input.c */
|
---|
342 | int tcp_reass _P((PNATState, register struct tcpcb *, register struct tcpiphdr *, struct mbuf *));
|
---|
343 | void tcp_input _P((PNATState, register struct mbuf *, int, struct socket *));
|
---|
344 | void tcp_dooptions _P((PNATState, struct tcpcb *, u_char *, int, struct tcpiphdr *));
|
---|
345 | void tcp_xmit_timer _P((PNATState, register struct tcpcb *, int));
|
---|
346 | int tcp_mss _P((PNATState, register struct tcpcb *, u_int));
|
---|
347 |
|
---|
348 | /* tcp_output.c */
|
---|
349 | int tcp_output _P((PNATState, register struct tcpcb *));
|
---|
350 | void tcp_setpersist _P((register struct tcpcb *));
|
---|
351 |
|
---|
352 | /* tcp_subr.c */
|
---|
353 | void tcp_init _P((PNATState));
|
---|
354 | void tcp_template _P((struct tcpcb *));
|
---|
355 | void tcp_respond _P((PNATState, struct tcpcb *, register struct tcpiphdr *, register struct mbuf *, tcp_seq, tcp_seq, int));
|
---|
356 | struct tcpcb * tcp_newtcpcb _P((PNATState, struct socket *));
|
---|
357 | struct tcpcb * tcp_close _P((PNATState, register struct tcpcb *));
|
---|
358 | void tcp_drain _P((void));
|
---|
359 | void tcp_sockclosed _P((PNATState, struct tcpcb *));
|
---|
360 | int tcp_fconnect _P((PNATState, struct socket *));
|
---|
361 | void tcp_connect _P((PNATState, struct socket *));
|
---|
362 | int tcp_attach _P((PNATState, struct socket *));
|
---|
363 | u_int8_t tcp_tos _P((struct socket *));
|
---|
364 | int tcp_emu _P((PNATState, struct socket *, struct mbuf *));
|
---|
365 | int tcp_ctl _P((PNATState, struct socket *));
|
---|
366 | struct tcpcb *tcp_drop(PNATState, struct tcpcb *tp, int err);
|
---|
367 |
|
---|
368 | #ifdef USE_PPP
|
---|
369 | #define MIN_MRU MINMRU
|
---|
370 | #define MAX_MRU MAXMRU
|
---|
371 | #else
|
---|
372 | #define MIN_MRU 128
|
---|
373 | #define MAX_MRU 16384
|
---|
374 | #endif
|
---|
375 |
|
---|
376 | #ifndef _WIN32
|
---|
377 | #define min(x,y) ((x) < (y) ? (x) : (y))
|
---|
378 | #define max(x,y) ((x) > (y) ? (x) : (y))
|
---|
379 | #endif
|
---|
380 |
|
---|
381 | #ifdef _WIN32
|
---|
382 | #undef errno
|
---|
383 | #define errno (WSAGetLastError())
|
---|
384 | #endif
|
---|
385 |
|
---|
386 | #endif
|
---|