1#ifndef __COMMON_H__
2#define __COMMON_H__
3
4#include "config-host.h"
5#include "slirp_config.h"
6
7#ifdef _WIN32
8# include <inttypes.h>
9
10typedef char *caddr_t;
11
12# include <windows.h>
13# include <winsock2.h>
14# include <ws2tcpip.h>
15# include <sys/timeb.h>
16# include <iphlpapi.h>
17
18#else
19# define ioctlsocket ioctl
20# define closesocket(s) close(s)
21# if !defined(__HAIKU__)
22# define O_BINARY 0
23# endif
24#endif
25
26#include <sys/types.h>
27#ifdef HAVE_SYS_BITYPES_H
28# include <sys/bitypes.h>
29#endif
30
31#include <sys/time.h>
32
33#ifdef HAVE_UNISTD_H
34# include <unistd.h>
35#endif
36
37#ifdef HAVE_STDLIB_H
38# include <stdlib.h>
39#endif
40
41#include <stdio.h>
42#include <errno.h>
43
44#ifndef HAVE_MEMMOVE
45#define memmove(x, y, z) bcopy(y, x, z)
46#endif
47
48#if TIME_WITH_SYS_TIME
49# include <sys/time.h>
50# include <time.h>
51#else
52# ifdef HAVE_SYS_TIME_H
53# include <sys/time.h>
54# else
55# include <time.h>
56# endif
57#endif
58
59#ifdef HAVE_STRING_H
60# include <string.h>
61#else
62# include <strings.h>
63#endif
64
65#ifndef _WIN32
66#include <sys/uio.h>
67#endif
68
69#ifndef _WIN32
70#include <netinet/in.h>
71#include <arpa/inet.h>
72#endif
73
74
75#if defined(ultrix)
76char *strdup(const char *);
77#endif
78
79
80#if defined(ultrix) || defined(hcx)
81void *malloc(size_t arg);
82void free(void *ptr);
83#endif
84
85#include <fcntl.h>
86#ifndef NO_UNIX_SOCKETS
87#include <sys/un.h>
88#endif
89#include <signal.h>
90#ifdef HAVE_SYS_SIGNAL_H
91# include <sys/signal.h>
92#endif
93#ifndef _WIN32
94#include <sys/socket.h>
95#endif
96
97#if defined(HAVE_SYS_IOCTL_H)
98# include <sys/ioctl.h>
99#endif
100
101#ifdef HAVE_SYS_SELECT_H
102# include <sys/select.h>
103#endif
104
105#ifdef HAVE_SYS_WAIT_H
106# include <sys/wait.h>
107#endif
108
109#ifdef HAVE_SYS_FILIO_H
110# include <sys/filio.h>
111#endif
112
113#ifdef USE_PPP
114#include <ppp/slirppp.h>
115#endif
116
117#ifdef __STDC__
118#include <stdarg.h>
119#else
120#include <varargs.h>
121#endif
122
123#include <sys/stat.h>
124
125
126
127#define insque slirp_insque
128#define remque slirp_remque
129
130#ifdef HAVE_SYS_STROPTS_H
131#include <sys/stropts.h>
132#endif
133
134#include "debug.h"
135
136#include "qemu/queue.h"
137#include "qemu/sockets.h"
138
139#include "libslirp.h"
140#include "ip.h"
141#include "tcp.h"
142#include "tcp_timer.h"
143#include "tcp_var.h"
144#include "tcpip.h"
145#include "udp.h"
146#include "ip_icmp.h"
147#include "mbuf.h"
148#include "sbuf.h"
149#include "socket.h"
150#include "if.h"
151#include "main.h"
152#include "misc.h"
153#ifdef USE_PPP
154#include "ppp/pppd.h"
155#include "ppp/ppp.h"
156#endif
157
158#include "bootp.h"
159#include "tftp.h"
160
161#define ETH_ALEN 6
162#define ETH_HLEN 14
163
164#define ETH_P_IP 0x0800
165#define ETH_P_ARP 0x0806
166
167#define ARPOP_REQUEST 1
168#define ARPOP_REPLY 2
169
170struct ethhdr {
171 unsigned char h_dest[ETH_ALEN];
172 unsigned char h_source[ETH_ALEN];
173 unsigned short h_proto;
174};
175
176struct arphdr {
177 unsigned short ar_hrd;
178 unsigned short ar_pro;
179 unsigned char ar_hln;
180 unsigned char ar_pln;
181 unsigned short ar_op;
182
183
184
185
186 unsigned char ar_sha[ETH_ALEN];
187 uint32_t ar_sip;
188 unsigned char ar_tha[ETH_ALEN];
189 uint32_t ar_tip;
190} QEMU_PACKED;
191
192#define ARP_TABLE_SIZE 16
193
194typedef struct ArpTable {
195 struct arphdr table[ARP_TABLE_SIZE];
196 int next_victim;
197} ArpTable;
198
199void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN]);
200
201bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
202 uint8_t out_ethaddr[ETH_ALEN]);
203
204struct Slirp {
205 QTAILQ_ENTRY(Slirp) entry;
206
207
208 struct in_addr vnetwork_addr;
209 struct in_addr vnetwork_mask;
210 struct in_addr vhost_addr;
211 struct in_addr vdhcp_startaddr;
212 struct in_addr vnameserver_addr;
213
214 struct in_addr client_ipaddr;
215 char client_hostname[33];
216
217 int restricted;
218 struct ex_list *exec_list;
219
220
221 struct mbuf m_freelist, m_usedlist;
222 int mbuf_alloced;
223
224
225 struct mbuf if_fastq;
226 struct mbuf if_batchq;
227 struct mbuf *next_m;
228 bool if_start_busy;
229
230
231 struct ipq ipq;
232 uint16_t ip_id;
233
234
235 BOOTPClient bootp_clients[NB_BOOTP_CLIENTS];
236 char *bootp_filename;
237 size_t vdnssearch_len;
238 uint8_t *vdnssearch;
239
240
241 struct socket tcb;
242 struct socket *tcp_last_so;
243 tcp_seq tcp_iss;
244 uint32_t tcp_now;
245
246
247 struct socket udb;
248 struct socket *udp_last_so;
249
250
251 struct socket icmp;
252 struct socket *icmp_last_so;
253
254
255 char *tftp_prefix;
256 struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
257
258 ArpTable arp_table;
259
260 void *opaque;
261};
262
263extern Slirp *slirp_instance;
264
265#ifndef NULL
266#define NULL (void *)0
267#endif
268
269#ifndef FULL_BOLT
270void if_start(Slirp *);
271#else
272void if_start(struct ttys *);
273#endif
274
275#ifndef HAVE_STRERROR
276 char *strerror(int error);
277#endif
278
279#ifndef HAVE_INDEX
280 char *index(const char *, int);
281#endif
282
283#ifndef HAVE_GETHOSTID
284 long gethostid(void);
285#endif
286
287void lprint(const char *, ...) GCC_FMT_ATTR(1, 2);
288
289#ifndef _WIN32
290#include <netdb.h>
291#endif
292
293#define DEFAULT_BAUD 115200
294
295#define SO_OPTIONS DO_KEEPALIVE
296#define TCP_MAXIDLE (TCPTV_KEEPCNT * TCPTV_KEEPINTVL)
297
298
299int translate_dnssearch(Slirp *s, const char ** names);
300
301
302int cksum(struct mbuf *m, int len);
303
304
305void if_init(Slirp *);
306void if_output(struct socket *, struct mbuf *);
307
308
309void ip_init(Slirp *);
310void ip_cleanup(Slirp *);
311void ip_input(struct mbuf *);
312void ip_slowtimo(Slirp *);
313void ip_stripoptions(register struct mbuf *, struct mbuf *);
314
315
316int ip_output(struct socket *, struct mbuf *);
317
318
319void tcp_input(register struct mbuf *, int, struct socket *);
320int tcp_mss(register struct tcpcb *, u_int);
321
322
323int tcp_output(register struct tcpcb *);
324void tcp_setpersist(register struct tcpcb *);
325
326
327void tcp_init(Slirp *);
328void tcp_cleanup(Slirp *);
329void tcp_template(struct tcpcb *);
330void tcp_respond(struct tcpcb *, register struct tcpiphdr *, register struct mbuf *, tcp_seq, tcp_seq, int);
331struct tcpcb * tcp_newtcpcb(struct socket *);
332struct tcpcb * tcp_close(register struct tcpcb *);
333void tcp_sockclosed(struct tcpcb *);
334int tcp_fconnect(struct socket *);
335void tcp_connect(struct socket *);
336int tcp_attach(struct socket *);
337uint8_t tcp_tos(struct socket *);
338int tcp_emu(struct socket *, struct mbuf *);
339int tcp_ctl(struct socket *);
340struct tcpcb *tcp_drop(struct tcpcb *tp, int err);
341
342#ifdef USE_PPP
343#define MIN_MRU MINMRU
344#define MAX_MRU MAXMRU
345#else
346#define MIN_MRU 128
347#define MAX_MRU 16384
348#endif
349
350#ifndef _WIN32
351#define min(x,y) ((x) < (y) ? (x) : (y))
352#define max(x,y) ((x) > (y) ? (x) : (y))
353#endif
354
355#ifdef _WIN32
356#undef errno
357#define errno (WSAGetLastError())
358#endif
359
360#endif
361