--- rtp.c.old	2005-01-19 03:33:54.000000000 +0100
+++ rtp.c	2005-09-21 16:35:19.000000000 +0200
@@ -113,12 +113,14 @@
 
 int ast_rtp_fd(struct ast_rtp *rtp)
 {
-	return rtp->s;
+	if (rtp)
+		return rtp->s;
+	return -1;
 }
 
 int ast_rtcp_fd(struct ast_rtp *rtp)
 {
-	if (rtp->rtcp)
+	if (rtp && rtp->rtcp)
 		return rtp->rtcp->s;
 	return -1;
 }
@@ -161,17 +163,20 @@
 
 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
 {
-	rtp->data = data;
+	if (rtp)
+		rtp->data = data;
 }
 
 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
 {
-	rtp->callback = callback;
+	if (rtp)
+		rtp->callback = callback;
 }
 
 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
 {
-	rtp->nat = nat;
+	if (rtp)
+		rtp->nat = nat;
 }
 
 static struct ast_frame *send_dtmf(struct ast_rtp *rtp)
@@ -210,6 +215,10 @@
 	unsigned int event;
 	char resp = 0;
 	struct ast_frame *f = NULL;
+	
+	if (!rtp)
+		return f;
+	
 	event = ntohl(*((unsigned int *)(data)));
 	event &= 0x001F;
 #if 0
@@ -241,6 +250,10 @@
 	unsigned int duration;
 	char resp = 0;
 	struct ast_frame *f = NULL;
+	
+	if (!rtp)
+		return f;
+		
 	event = ntohl(*((unsigned int *)(data)));
 	event >>= 24;
 	event_end = ntohl(*((unsigned int *)(data)));
@@ -288,6 +301,8 @@
 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
 {
 	struct ast_frame *f = NULL;
+	if (!rtp)
+		return f;
 	/* Convert comfort noise into audio with various codecs.  Unfortunately this doesn't
 	   totally help us out becuase we don't have an engine to keep it going and we are not
 	   guaranteed to have it every 20ms or anything */
@@ -342,7 +357,7 @@
 	unsigned int rtcpdata[1024];
 	char iabuf[INET_ADDRSTRLEN];
 	
-	if (!rtp->rtcp)
+	if (!rtp || !rtp->rtcp)
 		return &null_frame;
 
 	len = sizeof(sin);
@@ -380,6 +395,8 @@
 
 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
 {
+	if (!rtp)
+		return;
 	if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
 		gettimeofday(&rtp->rxcore, NULL);
 		rtp->rxcore.tv_sec -= timestamp / 8000;
@@ -417,6 +434,9 @@
 	static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
 	struct rtpPayloadType rtpPT;
 	
+	if (!rtp)
+		return f;
+	
 	len = sizeof(sin);
 	
 	/* Cache where the header will go */
@@ -652,6 +672,9 @@
 {
   int i;
 
+  if (!rtp)
+  	return;
+  
   for (i = 0; i < MAX_RTP_PT; ++i) {
     rtp->current_RTP_PT[i].isAstFormat = 0;
     rtp->current_RTP_PT[i].code = 0;
@@ -666,6 +689,8 @@
 {
   int i;
   /* Initialize to default payload types */
+  if (!rtp)
+  	return;
   for (i = 0; i < MAX_RTP_PT; ++i) {
     rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
     rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
@@ -680,7 +705,7 @@
 /* By default, use the well-known value for this type (although it may */
 /* still be set to a different value by a subsequent "a=rtpmap:" line): */
 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
-  if (pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
+  if (!rtp || pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
 
   if (static_RTP_PT[pt].code != 0) {
     rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
@@ -693,7 +718,7 @@
 			 char* mimeType, char* mimeSubtype) {
   int i;
 
-  if (pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
+  if (!rtp || pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
 
   for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
     if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
@@ -709,6 +734,9 @@
 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
 			     int* astFormats, int* nonAstFormats) {
   int pt;
+  
+  if (!rtp)
+  	return;
 
   *astFormats = *nonAstFormats = 0;
   for (pt = 0; pt < MAX_RTP_PT; ++pt) {
@@ -732,7 +760,7 @@
   struct rtpPayloadType result;
 
   result.isAstFormat = result.code = 0;
-  if (pt < 0 || pt > MAX_RTP_PT) {
+  if (!rtp || pt < 0 || pt > MAX_RTP_PT) {
     return result; /* bogus payload type */
   }
   /* Start with the negotiated codecs */
@@ -748,6 +776,8 @@
 int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code) {
   int pt;
 
+  if (!rtp)
+  	return -1;
 
   if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
       code == rtp->rtp_lookup_code_cache_code) {
@@ -911,6 +941,9 @@
 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
 {
 	int res;
+
+	if (!rtp)
+		return -1;
 	if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) 
 		ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
 	return res;
@@ -918,6 +951,8 @@
 
 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
 {
+	if (!rtp || !them)
+		return;
 	rtp->them.sin_port = them->sin_port;
 	rtp->them.sin_addr = them->sin_addr;
 	if (rtp->rtcp) {
@@ -928,6 +963,8 @@
 
 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
 {
+	if (!rtp || !them)
+  		return;
 	them->sin_family = AF_INET;
 	them->sin_port = rtp->them.sin_port;
 	them->sin_addr = rtp->them.sin_addr;
@@ -935,11 +972,15 @@
 
 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
 {
+	if (!rtp || !us)
+		return;
 	memcpy(us, &rtp->us, sizeof(rtp->us));
 }
 
 void ast_rtp_stop(struct ast_rtp *rtp)
 {
+	if (!rtp)
+		return;
 	memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
 	memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
 	if (rtp->rtcp) {
@@ -950,6 +991,8 @@
 
 void ast_rtp_destroy(struct ast_rtp *rtp)
 {
+	if (!rtp)
+		return;
 	if (rtp->smoother)
 		ast_smoother_free(rtp->smoother);
 	if (rtp->ioid)
@@ -967,6 +1010,7 @@
 {
 	struct timeval now;
 	unsigned int ms;
+
 	if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
 		gettimeofday(&rtp->txcore, NULL);
 		/* Round to 20ms for nice, pretty timestamps */
@@ -998,6 +1042,9 @@
 	int payload;
 	char data[256];
 	char iabuf[INET_ADDRSTRLEN];
+	
+	if (!rtp)
+		return 0;
 
 	if ((digit <= '9') && (digit >= '0'))
 		digit -= '0';
@@ -1064,6 +1111,10 @@
 	int ms;
 	int pred;
 	int mark = 0;
+	
+	if (!rtp || !f)
+		return 0;
+
 
 	ms = calc_txstamp(rtp, &f->delivery);
 	/* Default prediction */
@@ -1160,11 +1211,11 @@
 	
 
 	/* If we have no peer, return immediately */	
-	if (!rtp->them.sin_addr.s_addr)
+	if (!rtp || !rtp->them.sin_addr.s_addr)
 		return 0;
 
 	/* If there is no data length, return immediately */
-	if (!_f->datalen) 
+	if (!_f || !_f->datalen) 
 		return 0;
 	
 	/* Make sure we have enough space for RTP header */
@@ -1284,8 +1335,12 @@
 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
 {
 	struct ast_rtp_protocol *cur, *prev;
+	if (!proto)
+		return;
+
 	cur = protos;
 	prev = NULL;
+	
 	while(cur) {
 		if (cur == proto) {
 			if (prev)
@@ -1302,6 +1357,9 @@
 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
 {
 	struct ast_rtp_protocol *cur;
+	if (!proto)
+		return -1;
+		
 	cur = protos;
 	while(cur) {
 		if (cur->type == proto->type) {
@@ -1318,6 +1376,9 @@
 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
 {
 	struct ast_rtp_protocol *cur;
+	if (!chan)
+		return NULL;
+
 	cur = protos;
 	while(cur) {
 		if (cur->type == chan->type) {
