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
|
/**
* @file slpmsg.c SLP Message functions
*
* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* 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 2 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 02111-1301 USA
*/
#include "internal.h"
#include "debug.h"
#include "slpmsg.h"
#include "slpmsg_part.h"
#include "slplink.h"
/**************************************************************************
* SLP Message
**************************************************************************/
MsnSlpMessage *
msn_slpmsg_new(MsnSlpLink *slplink, MsnSlpCall *slpcall)
{
MsnSlpMessage *slpmsg;
MsnP2PVersion p2p;
g_return_val_if_fail(slplink != NULL, NULL);
slpmsg = g_new0(MsnSlpMessage, 1);
if (purple_debug_is_verbose())
purple_debug_info("msn", "slpmsg new (%p)\n", slpmsg);
msn_slpmsg_set_slplink(slpmsg, slplink);
slpmsg->slpcall = slpcall;
p2p = msn_slplink_get_p2p_version(slplink);
slpmsg->p2p_info = msn_p2p_info_new(p2p);
return slpmsg;
}
void
msn_slpmsg_destroy(MsnSlpMessage *slpmsg)
{
MsnSlpLink *slplink;
GList *cur;
g_return_if_fail(slpmsg != NULL);
if (purple_debug_is_verbose())
purple_debug_info("msn", "slpmsg destroy (%p)\n", slpmsg);
slplink = slpmsg->slplink;
purple_imgstore_unref(slpmsg->img);
/* We don't want to free the data of the PurpleStoredImage,
* but to avoid code duplication, it's sharing buffer. */
if (slpmsg->img == NULL)
g_free(slpmsg->buffer);
for (cur = slpmsg->parts; cur != NULL; cur = g_list_delete_link(cur, cur))
{
/* Something is pointing to this slpmsg, so we should remove that
* pointer to prevent a crash. */
/* Ex: a user goes offline and after that we receive an ACK */
MsnSlpMessagePart *part = cur->data;
part->ack_cb = NULL;
part->nak_cb = NULL;
part->ack_data = NULL;
msn_slpmsgpart_unref(part);
}
slplink->slp_msgs = g_list_remove(slplink->slp_msgs, slpmsg);
msn_p2p_info_free(slpmsg->p2p_info);
g_free(slpmsg);
}
void
msn_slpmsg_set_slplink(MsnSlpMessage *slpmsg, MsnSlpLink *slplink)
{
g_return_if_fail(slplink != NULL);
slpmsg->slplink = slplink;
slplink->slp_msgs =
g_list_append(slplink->slp_msgs, slpmsg);
}
void
msn_slpmsg_set_body(MsnSlpMessage *slpmsg, const char *body,
long long size)
{
/* We can only have one data source at a time. */
g_return_if_fail(slpmsg->buffer == NULL);
g_return_if_fail(slpmsg->img == NULL);
g_return_if_fail(slpmsg->ft == FALSE);
if (body != NULL)
slpmsg->buffer = g_memdup(body, size);
else
slpmsg->buffer = g_new0(guchar, size);
slpmsg->size = size;
}
void
msn_slpmsg_set_image(MsnSlpMessage *slpmsg, PurpleStoredImage *img)
{
/* We can only have one data source at a time. */
g_return_if_fail(slpmsg->buffer == NULL);
g_return_if_fail(slpmsg->img == NULL);
g_return_if_fail(slpmsg->ft == FALSE);
slpmsg->img = purple_imgstore_ref(img);
slpmsg->buffer = (guchar *)purple_imgstore_get_data(img);
slpmsg->size = purple_imgstore_get_size(img);
}
MsnSlpMessage *
msn_slpmsg_sip_new(MsnSlpCall *slpcall, int cseq,
const char *header, const char *branch,
const char *content_type, const char *content)
{
MsnSlpLink *slplink;
PurpleAccount *account;
MsnSlpMessage *slpmsg;
char *body;
gsize body_len;
gsize content_len;
g_return_val_if_fail(slpcall != NULL, NULL);
g_return_val_if_fail(header != NULL, NULL);
slplink = slpcall->slplink;
account = slplink->session->account;
/* Let's remember that "content" should end with a 0x00 */
content_len = (content != NULL) ? strlen(content) + 1 : 0;
body = g_strdup_printf(
"%s\r\n"
"To: <msnmsgr:%s>\r\n"
"From: <msnmsgr:%s>\r\n"
"Via: MSNSLP/1.0/TLP ;branch={%s}\r\n"
"CSeq: %d\r\n"
"Call-ID: {%s}\r\n"
"Max-Forwards: 0\r\n"
"Content-Type: %s\r\n"
"Content-Length: %" G_GSIZE_FORMAT "\r\n"
"\r\n",
header,
slplink->remote_user,
purple_account_get_username(account),
branch,
cseq,
slpcall->id,
content_type,
content_len);
body_len = strlen(body);
if (content_len > 0)
{
body_len += content_len;
body = g_realloc(body, body_len);
g_strlcat(body, content, body_len);
}
slpmsg = msn_slpmsg_new(slplink, slpcall);
msn_slpmsg_set_body(slpmsg, body, body_len);
g_free(body);
return slpmsg;
}
MsnSlpMessage *msn_slpmsg_ack_new(MsnSlpLink *slplink, MsnP2PInfo *ack_info)
{
MsnSlpMessage *slpmsg;
MsnP2PInfo *new_info;
slpmsg = msn_slpmsg_new(slplink, NULL);
new_info = slpmsg->p2p_info;
msn_p2p_info_create_ack(ack_info, new_info);
slpmsg->size = msn_p2p_info_get_total_size(ack_info);
slpmsg->info = "SLP ACK";
return slpmsg;
}
MsnSlpMessage *msn_slpmsg_obj_new(MsnSlpCall *slpcall, PurpleStoredImage *img)
{
MsnSlpMessage *slpmsg;
slpmsg = msn_slpmsg_new(slpcall->slplink, slpcall);
msn_p2p_info_set_flags(slpmsg->p2p_info, P2P_MSN_OBJ_DATA);
slpmsg->info = "SLP DATA";
msn_slpmsg_set_image(slpmsg, img);
return slpmsg;
}
MsnSlpMessage *msn_slpmsg_dataprep_new(MsnSlpCall *slpcall)
{
MsnSlpMessage *slpmsg;
slpmsg = msn_slpmsg_new(slpcall->slplink, slpcall);
msn_p2p_info_set_session_id(slpmsg->p2p_info, slpcall->session_id);
msn_slpmsg_set_body(slpmsg, NULL, 4);
slpmsg->info = "SLP DATA PREP";
return slpmsg;
}
MsnSlpMessage *msn_slpmsg_file_new(MsnSlpCall *slpcall, size_t size)
{
MsnSlpMessage *slpmsg;
slpmsg = msn_slpmsg_new(slpcall->slplink, slpcall);
msn_p2p_info_set_flags(slpmsg->p2p_info, P2P_FILE_DATA);
slpmsg->info = "SLP FILE";
slpmsg->size = size;
return slpmsg;
}
char *msn_slpmsg_serialize(MsnSlpMessage *slpmsg, size_t *ret_size)
{
char *header;
char *footer;
char *base;
char *tmp;
size_t header_size, footer_size;
header = msn_p2p_header_to_wire(slpmsg->p2p_info, &header_size);
footer = msn_p2p_footer_to_wire(slpmsg->p2p_info, &footer_size);
base = g_malloc(header_size + slpmsg->size + footer_size);
tmp = base;
/* Copy header */
memcpy(tmp, header, header_size);
tmp += header_size;
/* Copy body */
memcpy(tmp, slpmsg->buffer, slpmsg->size);
tmp += slpmsg->size;
/* Copy footer */
memcpy(tmp, footer, footer_size);
tmp += footer_size;
*ret_size = tmp - base;
g_free(header);
g_free(footer);
return base;
}
void msn_slpmsg_show_readable(MsnSlpMessage *slpmsg)
{
GString *str;
str = g_string_new(NULL);
msn_p2p_info_to_string(slpmsg->p2p_info, str);
if (purple_debug_is_verbose() && slpmsg->buffer != NULL) {
g_string_append_len(str, (gchar*)slpmsg->buffer, slpmsg->size);
if (slpmsg->buffer[slpmsg->size - 1] == '\0') {
str->len--;
g_string_append(str, " 0x00");
}
g_string_append(str, "\r\n");
}
purple_debug_info("msn", "SlpMessage %s:\n{%s}\n", slpmsg->info, str->str);
}
|