File: subscribe.c

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (342 lines) | stat: -rw-r--r-- 11,485 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * $Id$
 *
 * Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
 * Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
 * 
 * The initial version of this code was written by Dragos Vingarzan
 * (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
 * Fruanhofer Institute. It was and still is maintained in a separate
 * branch of the original SER. We are therefore migrating it to
 * Kamailio/SR and look forward to maintaining it from here on out.
 * 2011/2012 Smile Communications, Pty. Ltd.
 * ported/maintained/improved by 
 * Jason Penton (jason(dot)penton(at)smilecoms.com and
 * Richard Good (richard(dot)good(at)smilecoms.com) as part of an 
 * effort to add full IMS support to Kamailio/SR using a new and
 * improved architecture
 * 
 * NB: Alot of this code was originally part of OpenIMSCore,
 * FhG Fokus. 
 * Copyright (C) 2004-2006 FhG Fokus
 * Thanks for great work! This is an effort to 
 * break apart the various CSCF functions into logically separate
 * components. We hope this will drive wider use. We also feel
 * that in this way the architecture is more complete and thereby easier
 * to manage in the Kamailio/SR environment
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

#include "subscribe.h"
#include "ul_mod.h"
#include "utime.h"
#include "udomain.h"

#include "../presence/subscribe.h"
#include "../presence/utils_func.h"
#include "../presence/hash.h"

#include "../../hashes.h"

#include "ul_mod.h"


extern int sub_dialog_hash_size;
extern shtable_t sub_dialog_table;

int get_subscriber(impurecord_t* urec, str *presentity_uri, str *watcher_contact, int event, reg_subscriber** r_subscriber) {
    
    reg_subscriber* s = NULL;

    if (!watcher_contact || !presentity_uri) {
        LM_DBG("no valid presentity_uri/watcher contact pair");
        return 0;
    }

    if (!urec) {
        LM_WARN("No impurecord passed.... ignoring");
        return 1;
    }

    LM_DBG("Getting existing subscription to reg if it exists for watcher contact <%.*s> and presentity uri <%.*s>", watcher_contact->len, watcher_contact->s,
            presentity_uri->len, presentity_uri->s);

    s = urec->shead;
    while (s) {
        LM_DBG("Scrolling through subscription to reg events in IMPU record list");
        if (s->event == event &&
                (s->watcher_contact.len == watcher_contact->len)
                && (strncasecmp(s->watcher_contact.s, watcher_contact->s, watcher_contact->len) == 0)
                && (strncasecmp(s->presentity_uri.s, presentity_uri->s, presentity_uri->len) == 0)) {
            LM_DBG("Found subscription for watcher contact  <%.*s> and presentity_uri <%.*s>", watcher_contact->len, watcher_contact->s, presentity_uri->len, presentity_uri->s);
            *r_subscriber = s;
            return 0;
        }
        s = s->next;
    }
    LM_DBG("Did not find subscription for watcher contact  <%.*s> and presentity_uri <%.*s>", watcher_contact->len, watcher_contact->s, presentity_uri->len, presentity_uri->s);
    
    return 1;
}

reg_subscriber* new_subscriber(str* presentity_uri, str* watcher_uri, str* watcher_contact, subscriber_data_t* subscriber_data) {
    subs_t subs;
    reg_subscriber *s;

    int len;
    char *p;
    unsigned int hash_code = 0;
    
    memset(&subs, 0, sizeof(subs_t));
    
    len = sizeof (reg_subscriber) + subscriber_data->callid->len
            + subscriber_data->ftag->len + subscriber_data->ttag->len
            + watcher_contact->len + watcher_uri->len + presentity_uri->len
            + subscriber_data->record_route->len + subscriber_data->sockinfo_str->len;

    LM_DBG("Creating new subscription to reg");

    s = (reg_subscriber*) shm_malloc(len);
    if (s == 0) {
        LM_ERR("no more shm mem (%d)\n", len);
        return 0;
    }
    memset(s, 0, len);

    s->local_cseq = subscriber_data->local_cseq;

    s->event = subscriber_data->event;

    s->expires = subscriber_data->expires;

    p = (char*) (s + 1);

    s->call_id.s = p;
    s->call_id.len = subscriber_data->callid->len;
    memcpy(p, subscriber_data->callid->s, subscriber_data->callid->len);
    p += subscriber_data->callid->len;

    s->to_tag.s = p;
    s->to_tag.len = subscriber_data->ttag->len;
    memcpy(p, subscriber_data->ttag->s, subscriber_data->ttag->len);
    p += subscriber_data->ttag->len;

    s->from_tag.s = p;
    s->from_tag.len = subscriber_data->ftag->len;
    memcpy(p, subscriber_data->ftag->s, subscriber_data->ftag->len);
    p += subscriber_data->ftag->len;

    s->watcher_uri.s = p;
    s->watcher_uri.len = watcher_uri->len;
    memcpy(p, watcher_uri->s, watcher_uri->len);
    p += watcher_uri->len;

    s->watcher_contact.s = p;
    s->watcher_contact.len = watcher_contact->len;
    memcpy(p, watcher_contact->s, watcher_contact->len);
    p += watcher_contact->len;

    s->record_route.s = p;
    s->record_route.len = subscriber_data->record_route->len;
    memcpy(p, subscriber_data->record_route->s, subscriber_data->record_route->len);
    p += subscriber_data->record_route->len;

    s->sockinfo_str.s = p;
    s->sockinfo_str.len = subscriber_data->sockinfo_str->len;
    memcpy(p, subscriber_data->sockinfo_str->s, subscriber_data->sockinfo_str->len);
    p += subscriber_data->sockinfo_str->len;

    s->presentity_uri.s = p;
    s->presentity_uri.len = presentity_uri->len;
    memcpy(p, presentity_uri->s, presentity_uri->len);
    p += presentity_uri->len;

    if (p != (((char*) s) + len)) {
        LM_CRIT("buffer overflow\n");
        free_subscriber(s);
        return 0;
    }
    
    /*This lets us get presentity URI info for subsequent SUBSCRIBEs that don't have presentity URI as req URI*/
    
    subs.pres_uri = s->presentity_uri;
    subs.from_tag = s->from_tag;
    subs.to_tag = s->to_tag;
    subs.callid = s->call_id;
    
    hash_code = core_hash(&subs.callid, &subs.to_tag, sub_dialog_hash_size);
    
    LM_DBG("Adding sub dialog hash info with call_id: <%.*s> and ttag <%.*s> amd ftag <%.*s> and hash code <%d>", subs.callid.len, subs.callid.s, subs.to_tag.len, subs.to_tag.s, subs.from_tag.len,  subs.from_tag.s, hash_code);
    
    if (pres_insert_shtable(sub_dialog_table, hash_code, &subs))
    {
	LM_ERR("while adding new subscription\n");
	return 0;
    }

    return s;
}

/* Used for subsequent SUBSCRIBE messages to get presentity URI from dialog struct*/
/* NB: free returned result str when done from shm */
str get_presentity_from_subscriber_dialog(str *callid, str *to_tag, str *from_tag) {
    subs_t* s;
    unsigned int hash_code = 0;
    str pres_uri = {0,0};
    
    hash_code = core_hash(callid, to_tag, sub_dialog_hash_size);
    
    /* search the record in hash table */
    lock_get(&sub_dialog_table[hash_code].lock);

    LM_DBG("Searching sub dialog hash info with call_id: <%.*s> and ttag <%.*s> and ftag <%.*s> and hash code <%d>", callid->len, callid->s, to_tag->len, to_tag->s, from_tag->len, from_tag->s, hash_code);
    
    s= pres_search_shtable(sub_dialog_table, *callid,
		    *to_tag, *from_tag, hash_code);
    if(s== NULL)
    {
	    LM_DBG("Subscriber dialog record not found in hash table\n");
	    lock_release(&sub_dialog_table[hash_code].lock);
	    return pres_uri;
    }

    //make copy of pres_uri
    pres_uri.s = (char*) shm_malloc(s->pres_uri.len);
    if (pres_uri.s==0) {
	LM_ERR("no more shm mem\n");
	return pres_uri;
    }
    memcpy(pres_uri.s, s->pres_uri.s, s->pres_uri.len);
    pres_uri.len = s->pres_uri.len;
    
    lock_release(&sub_dialog_table[hash_code].lock);
    
    LM_DBG("Found subscriber dialog record in hash table with pres_uri: [%.*s]", pres_uri.len, pres_uri.s);
    return pres_uri;
}

int add_subscriber(impurecord_t* urec,
        str *watcher_uri, str *watcher_contact,
        subscriber_data_t* subscriber_data, reg_subscriber** _reg_subscriber) {

    LM_DBG("Adding reg subscription to IMPU record");

    if (!urec) {
        LM_ERR("no presentity impu record provided\n");
        return 0;
    }
    reg_subscriber *s = new_subscriber(&urec->public_identity, watcher_uri, watcher_contact, subscriber_data);

    if (!s) return 1;

    LM_DBG("Adding new subscription to IMPU record list");
    s->next = 0;
    s->prev = urec->stail;
    if (urec->stail) urec->stail->next = s;
    urec->stail = s;
    if (!urec->shead) urec->shead = s;

    *_reg_subscriber = s;
    return 0;
}


int update_subscriber(impurecord_t* urec,
        str *watcher_uri, str *watcher_contact,
        int *expires, reg_subscriber** _reg_subscriber) {


    reg_subscriber *rs = *_reg_subscriber;
    if (expires) {
        rs->expires = *expires;
        return 1;
    } else {
        LM_ERR("Failed to update subscriber as expires is expires is null");
        return 0;
    }
}


void external_delete_subscriber(reg_subscriber *s, udomain_t* _t, int lock_domain) {
    LM_DBG("Deleting subscriber");
    impurecord_t* urec;
   
    LM_DBG("Updating reg subscription in IMPU record");

    if(lock_domain) lock_udomain(_t, &s->presentity_uri);
    int res = get_impurecord(_t, &s->presentity_uri, &urec);
    if (res != 0) {
        if(lock_domain) unlock_udomain(_t, &s->presentity_uri);
        return;
    }

    if (urec->shead == s) urec->shead = s->next;
    else s->prev->next = s->next;
    if (urec->stail == s) urec->stail = s->prev;
    else s->next->prev = s->prev;
    LM_DBG("About to free subscriber memory");
    free_subscriber(s);

    if(lock_domain) unlock_udomain(_t, &s->presentity_uri);

}

void delete_subscriber(impurecord_t* urec, reg_subscriber *s) {
    LM_DBG("Deleting subscriber");
    if (urec->shead == s) urec->shead = s->next;
    else s->prev->next = s->next;
    if (urec->stail == s) urec->stail = s->prev;
    else s->next->prev = s->prev;
    LM_DBG("About to free subscriber memory");
    free_subscriber(s);
}

void free_subscriber(reg_subscriber *s) {
    
    unsigned int hash_code=0;
    subs_t subs;
    
    LM_DBG("Freeing subscriber memory");
    
    memset(&subs, 0, sizeof(subs_t));
    
    subs.pres_uri = s->presentity_uri;
    subs.from_tag = s->from_tag;
    subs.to_tag = s->to_tag;
    subs.callid = s->call_id;
    
    /* delete from cache table */
    hash_code= core_hash(&s->call_id, &s->to_tag, sub_dialog_hash_size);
    
    LM_DBG("Removing sub dialog hash info with call_id: <%.*s> and ttag <%.*s> and ftag <%.*s> and hash code <%d>", s->call_id.len, s->call_id.s, s->to_tag.len, s->to_tag.s, s->from_tag.len, s->from_tag.s, hash_code);
    if(pres_delete_shtable(sub_dialog_table,hash_code, &subs)< 0)
    {
	    LM_ERR("record not found in hash table\n");
    }
    
    if (s) {
        shm_free(s);
    }


}

int valid_subscriber(reg_subscriber *s) {
    return (s->expires > act_time);
}