Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/include/libslirp.h
2 views
/* SPDX-License-Identifier: BSD-3-Clause */1#ifndef LIBSLIRP_H2#define LIBSLIRP_H34#include <stdint.h>5#include <stdbool.h>6#include <sys/types.h>78#ifdef _WIN329#include <winsock2.h>10#include <ws2tcpip.h>11#include <in6addr.h>12#include <basetsd.h>13typedef SSIZE_T slirp_ssize_t;14#ifndef LIBSLIRP_STATIC_BUILD15# ifdef BUILDING_LIBSLIRP16# define SLIRP_EXPORT __declspec(dllexport)17# else18# define SLIRP_EXPORT __declspec(dllimport)19# endif20#else21# define SLIRP_EXPORT22#endif23#else24#include <sys/types.h>25typedef ssize_t slirp_ssize_t;26#include <netinet/in.h>27#include <arpa/inet.h>28#define SLIRP_EXPORT29#endif3031#include "libslirp-version.h"3233#ifdef __cplusplus34extern "C" {35#endif3637/* Opaque structure containing the slirp state */38typedef struct Slirp Slirp;3940/* Flags passed to SlirpAddPollCb and to be returned by SlirpGetREventsCb. */41enum {42SLIRP_POLL_IN = 1 << 0,43SLIRP_POLL_OUT = 1 << 1,44SLIRP_POLL_PRI = 1 << 2,45SLIRP_POLL_ERR = 1 << 3,46SLIRP_POLL_HUP = 1 << 4,47};4849/* Callback for application to get data from the guest */50typedef slirp_ssize_t (*SlirpReadCb)(void *buf, size_t len, void *opaque);51/* Callback for application to send data to the guest */52typedef slirp_ssize_t (*SlirpWriteCb)(const void *buf, size_t len, void *opaque);53/* Timer callback */54typedef void (*SlirpTimerCb)(void *opaque);55/* Callback for libslirp to register polling callbacks */56typedef int (*SlirpAddPollCb)(int fd, int events, void *opaque);57/* Callback for libslirp to get polling result */58typedef int (*SlirpGetREventsCb)(int idx, void *opaque);5960/* For now libslirp creates only a timer for the IPv6 RA */61typedef enum SlirpTimerId {62SLIRP_TIMER_RA,63SLIRP_TIMER_NUM,64} SlirpTimerId;6566/*67* Callbacks from slirp, to be set by the application.68*69* The opaque parameter is set to the opaque pointer given in the slirp_new /70* slirp_init call.71*/72typedef struct SlirpCb {73/*74* Send an ethernet frame to the guest network. The opaque parameter is the75* one given to slirp_init(). If the guest is not ready to receive a frame,76* the function can just drop the data. TCP will then handle retransmissions77* at a lower pace.78* <0 reports an IO error.79*/80SlirpWriteCb send_packet;81/* Print a message for an error due to guest misbehavior. */82void (*guest_error)(const char *msg, void *opaque);83/* Return the virtual clock value in nanoseconds */84int64_t (*clock_get_ns)(void *opaque);85/* Create a new timer with the given callback and opaque data. Not86* needed if timer_new_opaque is provided. */87void *(*timer_new)(SlirpTimerCb cb, void *cb_opaque, void *opaque);88/* Remove and free a timer */89void (*timer_free)(void *timer, void *opaque);90/* Modify a timer to expire at @expire_time (ms) */91void (*timer_mod)(void *timer, int64_t expire_time, void *opaque);92/* Register a fd for future polling */93void (*register_poll_fd)(int fd, void *opaque);94/* Unregister a fd */95void (*unregister_poll_fd)(int fd, void *opaque);96/* Kick the io-thread, to signal that new events may be processed because some TCP buffer97* can now receive more data, i.e. slirp_socket_can_recv will return 1. */98void (*notify)(void *opaque);99100/*101* Fields introduced in SlirpConfig version 4 begin102*/103104/* Initialization has completed and a Slirp* has been created. */105void (*init_completed)(Slirp *slirp, void *opaque);106/* Create a new timer. When the timer fires, the application passes107* the SlirpTimerId and cb_opaque to slirp_handle_timer. */108void *(*timer_new_opaque)(SlirpTimerId id, void *cb_opaque, void *opaque);109} SlirpCb;110111#define SLIRP_CONFIG_VERSION_MIN 1112#define SLIRP_CONFIG_VERSION_MAX 5113114typedef struct SlirpConfig {115/* Version must be provided */116uint32_t version;117/*118* Fields introduced in SlirpConfig version 1 begin119*/120/* Whether to prevent the guest from accessing the Internet */121int restricted;122/* Whether IPv4 is enabled */123bool in_enabled;124/* Virtual network for the guest */125struct in_addr vnetwork;126/* Mask for the virtual network for the guest */127struct in_addr vnetmask;128/* Virtual address for the host exposed to the guest */129struct in_addr vhost;130/* Whether IPv6 is enabled */131bool in6_enabled;132/* Virtual IPv6 network for the guest */133struct in6_addr vprefix_addr6;134/* Len of the virtual IPv6 network for the guest */135uint8_t vprefix_len;136/* Virtual address for the host exposed to the guest */137struct in6_addr vhost6;138/* Hostname exposed to the guest in DHCP hostname option */139const char *vhostname;140/* Hostname exposed to the guest in the DHCP TFTP server name option */141const char *tftp_server_name;142/* Path of the files served by TFTP */143const char *tftp_path;144/* Boot file name exposed to the guest via DHCP */145const char *bootfile;146/* Start of the DHCP range */147struct in_addr vdhcp_start;148/* Virtual address for the DNS server exposed to the guest */149struct in_addr vnameserver;150/* Virtual IPv6 address for the DNS server exposed to the guest */151struct in6_addr vnameserver6;152/* DNS search names exposed to the guest via DHCP */153const char **vdnssearch;154/* Domain name exposed to the guest via DHCP */155const char *vdomainname;156/* MTU when sending packets to the guest */157/* Default: IF_MTU_DEFAULT */158size_t if_mtu;159/* MRU when receiving packets from the guest */160/* Default: IF_MRU_DEFAULT */161size_t if_mru;162/* Prohibit connecting to 127.0.0.1:* */163bool disable_host_loopback;164/*165* Enable emulation code (*warning*: this code isn't safe, it is not166* recommended to enable it)167*/168bool enable_emu;169170/*171* Fields introduced in SlirpConfig version 2 begin172*/173/* Address to be used when sending data to the Internet */174struct sockaddr_in *outbound_addr;175/* IPv6 Address to be used when sending data to the Internet */176struct sockaddr_in6 *outbound_addr6;177178/*179* Fields introduced in SlirpConfig version 3 begin180*/181/* slirp will not redirect/serve any DNS packet */182bool disable_dns;183184/*185* Fields introduced in SlirpConfig version 4 begin186*/187/* slirp will not reply to any DHCP requests */188bool disable_dhcp;189190/*191* Fields introduced in SlirpConfig version 5 begin192*/193/* Manufacturer ID (IANA Private Enterprise number) */194uint32_t mfr_id;195/*196* MAC address allocated for an out-of-band management controller, to be197* retrieved through NC-SI.198*/199uint8_t oob_eth_addr[6];200} SlirpConfig;201202/* Create a new instance of a slirp stack */203SLIRP_EXPORT204Slirp *slirp_new(const SlirpConfig *cfg, const SlirpCb *callbacks,205void *opaque);206/* slirp_init is deprecated in favor of slirp_new */207SLIRP_EXPORT208Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,209struct in_addr vnetmask, struct in_addr vhost,210bool in6_enabled, struct in6_addr vprefix_addr6,211uint8_t vprefix_len, struct in6_addr vhost6,212const char *vhostname, const char *tftp_server_name,213const char *tftp_path, const char *bootfile,214struct in_addr vdhcp_start, struct in_addr vnameserver,215struct in6_addr vnameserver6, const char **vdnssearch,216const char *vdomainname, const SlirpCb *callbacks,217void *opaque);218/* Shut down an instance of a slirp stack */219SLIRP_EXPORT220void slirp_cleanup(Slirp *slirp);221222/* This is called by the application when it is about to sleep through poll().223* *timeout is set to the amount of virtual time (in ms) that the application intends to224* wait (UINT32_MAX if infinite). slirp_pollfds_fill updates it according to225* e.g. TCP timers, so the application knows it should sleep a smaller amount of226* time. slirp_pollfds_fill calls add_poll for each file descriptor227* that should be monitored along the sleep. The opaque pointer is passed as228* such to add_poll, and add_poll returns an index. */229SLIRP_EXPORT230void slirp_pollfds_fill(Slirp *slirp, uint32_t *timeout,231SlirpAddPollCb add_poll, void *opaque);232233/* This is called by the application after sleeping, to report which file234* descriptors are available. slirp_pollfds_poll calls get_revents on each file235* descriptor, giving it the index that add_poll returned during the236* slirp_pollfds_fill call, to know whether the descriptor is available for237* read/write/etc. (SLIRP_POLL_*)238* select_error should be passed 1 if poll() returned an error. */239SLIRP_EXPORT240void slirp_pollfds_poll(Slirp *slirp, int select_error,241SlirpGetREventsCb get_revents, void *opaque);242243/* This is called by the application when the guest emits a packet on the244* guest network, to be interpreted by slirp. */245SLIRP_EXPORT246void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len);247248/* This is called by the application when a timer expires, if it provides249* the timer_new_opaque callback. It is not needed if the application only250* uses timer_new. */251SLIRP_EXPORT252void slirp_handle_timer(Slirp *slirp, SlirpTimerId id, void *cb_opaque);253254/* These set up / remove port forwarding between a host port in the real world255* and the guest network.256* Note: guest_addr must be in network order, while guest_port must be in host257* order.258*/259SLIRP_EXPORT260int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,261int host_port, struct in_addr guest_addr, int guest_port);262SLIRP_EXPORT263int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,264int host_port);265266#define SLIRP_HOSTFWD_UDP 1267#define SLIRP_HOSTFWD_V6ONLY 2268SLIRP_EXPORT269int slirp_add_hostxfwd(Slirp *slirp,270const struct sockaddr *haddr, socklen_t haddrlen,271const struct sockaddr *gaddr, socklen_t gaddrlen,272int flags);273SLIRP_EXPORT274int slirp_remove_hostxfwd(Slirp *slirp,275const struct sockaddr *haddr, socklen_t haddrlen,276int flags);277278/* Set up port forwarding between a port in the guest network and a279* command running on the host */280SLIRP_EXPORT281int slirp_add_exec(Slirp *slirp, const char *cmdline,282struct in_addr *guest_addr, int guest_port);283/* Set up port forwarding between a port in the guest network and a284* Unix port on the host */285SLIRP_EXPORT286int slirp_add_unix(Slirp *slirp, const char *unixsock,287struct in_addr *guest_addr, int guest_port);288/* Set up port forwarding between a port in the guest network and a289* callback that will receive the data coming from the port */290SLIRP_EXPORT291int slirp_add_guestfwd(Slirp *slirp, SlirpWriteCb write_cb, void *opaque,292struct in_addr *guest_addr, int guest_port);293294/* TODO: rather identify a guestfwd through an opaque pointer instead of through295* the guest_addr */296297/* This is called by the application for a guestfwd, to determine how much data298* can be received by the forwarded port through a call to slirp_socket_recv. */299SLIRP_EXPORT300size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,301int guest_port);302/* This is called by the application for a guestfwd, to provide the data to be303* sent on the forwarded port */304SLIRP_EXPORT305void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,306const uint8_t *buf, int size);307308/* Remove entries added by slirp_add_exec, slirp_add_unix or slirp_add_guestfwd */309SLIRP_EXPORT310int slirp_remove_guestfwd(Slirp *slirp, struct in_addr guest_addr,311int guest_port);312313/* Return a human-readable state of the slirp stack */314SLIRP_EXPORT315char *slirp_connection_info(Slirp *slirp);316317/* Return a human-readable state of the NDP/ARP tables */318SLIRP_EXPORT319char *slirp_neighbor_info(Slirp *slirp);320321/* Save the slirp state through the write_cb. The opaque pointer is passed as322* such to the write_cb. */323SLIRP_EXPORT324int slirp_state_save(Slirp *s, SlirpWriteCb write_cb, void *opaque);325326/* Returns the version of the slirp state, to be saved along the state */327SLIRP_EXPORT328int slirp_state_version(void);329330/* Load the slirp state through the read_cb. The opaque pointer is passed as331* such to the read_cb. The version should be given as it was obtained from332* slirp_state_version when slirp_state_save was called. */333SLIRP_EXPORT334int slirp_state_load(Slirp *s, int version_id, SlirpReadCb read_cb,335void *opaque);336337/* Return the version of the slirp implementation */338SLIRP_EXPORT339const char *slirp_version_string(void);340341#ifdef __cplusplus342} /* extern "C" */343#endif344345#endif /* LIBSLIRP_H */346347348