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
|
/*
* Presence Agent, XPIDF document support
*
* $Id: xpidf.c,v 1.3 2005/12/08 10:58:56 bogdan_iancu Exp $
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of openser, a free SIP server.
*
* openser 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
*
* openser 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include "../../dprint.h"
#include "paerrno.h"
#include "common.h"
#include "xpidf.h"
#define CRLF "\r\n"
#define CRLF_L (sizeof(CRLF) - 1)
#define PUBLIC_ID "//IETF//DTD RFCxxxx XPIDF 1.0//EN"
#define PUBLIC_ID_L (sizeof(PUBLIC_ID) - 1)
#define MIME_TYPE "application/xpidf+xml"
#define MIME_TYPE_L (sizeof(MIME_TYPE) - 1)
#define XML_VERSION "<?xml version=\"1.0\"?>"
#define XML_VERSION_L (sizeof(XML_VERSION) - 1)
#define PRESENCE_STAG "<presence>"
#define PRESENCE_STAG_L (sizeof(PRESENCE_STAG) - 1)
#define PRESENCE_ETAG "</presence>"
#define PRESENCE_ETAG_L (sizeof(PRESENCE_ETAG) - 1)
#define ADDRESS_ETAG "</address>"
#define ADDRESS_ETAG_L (sizeof(ADDRESS_ETAG) - 1)
#define ATOM_ETAG "</atom>"
#define ATOM_ETAG_L (sizeof(ATOM_ETAG) - 1)
#define XPIDF_DTD "xpidf.dtd"
#define XPIDF_DTD_L (sizeof(XPDIF_DTD) - 1)
#define DOCTYPE "<!DOCTYPE presence PUBLIC \"" PUBLIC_ID "\" \"" XPIDF_DTD "\">"
#define DOCTYPE_L (sizeof(DOCTYPE) - 1)
#define PRESENTITY_START "<presentity uri=\"sip:"
#define PRESENTITY_START_L (sizeof(PRESENTITY_START) - 1)
#define PRESENTITY_END ";method=SUBSCRIBE\"/>"
#define PRESENTITY_END_L (sizeof(PRESENTITY_END) - 1)
#define ATOM_STAG "<atom id=\"9r28r49\">"
#define ATOM_STAG_L (sizeof(ATOM_STAG) - 1)
#define ADDRESS_START "<address uri=\"sip:"
#define ADDRESS_START_L (sizeof(ADDRESS_START) - 1)
#define ADDRESS_END ";user=ip\" priority=\"0,800000\">"
#define ADDRESS_END_L (sizeof(ADDRESS_END) - 1)
#define STATUS_OPEN "<status status=\"open\"/>"
#define STATUS_OPEN_L (sizeof(STATUS_OPEN) - 1)
#define STATUS_CLOSED "<status status=\"closed\"/>"
#define STATUS_CLOSED_L (sizeof(STATUS_CLOSED) - 1)
#define MSNSUBSTATUS_ONLINE "<msnsubstatus substatus=\"online\"/>\r\n"
#define MSNSUBSTATUS_ONLINE_L (sizeof(MSNSUBSTATUS_ONLINE)-1)
#define MSNSUBSTATUS_OFFLINE "<msnsubstatus substatus=\"offline\"/>\r\n"
#define MSNSUBSTATUS_OFFLINE_L (sizeof(MSNSUBSTATUS_OFFLINE)-1)
/*
* Create start of pidf document
*/
int start_xpidf_doc(str* _b, int _l)
{
if (!_b || !_b->s) {
LOG(L_ERR, "start_xpidf_doc: Invalid parameter value\n");
paerrno = PA_INTERNAL_ERROR;
return -1;
}
if ((XML_VERSION_L +
CRLF_L +
DOCTYPE_L +
CRLF_L +
PRESENCE_STAG_L +
CRLF_L
) > _l) {
paerrno = PA_SMALL_BUFFER;
LOG(L_ERR, "start_xpidf_doc(): Buffer too small\n");
return -1;
}
str_append(_b, XML_VERSION CRLF DOCTYPE CRLF PRESENCE_STAG CRLF,
XML_VERSION_L + CRLF_L + DOCTYPE_L + CRLF_L + PRESENCE_STAG_L + CRLF_L);
return 0;
}
/*
* Add a presentity information
*/
int xpidf_add_presentity(str* _b, int _l, str* _uri)
{
if (!_b || !_b->s || !_uri || !_uri->s) {
LOG(L_ERR, "xpidf_add_presentity: Invalid parameter value\n");
paerrno = PA_INTERNAL_ERROR;
return -1;
}
if (_l < PRESENTITY_START_L + _uri->len + PRESENTITY_END_L + CRLF_L) {
paerrno = PA_SMALL_BUFFER;
LOG(L_ERR, "xpidf_add_presentity(): Buffer too small\n");
return -1;
}
str_append(_b, PRESENTITY_START, PRESENTITY_START_L);
str_append(_b, _uri->s, _uri->len);
str_append(_b, PRESENTITY_END CRLF, PRESENTITY_END_L + CRLF_L);
return 0;
}
/*
* Add a contact address with given status
*/
int xpidf_add_address(str* _b, int _l, str* _addr, xpidf_status_t _st)
{
int len = 0;
char* p;
#ifdef WITH_MSN
int len_available = 0;
char * available;
#endif
switch(_st) {
case XPIDF_ST_OPEN:
p = STATUS_OPEN;
len = STATUS_OPEN_L;
#ifdef WITH_MSN
available = MSNSUBSTATUS_ONLINE;
len_available = MSNSUBSTATUS_ONLINE_L;
#endif
break;
#if 0
case XPIDF_ST_INUSE:
p = STATUS_INUSE;
len = STATUS_INUSE_L;
available = MSNSUBSTATUS_OFFLINE;
len_available = MSNSUBSTATUS_OFFLINE_L;
break;
#endif
default:
case XPIDF_ST_CLOSED:
p = STATUS_CLOSED;
len = STATUS_CLOSED_L;
#ifdef WITH_MSN
available = MSNSUBSTATUS_OFFLINE;
len_available = MSNSUBSTATUS_OFFLINE_L;
#endif
break;
}
if (!_b || !_b->s || !_addr || !_addr->s) {
LOG(L_ERR, "xpidf_add_address: Invalid parameter value\n");
paerrno = PA_INTERNAL_ERROR;
return -1;
}
if (_l < (ATOM_STAG_L +
CRLF_L +
ADDRESS_START_L +
_addr->len +
ADDRESS_END_L +
CRLF_L +
len +
#ifdef WITH_MSN
len_available +
#endif
CRLF_L +
ADDRESS_ETAG_L +
CRLF_L +
ATOM_ETAG_L +
CRLF_L
)
) {
paerrno = PA_SMALL_BUFFER;
LOG(L_ERR, "xpidf_add_address(): Buffer too small\n");
return -1;
}
str_append(_b, ATOM_STAG CRLF ADDRESS_START, ATOM_STAG_L + CRLF_L + ADDRESS_START_L);
str_append(_b, _addr->s, _addr->len);
str_append(_b, ADDRESS_END CRLF, ADDRESS_END_L + CRLF_L);
str_append(_b, p, len);
#ifdef WITH_MSN
str_append(_b,available, len_available);
#endif
str_append(_b, CRLF ADDRESS_ETAG CRLF ATOM_ETAG CRLF,
CRLF_L + ADDRESS_ETAG_L + CRLF_L + ATOM_ETAG_L + CRLF_L);
return 0;
}
/*
* End the document
*/
int end_xpidf_doc(str* _b, int _l)
{
if (_l < (PRESENCE_ETAG_L + CRLF_L)) {
paerrno = PA_SMALL_BUFFER;
LOG(L_ERR, "end_xpidf_doc(): Buffer too small\n");
return -1;
}
str_append(_b, PRESENCE_ETAG CRLF, PRESENCE_ETAG_L + CRLF_L);
return 0;
}
|