sippublish.cpp 7.24 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
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
*
*  Author: Patrick Keroulas <patrick.keroulas@savoirfairelinux.com>
*
*  This program 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 3 of the License, or
*  (at your option) any later version.
*
*  This program 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.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
*/

#include <pjsip/sip_endpoint.h>

34
#include "sippresence.h"
35
#include "sip_utils.h"
36 37 38 39 40
#include "sippublish.h"
#include "logger.h"
#include "sipvoiplink.h"

void pres_publish_cb(struct pjsip_publishc_cbparam *param);
41
pj_status_t pres_send_publish(SIPPresence * pres, pj_bool_t active);
Tristan Matthews's avatar
Tristan Matthews committed
42
const pjsip_publishc_opt my_publish_opt = {true}; // this is queue_request
43 44 45 46 47 48 49


/*
 * Client presence publication callback.
 */
void pres_publish_cb(struct pjsip_publishc_cbparam *param)
{
50
    SIPPresence *pres = (SIPPresence*) param->token;
51

Tristan Matthews's avatar
Tristan Matthews committed
52
    if (param->code / 100 != 2 || param->status != PJ_SUCCESS) {
53

Tristan Matthews's avatar
Tristan Matthews committed
54 55
        pjsip_publishc_destroy(param->pubc);
        pres->publish_sess = NULL;
56

Tristan Matthews's avatar
Tristan Matthews committed
57 58
        if (param->status != PJ_SUCCESS) {
            char errmsg[PJ_ERR_MSG_SIZE];
59

Tristan Matthews's avatar
Tristan Matthews committed
60 61 62 63 64 65
            pj_strerror(param->status, errmsg, sizeof(errmsg));
            ERROR("Client publication (PUBLISH) failed, status=%d, msg=%s", param->status, errmsg);
        } else if (param->code == 412) {
            /* 412 (Conditional Request Failed)
             * The PUBLISH refresh has failed, retry with new one.
             */
66
            WARN("Publish retry.");
Tristan Matthews's avatar
Tristan Matthews committed
67 68 69 70 71
            pres_publish(pres);
        } else {
            ERROR("Client publication (PUBLISH) failed (%u/%.*s)",
                  param->code, param->reason.slen, param->reason.ptr);
        }
72 73

    } else {
Tristan Matthews's avatar
Tristan Matthews committed
74 75 76 77 78 79 80
        if (param->expiration < 1) {
            /* Could happen if server "forgot" to include Expires header
             * in the response. We will not renew, so destroy the pubc.
             */
            pjsip_publishc_destroy(param->pubc);
            pres->publish_sess = NULL;
        }
81 82 83 84 85 86
    }
}

/*
 * Send PUBLISH request.
 */
87
pj_status_t pres_send_publish(SIPPresence * pres, pj_bool_t active)
88 89 90 91 92 93
{
    pjsip_tx_data *tdata;
    pj_status_t status;

    DEBUG("Send presence %sPUBLISH..", (active ? "" : "un-"));

94
    SIPAccount * acc = pres->getAccount();
95 96 97 98
    std::string contactWithAngles =  acc->getFromUri();
    contactWithAngles.erase(contactWithAngles.find('>'));
    int semicolon = contactWithAngles.find_first_of(":");
    std::string contactWithoutAngles = contactWithAngles.substr(semicolon + 1);
99
//    pj_str_t contact = pj_str(strdup(contactWithoutAngles.c_str()));
100 101 102 103
//    pj_memcpy(&pres_status_data.info[0].contact, &contt, sizeof(pj_str_t));;

    /* Create PUBLISH request */
    if (active) {
Tristan Matthews's avatar
Tristan Matthews committed
104 105 106 107
        char *bpos;
        pj_str_t entity;

        status = pjsip_publishc_publish(pres->publish_sess, PJ_TRUE, &tdata);
108

Tristan Matthews's avatar
Tristan Matthews committed
109 110 111 112
        if (status != PJ_SUCCESS) {
            ERROR("Error creating PUBLISH request", status);
            goto on_error;
        }
113 114

        pj_str_t from = pj_str(strdup(acc->getFromUri().c_str()));
Tristan Matthews's avatar
Tristan Matthews committed
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

        if ((bpos = pj_strchr(&from, '<')) != NULL) {
            char *epos = pj_strchr(&from, '>');

            if (epos - bpos < 2) {
                pj_assert(!"Unexpected invalid URI");
                status = PJSIP_EINVALIDURI;
                goto on_error;
            }

            entity.ptr = bpos + 1;
            entity.slen = epos - bpos - 1;
        } else {
            entity = from;
        }

        /* Create and add PIDF message body */
        status = pjsip_pres_create_pidf(tdata->pool, pres->getStatus(),
                                        &entity, &tdata->msg->body);

        if (status != PJ_SUCCESS) {
            ERROR("Error creating PIDF for PUBLISH request");
            pjsip_tx_data_dec_ref(tdata);
            goto on_error;
        }
140 141

    } else {
142
        WARN("Unpublish is not implemented.");
143 144 145 146 147 148 149 150 151
    }


    pres_msg_data msg_data;
    pj_bzero(&msg_data, sizeof(msg_data));
    pj_list_init(&msg_data.hdr_list);
    pjsip_media_type_init(&msg_data.multipart_ctype, NULL, NULL);
    pj_list_init(&msg_data.multipart_parts);

152
    pres->fillDoc(tdata, &msg_data);
153 154

    /* Send the PUBLISH request */
155
    status = pjsip_publishc_send(pres->publish_sess, tdata);
Tristan Matthews's avatar
Tristan Matthews committed
156

157
    if (status == PJ_EPENDING) {
Tristan Matthews's avatar
Tristan Matthews committed
158
        WARN("Previous request is in progress, ");
159
    } else if (status != PJ_SUCCESS) {
Tristan Matthews's avatar
Tristan Matthews committed
160 161
        ERROR("Error sending PUBLISH request");
        goto on_error;
162 163 164 165 166
    }

    return PJ_SUCCESS;

on_error:
Tristan Matthews's avatar
Tristan Matthews committed
167

168
    if (pres->publish_sess) {
Tristan Matthews's avatar
Tristan Matthews committed
169 170
        pjsip_publishc_destroy(pres->publish_sess);
        pres->publish_sess = NULL;
171
    }
Tristan Matthews's avatar
Tristan Matthews committed
172

173 174 175 176 177
    return status;
}


/* Create client publish session */
178
pj_status_t pres_publish(SIPPresence *pres)
179 180
{
    pj_status_t status;
181
    const pj_str_t STR_PRESENCE = pj_str("presence");
182
    SIPAccount * acc = pres->getAccount();
183 184 185 186
    pjsip_endpoint *endpt = ((SIPVoIPLink*) acc->getVoIPLink())->getEndpoint();

    /* Create and init client publication session */

187
    /* Create client publication */
Tristan Matthews's avatar
Tristan Matthews committed
188
    status = pjsip_publishc_create(endpt, &my_publish_opt,
189 190
                                   pres, &pres_publish_cb,
                                   &pres->publish_sess);
Tristan Matthews's avatar
Tristan Matthews committed
191

192 193 194 195 196
    if (status != PJ_SUCCESS) {
        pres->publish_sess = NULL;
        ERROR("Failed to create a publish seesion.");
        return status;
    }
197

198 199
    /* Initialize client publication */
    pj_str_t from = pj_str(strdup(acc->getFromUri().c_str()));
Tristan Matthews's avatar
Tristan Matthews committed
200 201
    status = pjsip_publishc_init(pres->publish_sess, &STR_PRESENCE, &from, &from, &from, 0xFFFF);

202 203 204 205 206
    if (status != PJ_SUCCESS) {
        ERROR("Failed to init a publish session");
        pres->publish_sess = NULL;
        return status;
    }
207

208 209 210 211 212
    /* Add credential for authentication */
    if (acc->hasCredentials() and pjsip_publishc_set_credentials(pres->publish_sess, acc->getCredentialCount(), acc->getCredInfo()) != PJ_SUCCESS) {
        ERROR("Could not initialize credentials for invite session authentication");
        return status;
    }
213

214 215 216
    /* Set route-set */
    if (acc->hasServiceRoute())
        pjsip_regc_set_route_set(acc->getRegistrationInfo(), sip_utils::createRouteSet(acc->getServiceRoute(), pres->getPool()));
217

218 219
    /* Send initial PUBLISH request */
    status = pres_send_publish(pres, PJ_TRUE);
Tristan Matthews's avatar
Tristan Matthews committed
220

221 222
    if (status != PJ_SUCCESS)
        return status;
223 224 225

    return PJ_SUCCESS;
}