jabberd2  2.5.0
ack.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2007 Tomasz Sterna
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
17  */
18 
19 /*
20  * this sx plugin implements stanza acknowledgements
21  * as described in XEP-0198: Stanza Acknowledgements
22  */
23 
24 #include "sx.h"
25 
26 #define STREAM_ACK_NS_DECL " xmlns:ack='" uri_ACK "'"
27 
28 static void _sx_ack_header(sx_t s, sx_plugin_t p, sx_buf_t buf) {
29 
30  /* WebSocket framing has own acks */
31  if (s->flags & SX_WEBSOCKET_WRAPPER)
32  return;
33 
34  log_debug(ZONE, "hacking ack namespace decl onto stream header");
35 
36  /* get enough space */
37  _sx_buffer_alloc_margin(buf, 0, strlen(STREAM_ACK_NS_DECL) + 2);
38 
39  /* overwrite the trailing ">" with a decl followed by a new ">" */
40  memcpy(&buf->data[buf->len - 1], STREAM_ACK_NS_DECL ">", strlen(STREAM_ACK_NS_DECL)+1);
41  buf->len += strlen(STREAM_ACK_NS_DECL);
42 }
43 
45 static void _sx_ack_features(sx_t s, sx_plugin_t p, nad_t nad) {
46  /* offer feature only when authenticated and not enabled yet and not on WebSocket framing */
47  if(s->state == state_OPEN && s->plugin_data[p->index] == NULL && !(s->flags & SX_WEBSOCKET_WRAPPER))
48  nad_append_elem(nad, -1, "ack:ack", 1);
49 }
50 
52 static int _sx_ack_process(sx_t s, sx_plugin_t p, nad_t nad) {
53  int attr;
54 
55  /* not interested if we're not a server or have WebSocket framing */
56  if(s->type != type_SERVER || s->flags & SX_WEBSOCKET_WRAPPER)
57  return 1;
58 
59  /* only want ack packets */
60  if((NAD_ENS(nad, 0) < 0 || NAD_NURI_L(nad, NAD_ENS(nad, 0)) != strlen(uri_ACK) || strncmp(NAD_NURI(nad, NAD_ENS(nad, 0)), uri_ACK, strlen(uri_ACK)) != 0))
61  return 1;
62 
63  /* pings */
64  if(NAD_ENAME_L(nad, 0) == 4 && strncmp(NAD_ENAME(nad, 0), "ping", 4) == 0) {
65  jqueue_push(s->wbufq, _sx_buffer_new("<ack:pong/>", 11, NULL, NULL), 0);
66  s->want_write = 1;
67 
68  /* handled the packet */
69  nad_free(nad);
70  return 0;
71  }
72 
73  /* enable only when authenticated */
74  if(s->state == state_OPEN && NAD_ENAME_L(nad, 0) == 6 && strncmp(NAD_ENAME(nad, 0), "enable", 6) == 0) {
75  jqueue_push(s->wbufq, _sx_buffer_new("<ack:enabled/>", 14, NULL, NULL), 254);
76  s->want_write = 1;
77 
78  s->plugin_data[p->index] = (void *) 1;
79 
80  /* handled the packet */
81  nad_free(nad);
82  return 0;
83  }
84 
85  /* 'r' or 'a' when enabled */
86  if(s->plugin_data[p->index] != NULL && NAD_ENAME_L(nad, 0) == 1 && (strncmp(NAD_ENAME(nad, 0), "r", 1) == 0 || strncmp(NAD_ENAME(nad, 0), "a", 1) == 0) ) {
87  attr = nad_find_attr(nad, 0, -1, "c", NULL);
88  if(attr >= 0) {
89  char *buf = (char *) malloc(sizeof(char) * (NAD_AVAL_L(nad, attr) + 13 + 1));
90  snprintf(buf, NAD_AVAL_L(nad, attr) + 13 + 1, "<ack:a b='%.*s'/>", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
91  jqueue_push(s->wbufq, _sx_buffer_new(buf, NAD_AVAL_L(nad, attr) + 13, NULL, NULL), 255);
92  free(buf);
93  s->want_write = 1;
94  }
95 
96  /* handled the packet */
97  nad_free(nad);
98  return 0;
99  }
100 
101  _sx_debug(ZONE, "unhandled ack namespace element '%.*s', dropping packet", NAD_ENAME_L(nad, 0), NAD_ENAME(nad, 0));
102  nad_free(nad);
103  return 0;
104 }
105 
107 int sx_ack_init(sx_env_t env, sx_plugin_t p, va_list args) {
108  log_debug(ZONE, "initialising stanza acknowledgements sx plugin");
109 
110  p->header = _sx_ack_header;
113 
114  return 0;
115 }
Definition: nad.h:93
Definition: sx.h:113
_sx_state_t state
Definition: sx.h:319
static void _sx_ack_features(sx_t s, sx_plugin_t p, nad_t nad)
sx features callback
Definition: ack.c:45
unsigned int flags
Definition: sx.h:276
int sx_ack_init(sx_env_t env, sx_plugin_t p, va_list args)
args: none
Definition: ack.c:107
jqueue_t wbufq
Definition: sx.h:301
#define uri_ACK
Definition: uri.h:47
int nad_find_attr(nad_t nad, unsigned int elem, int ns, const char *name, const char *val)
get a matching attr on this elem, both name and optional val
Definition: nad.c:237
void(* features)(sx_t s, sx_plugin_t p, nad_t nad)
Definition: sx.h:373
an environment
Definition: sx.h:382
a plugin
Definition: sx.h:347
#define NAD_ENAME(N, E)
Definition: nad.h:183
int nad_append_elem(nad_t nad, int ns, const char *name, int depth)
create a new elem on the list
Definition: nad.c:711
void nad_free(nad_t nad)
free that nad
Definition: nad.c:180
void _sx_buffer_alloc_margin(sx_buf_t buf, int before, int after)
utility: ensure a certain amount of allocated space adjacent to buf->data
Definition: sx.c:262
holds the state for a single stream
Definition: sx.h:253
char * data
Definition: sx.h:114
static void _sx_ack_header(sx_t s, sx_plugin_t p, sx_buf_t buf)
Definition: ack.c:28
#define NAD_ENAME_L(N, E)
Definition: nad.h:184
void jqueue_push(jqueue_t q, void *data, int priority)
Definition: jqueue.c:44
#define NAD_NURI_L(N, NS)
Definition: nad.h:192
sx_buf_t _sx_buffer_new(const char *data, int len, _sx_notify_t notify, void *notify_arg)
utility: make a new buffer if len>0 but data is NULL, the buffer will contain that many bytes of garb...
Definition: sx.c:220
#define NAD_AVAL_L(N, A)
Definition: nad.h:190
static int _sx_ack_process(sx_t s, sx_plugin_t p, nad_t nad)
process handshake packets from the client
Definition: ack.c:52
#define log_debug(...)
Definition: log.h:65
#define SX_WEBSOCKET_WRAPPER
Definition: plugins.h:34
#define _sx_debug
Definition: sx.h:408
#define NAD_AVAL(N, A)
Definition: nad.h:189
_sx_type_t type
Definition: sx.h:273
#define STREAM_ACK_NS_DECL
Definition: ack.c:26
Definition: sx.h:83
unsigned int len
Definition: sx.h:115
#define ZONE
Definition: mio_impl.h:76
#define NAD_NURI(N, NS)
Definition: nad.h:191
int(* process)(sx_t s, sx_plugin_t p, nad_t nad)
Definition: sx.h:376
Definition: sx.h:74
int index
Definition: sx.h:352
int want_write
Definition: sx.h:306
void ** plugin_data
Definition: sx.h:330
#define NAD_ENS(N, E)
Definition: nad.h:196
void(* header)(sx_t s, sx_plugin_t p, sx_buf_t buf)
Definition: sx.h:370