eliminate some int overflows in iax.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@856 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Michael Jerris 2006-03-13 20:36:58 +00:00
parent 125a2b2de5
commit c5e3144711
6 changed files with 88 additions and 84 deletions

View File

@ -107,7 +107,7 @@ typedef int (*recvfrom_t)(int s, void *buf, size_t len, int flags, struct sockad
struct iax_event {
int etype; /* Type of event */
int subclass; /* Subclass data (event specific) */
unsigned int ts; /* Timestamp */
time_in_ms_t ts; /* Timestamp */
struct iax_session *session; /* Applicable session */
int datalen; /* Length of raw data */
struct iax_ies ies; /* IE's for IAX2 frames */
@ -133,7 +133,7 @@ extern int iax_shutdown(void);
extern int iax_get_fd(void);
/* Find out how many milliseconds until the next scheduled event */
extern int iax_time_to_next_event(void);
extern time_in_ms_t iax_time_to_next_event(void);
/* Generate a new IAX session */
extern struct iax_session *iax_session_new(void);
@ -196,16 +196,16 @@ extern void iax_disable_debug(void);
extern int iax_setup_transfer(struct iax_session *s0, struct iax_session *s1);
struct iax_netstat {
int jitter;
time_in_ms_t jitter;
int losspct;
int losscnt;
int packets;
int delay;
time_in_ms_t delay;
int dropped;
int ooo;
};
/* fills in rtt, and an iax_netstat structure for each of local/remote directions of call */
extern int iax_get_netstats(struct iax_session *s, int *rtt, struct iax_netstat *local, struct iax_netstat *remote);
extern int iax_get_netstats(struct iax_session *s, time_in_ms_t *rtt, struct iax_netstat *local, struct iax_netstat *remote);
extern void iax_set_private(struct iax_session *s, void *pvt);

View File

@ -62,15 +62,15 @@ void gettimeofday(struct timeval *tv, void /*struct timezone*/ *tz);
#include <time.h>
#endif
#include "iax2.h"
#include "iax-client.h"
#include "md5.h"
#ifdef NEWJB
#include "jitterbuf.h"
#endif
typedef long long time_in_ms_t;
#include "iax-client.h"
#include "md5.h"
/*
work around jitter-buffer shrinking in asterisk:
channels/chan_iax2.c:schedule_delivery() shrinks jitter buffer by 2.
@ -172,17 +172,17 @@ struct iax_session {
/* Per session capability */
int capability;
/* Last received timestamp */
unsigned int last_ts;
time_in_ms_t last_ts;
/* Last transmitted timestamp */
unsigned int lastsent;
time_in_ms_t lastsent;
/* Last transmitted voice timestamp */
unsigned int lastvoicets;
/* Next predicted voice ts */
unsigned int nextpred;
time_in_ms_t nextpred;
/* True if the last voice we transmitted was not silence/CNG */
int notsilenttx;
/* Our last measured ping time */
unsigned int pingtime;
time_in_ms_t pingtime;
/* Address of peer */
struct sockaddr_in peeraddr;
/* Our call number */
@ -403,7 +403,7 @@ static int inaddrcmp(struct sockaddr_in *sin1, struct sockaddr_in *sin2)
return (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) || (sin1->sin_port != sin2->sin_port);
}
static int iax_sched_add(struct iax_event *event, struct iax_frame *frame, sched_func func, void *arg, int ms)
static int iax_sched_add(struct iax_event *event, struct iax_frame *frame, sched_func func, void *arg, time_in_ms_t ms)
{
/* Schedule event to be delivered to the client
@ -469,27 +469,27 @@ static int iax_sched_del(struct iax_event *event, struct iax_frame *frame, sched
}
int iax_time_to_next_event(void)
time_in_ms_t iax_time_to_next_event(void)
{
struct iax_sched *cur = schedq;
int min = 999999999;
time_in_ms_t minimum = 999999999;
/* If there are no pending events, we don't need to timeout */
if (!cur)
return -1;
while(cur) {
if (cur->when < min) {
min = cur->when;
if (cur->when < minimum) {
minimum = cur->when;
}
cur = cur->next;
}
if (min <= 0) {
if (minimum <= 0) {
return -1;
}
return (int) (min - current_time_in_ms());
return minimum - current_time_in_ms();
}
struct iax_session *iax_session_new(void)
@ -540,7 +540,7 @@ static int iax_session_valid(struct iax_session *session)
return 0;
}
int iax_get_netstats(struct iax_session *session, int *rtt, struct iax_netstat *local, struct iax_netstat *remote) {
int iax_get_netstats(struct iax_session *session, time_in_ms_t *rtt, struct iax_netstat *local, struct iax_netstat *remote) {
if(!iax_session_valid(session)) return -1;
@ -567,9 +567,9 @@ int iax_get_netstats(struct iax_session *session, int *rtt, struct iax_netstat *
return 0;
}
static int calc_timestamp(struct iax_session *session, unsigned int ts, struct ast_frame *f)
static time_in_ms_t calc_timestamp(struct iax_session *session, time_in_ms_t ts, struct ast_frame *f)
{
unsigned int ms;
time_in_ms_t ms;
time_in_ms_t time_in_ms;
int voice = 0;
int genuine = 0;
@ -607,7 +607,7 @@ static int calc_timestamp(struct iax_session *session, unsigned int ts, struct a
/* If we haven't most recently sent silence, and we're
* close in time, use predicted time */
if(session->notsilenttx && abs(ms - session->nextpred) <= 240) {
if(session->notsilenttx && iax_abs(ms - session->nextpred) <= 240) {
/* Adjust our txcore, keeping voice and non-voice
* synchronized */
session->offset += (int)(ms - session->nextpred)/10;
@ -623,7 +623,7 @@ static int calc_timestamp(struct iax_session *session, unsigned int ts, struct a
* time. Also, round ms to the next multiple of
* frame size (so our silent periods are multiples
* of frame size too) */
int diff = ms % (f->samples / 8);
time_in_ms_t diff = ms % (f->samples / 8);
if(diff)
ms += f->samples/8 - diff;
session->nextpred = ms;
@ -639,7 +639,7 @@ static int calc_timestamp(struct iax_session *session, unsigned int ts, struct a
if (genuine) {
if (ms <= session->lastsent)
ms = session->lastsent + 3;
} else if (abs(ms - session->lastsent) <= 240) {
} else if (iax_abs(ms - session->lastsent) <= 240) {
ms = session->lastsent + 3;
}
@ -1014,7 +1014,7 @@ static unsigned char compress_subclass(int subclass)
return power | IAX_FLAG_SC_LOG;
}
static int iax_send(struct iax_session *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
static int iax_send(struct iax_session *pvt, struct ast_frame *f, time_in_ms_t ts, int seqno, int now, int transfer, int final)
{
/* Queue a packet for delivery on a given private structure. Use "ts" for
timestamp, or calculate if ts is 0. Send immediately without retransmission
@ -1025,8 +1025,8 @@ static int iax_send(struct iax_session *pvt, struct ast_frame *f, unsigned int t
struct iax_frame *fr;
int res;
int sendmini=0;
unsigned int lastsent;
unsigned int fts;
time_in_ms_t lastsent;
time_in_ms_t fts;
if (!pvt) {
IAXERROR "No private structure for packet?\n");
@ -1084,7 +1084,7 @@ static int iax_send(struct iax_session *pvt, struct ast_frame *f, unsigned int t
fr->iseqno = pvt->iseqno;
fh = (struct ast_iax2_full_hdr *)(((char *)fr->af.data) - sizeof(struct ast_iax2_full_hdr));
fh->scallno = htons(fr->callno | IAX_FLAG_FULL);
fh->ts = htonl(fr->ts);
fh->ts = htonl((u_long)(fr->ts));
fh->oseqno = fr->oseqno;
if (transfer) {
fh->iseqno = 0;
@ -1125,7 +1125,7 @@ static int iax_send(struct iax_session *pvt, struct ast_frame *f, unsigned int t
/* Mini frame will do */
mh = (struct ast_iax2_mini_hdr *)(((char *)fr->af.data) - sizeof(struct ast_iax2_mini_hdr));
mh->callno = htons(fr->callno);
mh->ts = htons(fr->ts & 0xFFFF);
mh->ts = htons((u_short)(fr->ts & 0xFFFF));
fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_mini_hdr);
fr->data = mh;
fr->retries = -1;
@ -1162,7 +1162,7 @@ static int iax_predestroy(struct iax_session *pvt)
}
#endif
static int __send_command(struct iax_session *i, char type, int command, unsigned int ts, unsigned char *data, int datalen, int seqno,
static int __send_command(struct iax_session *i, char type, int command, time_in_ms_t ts, unsigned char *data, int datalen, int seqno,
int now, int transfer, int final, int samples)
{
struct ast_frame f;
@ -1181,7 +1181,7 @@ static int __send_command(struct iax_session *i, char type, int command, unsigne
return iax_send(i, &f, ts, seqno, now, transfer, final);
}
static int send_command(struct iax_session *i, char type, int command, unsigned int ts, unsigned char *data, int datalen, int seqno)
static int send_command(struct iax_session *i, char type, int command, time_in_ms_t ts, unsigned char *data, int datalen, int seqno)
{
return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 0, 0);
}
@ -1483,8 +1483,8 @@ static void destroy_session(struct iax_session *session)
}
}
static int iax_send_lagrp(struct iax_session *session, unsigned int ts);
static int iax_send_pong(struct iax_session *session, unsigned int ts);
static int iax_send_lagrp(struct iax_session *session, time_in_ms_t ts);
static int iax_send_pong(struct iax_session *session, time_in_ms_t ts);
static struct iax_event *handle_event(struct iax_event *event)
{
@ -1673,7 +1673,7 @@ int iax_send_link_reject(struct iax_session *session)
return send_command(session, AST_FRAME_HTML, AST_HTML_LINKREJECT, 0, NULL, 0, -1);
}
static int iax_send_pong(struct iax_session *session, unsigned int ts)
static int iax_send_pong(struct iax_session *session, time_in_ms_t ts)
{
struct iax_ie_data ied;
@ -1683,7 +1683,7 @@ static int iax_send_pong(struct iax_session *session, unsigned int ts)
jb_info stats;
jb_getinfo(session->jb, &stats);
iax_ie_append_int(&ied,IAX_IE_RR_JITTER, stats.jitter);
iax_ie_append_int(&ied,IAX_IE_RR_JITTER, (int)stats.jitter);
/* XXX: should be short-term loss pct.. */
if(stats.frames_in == 0) stats.frames_in = 1;
iax_ie_append_int(&ied,IAX_IE_RR_LOSS,
@ -1722,7 +1722,7 @@ static void send_ping(void *s)
return;
}
static int iax_send_lagrp(struct iax_session *session, unsigned int ts)
static int iax_send_lagrp(struct iax_session *session, time_in_ms_t ts)
{
return send_command(session, AST_FRAME_IAX, IAX_COMMAND_LAGRP, ts, NULL, 0, -1);
}
@ -1997,7 +1997,7 @@ int iax_call(struct iax_session *session, char *cidnum, char *cidname, char *ich
return res;
}
static int calc_rxstamp(struct iax_session *session)
static time_in_ms_t calc_rxstamp(struct iax_session *session)
{
time_in_ms_t time_in_ms;
@ -2139,7 +2139,7 @@ static int display_time(int ms)
/* From chan_iax2/steve davies: need to get permission from steve or digium, I guess */
static time_in_ms_t unwrap_timestamp(time_in_ms_t ts, time_in_ms_t last)
{
int x;
time_in_ms_t x;
if ( (ts & 0xFFFF0000) == (last & 0xFFFF0000) ) {
x = ts - last;
@ -2165,7 +2165,7 @@ static time_in_ms_t unwrap_timestamp(time_in_ms_t ts, time_in_ms_t last)
#endif
static struct iax_event *schedule_delivery(struct iax_event *e, unsigned int ts, int updatehistory)
static struct iax_event *schedule_delivery(struct iax_event *e, time_in_ms_t ts, int updatehistory)
{
/*
* This is the core of the IAX jitterbuffer delivery mechanism:
@ -2397,7 +2397,7 @@ static struct iax_event *iax_header_to_event(struct iax_session *session,
struct iax_sched *sch;
unsigned int ts;
int subclass = uncompress_subclass(fh->csub);
int nowts;
time_in_ms_t nowts;
int updatehistory = 1;
ts = ntohl(fh->ts);
/* don't run last_ts backwards; i.e. for retransmits and the like */
@ -2824,7 +2824,7 @@ static struct iax_event *iax_miniheader_to_event(struct iax_session *session,
int datalen)
{
struct iax_event *e;
unsigned int ts;
time_in_ms_t ts;
int updatehistory = 1;
e = (struct iax_event *)malloc(sizeof(struct iax_event) + datalen);
if (e) {
@ -3120,7 +3120,7 @@ struct iax_event *iax_get_event(int blocking)
if (blocking) {
/* Block until there is data if desired */
fd_set fds;
int nextEventTime;
time_in_ms_t nextEventTime;
FD_ZERO(&fds);
FD_SET(netfd, &fds);
@ -3135,8 +3135,8 @@ struct iax_event *iax_get_event(int blocking)
{
struct timeval nextEvent;
nextEvent.tv_sec = nextEventTime / 1000;
nextEvent.tv_usec = (nextEventTime % 1000) * 1000;
nextEvent.tv_sec = (long)(nextEventTime / 1000);
nextEvent.tv_usec = (long)((nextEventTime % 1000) * 1000);
select(netfd + 1, &fds, NULL, NULL, &nextEvent);
}

View File

@ -86,9 +86,9 @@ struct iax_frame {
/* How many retries so far? */
int retries;
/* Outgoing relative timestamp (ms) */
unsigned int ts;
time_in_ms_t ts;
/* How long to wait before retrying */
int retrytime;
time_in_ms_t retrytime;
/* Are we received out of order? */
int outoforder;
/* Have we been sent at all yet? */

View File

@ -14,6 +14,9 @@
#ifndef _IAX2_H
#define _IAX2_H
typedef long long time_in_ms_t;
#define iax_abs(x) ((x) >= 0 ? (x) : -(x))
/* Max version of IAX protocol we support */
#define IAX_PROTO_VERSION 2

View File

@ -13,6 +13,7 @@
* Copyright on this file is disclaimed to Digium for inclusion in Asterisk
*/
#include "iax2.h"
#include "jitterbuf.h"
#include <stdio.h>
#include <stdlib.h>
@ -123,11 +124,11 @@ static int longcmp(const void *a, const void *b)
}
#endif
static int history_put(jitterbuf *jb, long ts, long now, long ms)
static int history_put(jitterbuf *jb, time_in_ms_t ts, time_in_ms_t now, long ms)
{
long delay = now - (ts - jb->info.resync_offset);
long threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold;
long kicked;
time_in_ms_t delay = now - (ts - jb->info.resync_offset);
time_in_ms_t threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold;
time_in_ms_t kicked;
/* don't add special/negative times to history */
if (ts <= 0)
@ -135,7 +136,7 @@ static int history_put(jitterbuf *jb, long ts, long now, long ms)
/* check for drastic change in delay */
if (jb->info.conf.resync_threshold != -1) {
if (abs(delay - jb->info.last_delay) > threshold) {
if (iax_abs(delay - jb->info.last_delay) > threshold) {
jb->info.cnt_delay_discont++;
if (jb->info.cnt_delay_discont > 3) {
/* resync the jitterbuffer */
@ -222,7 +223,7 @@ static void history_calc_maxbuf(jitterbuf *jb)
i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0;
for(;i<jb->hist_ptr;i++) {
long toins = jb->history[i % JB_HISTORY_SZ];
time_in_ms_t toins = jb->history[i % JB_HISTORY_SZ];
/* if the maxbuf should get this */
if(toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) {
@ -276,7 +277,7 @@ static void history_calc_maxbuf(jitterbuf *jb)
static void history_get(jitterbuf *jb)
{
long max, min, jitter;
time_in_ms_t max, min, jitter;
int index;
int count;
@ -316,12 +317,12 @@ static void history_get(jitterbuf *jb)
}
/* returns 1 if frame was inserted into head of queue, 0 otherwise */
static int queue_put(jitterbuf *jb, void *data, int type, long ms, long ts)
static int queue_put(jitterbuf *jb, void *data, int type, long ms, time_in_ms_t ts)
{
jb_frame *frame;
jb_frame *p;
int head = 0;
long resync_ts = ts - jb->info.resync_offset;
time_in_ms_t resync_ts = ts - jb->info.resync_offset;
frame = jb->free;
if(frame) {
@ -382,19 +383,19 @@ static int queue_put(jitterbuf *jb, void *data, int type, long ms, long ts)
return head;
}
static long queue_next(jitterbuf *jb)
static time_in_ms_t queue_next(jitterbuf *jb)
{
if(jb->frames) return jb->frames->ts;
else return -1;
}
static long queue_last(jitterbuf *jb)
static time_in_ms_t queue_last(jitterbuf *jb)
{
if(jb->frames) return jb->frames->prev->ts;
else return -1;
}
static jb_frame *_queue_get(jitterbuf *jb, long ts, int all)
static jb_frame *_queue_get(jitterbuf *jb, time_in_ms_t ts, int all)
{
jb_frame *frame;
frame = jb->frames;
@ -429,7 +430,7 @@ static jb_frame *_queue_get(jitterbuf *jb, long ts, int all)
return NULL;
}
static jb_frame *queue_get(jitterbuf *jb, long ts)
static jb_frame *queue_get(jitterbuf *jb, time_in_ms_t ts)
{
return _queue_get(jb,ts,0);
}
@ -504,7 +505,7 @@ static void jb_dbgqueue(jitterbuf *jb)
}
#endif
int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now)
int jb_put(jitterbuf *jb, void *data, int type, long ms, time_in_ms_t ts, time_in_ms_t now)
{
jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now);
@ -526,10 +527,10 @@ int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now)
}
static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
static int _jb_get(jitterbuf *jb, jb_frame *frameout, time_in_ms_t now, long interpl)
{
jb_frame *frame;
long diff;
time_in_ms_t diff;
/*if((now - jb_next(jb)) > 2 * jb->info.last_voice_ms) jb_warn("SCHED: %ld", (now - jb_next(jb))); */
/* get jitter info */
@ -742,10 +743,10 @@ static int _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
}
}
long jb_next(jitterbuf *jb)
time_in_ms_t jb_next(jitterbuf *jb)
{
if(jb->info.silence_begin_ts) {
long next = queue_next(jb);
time_in_ms_t next = queue_next(jb);
if(next > 0) {
/* shrink during silence */
if (jb->info.target - jb->info.current < -JB_TARGET_EXTRA)
@ -758,7 +759,7 @@ long jb_next(jitterbuf *jb)
}
}
int jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl)
int jb_get(jitterbuf *jb, jb_frame *frameout, time_in_ms_t now, long interpl)
{
int ret = _jb_get(jb,frameout,now,interpl);
#if 0

View File

@ -69,24 +69,24 @@ typedef struct jb_info {
long frames_dropped; /* number of frames dropped (shrinkage) */
long frames_ooo; /* number of frames received out-of-order */
long frames_cur; /* number of frames presently in jb, awaiting delivery.*/
long jitter; /* jitter measured within current history interval*/
long min; /* minimum lateness within current history interval */
long current; /* the present jitterbuffer adjustment */
long target; /* the target jitterbuffer adjustment */
time_in_ms_t jitter; /* jitter measured within current history interval*/
time_in_ms_t min; /* minimum lateness within current history interval */
time_in_ms_t current; /* the present jitterbuffer adjustment */
time_in_ms_t target; /* the target jitterbuffer adjustment */
long losspct; /* recent lost frame percentage (* 1000) */
long next_voice_ts; /* the ts of the next frame to be read from the jb - in receiver's time */
time_in_ms_t next_voice_ts; /* the ts of the next frame to be read from the jb - in receiver's time */
long last_voice_ms; /* the duration of the last voice frame */
long silence_begin_ts; /* the time of the last CNG frame, when in silence */
long last_adjustment; /* the time of the last adjustment */
long last_delay; /* the last now added to history */
time_in_ms_t silence_begin_ts; /* the time of the last CNG frame, when in silence */
time_in_ms_t last_adjustment; /* the time of the last adjustment */
time_in_ms_t last_delay; /* the last now added to history */
long cnt_delay_discont; /* the count of discontinuous delays */
long resync_offset; /* the amount to offset ts to support resyncs */
time_in_ms_t resync_offset; /* the amount to offset ts to support resyncs */
long cnt_contig_interp; /* the number of contiguous interp frames returned */
} jb_info;
typedef struct jb_frame {
void *data; /* the frame data */
long ts; /* the relative delivery time expected */
time_in_ms_t ts; /* the relative delivery time expected */
long ms; /* the time covered by this frame, in sec/8000 */
int type; /* the type of frame */
struct jb_frame *next, *prev;
@ -96,10 +96,10 @@ typedef struct jitterbuf {
jb_info info;
/* history */
long history[JB_HISTORY_SZ]; /* history */
time_in_ms_t history[JB_HISTORY_SZ]; /* history */
int hist_ptr; /* points to index in history for next entry */
long hist_maxbuf[JB_HISTORY_MAXBUF_SZ]; /* a sorted buffer of the max delays (highest first) */
long hist_minbuf[JB_HISTORY_MAXBUF_SZ]; /* a sorted buffer of the min delays (lowest first) */
time_in_ms_t hist_maxbuf[JB_HISTORY_MAXBUF_SZ]; /* a sorted buffer of the max delays (highest first) */
time_in_ms_t hist_minbuf[JB_HISTORY_MAXBUF_SZ]; /* a sorted buffer of the min delays (lowest first) */
int hist_maxbuf_valid; /* are the "maxbuf"/minbuf valid? */
@ -125,7 +125,7 @@ extern void jb_reset(jitterbuf *jb);
* JB_DROP: Drop this frame immediately
* JB_SCHED: Frame added. Call jb_next() to get a new time for the next frame
*/
extern int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long now);
extern int jb_put(jitterbuf *jb, void *data, int type, long ms, time_in_ms_t ts, time_in_ms_t now);
/* get a frame for time now (receiver's time) return value is one of
* JB_OK: You've got frame!
@ -134,14 +134,14 @@ extern int jb_put(jitterbuf *jb, void *data, int type, long ms, long ts, long
* JB_INTERP: Please interpolate an interpl-length frame for this time (either we need to grow, or there was a lost frame)
* JB_EMPTY: The jb is empty.
*/
extern int jb_get(jitterbuf *jb, jb_frame *frame, long now, long interpl);
extern int jb_get(jitterbuf *jb, jb_frame *frame, time_in_ms_t now, long interpl);
/* unconditionally get frames from jitterbuf until empty */
extern int jb_getall(jitterbuf *jb, jb_frame *frameout);
/* when is the next frame due out, in receiver's time (0=EMPTY)
* This value may change as frames are added (esp non-audio frames) */
extern long jb_next(jitterbuf *jb);
extern time_in_ms_t jb_next(jitterbuf *jb);
/* get jitterbuf info: only "statistics" may be valid */
extern int jb_getinfo(jitterbuf *jb, jb_info *stats);