Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/include/libslirp.h
2 views
1
/* SPDX-License-Identifier: BSD-3-Clause */
2
#ifndef LIBSLIRP_H
3
#define LIBSLIRP_H
4
5
#include <stdint.h>
6
#include <stdbool.h>
7
#include <sys/types.h>
8
9
#ifdef _WIN32
10
#include <winsock2.h>
11
#include <ws2tcpip.h>
12
#include <in6addr.h>
13
#include <basetsd.h>
14
typedef SSIZE_T slirp_ssize_t;
15
#ifndef LIBSLIRP_STATIC_BUILD
16
# ifdef BUILDING_LIBSLIRP
17
# define SLIRP_EXPORT __declspec(dllexport)
18
# else
19
# define SLIRP_EXPORT __declspec(dllimport)
20
# endif
21
#else
22
# define SLIRP_EXPORT
23
#endif
24
#else
25
#include <sys/types.h>
26
typedef ssize_t slirp_ssize_t;
27
#include <netinet/in.h>
28
#include <arpa/inet.h>
29
#define SLIRP_EXPORT
30
#endif
31
32
#include "libslirp-version.h"
33
34
#ifdef __cplusplus
35
extern "C" {
36
#endif
37
38
/* Opaque structure containing the slirp state */
39
typedef struct Slirp Slirp;
40
41
/* Flags passed to SlirpAddPollCb and to be returned by SlirpGetREventsCb. */
42
enum {
43
SLIRP_POLL_IN = 1 << 0,
44
SLIRP_POLL_OUT = 1 << 1,
45
SLIRP_POLL_PRI = 1 << 2,
46
SLIRP_POLL_ERR = 1 << 3,
47
SLIRP_POLL_HUP = 1 << 4,
48
};
49
50
/* Callback for application to get data from the guest */
51
typedef slirp_ssize_t (*SlirpReadCb)(void *buf, size_t len, void *opaque);
52
/* Callback for application to send data to the guest */
53
typedef slirp_ssize_t (*SlirpWriteCb)(const void *buf, size_t len, void *opaque);
54
/* Timer callback */
55
typedef void (*SlirpTimerCb)(void *opaque);
56
/* Callback for libslirp to register polling callbacks */
57
typedef int (*SlirpAddPollCb)(int fd, int events, void *opaque);
58
/* Callback for libslirp to get polling result */
59
typedef int (*SlirpGetREventsCb)(int idx, void *opaque);
60
61
/* For now libslirp creates only a timer for the IPv6 RA */
62
typedef enum SlirpTimerId {
63
SLIRP_TIMER_RA,
64
SLIRP_TIMER_NUM,
65
} SlirpTimerId;
66
67
/*
68
* Callbacks from slirp, to be set by the application.
69
*
70
* The opaque parameter is set to the opaque pointer given in the slirp_new /
71
* slirp_init call.
72
*/
73
typedef struct SlirpCb {
74
/*
75
* Send an ethernet frame to the guest network. The opaque parameter is the
76
* one given to slirp_init(). If the guest is not ready to receive a frame,
77
* the function can just drop the data. TCP will then handle retransmissions
78
* at a lower pace.
79
* <0 reports an IO error.
80
*/
81
SlirpWriteCb send_packet;
82
/* Print a message for an error due to guest misbehavior. */
83
void (*guest_error)(const char *msg, void *opaque);
84
/* Return the virtual clock value in nanoseconds */
85
int64_t (*clock_get_ns)(void *opaque);
86
/* Create a new timer with the given callback and opaque data. Not
87
* needed if timer_new_opaque is provided. */
88
void *(*timer_new)(SlirpTimerCb cb, void *cb_opaque, void *opaque);
89
/* Remove and free a timer */
90
void (*timer_free)(void *timer, void *opaque);
91
/* Modify a timer to expire at @expire_time (ms) */
92
void (*timer_mod)(void *timer, int64_t expire_time, void *opaque);
93
/* Register a fd for future polling */
94
void (*register_poll_fd)(int fd, void *opaque);
95
/* Unregister a fd */
96
void (*unregister_poll_fd)(int fd, void *opaque);
97
/* Kick the io-thread, to signal that new events may be processed because some TCP buffer
98
* can now receive more data, i.e. slirp_socket_can_recv will return 1. */
99
void (*notify)(void *opaque);
100
101
/*
102
* Fields introduced in SlirpConfig version 4 begin
103
*/
104
105
/* Initialization has completed and a Slirp* has been created. */
106
void (*init_completed)(Slirp *slirp, void *opaque);
107
/* Create a new timer. When the timer fires, the application passes
108
* the SlirpTimerId and cb_opaque to slirp_handle_timer. */
109
void *(*timer_new_opaque)(SlirpTimerId id, void *cb_opaque, void *opaque);
110
} SlirpCb;
111
112
#define SLIRP_CONFIG_VERSION_MIN 1
113
#define SLIRP_CONFIG_VERSION_MAX 5
114
115
typedef struct SlirpConfig {
116
/* Version must be provided */
117
uint32_t version;
118
/*
119
* Fields introduced in SlirpConfig version 1 begin
120
*/
121
/* Whether to prevent the guest from accessing the Internet */
122
int restricted;
123
/* Whether IPv4 is enabled */
124
bool in_enabled;
125
/* Virtual network for the guest */
126
struct in_addr vnetwork;
127
/* Mask for the virtual network for the guest */
128
struct in_addr vnetmask;
129
/* Virtual address for the host exposed to the guest */
130
struct in_addr vhost;
131
/* Whether IPv6 is enabled */
132
bool in6_enabled;
133
/* Virtual IPv6 network for the guest */
134
struct in6_addr vprefix_addr6;
135
/* Len of the virtual IPv6 network for the guest */
136
uint8_t vprefix_len;
137
/* Virtual address for the host exposed to the guest */
138
struct in6_addr vhost6;
139
/* Hostname exposed to the guest in DHCP hostname option */
140
const char *vhostname;
141
/* Hostname exposed to the guest in the DHCP TFTP server name option */
142
const char *tftp_server_name;
143
/* Path of the files served by TFTP */
144
const char *tftp_path;
145
/* Boot file name exposed to the guest via DHCP */
146
const char *bootfile;
147
/* Start of the DHCP range */
148
struct in_addr vdhcp_start;
149
/* Virtual address for the DNS server exposed to the guest */
150
struct in_addr vnameserver;
151
/* Virtual IPv6 address for the DNS server exposed to the guest */
152
struct in6_addr vnameserver6;
153
/* DNS search names exposed to the guest via DHCP */
154
const char **vdnssearch;
155
/* Domain name exposed to the guest via DHCP */
156
const char *vdomainname;
157
/* MTU when sending packets to the guest */
158
/* Default: IF_MTU_DEFAULT */
159
size_t if_mtu;
160
/* MRU when receiving packets from the guest */
161
/* Default: IF_MRU_DEFAULT */
162
size_t if_mru;
163
/* Prohibit connecting to 127.0.0.1:* */
164
bool disable_host_loopback;
165
/*
166
* Enable emulation code (*warning*: this code isn't safe, it is not
167
* recommended to enable it)
168
*/
169
bool enable_emu;
170
171
/*
172
* Fields introduced in SlirpConfig version 2 begin
173
*/
174
/* Address to be used when sending data to the Internet */
175
struct sockaddr_in *outbound_addr;
176
/* IPv6 Address to be used when sending data to the Internet */
177
struct sockaddr_in6 *outbound_addr6;
178
179
/*
180
* Fields introduced in SlirpConfig version 3 begin
181
*/
182
/* slirp will not redirect/serve any DNS packet */
183
bool disable_dns;
184
185
/*
186
* Fields introduced in SlirpConfig version 4 begin
187
*/
188
/* slirp will not reply to any DHCP requests */
189
bool disable_dhcp;
190
191
/*
192
* Fields introduced in SlirpConfig version 5 begin
193
*/
194
/* Manufacturer ID (IANA Private Enterprise number) */
195
uint32_t mfr_id;
196
/*
197
* MAC address allocated for an out-of-band management controller, to be
198
* retrieved through NC-SI.
199
*/
200
uint8_t oob_eth_addr[6];
201
} SlirpConfig;
202
203
/* Create a new instance of a slirp stack */
204
SLIRP_EXPORT
205
Slirp *slirp_new(const SlirpConfig *cfg, const SlirpCb *callbacks,
206
void *opaque);
207
/* slirp_init is deprecated in favor of slirp_new */
208
SLIRP_EXPORT
209
Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,
210
struct in_addr vnetmask, struct in_addr vhost,
211
bool in6_enabled, struct in6_addr vprefix_addr6,
212
uint8_t vprefix_len, struct in6_addr vhost6,
213
const char *vhostname, const char *tftp_server_name,
214
const char *tftp_path, const char *bootfile,
215
struct in_addr vdhcp_start, struct in_addr vnameserver,
216
struct in6_addr vnameserver6, const char **vdnssearch,
217
const char *vdomainname, const SlirpCb *callbacks,
218
void *opaque);
219
/* Shut down an instance of a slirp stack */
220
SLIRP_EXPORT
221
void slirp_cleanup(Slirp *slirp);
222
223
/* This is called by the application when it is about to sleep through poll().
224
* *timeout is set to the amount of virtual time (in ms) that the application intends to
225
* wait (UINT32_MAX if infinite). slirp_pollfds_fill updates it according to
226
* e.g. TCP timers, so the application knows it should sleep a smaller amount of
227
* time. slirp_pollfds_fill calls add_poll for each file descriptor
228
* that should be monitored along the sleep. The opaque pointer is passed as
229
* such to add_poll, and add_poll returns an index. */
230
SLIRP_EXPORT
231
void slirp_pollfds_fill(Slirp *slirp, uint32_t *timeout,
232
SlirpAddPollCb add_poll, void *opaque);
233
234
/* This is called by the application after sleeping, to report which file
235
* descriptors are available. slirp_pollfds_poll calls get_revents on each file
236
* descriptor, giving it the index that add_poll returned during the
237
* slirp_pollfds_fill call, to know whether the descriptor is available for
238
* read/write/etc. (SLIRP_POLL_*)
239
* select_error should be passed 1 if poll() returned an error. */
240
SLIRP_EXPORT
241
void slirp_pollfds_poll(Slirp *slirp, int select_error,
242
SlirpGetREventsCb get_revents, void *opaque);
243
244
/* This is called by the application when the guest emits a packet on the
245
* guest network, to be interpreted by slirp. */
246
SLIRP_EXPORT
247
void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len);
248
249
/* This is called by the application when a timer expires, if it provides
250
* the timer_new_opaque callback. It is not needed if the application only
251
* uses timer_new. */
252
SLIRP_EXPORT
253
void slirp_handle_timer(Slirp *slirp, SlirpTimerId id, void *cb_opaque);
254
255
/* These set up / remove port forwarding between a host port in the real world
256
* and the guest network.
257
* Note: guest_addr must be in network order, while guest_port must be in host
258
* order.
259
*/
260
SLIRP_EXPORT
261
int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
262
int host_port, struct in_addr guest_addr, int guest_port);
263
SLIRP_EXPORT
264
int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
265
int host_port);
266
267
#define SLIRP_HOSTFWD_UDP 1
268
#define SLIRP_HOSTFWD_V6ONLY 2
269
SLIRP_EXPORT
270
int slirp_add_hostxfwd(Slirp *slirp,
271
const struct sockaddr *haddr, socklen_t haddrlen,
272
const struct sockaddr *gaddr, socklen_t gaddrlen,
273
int flags);
274
SLIRP_EXPORT
275
int slirp_remove_hostxfwd(Slirp *slirp,
276
const struct sockaddr *haddr, socklen_t haddrlen,
277
int flags);
278
279
/* Set up port forwarding between a port in the guest network and a
280
* command running on the host */
281
SLIRP_EXPORT
282
int slirp_add_exec(Slirp *slirp, const char *cmdline,
283
struct in_addr *guest_addr, int guest_port);
284
/* Set up port forwarding between a port in the guest network and a
285
* Unix port on the host */
286
SLIRP_EXPORT
287
int slirp_add_unix(Slirp *slirp, const char *unixsock,
288
struct in_addr *guest_addr, int guest_port);
289
/* Set up port forwarding between a port in the guest network and a
290
* callback that will receive the data coming from the port */
291
SLIRP_EXPORT
292
int slirp_add_guestfwd(Slirp *slirp, SlirpWriteCb write_cb, void *opaque,
293
struct in_addr *guest_addr, int guest_port);
294
295
/* TODO: rather identify a guestfwd through an opaque pointer instead of through
296
* the guest_addr */
297
298
/* This is called by the application for a guestfwd, to determine how much data
299
* can be received by the forwarded port through a call to slirp_socket_recv. */
300
SLIRP_EXPORT
301
size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
302
int guest_port);
303
/* This is called by the application for a guestfwd, to provide the data to be
304
* sent on the forwarded port */
305
SLIRP_EXPORT
306
void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
307
const uint8_t *buf, int size);
308
309
/* Remove entries added by slirp_add_exec, slirp_add_unix or slirp_add_guestfwd */
310
SLIRP_EXPORT
311
int slirp_remove_guestfwd(Slirp *slirp, struct in_addr guest_addr,
312
int guest_port);
313
314
/* Return a human-readable state of the slirp stack */
315
SLIRP_EXPORT
316
char *slirp_connection_info(Slirp *slirp);
317
318
/* Return a human-readable state of the NDP/ARP tables */
319
SLIRP_EXPORT
320
char *slirp_neighbor_info(Slirp *slirp);
321
322
/* Save the slirp state through the write_cb. The opaque pointer is passed as
323
* such to the write_cb. */
324
SLIRP_EXPORT
325
int slirp_state_save(Slirp *s, SlirpWriteCb write_cb, void *opaque);
326
327
/* Returns the version of the slirp state, to be saved along the state */
328
SLIRP_EXPORT
329
int slirp_state_version(void);
330
331
/* Load the slirp state through the read_cb. The opaque pointer is passed as
332
* such to the read_cb. The version should be given as it was obtained from
333
* slirp_state_version when slirp_state_save was called. */
334
SLIRP_EXPORT
335
int slirp_state_load(Slirp *s, int version_id, SlirpReadCb read_cb,
336
void *opaque);
337
338
/* Return the version of the slirp implementation */
339
SLIRP_EXPORT
340
const char *slirp_version_string(void);
341
342
#ifdef __cplusplus
343
} /* extern "C" */
344
#endif
345
346
#endif /* LIBSLIRP_H */
347
348