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