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 <in6addr.h> |
12 | #else |
13 | #include <netinet/in.h> |
14 | #include <arpa/inet.h> |
15 | #endif |
16 | |
17 | #include "libslirp-version.h" |
18 | |
19 | #ifdef __cplusplus |
20 | extern "C" { |
21 | #endif |
22 | |
23 | typedef struct Slirp Slirp; |
24 | |
25 | enum { |
26 | SLIRP_POLL_IN = 1 << 0, |
27 | SLIRP_POLL_OUT = 1 << 1, |
28 | SLIRP_POLL_PRI = 1 << 2, |
29 | SLIRP_POLL_ERR = 1 << 3, |
30 | SLIRP_POLL_HUP = 1 << 4, |
31 | }; |
32 | |
33 | typedef ssize_t (*SlirpReadCb)(void *buf, size_t len, void *opaque); |
34 | typedef ssize_t (*SlirpWriteCb)(const void *buf, size_t len, void *opaque); |
35 | typedef void (*SlirpTimerCb)(void *opaque); |
36 | typedef int (*SlirpAddPollCb)(int fd, int events, void *opaque); |
37 | typedef int (*SlirpGetREventsCb)(int idx, void *opaque); |
38 | |
39 | /* |
40 | * Callbacks from slirp |
41 | */ |
42 | typedef struct SlirpCb { |
43 | /* |
44 | * Send an ethernet frame to the guest network. The opaque |
45 | * parameter is the one given to slirp_init(). The function |
46 | * doesn't need to send all the data and may return <len (no |
47 | * buffering is done on libslirp side, so the data will be dropped |
48 | * in this case). <0 reports an IO error. |
49 | */ |
50 | SlirpWriteCb send_packet; |
51 | /* Print a message for an error due to guest misbehavior. */ |
52 | void (*guest_error)(const char *msg, void *opaque); |
53 | /* Return the virtual clock value in nanoseconds */ |
54 | int64_t (*clock_get_ns)(void *opaque); |
55 | /* Create a new timer with the given callback and opaque data */ |
56 | void *(*timer_new)(SlirpTimerCb cb, void *cb_opaque, void *opaque); |
57 | /* Remove and free a timer */ |
58 | void (*timer_free)(void *timer, void *opaque); |
59 | /* Modify a timer to expire at @expire_time */ |
60 | void (*timer_mod)(void *timer, int64_t expire_time, void *opaque); |
61 | /* Register a fd for future polling */ |
62 | void (*register_poll_fd)(int fd, void *opaque); |
63 | /* Unregister a fd */ |
64 | void (*unregister_poll_fd)(int fd, void *opaque); |
65 | /* Kick the io-thread, to signal that new events may be processed */ |
66 | void (*notify)(void *opaque); |
67 | } SlirpCb; |
68 | |
69 | |
70 | Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork, |
71 | struct in_addr vnetmask, struct in_addr vhost, |
72 | bool in6_enabled, struct in6_addr vprefix_addr6, |
73 | uint8_t vprefix_len, struct in6_addr vhost6, |
74 | const char *vhostname, const char *tftp_server_name, |
75 | const char *tftp_path, const char *bootfile, |
76 | struct in_addr vdhcp_start, struct in_addr vnameserver, |
77 | struct in6_addr vnameserver6, const char **vdnssearch, |
78 | const char *vdomainname, const SlirpCb *callbacks, |
79 | void *opaque); |
80 | void slirp_cleanup(Slirp *slirp); |
81 | |
82 | void slirp_pollfds_fill(Slirp *slirp, uint32_t *timeout, |
83 | SlirpAddPollCb add_poll, void *opaque); |
84 | |
85 | void slirp_pollfds_poll(Slirp *slirp, int select_error, |
86 | SlirpGetREventsCb get_revents, void *opaque); |
87 | |
88 | void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len); |
89 | |
90 | int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
91 | int host_port, struct in_addr guest_addr, int guest_port); |
92 | int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
93 | int host_port); |
94 | int slirp_add_exec(Slirp *slirp, const char *cmdline, |
95 | struct in_addr *guest_addr, int guest_port); |
96 | int slirp_add_guestfwd(Slirp *slirp, SlirpWriteCb write_cb, void *opaque, |
97 | struct in_addr *guest_addr, int guest_port); |
98 | |
99 | char *slirp_connection_info(Slirp *slirp); |
100 | |
101 | void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port, |
102 | const uint8_t *buf, int size); |
103 | size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr, |
104 | int guest_port); |
105 | |
106 | void slirp_state_save(Slirp *s, SlirpWriteCb write_cb, void *opaque); |
107 | |
108 | int slirp_state_load(Slirp *s, int version_id, SlirpReadCb read_cb, |
109 | void *opaque); |
110 | |
111 | int slirp_state_version(void); |
112 | |
113 | const char *slirp_version_string(void); |
114 | |
115 | #ifdef __cplusplus |
116 | } /* extern "C" */ |
117 | #endif |
118 | |
119 | #endif /* LIBSLIRP_H */ |
120 | |