jabberd2  2.3.1
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  log_debug(ZONE, "hacking ack namespace decl onto stream header");
31 
32  /* get enough space */
33  _sx_buffer_alloc_margin(buf, 0, strlen(STREAM_ACK_NS_DECL) + 2);
34 
35  /* overwrite the trailing ">" with a decl followed by a new ">" */
36  memcpy(&buf->data[buf->len - 1], STREAM_ACK_NS_DECL ">", strlen(STREAM_ACK_NS_DECL)+1);
37  buf->len += strlen(STREAM_ACK_NS_DECL);
38 }
39 
41 static void _sx_ack_features(sx_t s, sx_plugin_t p, nad_t nad) {
42  /* offer feature only when authenticated and not enabled yet */
43  if(s->state == state_OPEN && s->plugin_data[p->index] == NULL)
44  nad_append_elem(nad, -1, "ack:ack", 1);
45 }
46 
48 static int _sx_ack_process(sx_t s, sx_plugin_t p, nad_t nad) {
49  int attr;
50 
51  /* not interested if we're not a server */
52  if(s->type != type_SERVER)
53  return 1;
54 
55  /* only want ack packets */
56  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))
57  return 1;
58 
59  /* pings */
60  if(NAD_ENAME_L(nad, 0) == 4 && strncmp(NAD_ENAME(nad, 0), "ping", 4) == 0) {
61  jqueue_push(s->wbufq, _sx_buffer_new("<ack:pong/>", 11, NULL, NULL), 0);
62  s->want_write = 1;
63 
64  /* handled the packet */
65  nad_free(nad);
66  return 0;
67  }
68 
69  /* enable only when authenticated */
70  if(s->state == state_OPEN && NAD_ENAME_L(nad, 0) == 6 && strncmp(NAD_ENAME(nad, 0), "enable", 6) == 0) {
71  jqueue_push(s->wbufq, _sx_buffer_new("<ack:enabled/>", 14, NULL, NULL), 254);
72  s->want_write = 1;
73 
74  s->plugin_data[p->index] = (void *) 1;
75 
76  /* handled the packet */
77  nad_free(nad);
78  return 0;
79  }
80 
81  /* 'r' or 'a' when enabled */
82  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) ) {
83  attr = nad_find_attr(nad, 0, -1, "c", NULL);
84  if(attr >= 0) {
85  char *buf = (char *) malloc(sizeof(char) * (NAD_AVAL_L(nad, attr) + 13 + 1));
86  snprintf(buf, NAD_AVAL_L(nad, attr) + 13 + 1, "<ack:a b='%.*s'/>", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
87  jqueue_push(s->wbufq, _sx_buffer_new(buf, NAD_AVAL_L(nad, attr) + 13, NULL, NULL), 255);
88  free(buf);
89  s->want_write = 1;
90  }
91 
92  /* handled the packet */
93  nad_free(nad);
94  return 0;
95  }
96 
97  _sx_debug(ZONE, "unhandled ack namespace element '%.*s', dropping packet", NAD_ENAME_L(nad, 0), NAD_ENAME(nad, 0));
98  nad_free(nad);
99  return 0;
100 }
101 
103 int sx_ack_init(sx_env_t env, sx_plugin_t p, va_list args) {
104  log_debug(ZONE, "initialising stanza acknowledgements sx plugin");
105 
106  p->header = _sx_ack_header;
109 
110  return 0;
111 }
void(* header)(sx_t s, sx_plugin_t p, sx_buf_t buf)
Definition: sx.h:367
Definition: nad.h:93
Definition: sx.h:113
_sx_state_t state
Definition: sx.h:313
static void _sx_ack_features(sx_t s, sx_plugin_t p, nad_t nad)
sx features callback
Definition: ack.c:41
int sx_ack_init(sx_env_t env, sx_plugin_t p, va_list args)
args: none
Definition: ack.c:103
jqueue_t wbufq
Definition: sx.h:299
#define uri_ACK
Definition: uri.h:46
an environment
Definition: sx.h:379
a plugin
Definition: sx.h:344
#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:667
void nad_free(nad_t nad)
free that nad
Definition: nad.c:178
void(* features)(sx_t s, sx_plugin_t p, nad_t nad)
Definition: sx.h:370
void _sx_buffer_alloc_margin(sx_buf_t buf, int before, int after)
utility: ensure a certain amount of allocated space adjacent to buf-&gt;data
Definition: sx.c:264
holds the state for a single stream
Definition: sx.h:251
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&gt;0 but data is NULL, the buffer will contain that many bytes of garb...
Definition: sx.c:222
#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:48
#define log_debug(...)
Definition: log.h:65
#define _sx_debug
Definition: sx.h:405
#define NAD_AVAL(N, A)
Definition: nad.h:189
_sx_type_t type
Definition: sx.h:271
#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 nad_find_attr(nad_t nad, 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:235
Definition: sx.h:74
int index
Definition: sx.h:349
int want_write
Definition: sx.h:304
int(* process)(sx_t s, sx_plugin_t p, nad_t nad)
Definition: sx.h:373
void ** plugin_data
Definition: sx.h:324
#define NAD_ENS(N, E)
Definition: nad.h:196