From 4d79bddf566ccc207a6b13e798f3715d5ccfff49 Mon Sep 17 00:00:00 2001 From: Michael Santos Date: Thu, 14 Sep 2023 09:33:17 -0400 Subject: [PATCH] ping: install a pong handler Respond to server pings: https://xmpp.org/extensions/xep-0199.html#s2c ``` xmpp DEBUG RECV: conn DEBUG SENT: ``` Thanks @jessiehowell ! Fixes https://github.com/msantos/xmppipe/issues/9 --- src/ping.c | 33 ++++++++++++++++++++++++++++++++- src/xmppipe.c | 2 ++ src/xmppipe.h | 3 ++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/ping.c b/src/ping.c index 76b5b03..ac0e4f5 100644 --- a/src/ping.c +++ b/src/ping.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2015-2019, Michael Santos +/* Copyright (c) 2015-2023, Michael Santos * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -44,3 +44,34 @@ void xmppipe_ping(xmppipe_state_t *state) { state->keepalive_fail++; } + +// +int handle_pong(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, + void *const userdata) { + xmppipe_state_t *state = userdata; + xmpp_stanza_t *iq; + const char *from; + const char *id; + + from = xmpp_stanza_get_attribute(stanza, "from"); + if (from == NULL) + return 1; + + id = xmpp_stanza_get_attribute(stanza, "id"); + if (id == NULL) + return 1; + + iq = xmppipe_stanza_new(state->ctx); + xmppipe_stanza_set_name(iq, "iq"); + xmppipe_stanza_set_type(iq, "result"); + xmppipe_stanza_set_id(iq, id); + xmppipe_stanza_set_attribute(iq, "from", + xmpp_conn_get_bound_jid(state->conn)); + xmppipe_stanza_set_attribute(iq, "to", from); + + xmppipe_send(state, iq); + (void)xmpp_stanza_release(iq); + + return 1; +} diff --git a/src/xmppipe.c b/src/xmppipe.c index 9fc8620..2b9de53 100644 --- a/src/xmppipe.c +++ b/src/xmppipe.c @@ -346,6 +346,8 @@ int xmppipe_stream_init(xmppipe_state_t *state) { xmpp_handler_add(state->conn, handle_version, "jabber:iq:version", "iq", NULL, state); xmpp_handler_add(state->conn, handle_iq, NULL, "iq", "result", state); + xmpp_handler_add(state->conn, handle_pong, "urn:xmpp:ping", "iq", "get", + state); xmpp_id_handler_add(state->conn, handle_ping_reply, "c2s1", state); /* XXX multiple handlers can be called for each event diff --git a/src/xmppipe.h b/src/xmppipe.h index 10c5120..8eceded 100644 --- a/src/xmppipe.h +++ b/src/xmppipe.h @@ -26,7 +26,7 @@ #include "strtonum.h" #endif -#define XMPPIPE_VERSION "0.14.9" +#define XMPPIPE_VERSION "0.15.0" #define XMPPIPE_RESOURCE "xmppipe" #define XMPPIPE_STREQ(a, b) (strcmp((a), (b)) == 0) @@ -111,6 +111,7 @@ void event_loop(xmppipe_state_t *state); int handle_message(xmpp_conn_t *const, xmpp_stanza_t *const, void *const); int handle_null(xmpp_conn_t *const, xmpp_stanza_t *const, void *const); int handle_ping_reply(xmpp_conn_t *const, xmpp_stanza_t *const, void *const); +int handle_pong(xmpp_conn_t *const, xmpp_stanza_t *const, void *const); int handle_presence(xmpp_conn_t *const, xmpp_stanza_t *const, void *const); void xmppipe_ping(xmppipe_state_t *); int handle_presence_error(xmpp_conn_t *const, xmpp_stanza_t *const,