ping: install a pong handler

Respond to server pings:

https://xmpp.org/extensions/xep-0199.html#s2c

```
xmpp DEBUG RECV: <iq id="rr-1694698253331-17375542133555098656-wf3J7kbG6j8Ft5Vx0cMY99txsW8=-55238004" type="get" to="msantos@example.com/110052433108464573191602" from="example.com"><ping xmlns="urn:xmpp:ping"/></iq>
conn DEBUG SENT: <iq id="rr-1694698253331-17375542133555098656-wf3J7kbG6j8Ft5Vx0cMY99txsW8=-55238004" to="example.com" type="result" from="msantos@example.com/110052433108464573191602"/>
```

Thanks @jessiehowell !

Fixes https://github.com/msantos/xmppipe/issues/9
master 0.15.0
Michael Santos 8 months ago
parent 78807f1b15
commit 4d79bddf56

@ -1,4 +1,4 @@
/* Copyright (c) 2015-2019, Michael Santos <michael.santos@gmail.com>
/* Copyright (c) 2015-2023, Michael Santos <michael.santos@gmail.com>
*
* 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++;
}
// <iq from='juliet@capulet.lit/balcony' to='capulet.lit' id='s2c1'
// type='result'/>
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;
}

@ -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

@ -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,

Loading…
Cancel
Save