/*
 * Copyright (c) 2001, 2002 Shiman Associates Inc. All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
/*

This file is a part of JRTPLIB
Copyright (c) 1999-2000 Jori Liesenborgs

Contact: jori@lumumba.luc.ac.be

This library (JRTPLIB) was partially developed for my thesis at the
School for Knowledge Technology (Belgium/The Netherlands)

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

*/

#include <stdlib.h>
#include <string.h>
# include <sys/socket.h>
# include <sys/un.h>
#include <sys/time.h>

#ifdef RTP_HAVE_SYS_FILIO
#include <sys/filio.h>
#endif // RTP_HAVE_SYS_FILIO

#ifdef RTP_VXWORKS
#include <resolvLib.h>
#include <ioLib.h>
#include <hostLib.h>
#else
#include <memory.h>
#endif // RTP_VXWORKS
#include "rtpconnection.h"
#include "rtpcontributingsources.h"
#include "rtptimeutil.h"

#define RTP_IS_MCASTADDR(x)				(((x)&0xF0000000) == 0xE0000000)

#define RTP_MCASTMEMBERSHIP(socket,type,mcastip,status)	{\
								struct ip_mreq mreq;\
								\
								mreq.imr_multiaddr.s_addr = htonl(mcastip);\
								mreq.imr_interface.s_addr = htonl(localip);\
								status = setsockopt(socket,IPPROTO_IP,type,(const char *)&mreq,sizeof(struct ip_mreq));\
							}
							
struct RawDataBlock
{
    unsigned char *data;
    int datalen;
    unsigned long ip;
    int port;
    RawDataBlock *next;
};

RTPConnection::RTPConnection()
{
    rtpsock = 0;
    rtcpsock = 0;
    sendsock = 0;
    session_type  = RTP_SESSTYPE_NONE;
    socketsopened = false;
    portbase = 0;
    receivemode = RECEIVEMODE_ALL;
    maxpacksize = RTP_DEFAULT_MAXPACKSIZE;
    rtp_first = NULL;
    rtp_last = NULL;
    rtcp_first = NULL;
    rtcp_last = NULL;
    sendport = 0;
    localip = 0;
    rtcprecvtime.tv_sec = 0;
    rtcprecvtime.tv_usec = 0;

}

RTPConnection::~RTPConnection()
{
    Destroy();
}

int RTPConnection::Create(int pbase,unsigned long localipaddress)
{
    int size;
    RTPSOCKLENTYPE socklen;

    if (socketsopened)
        return ERR_RTP_CONNALREADYCREATED;
		
    /* Check if the localportbase is even, conforming to RFC 1889 */
	
    if (pbase%2 != 0)
        return ERR_RTP_PORTBASENOTEVEN;
	
    /* Create the sockets */

    rtpsock = socket(AF_INET,SOCK_DGRAM,0);
    if (rtpsock == RTPSOCKERR)
        return ERR_RTP_CANTCREATESOCKET;
    rtcpsock = socket(AF_INET,SOCK_DGRAM,0);
    if (rtcpsock == RTPSOCKERR)
        return ERR_RTP_CANTCREATESOCKET;
    sendsock = socket(AF_INET,SOCK_DGRAM,0);
    if (sendsock == RTPSOCKERR)
        return ERR_RTP_CANTCREATESOCKET;

    /* Set socket receive and send buffers */

    size = RTP_RECEIVEBUFFERSIZE;
    if (setsockopt(rtpsock,SOL_SOCKET,SO_RCVBUF,(const char *)&size,sizeof(int)) != 0)
        return ERR_RTP_CANTSETSOCKETBUFFER;
    size = RTP_RECEIVEBUFFERSIZE;
    if (setsockopt(rtcpsock,SOL_SOCKET,SO_RCVBUF,(const char *)&size,sizeof(int)) != 0)
        return ERR_RTP_CANTSETSOCKETBUFFER;
    size = RTP_SENDBUFFERSIZE;
    if (setsockopt(sendsock,SOL_SOCKET,SO_SNDBUF,(const char *)&size,sizeof(int)) != 0)
        return ERR_RTP_CANTSETSOCKETBUFFER;

    /* Bind the sockets */
	
    struct sockaddr_in addr;

    addr.sin_family = AF_INET;
    addr.sin_port = htons(0);
    addr.sin_addr.s_addr = htonl(0);
    if (bind(sendsock,(struct sockaddr *)&addr,sizeof(struct sockaddr)) != 0)
    {
        RTPCLOSESOCKET(rtpsock);
        RTPCLOSESOCKET(rtcpsock);
        RTPCLOSESOCKET(sendsock);
        return ERR_RTP_CANTBINDSOCKET;
    }
	
    addr.sin_family = AF_INET;
    addr.sin_port = htons(pbase);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(rtpsock,(struct sockaddr *)&addr,sizeof(struct sockaddr)) != 0)
    {
        RTPCLOSESOCKET(rtpsock);
        RTPCLOSESOCKET(rtcpsock);
        RTPCLOSESOCKET(sendsock);
        return ERR_RTP_CANTBINDSOCKET;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(pbase+1);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(rtcpsock,(struct sockaddr *)&addr,sizeof(struct sockaddr)) != 0)
    {
        RTPCLOSESOCKET(rtpsock);
        RTPCLOSESOCKET(rtcpsock);
        RTPCLOSESOCKET(sendsock);
        return ERR_RTP_CANTBINDSOCKET;
    }

    /* Get the port number of the send socket */

    socklen = sizeof(struct sockaddr_in);
    if (getsockname(sendsock,(struct sockaddr *)&addr,&socklen) != 0)
    {
        RTPCLOSESOCKET(rtpsock);
        RTPCLOSESOCKET(rtcpsock);
        RTPCLOSESOCKET(sendsock);
        return ERR_RTP_CANTGETSOCKETPORT;
    }

    sendport = ntohs(addr.sin_port);

    /* Get the local IP address */

    if (localipaddress == 0) // user didn't supply an IP address
    {
        localip = CalcLocalIP();
        if (localip == 0)
        {
            RTPCLOSESOCKET(rtpsock);
            RTPCLOSESOCKET(rtcpsock);
            RTPCLOSESOCKET(sendsock);
            return ERR_RTP_CANTGETLOCALIP;
        }
    }
    else // user specified the local IP address
        localip = localipaddress;

    socketsopened = true;

#ifdef RTP_MULTICAST
    if (SetMulticastTTL(RTP_DEFAULTMULTICAST_TTL) < 0)
    {
        socketsopened = false;
        RTPCLOSESOCKET(rtpsock);
        RTPCLOSESOCKET(rtcpsock);
        RTPCLOSESOCKET(sendsock);
        return ERR_RTP_CANTSETMULTICASTTTL;
    }
#endif // RTP_MULTICAST

    session_type = RTP_SESSTYPE_UDP;

    portbase = pbase;
    return 0;
}

int RTPConnection::CreateFromStreamSocketPair(int new_session_type, 
					      RTPSOCKET new_rtpsock,
					      RTPSOCKET new_rtcpsock,
					      int localipaddress)
{
    int size;
    RTPSOCKLENTYPE socklen;

    if (socketsopened)
        return ERR_RTP_CONNALREADYCREATED;
	
    if (new_session_type == RTP_SESSTYPE_TCP)
    {
        /* Get the local IP address */
        if (localipaddress == 0) // user didn't supply an IP address
        {
            localip = CalcLocalIP();
//
//              don't fail if it doesn't find one - we don't care
//		for socket stream pair
//		
// 		if (localip == 0)
// 		{
// 		    RTPCLOSESOCKET(rtpsock);
// 		    RTPCLOSESOCKET(rtcpsock);
// 		    RTPCLOSESOCKET(sendsock);
// 		    return ERR_RTP_CANTGETLOCALIP;
// 		}
        }
        else // user specified the local IP address
            localip = localipaddress;
    }

    rtpsock = new_rtpsock;
    rtcpsock = new_rtcpsock;
    socketsopened = true;
    session_type = new_session_type;

    return 0;
}

void RTPConnection::Destroy()
{
    CloseConnection();
    destinations.Clear();
    ignoreIPs.Clear();
    acceptIPs.Clear();
    mcasttable.Clear();
    receivemode = RECEIVEMODE_ALL;
    maxpacksize = RTP_DEFAULT_MAXPACKSIZE;
    FlushPackets();
}

int RTPConnection::GetPortBase()
{
    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;
    return portbase;
}

// version without header extension
int RTPConnection::SetToS(int tos)
{
    int status;

    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;

    status = setsockopt(sendsock,IPPROTO_IP,IP_TOS,(const char *)&tos,sizeof(int));
    if (status != 0)
        return ERR_RTP_CANTSETTOS;

    return 0;
}

int RTPConnection::SendRTPData(void *data,int len,RTPContributingSources *srcs,unsigned char pt,bool mark,RTPuint16 seqnr,RTPuint32 timestamp)
{
    RTPHeader *hdr;
    RTPuint32 blocklen;
    int       numcsrcs;
    int       offset = 0;
    int       err;
    RTPuint32 net_order_bl;

    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;

    if (len <= 0) // nothing to send
        return 0;

    numcsrcs = srcs->GetNumberOfCSRCs();
    if (numcsrcs > 15)
        numcsrcs = 15;

    blocklen = sizeof(RTPHeader)+sizeof(RTPuint32)*numcsrcs+len;

    /* if session is a stream session, we need to frame this
       message with the total length, expressed in a 32-bit
       unsigned integer */
    if (session_type == RTP_SESSTYPE_UNIX ||
        session_type == RTP_SESSTYPE_TCP)
    {	
        net_order_bl = htonl(blocklen);
        memcpy(packetbuffer, &net_order_bl, sizeof(RTPuint32));
        blocklen += sizeof(RTPuint32); /* total packetsize is now
                                          increased by the 32-bit
                                          length */
        offset += sizeof(RTPuint32); /* add offset, so packet
                                        header starts after the
                                        32-bit length indicator */
    }

    /* make sure we didn't blow the packet size */
    if (blocklen > maxpacksize)
        return ERR_RTP_PACKETTOOLARGE;
	
    /* Init header fields */
    hdr = (RTPHeader *)(packetbuffer + offset);
    hdr->version = RTP_VERSION;
    hdr->padding = 0;
    hdr->extension = 0;
    hdr->cc = numcsrcs;
    hdr->marker = (mark)?1:0;
    hdr->payloadtype = pt;
    hdr->seqnum = htons(seqnr);
    hdr->timestamp = htonl(timestamp);
    hdr->ssrc = srcs->GetLocalSSRC();
	
    // copy CSRC members (CSRC numbers are already in network byte order)
    if (numcsrcs > 0)
        srcs->FillCSRCs(packetbuffer + offset +
                        sizeof(RTPHeader)); 
    // copy data
    memcpy((packetbuffer + offset + sizeof(RTPHeader) +
            sizeof(RTPuint32)*numcsrcs), data, len);
	
    if (session_type == RTP_SESSTYPE_UDP)
    {
        // send the data to all destinations
        struct sockaddr_in addr;
        unsigned long ip;
        int port,rtcpport;

        destinations.InitRetrieval();
        addr.sin_family = AF_INET;
        while (destinations.GetNext(&ip,&port,&rtcpport))
        {
            addr.sin_port = port; // already in network byte order
            addr.sin_addr.s_addr = ip; // already in network byte order
            err = sendto(sendsock,(const char *)packetbuffer,blocklen,0,(struct sockaddr *)&addr,sizeof(struct sockaddr));
        }
    }

    /* if it's a stream socket, just send it */
    if (session_type == RTP_SESSTYPE_TCP || 
        session_type == RTP_SESSTYPE_UNIX)
        err = send(rtpsock, (const char*)packetbuffer, blocklen, 0);

    if ( err < 0 ) return ERR_RTP_SEND;
	
    return 0;
}


// The version with header extension
int RTPConnection::SendRTPData(void *data,int len,RTPContributingSources *srcs,unsigned char pt,bool mark,RTPuint16 seqnr,RTPuint32 timestamp,unsigned short hdrextID,void *hdrextdata,int numhdrextwords)
{
    RTPHeader *hdr;
    RTPHeaderExtension *hdrext;
    RTPuint32 blocklen;
    int       numcsrcs, numextwords;
    int       offset = 0;
    unsigned char *datapos;
    int err;
    RTPuint32 net_order_bl;
		
    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;

    if (len <= 0) // nothing to send
        return 0;

    numcsrcs = srcs->GetNumberOfCSRCs();
    if (numcsrcs > 15)
        numcsrcs = 15;
		
    numextwords = numhdrextwords;
    if (numextwords < 0)
        numextwords = 0;

    blocklen = sizeof(RTPHeader) + sizeof(RTPuint32)*numcsrcs +
        len + numextwords*sizeof(RTPuint32) +
        sizeof(RTPHeaderExtension); 

    /* if session is a stream session, we need to frame this
       message with the total length, expressed in a 32-bit
       unsigned integer */
    if (session_type == RTP_SESSTYPE_UNIX ||
        session_type == RTP_SESSTYPE_TCP)
    {
        net_order_bl = htonl(blocklen);
        memcpy(packetbuffer, &net_order_bl, sizeof(RTPuint32));
        blocklen += sizeof(RTPuint32); /* total packetsize is now
                                          increased by the 32-bit
                                          length */
        offset += sizeof(RTPuint32); /* add offset, so packet
                                        header starts after the
                                        32-bit length indicator */
    }
	
    if (blocklen > maxpacksize)
        return ERR_RTP_PACKETTOOLARGE;

    /* Init header fields */
		
    hdr = (RTPHeader *)(packetbuffer + offset);
    hdr->version = RTP_VERSION;
    hdr->padding = 0;
    hdr->extension = 1;
    hdr->cc = numcsrcs;
    hdr->marker = (mark)?1:0;
    hdr->payloadtype = pt;
    hdr->seqnum = htons(seqnr);
    hdr->timestamp = htonl(timestamp);
    hdr->ssrc = srcs->GetLocalSSRC();
	
    // copy CSRC members (CSRC numbers are already in network byte order)
    if (numcsrcs > 0)
        srcs->FillCSRCs(packetbuffer + offset + sizeof(RTPHeader));
	
    // set the header extension
    datapos = packetbuffer + offset + sizeof(RTPHeader) +
        sizeof(RTPuint32)*numcsrcs; 
    hdrext = (RTPHeaderExtension *)datapos;
    hdrext->userdefined = htons(hdrextID);
    hdrext->length = htons(numextwords);
    datapos += sizeof(RTPHeaderExtension);
    if (numextwords > 0)
        memcpy(datapos, hdrextdata, numextwords*sizeof(RTPuint32));
		
    // copy data
    datapos += numextwords*sizeof(RTPuint32);
    memcpy(datapos,data,len);
	
    if (session_type == RTP_SESSTYPE_UDP)
    {
        // send the data to all destinations
        struct sockaddr_in addr;	
        unsigned long ip;
        int port,rtcpport;

        destinations.InitRetrieval();
        addr.sin_family = AF_INET;
        while (destinations.GetNext(&ip,&port,&rtcpport))
        {
            addr.sin_port = port; // already in network byte order
            addr.sin_addr.s_addr = ip; // already in network byte order
            err = sendto(sendsock,(const char *)packetbuffer,blocklen,0,(struct sockaddr *)&addr,sizeof(struct sockaddr));
        }
    }

    /* if it's a stream socket, just send it */
    if (session_type == RTP_SESSTYPE_TCP || 
        session_type == RTP_SESSTYPE_UNIX)
        err = send(rtpsock, (const char*)packetbuffer, blocklen, 0);

    if ( err < 0 ) return ERR_RTP_SEND;

    return 0;
}

int RTPConnection::SendRTCPCompoundData(void *data,int len)
{
    struct sockaddr_in addr;
    RTPuint32 blocklen;
    int offset = 0;
    unsigned long ip;
    int port,rtcpport;
    RTPuint32 net_order_bl;
    int err;

    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;

    if (len <= 0) // nothing to send
        return 0;

    blocklen = len;

    /* if session is a stream session, we need to frame this
       message with the total length, expressed in a 32-bit
       unsigned integer */
    if (session_type == RTP_SESSTYPE_UNIX ||
        session_type == RTP_SESSTYPE_TCP)
    {
        net_order_bl = htonl(blocklen);
        memcpy(packetbuffer, &net_order_bl, sizeof(RTPuint32));
        blocklen += sizeof(RTPuint32); /* total packetsize is now
                                          increased by the 32-bit
                                          length */
        offset += sizeof(RTPuint32); /* add offset, so packet
                                        header starts after the
                                        32-bit length indicator */
    }
	
    if (blocklen > maxpacksize) return ERR_RTP_PACKETTOOLARGE;

    // copy RTCP packet
    memcpy((packetbuffer + offset), data, len);
	
    if (session_type == RTP_SESSTYPE_UDP)
    {
        destinations.InitRetrieval();
        addr.sin_family = AF_INET;
        while (destinations.GetNext(&ip,&port,&rtcpport))
        {
            addr.sin_port = rtcpport; // already in network byte order
            addr.sin_addr.s_addr = ip; // already in network byte order
            err = sendto(sendsock, (const char *)packetbuffer ,blocklen,
                         0, (struct sockaddr *)&addr, sizeof(struct sockaddr));
        }
    }

    /* if it's a stream socket, just send it out the RTCP socket */
    if (session_type == RTP_SESSTYPE_TCP || 
        session_type == RTP_SESSTYPE_UNIX)
        err = send(rtcpsock, (const char*)packetbuffer, blocklen, 0);

    if ( err < 0 ) return ERR_RTP_SEND;

    return 0;
}

int RTPConnection::ReceiveRTPData(unsigned char **data,int *len,unsigned long *ip,int *port)
{
    RawDataBlock *tmp;
	
    if (rtp_first == NULL)
        return ERR_RTP_NORTPDATAAVAILABLE;
    *data = rtp_first->data;
    *len = rtp_first->datalen;
    *ip = rtp_first->ip;
    *port = rtp_first->port;
    tmp = rtp_first;
    rtp_first = rtp_first->next;
    RTP_DELETE(tmp);
    return 0;
}

int RTPConnection::ReceiveRTCPData(unsigned char **data,int *len,unsigned long *ip,int *port)
{
    RawDataBlock *tmp;
    
    if (rtcp_first == NULL)
        return ERR_RTP_NORTCPDATAAVAILABLE;
    *data = rtcp_first->data;
    *len = rtcp_first->datalen;
    *ip = rtcp_first->ip;
    *port = rtcp_first->port;
    tmp = rtcp_first;
    rtcp_first = rtcp_first->next;
    RTP_DELETE(tmp);
    return 0;
}

int RTPConnection::Poll(bool rtp, bool check_for_data)
{
    unsigned long len,ip;
    RawDataBlock *tmpblk;
    bool acceptpacket;
    int port,sock;
    struct sockaddr_in addr;
    unsigned char *data2 = NULL;
    int len2 = 0;
    RTPuint32 actual_length;
    RTPSOCKLENTYPE fromlen;

    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;

    if (rtp)
        sock = rtpsock;
    else
        sock = rtcpsock;

    len = 0;
    if (check_for_data)
    {
        RTPIOCTL(sock,FIONREAD,&len);
    }
    else // just get all we can
    {
        len = RTP_MAXIMUMPACKETSIZE;
    }

    if (!rtp && len > 0)
        gettimeofday(&rtcprecvtime,NULL);

    // I assume that we can read faster than the packets arrive
	
    // This version of the routine should also work with buggy ioctl versions:
    // in some ioctl versions, ioctl(sock,FIONREAD,&len) doesn't put the size of
    // the first packet in 'len', but the total size of all packets in the queue.

    // Trying looping again -- 30 OCT 2002 - rocko
    // This will not EVER loop if check_for_data is false.
    // Otherwise, it'll loop if there's data waiting to be read.

    // 02 APR 2003 - rocko
    // 
    // If check_for_data is true, the while loop below will
    // execute until the ioctl at the end returns no data waiting
    // to be read.  If not, only one packet is read from the
    // interface.  On linux, the FIONREAD ioctl was preserving
    // message boundaries, instead of returning the full amount of
    // the data waiting in the socket -- that's why I had to do
    // the ioctl again at the end of the loop.  I do not know what
    // other platforms may do.
        
    while (len > 0)
    {
        fromlen = sizeof(struct sockaddr);

        /* if it's a UDP socket, just read the datagram */
        if (session_type == RTP_SESSTYPE_UDP)
        {
            len2 = recvfrom(sock, (char *)packetbuffer, (int)len,
                            0, (struct sockaddr *)&addr,
                            &fromlen);
        }
                

        /* if it's a stream socket, we have to recv the
           length, then the actual packet */
        if (session_type == RTP_SESSTYPE_TCP || session_type == RTP_SESSTYPE_UNIX)
        {
            /* Get the length */
            len2 = recv(sock, (char *)&actual_length, sizeof (RTPuint32), MSG_WAITALL);

            /* be sure we read in a uint32! */
            if ( len2 == sizeof (RTPuint32) )
            {
                actual_length = ntohl(actual_length);

                /* avoid copying from the static packetbuffer,
                   just allocate and pull in the packet since
                   we know the "actual length". */
                data2 = RTP_NEW(unsigned char[actual_length]);
                if (data2 == NULL)
                    return ERR_RTP_OUTOFMEM;
                        
                // this will block.  this isn't great, and we
                // should really fix this at some point.
                len2 = recv(sock, (char *)data2, (int)actual_length, MSG_WAITALL); 
            }
            else
            {
                return ERR_RTP_RECV;
            }
                    
        }
                
		
        if ( len2 < 0 )
        {
            if ( data2 != NULL )
                RTP_DELETE( data2 );
            return ERR_RTP_RECV;
        }
                
        if (len2 > 0)
        {
            // only check source if this is a connectionless
            // session

            if ( session_type == RTP_SESSTYPE_UDP )
            {
                ip = ntohl(addr.sin_addr.s_addr);
                port = ntohs(addr.sin_port);
            }
            else
            {
                ip = 0;
                port = 0;
            }
		    
            /* Check if this packet comes from a source from
               which we want to receive */
		    
            if ( receivemode  != RECEIVEMODE_ALL  &&
                 session_type != RTP_SESSTYPE_TCP &&
                 session_type != RTP_SESSTYPE_UNIX )
            {
                if (receivemode == RECEIVEMODE_IGNORESOME)
                {
                    if (ignoreIPs.Exists(ip,true,0))
                        acceptpacket = false;
                    else if (ignoreIPs.Exists(ip,false,port))
                        acceptpacket = false;
                    else
                        acceptpacket = true;
                }
                else // RECEIVEMODE_ACCEPTSOME
                {
                    if (acceptIPs.Exists(ip,true,0))
                        acceptpacket = true;
                    else if (acceptIPs.Exists(ip,false,port))
                        acceptpacket = true;
                    else
                        acceptpacket = false;
                }
            }
            else
                acceptpacket = true;

            if (acceptpacket)
            {
                /* Process packet */
                /* copy from statically allocated packetbuffer if
                   using UDP, otherwise assume the packet is already
                   in data2. */
                if (session_type == RTP_SESSTYPE_UDP)
                {
                    data2 = RTP_NEW(unsigned char[len2]);
                    if (data2 == NULL)
                        return ERR_RTP_OUTOFMEM;
                    memcpy(data2,packetbuffer,len2);
                }
                
                tmpblk = RTP_NEW(RawDataBlock);
                if (tmpblk == NULL)
                {
                    RTP_DELETE_ARRAY(data2);
                    return ERR_RTP_OUTOFMEM;
                }

                if (rtp)
                {
                    if (rtp_first == NULL)
                        rtp_first = tmpblk;
                    else
                        rtp_last->next = tmpblk;
                    rtp_last = tmpblk;
                }
                else	
                {
                    if (rtcp_first == NULL)
                        rtcp_first = tmpblk;
                    else
                        rtcp_last->next = tmpblk;
                    rtcp_last = tmpblk;
                }
                tmpblk->next = NULL;
                tmpblk->data = data2;
                tmpblk->datalen = len2;
                tmpblk->ip = ip;
                tmpblk->port = port;
            }
        }

        len = 0;
        if ( check_for_data )
        {
            RTPIOCTL(sock,FIONREAD,&len);
        }
    }

    return 0;
}

void RTPConnection::FlushPackets()
{
    while (rtp_first != NULL)
    {
        rtp_last = rtp_first->next;
        if (rtp_first->data != NULL)
            RTP_DELETE_ARRAY(rtp_first->data);
        RTP_DELETE(rtp_first);
        rtp_first = rtp_last;
    }
    while (rtcp_first != NULL)
    {
        rtcp_last = rtcp_first->next;
        if (rtcp_first->data != NULL)
            RTP_DELETE_ARRAY(rtcp_first->data);
        RTP_DELETE(rtcp_first);
        rtcp_first = rtcp_last;
    }
}

#ifdef RTP_VXWORKS

unsigned long RTPConnection::CalcLocalIP()
{
    unsigned long ip;
    char name[256];

    gethostname(name,255);
    ip = (unsigned long)hostGetByName(name);
    ip = ntohl(ip);
}

#else // other systems

unsigned long RTPConnection::CalcLocalIP()
{
    struct hostent *he;
    char name[256];
    unsigned long ip;
    bool done;
    int i,j;

    gethostname(name,255);
    name[255] = 0;
    he = gethostbyname(name);
    if (he == NULL)
        return 0;
	
    ip = 0;
    i = 0;
    done = false;
    while (!done)
    {
        if (he->h_addr_list[i] == NULL)
            done = true;
        else
        {
            ip = 0;
            for (j = 0 ; j < 4 ; j++)
                ip |= ((unsigned long)((unsigned char)he->h_addr_list[i][j])<<((3-j)*8));
			
            if (he->h_addr_list[i][0] != 127 && he->h_addr_list[i][0] != 0)
                done = true;
            else
                i++;
        }
    }

    return ip;
}

#endif // RTP_VXWORKS

#ifndef RTP_MULTICAST
/*****************************************************************************

		ROUTINES FOR USE WITHOUT MULTICASTING SUPPORT

*****************************************************************************/

bool RTPConnection::SupportsMulticasting()
{
    return false;
}

int RTPConnection::JoinMulticastGroup(unsigned long mcastIP)
{
    return ERR_RTP_MULTICASTINGNOTSUPPORTED;
}

int RTPConnection::LeaveMulticastGroup(unsigned long mcastIP)
{
    return ERR_RTP_MULTICASTINGNOTSUPPORTED;
}

void RTPConnection::LeaveAllMulticastGroups()
{
}

int RTPConnection::SetMulticastTTL(unsigned char ttl)
{
    return ERR_RTP_MULTICASTINGNOTSUPPORTED;
}

#else
/*****************************************************************************

	    ROUTINES FOR USE WITH MULTICASTING SUPPORT ENABLED

*****************************************************************************/

bool RTPConnection::SupportsMulticasting()
{
    return true;
}

int RTPConnection::JoinMulticastGroup(unsigned long mcastIP)
{
    int status;
    if (session_type != RTP_SESSTYPE_UDP) return ERR_RTP_MULTICASTJOINFAILED;
	
    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;
	
    if (!RTP_IS_MCASTADDR(mcastIP))
        return ERR_RTP_NOTAMULTICASTADDRESS;
		
    status = mcasttable.AddMCastAddress(mcastIP);
    if (status < 0)
        return status;
	
    RTP_MCASTMEMBERSHIP(rtpsock,IP_ADD_MEMBERSHIP,mcastIP,status);
    if (status != 0)
    {
        mcasttable.DeleteMCastAddress(mcastIP);
        return ERR_RTP_MULTICASTJOINFAILED;
    }
	
    RTP_MCASTMEMBERSHIP(rtcpsock,IP_ADD_MEMBERSHIP,mcastIP,status);
    if (status != 0)
    {
        RTP_MCASTMEMBERSHIP(rtpsock,IP_DROP_MEMBERSHIP,mcastIP,status);
        mcasttable.DeleteMCastAddress(mcastIP);
        return ERR_RTP_MULTICASTJOINFAILED;
    }
	
    return 0;
}

int RTPConnection::LeaveMulticastGroup(unsigned long mcastIP)
{
    int status;
	
    if (session_type != RTP_SESSTYPE_UDP) return ERR_RTP_NOTAMULTICASTADDRESS;

    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;
	
    if (!RTP_IS_MCASTADDR(mcastIP))
        return ERR_RTP_NOTAMULTICASTADDRESS;
		
    status = mcasttable.DeleteMCastAddress(mcastIP);
    if (status < 0)
        return status;
	
    RTP_MCASTMEMBERSHIP(rtpsock,IP_DROP_MEMBERSHIP,mcastIP,status);
    RTP_MCASTMEMBERSHIP(rtcpsock,IP_DROP_MEMBERSHIP,mcastIP,status);
	
    return 0;
}

void RTPConnection::LeaveAllMulticastGroups()
{
    unsigned long mcastip;
    int status;
	
    if (session_type != RTP_SESSTYPE_UDP) return;

    if (!socketsopened)
        return;
	
    mcasttable.GotoFirstMCastAddress();
    while (mcasttable.GetNextMCastAddress(&mcastip))
    {
        RTP_MCASTMEMBERSHIP(rtpsock,IP_DROP_MEMBERSHIP,mcastip,status);
        RTP_MCASTMEMBERSHIP(rtcpsock,IP_DROP_MEMBERSHIP,mcastip,status);
    }
    mcasttable.Clear();
}

int RTPConnection::SetMulticastTTL(unsigned char ttl)
{
    int status;
    int ttl2;
	
    if (session_type != RTP_SESSTYPE_UDP) return ERR_RTP_NOTAMULTICASTADDRESS;

    if (!socketsopened)
        return ERR_RTP_SOCKETSNOTOPEN;
	
    ttl2 = (int)ttl;
    status = setsockopt(sendsock,IPPROTO_IP,IP_MULTICAST_TTL,(const char *)&ttl2,sizeof(int));
    if (status != 0)
        return ERR_RTP_CANTSETMULTICASTTTL;
	
    return 0;
}

#endif // RTP_MULTICAST

void RTPConnection::CloseConnection(void)
{
    if (!socketsopened) return;
    
    switch (session_type)
    {
    case RTP_SESSTYPE_UDP:
	LeaveAllMulticastGroups();
	socketsopened = false;
	session_type = RTP_SESSTYPE_NONE;
	RTPCLOSESOCKET(rtpsock);
	RTPCLOSESOCKET(rtcpsock);
	RTPCLOSESOCKET(sendsock);
	break;
    case RTP_SESSTYPE_UNIX:
	socketsopened = false;
	session_type = RTP_SESSTYPE_NONE;
	RTPCLOSESOCKET(rtpsock);
	RTPCLOSESOCKET(rtcpsock);
	break;
    case RTP_SESSTYPE_TCP:
	socketsopened = false;
	session_type = RTP_SESSTYPE_NONE;
	RTPCLOSESOCKET(rtpsock);
	RTPCLOSESOCKET(rtcpsock);
	break;
    default: break;
    }
}
