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