aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2021-07-28 17:09:19 +0200
committerPaolo Bonzini <pbonzini@redhat.com>2022-04-10 15:49:05 +0200
commita57ee6e6ef7472bce2c30f800e09daa89ea6fd47 (patch)
tree804d55315c3287bf0f222b911693208f63dd19e7
parente9070f7ca279e2aede46540332842694c0021939 (diff)
downloadslirp-a57ee6e6ef7472bce2c30f800e09daa89ea6fd47.zip
slirp-a57ee6e6ef7472bce2c30f800e09daa89ea6fd47.tar.gz
slirp-a57ee6e6ef7472bce2c30f800e09daa89ea6fd47.tar.bz2
introduce slirp_timer_new wrapper
In order to support CFI, we would like to avoid passing function pointers out of libslirp, and instead use opaque integers for the timer callbacks. This patch introduces an internal API for creating a timer that is based on such a timer callback. For now, it receives the id (SLIRP_TIMER_RA is the only one) and calls slirp->cb->timer_new. The prototype of ra_timer_handler is changed slightly to fit better with the next patch which introduces a new libslirp function, to be called on timer expiration. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--src/ip6_icmp.c8
-rw-r--r--src/ip6_icmp.h1
-rw-r--r--src/libslirp.h5
-rw-r--r--src/slirp.c21
-rw-r--r--src/slirp.h1
5 files changed, 30 insertions, 6 deletions
diff --git a/src/ip6_icmp.c b/src/ip6_icmp.c
index e259ff5..181989c 100644
--- a/src/ip6_icmp.c
+++ b/src/ip6_icmp.c
@@ -10,8 +10,6 @@
#define NDP_Interval \
g_rand_int_range(slirp->grand, NDP_MinRtrAdvInterval, NDP_MaxRtrAdvInterval)
-static void ra_timer_handler(void *opaque);
-
void icmp6_init(Slirp *slirp)
{
if (!slirp->in6_enabled) {
@@ -19,7 +17,7 @@ void icmp6_init(Slirp *slirp)
}
slirp->ra_timer =
- slirp->cb->timer_new(ra_timer_handler, slirp, slirp->opaque);
+ slirp_timer_new(slirp, SLIRP_TIMER_RA, NULL);
slirp->cb->timer_mod(slirp->ra_timer,
slirp->cb->clock_get_ns(slirp->opaque) / SCALE_MS +
NDP_Interval,
@@ -210,10 +208,8 @@ static void ndp_send_ra(Slirp *slirp)
ip6_output(NULL, t, 0);
}
-static void ra_timer_handler(void *opaque)
+void ra_timer_handler(Slirp *slirp, void *unused)
{
- Slirp *slirp = opaque;
-
slirp->cb->timer_mod(slirp->ra_timer,
slirp->cb->clock_get_ns(slirp->opaque) / SCALE_MS +
NDP_Interval,
diff --git a/src/ip6_icmp.h b/src/ip6_icmp.h
index 2d3a795..4420841 100644
--- a/src/ip6_icmp.h
+++ b/src/ip6_icmp.h
@@ -215,5 +215,6 @@ void icmp6_input(struct mbuf *);
void icmp6_forward_error(struct mbuf *m, uint8_t type, uint8_t code, struct in6_addr *src);
void icmp6_send_error(struct mbuf *m, uint8_t type, uint8_t code);
void ndp_send_ns(Slirp *slirp, struct in6_addr addr);
+void ra_timer_handler(Slirp *slirp, void *unused);
#endif
diff --git a/src/libslirp.h b/src/libslirp.h
index 1e75501..546f2f9 100644
--- a/src/libslirp.h
+++ b/src/libslirp.h
@@ -39,6 +39,11 @@ typedef void (*SlirpTimerCb)(void *opaque);
typedef int (*SlirpAddPollCb)(int fd, int events, void *opaque);
typedef int (*SlirpGetREventsCb)(int idx, void *opaque);
+typedef enum SlirpTimerId {
+ SLIRP_TIMER_RA,
+ SLIRP_TIMER_NUM,
+} SlirpTimerId;
+
/*
* Callbacks from slirp, to be set by the application.
*
diff --git a/src/slirp.c b/src/slirp.c
index 5e08b53..a2876f2 100644
--- a/src/slirp.c
+++ b/src/slirp.c
@@ -528,6 +528,27 @@ static void slirp_init_once(void)
}
}
+static void ra_timer_handler_cb(void *opaque)
+{
+ Slirp *slirp = opaque;
+
+ return ra_timer_handler(slirp, NULL);
+}
+
+void *slirp_timer_new(Slirp *slirp, SlirpTimerId id, void *cb_opaque)
+{
+ g_return_val_if_fail(id >= 0 && id < SLIRP_TIMER_NUM, NULL);
+
+ switch (id) {
+ case SLIRP_TIMER_RA:
+ g_return_val_if_fail(cb_opaque == NULL, NULL);
+ return slirp->cb->timer_new(ra_timer_handler_cb, slirp, slirp->opaque);
+
+ default:
+ abort();
+ }
+}
+
Slirp *slirp_new(const SlirpConfig *cfg, const SlirpCb *callbacks, void *opaque)
{
Slirp *slirp;
diff --git a/src/slirp.h b/src/slirp.h
index 239d570..bfe7b83 100644
--- a/src/slirp.h
+++ b/src/slirp.h
@@ -280,5 +280,6 @@ struct socket *slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr,
int guest_port);
void slirp_send_packet_all(Slirp *slirp, const void *buf, size_t len);
+void *slirp_timer_new(Slirp *slirp, SlirpTimerId id, void *cb_opaque);
#endif