jabberd2  2.6.1
sm.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 #include "c2s.h"
22 
24 static void _sm_generate_id(sess_t sess, bres_t res, const char *type) {
25  char str[3094]; /* JID=3071 chars max + time = 12 chars max + type = 10 chars max + terminator = 3094 */
26 
27  snprintf(str, 3094, "%s%d%s", type, (int) time(NULL), jid_full(res->jid));
28  str[3093] = '\0';
29 
30  shahash_r(str, res->sm_request);
31 }
32 
34 static nad_t _sm_build_route(sess_t sess, bres_t res, const char *action, const char *target, const char *id) {
35  nad_t nad;
36  int ns, ans;
37 
38  nad = nad_new();
39 
40  ns = nad_add_namespace(nad, uri_COMPONENT, NULL);
41  nad_append_elem(nad, ns, "route", 0);
42 
43  nad_append_attr(nad, -1, "to", sess->smcomp?sess->smcomp:((char *) res->jid->domain));
44  nad_append_attr(nad, -1, "from", sess->c2s->id);
45 
46  ans = nad_add_namespace(nad, uri_SESSION, "sc");
47  nad_append_elem(nad, ans, "session", 1);
48 
49  if(res->c2s_id[0] != '\0')
50  nad_append_attr(nad, ans, "c2s", res->c2s_id);
51  if(res->sm_id[0] != '\0')
52  nad_append_attr(nad, ans, "sm", res->sm_id);
53 
54  nad_append_attr(nad, -1, "action", action);
55 
56  if(target != NULL)
57  nad_append_attr(nad, -1, "target", target);
58  if(id != NULL)
59  nad_append_attr(nad, -1, "id", id);
60 
61  log_debug(ZONE, "built new route nad for %s action %s target %s id %s", jid_full(res->jid), action, target, id);
62 
63  return nad;
64 }
65 
66 void sm_start(sess_t sess, bres_t res) {
67  _sm_generate_id(sess, res, "start");
68 
69  sx_nad_write(sess->c2s->router, _sm_build_route(sess, res, "start", jid_full(res->jid), res->sm_request));
70 }
71 
72 void sm_end(sess_t sess, bres_t res) {
73  sx_nad_write(sess->c2s->router, _sm_build_route(sess, res, "end", NULL, NULL));
74 }
75 
76 void sm_create(sess_t sess, bres_t res) {
77  _sm_generate_id(sess, res, "create");
78 
79  sx_nad_write(sess->c2s->router, _sm_build_route(sess, res, "create", jid_user(res->jid), res->sm_request));
80 }
81 
82 void sm_delete(sess_t sess, bres_t res) {
83  sx_nad_write(sess->c2s->router, _sm_build_route(sess, res, "delete", jid_user(res->jid), NULL));
84 }
85 
86 void sm_packet(sess_t sess, bres_t res, nad_t nad) {
87  int ns;
88 
89  ns = nad_add_namespace(nad, uri_COMPONENT, NULL);
90  nad_wrap_elem(nad, 0, ns, "route");
91 
92  nad_set_attr(nad, 0, -1, "to", sess->smcomp?sess->smcomp:((char *) res->jid->domain), 0);
93  nad_set_attr(nad, 0, -1, "from", sess->c2s->id, 0);
94 
95  ns = nad_append_namespace(nad, 1, uri_SESSION, "sc");
96 
97  nad_set_attr(nad, 1, ns, "c2s", res->c2s_id, 0);
98  if(res->c2s_id[0] != '\0')
99  nad_set_attr(nad, 1, ns, "sm", res->sm_id, 0);
100 
101  sx_nad_write(sess->c2s->router, nad);
102 }
void nad_wrap_elem(nad_t nad, unsigned int elem, int ns, const char *name)
wrap an element with another element
Definition: nad.c:507
Definition: nad.h:93
nad_t nad_new(void)
create a new nad
Definition: nad.c:125
int nad_append_attr(nad_t nad, int ns, const char *name, const char *val)
attach new attr to the last elem
Definition: nad.c:745
char sm_request[41]
this holds the id of the current pending SM request
Definition: c2s.h:65
#define sx_nad_write(s, nad)
Definition: sx.h:167
const char * jid_user(jid_t jid)
expand and return the user
Definition: jid.c:338
const char * jid_full(jid_t jid)
expand and return the full
Definition: jid.c:346
void sm_delete(sess_t sess, bres_t res)
Definition: sm.c:82
void nad_set_attr(nad_t nad, unsigned int elem, int ns, const char *name, const char *val, int vallen)
create, update, or zap any matching attr on this elem
Definition: nad.c:407
void shahash_r(const char *str, char hashbuf[41])
convenience (originally by Thomas Muldowney)
Definition: str.c:358
list of resources bound to session
Definition: c2s.h:59
int nad_add_namespace(nad_t nad, const char *uri, const char *prefix)
bring a new namespace into scope
Definition: nad.c:778
void sm_end(sess_t sess, bres_t res)
Definition: sm.c:72
const char * id
our id (hostname) with the router
Definition: c2s.h:159
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
sx_t router
router's conn
Definition: c2s.h:183
jid_t jid
full bound jid
Definition: c2s.h:61
const char * smcomp
Definition: c2s.h:81
char * domain
Definition: jid.h:45
static void _sm_generate_id(sess_t sess, bres_t res, const char *type)
generate a new session request id
Definition: sm.c:24
void sm_packet(sess_t sess, bres_t res, nad_t nad)
Definition: sm.c:86
#define log_debug(...)
Definition: log.h:65
char c2s_id[44]
session id for this jid for us and them
Definition: c2s.h:63
There is one instance of this struct per user who is logged in to this c2s instance.
Definition: c2s.h:74
void sm_create(sess_t sess, bres_t res)
Definition: sm.c:76
#define uri_COMPONENT
Definition: uri.h:52
#define ZONE
Definition: mio_impl.h:76
c2s_t c2s
Definition: c2s.h:75
#define uri_SESSION
Definition: uri.h:53
static nad_t _sm_build_route(sess_t sess, bres_t res, const char *action, const char *target, const char *id)
make a new action route
Definition: sm.c:34
int nad_append_namespace(nad_t nad, unsigned int elem, const char *uri, const char *prefix)
declare a namespace on an already-existing element
Definition: nad.c:814
char sm_id[41]
Definition: c2s.h:63
void sm_start(sess_t sess, bres_t res)
Definition: sm.c:66