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