1
/* $Id: stun_simple.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjlib-util/stun_simple.h>
21
#include <pjlib-util/errno.h>
27
#define THIS_FILE "stun_simple.c"
29
PJ_DEF(pj_status_t) pjstun_create_bind_req( pj_pool_t *pool,
30
void **msg, pj_size_t *len,
39
hdr = PJ_POOL_ZALLOC_T(pool, pjstun_msg_hdr);
43
hdr->type = pj_htons(PJSTUN_BINDING_REQUEST);
44
hdr->tsx[2] = pj_htonl(id_hi);
45
hdr->tsx[3] = pj_htonl(id_lo);
47
*len = sizeof(pjstun_msg_hdr);
52
PJ_DEF(pj_status_t) pjstun_parse_msg( void *buf, pj_size_t len,
55
pj_uint16_t msg_type, msg_len;
60
msg->hdr = (pjstun_msg_hdr*)buf;
61
msg_type = pj_ntohs(msg->hdr->type);
64
case PJSTUN_BINDING_REQUEST:
65
case PJSTUN_BINDING_RESPONSE:
66
case PJSTUN_BINDING_ERROR_RESPONSE:
67
case PJSTUN_SHARED_SECRET_REQUEST:
68
case PJSTUN_SHARED_SECRET_RESPONSE:
69
case PJSTUN_SHARED_SECRET_ERROR_RESPONSE:
72
PJ_LOG(4,(THIS_FILE, "Error: unknown msg type %d", msg_type));
73
return PJLIB_UTIL_ESTUNINMSGTYPE;
76
msg_len = pj_ntohs(msg->hdr->length);
77
if (msg_len != len - sizeof(pjstun_msg_hdr)) {
78
PJ_LOG(4,(THIS_FILE, "Error: invalid msg_len %d (expecting %d)",
79
msg_len, len - sizeof(pjstun_msg_hdr)));
80
return PJLIB_UTIL_ESTUNINMSGLEN;
84
p_attr = (char*)buf + sizeof(pjstun_msg_hdr);
87
pjstun_attr_hdr **attr = &msg->attr[msg->attr_count];
89
pj_uint16_t attr_type;
91
*attr = (pjstun_attr_hdr*)p_attr;
92
len = pj_ntohs((pj_uint16_t) ((*attr)->length)) + sizeof(pjstun_attr_hdr);
96
PJ_LOG(4,(THIS_FILE, "Error: length mismatch in attr %d",
98
return PJLIB_UTIL_ESTUNINATTRLEN;
101
attr_type = pj_ntohs((*attr)->type);
102
if (attr_type > PJSTUN_ATTR_REFLECTED_FROM &&
103
attr_type != PJSTUN_ATTR_XOR_MAPPED_ADDR)
105
PJ_LOG(5,(THIS_FILE, "Warning: unknown attr type %x in attr %d. "
106
"Attribute was ignored.",
107
attr_type, msg->attr_count));
110
msg_len = (pj_uint16_t)(msg_len - len);
118
PJ_DEF(void*) pjstun_msg_find_attr( pjstun_msg *msg, pjstun_attr_type t)
124
for (i=0; i<msg->attr_count; ++i) {
125
pjstun_attr_hdr *attr = msg->attr[i];
126
if (pj_ntohs(attr->type) == t)