a2a334f1 |
/*
* Copyright (C) 2002-2003 Fhg Fokus
*
|
7dcb7e2a |
* This file is part of SEMS, a free SIP media server.
|
a2a334f1 |
*
|
7dcb7e2a |
* SEMS is free software; you can redistribute it and/or modify
|
a2a334f1 |
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
|
7dcb7e2a |
* (at your option) any later version. This program is released under
* the GPL with the additional exemption that compiling, linking,
* and/or using OpenSSL is allowed.
|
a2a334f1 |
*
|
7dcb7e2a |
* For a license to use the SEMS software under conditions
|
a2a334f1 |
* other than those described here, or to purchase support for this
* software, please contact iptel.org by e-mail at the following addresses:
* info@iptel.org
*
|
7dcb7e2a |
* SEMS is distributed in the hope that it will be useful,
|
a2a334f1 |
* 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 "AmRtpPacket.h"
#include "rtp/rtp.h"
#include "log.h"
|
019791dd |
#include "AmConfig.h"
|
a2a334f1 |
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
AmRtpPacket::AmRtpPacket()
|
7c964b9b |
: data_offset(0)
|
a2a334f1 |
{
|
7c964b9b |
// buffer will be overwritten by received packet
// of hdr+data - does not need to be set to 0s
// memset(buffer,0,4096);
|
a2a334f1 |
}
AmRtpPacket::~AmRtpPacket()
{
}
#ifdef SUPPORT_IPV6
void AmRtpPacket::setAddr(struct sockaddr_storage* a)
{
|
7c964b9b |
memcpy(&addr,a,sizeof(sockaddr_storage));
|
a2a334f1 |
}
void AmRtpPacket::getAddr(struct sockaddr_storage* a)
{
|
7c964b9b |
memcpy(a,&addr,sizeof(sockaddr_storage));
|
a2a334f1 |
}
#else
void AmRtpPacket::setAddr(struct sockaddr_in* a)
{
|
7c964b9b |
memcpy(&addr,a,sizeof(sockaddr_in));
|
a2a334f1 |
}
void AmRtpPacket::getAddr(struct sockaddr_in* a)
{
|
7c964b9b |
memcpy(a,&addr,sizeof(sockaddr_in));
|
a2a334f1 |
}
#endif
int AmRtpPacket::parse()
{
|
7c964b9b |
assert(buffer);
assert(b_size);
rtp_hdr_t* hdr = (rtp_hdr_t*)buffer;
|
a5d8e78a |
if
#ifndef WITH_ZRTP
(hdr->version != RTP_VERSION)
#else
((hdr->version != RTP_VERSION) && (hdr->version != 0))
#endif
{
DBG("received RTP packet with unsupported version (%i).\n",
hdr->version);
return -1;
}
|
7c964b9b |
|
019791dd |
data_offset = sizeof(rtp_hdr_t) + (hdr->cc*4);
|
7c964b9b |
if(hdr->x != 0){
|
a5d8e78a |
#ifndef WITH_ZRTP
|
019791dd |
if (AmConfig::IgnoreRTPXHdrs) {
// skip the extension header
|
a5d8e78a |
#endif
|
019791dd |
if (b_size >= data_offset + 4) {
data_offset +=
ntohs(((rtp_xhdr_t*) (buffer + data_offset))->len)*4;
}
|
a5d8e78a |
#ifndef WITH_ZRTP
|
019791dd |
} else {
DBG("RTP extension headers not supported.\n");
return -1;
}
|
a5d8e78a |
#endif
|
7c964b9b |
}
payload = hdr->pt;
marker = hdr->m;
sequence = ntohs(hdr->seq);
timestamp = ntohl(hdr->ts);
ssrc = ntohl(hdr->ssrc);
if (data_offset >= b_size) {
ERROR("bad rtp packet (header size too big) !\n");
return -1;
}
d_size = b_size - data_offset;
if(hdr->p){
if (buffer[b_size-1]>=d_size){
ERROR("bad rtp packet (invalid padding size) !\n");
return -1;
|
a2a334f1 |
}
|
7c964b9b |
d_size -= buffer[b_size-1];
}
|
a2a334f1 |
|
7c964b9b |
return 0;
|
a2a334f1 |
}
|
58f33c85 |
unsigned char *AmRtpPacket::getData()
{
|
7c964b9b |
return &buffer[data_offset];
|
58f33c85 |
}
|
a5d8e78a |
unsigned char *AmRtpPacket::getBuffer()
{
return &buffer[0];
}
|
a2a334f1 |
int AmRtpPacket::compile(unsigned char* data_buf, unsigned int size)
{
|
7c964b9b |
assert(data_buf);
assert(size);
d_size = size;
b_size = d_size + sizeof(rtp_hdr_t);
assert(b_size <= 4096);
rtp_hdr_t* hdr = (rtp_hdr_t*)buffer;
if(b_size>sizeof(buffer)){
ERROR("builtin buffer size (%d) exceeded: %d\n",
(int)sizeof(buffer), b_size);
return -1;
}
memset(hdr,0,sizeof(rtp_hdr_t));
hdr->version = RTP_VERSION;
hdr->m = marker;
hdr->pt = payload;
|
a2a334f1 |
|
7c964b9b |
hdr->seq = htons(sequence);
hdr->ts = htonl(timestamp);
hdr->ssrc = htonl(ssrc);
|
a2a334f1 |
|
7c964b9b |
data_offset = sizeof(rtp_hdr_t);
memcpy(&buffer[data_offset],data_buf,d_size);
|
a2a334f1 |
|
7c964b9b |
return 0;
|
a2a334f1 |
}
|
a5d8e78a |
int AmRtpPacket::compile_raw(unsigned char* data_buf, unsigned int size)
{
if ((!size) || (!data_buf))
return -1;
if(size>sizeof(buffer)){
ERROR("builtin buffer size (%d) exceeded: %d\n",
(int)sizeof(buffer), size);
return -1;
}
memcpy(&buffer[0], data_buf, size);
b_size = size;
return size;
}
|
a2a334f1 |
int AmRtpPacket::send(int sd)
{
|
7c964b9b |
int err;
|
a2a334f1 |
#ifdef SUPPORT_IPV6
|
7c964b9b |
if(addr.ss_family != PF_INET)
err = sendto(sd,buffer,b_size,0,
|
2015e9dd |
(const struct sockaddr *)&addr,
|
7c964b9b |
sizeof(struct sockaddr_in6));
else
|
a2a334f1 |
#endif
|
7c964b9b |
err = sendto(sd,buffer,b_size,0,
(const struct sockaddr *)&addr,
sizeof(struct sockaddr_in));
|
a2a334f1 |
|
7c964b9b |
if(err == -1){
ERROR("while sending RTP packet: %s\n",strerror(errno));
return -1;
}
|
a2a334f1 |
|
7c964b9b |
return 0;
|
a2a334f1 |
}
int AmRtpPacket::recv(int sd)
{
#ifdef SUPPORT_IPV6
|
7c964b9b |
socklen_t recv_addr_len = sizeof(struct sockaddr_storage);
|
a2a334f1 |
#else
|
7c964b9b |
socklen_t recv_addr_len = sizeof(struct sockaddr_in);
|
a2a334f1 |
#endif
|
7c964b9b |
int ret = recvfrom(sd,buffer,sizeof(buffer),0,
(struct sockaddr*)&addr,
&recv_addr_len);
|
a2a334f1 |
|
7c964b9b |
if(ret > 0){
|
a2a334f1 |
|
7c964b9b |
if(ret > 4096)
return -1;
|
a2a334f1 |
|
7c964b9b |
b_size = ret;
}
|
a2a334f1 |
|
7c964b9b |
return ret;
|
a2a334f1 |
}
|