all: updated FSF address in GPL text
[sip-router] / modules / ims_registrar_pcscf / subscribe.c
1 /*
2  * pua_reginfo module - Presence-User-Agent Handling of reg events
3  *
4  * Copyright (C) 2011 Carsten Bock, carsten@ng-voice.com
5  * http://www.ng-voice.com
6  *
7  * This file is part of Kamailio, a free SIP server.
8  *
9  * Kamailio is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * Kamailio is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License 
20  * along with this program; if not, write to the Free Software 
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  * 
23  * * History:
24  * ========
25  * 
26  * Nov 2013 Richard Good migrated pua_reginfo funtionality to ims_registrar_pcscf
27  */
28
29 #include "subscribe.h"
30
31 #include "../pua/send_subscribe.h"
32 #include "../pua/pua.h"
33
34 #include "../pua/pua_bind.h"
35
36
37 extern pua_api_t pua;
38 extern char* pcscf_uri;
39
40 #define P_ASSERTED_IDENTITY_HDR_PREFIX  "P-Asserted-Identity: <"
41
42 int reginfo_subscribe_real(struct sip_msg* msg, pv_elem_t* uri, str* service_routes, int expires) {
43         str uri_str = {0, 0};
44         char uri_buf[512];
45         int uri_buf_len = 512;
46         subs_info_t subs;
47         str server_address = {pcscf_uri, strlen(pcscf_uri)};
48         str p_asserted_identity_header;
49         
50         int len = strlen(P_ASSERTED_IDENTITY_HDR_PREFIX) + server_address.len + 1 + CRLF_LEN;
51         p_asserted_identity_header.s = (char *)pkg_malloc( len );
52         if ( p_asserted_identity_header.s == NULL ) {
53             LM_ERR( "insert_asserted_identity: pkg_malloc %d bytes failed", len );
54             return -1;
55         }
56
57         memcpy(p_asserted_identity_header.s, P_ASSERTED_IDENTITY_HDR_PREFIX, strlen(P_ASSERTED_IDENTITY_HDR_PREFIX));
58         p_asserted_identity_header.len = strlen(P_ASSERTED_IDENTITY_HDR_PREFIX);
59         memcpy(p_asserted_identity_header.s + p_asserted_identity_header.len, server_address.s, server_address.len);
60         p_asserted_identity_header.len += server_address.len;
61         *(p_asserted_identity_header.s + p_asserted_identity_header.len) = '>';
62         p_asserted_identity_header.len ++;
63         memcpy( p_asserted_identity_header.s + p_asserted_identity_header.len, CRLF, CRLF_LEN );
64         p_asserted_identity_header.len += CRLF_LEN;
65         
66         if (pv_printf(msg, uri, uri_buf, &uri_buf_len) < 0) {
67                 LM_ERR("cannot print uri into the format\n");
68                 if (p_asserted_identity_header.s) {
69                     pkg_free(p_asserted_identity_header.s);
70                 }
71                 return -1;
72         }
73         uri_str.s = uri_buf;
74         uri_str.len = uri_buf_len;
75         
76         LM_DBG("p_asserted_identity_header: [%.*s]", p_asserted_identity_header.len, p_asserted_identity_header.s);
77
78         LM_DBG("Subscribing to %.*s\n", uri_str.len, uri_str.s);
79
80         memset(&subs, 0, sizeof(subs_info_t));
81
82         subs.remote_target = &uri_str;
83         subs.pres_uri= &uri_str;
84         subs.watcher_uri= &server_address;
85         subs.expires = expires;
86
87         subs.source_flag= REGINFO_SUBSCRIBE;
88         subs.event= REGINFO_EVENT;
89         subs.contact= &server_address;
90         subs.extra_headers = &p_asserted_identity_header;
91         
92         subs.flag|= UPDATE_TYPE;
93         
94         if(pua.send_subscribe(&subs)< 0) {
95                 LM_ERR("while sending subscribe\n");
96         }       
97         
98         if (p_asserted_identity_header.s) {
99                 pkg_free(p_asserted_identity_header.s);
100         }
101
102         return 1;
103 }
104
105
106