Ejemplo n.º 1
0
// only called for incoming packets
void TCPProcess(IPSTACK *  pIpStack)
{
    TCPSOCKET *     pSocketCur      = NULL;
    TCPSOCKET *     pSocketListen   = NULL;
    TCPSOCKET *     pSocketExact    = NULL;
    
    // see if this is directed to my IP
    if( !(  // this is not my IP address

        // my IPv4 address
        (!ILIsIPv6(pIpStack->pLLAdp) && pIpStack->pLLAdp->ipMy.ipv4.u32 == pIpStack->pIPv4Hdr->ipDest.u32)
                                    ||
        // my IPv6 addresss
        (memcmp(&pIpStack->pLLAdp->ipMy.ipv6, &pIpStack->pIPv6Hdr->ipDest, sizeof(IPv6)) == 0)     ))
    {
        // just get out if it is not for me
        return;
    }

    // look at all of the sockets
    while((pSocketCur = (TCPSOCKET*)FFNext(&g_ffptActiveTCPSockets, pSocketCur)) != NULL)
    {
        if( pSocketCur->tcpState > tcpListen   &&
            pIpStack->pTCPHdr->portPair == pSocketCur->s.portPair  &&

        // matches the remote IP
        ((!ILIsIPv6(pIpStack->pLLAdp) && pSocketCur->s.ipRemote.ipv4.u32 == pIpStack->pIPv4Hdr->ipSrc.u32)
                                    ||
        (memcmp(&pSocketCur->s.ipRemote.ipv6, &pIpStack->pIPv4Hdr->ipSrc, sizeof(IPv6)) == 0))   )

        {
            pSocketExact = pSocketCur;
            break;
        }
        else if(pSocketCur->tcpState    == tcpListen                    &&
                pSocketCur->s.portLocal == pIpStack->pTCPHdr->portDest  &&
                pIpStack->pTCPHdr->fSyn)
        {
            pSocketListen = pSocketCur;
        }
    }

    // see if we did not find an exact match
    // but we are listening
    if(pSocketExact == NULL)
    {
        pSocketExact = pSocketListen;
    }

    // a match to an active socket
    if(pSocketExact != NULL)
    {
        IPSUpdateARPEntry(pIpStack);
        memcpy(&pSocketExact->s.macRemote, &pIpStack->pFrameII->macSrc, sizeof(MACADDR));
        TCPStateMachine(pIpStack, pSocketExact, NULL);
    }

    // to no socket at all, this will cause a RST to be sent.
    else
    {
        TCPStateMachine(pIpStack, NULL, NULL);
    }
}
Ejemplo n.º 2
0
void UDPProcess(IPSTACK * pIpStack)
{
    UDPSOCKET *     pSocketCur          = NULL;
    UDPSOCKET *     pSocketListen       = NULL;
    UDPSOCKET *     pSocketAnyRemoteIP  = NULL;
    UDPSOCKET *     pSocketExact        = NULL;
    bool            fBroadcast          = false;
    bool            fMyIP               = ILIsIPv6(pIpStack->pLLAdp) ?
                                            ILIsMyIP(pIpStack->pLLAdp, &pIpStack->pIPv6Hdr->ipDest, &fBroadcast) :
                                            ILIsMyIP(pIpStack->pLLAdp, &pIpStack->pIPv4Hdr->ipDest, &fBroadcast);

    // see if this is directed to my IP
    if( !fMyIP )
    {
        return;
    }

    // look at all listening sockets to see if the ports and remote IP match
    while((pSocketCur = FFNext(&g_ffptListeningUDPSockets, pSocketCur)) != NULL)
    {
        // make sure we have consistant IPs
        if(ILIsIPv6(pSocketCur->s.pLLAdp) == ILIsIPv6(pIpStack->pLLAdp))
        {
            // listening socket, only check local port, remote port/IP will be set
            if( pSocketCur->s.portRemote == portListen &&
                pSocketCur->s.portLocal == pIpStack->pUDPHdr->portDest )
            {
                pSocketListen = pSocketCur;
            }

            // all connected sockets
            else if(pIpStack->pUDPHdr->portPair == pSocketCur->s.portPair)
            {
                // this is an IPv4 address
                if(!ILIsIPv6(pIpStack->pLLAdp))
                {
                    // exact match
                    if(pSocketCur->s.ipRemote.ipv4.u32 == pIpStack->pIPv4Hdr->ipSrc.u32)
                    {
                        pSocketExact = pSocketCur;
                        break;
                    }

                    // this is only supported for DHCP IPv4
                    else if(pSocketCur->s.ipRemote.ipv4.u32 == UDPAnyRemoteIPv4.u32)
                    {
                        pSocketAnyRemoteIP = pSocketCur;
                    }
                }

                // else an IPv6 address
                else
                {
                    // exact match
                    if(memcmp(&pSocketCur->s.ipRemote.ipv6, &pIpStack->pIPv6Hdr->ipSrc, sizeof(IPv6)) == 0)
                    {
                        pSocketExact = pSocketCur;
                        break;
                    }
                }
            }
        }
    }

    // a match to an active socket
    if(pSocketExact == NULL)
    {
        if(pSocketAnyRemoteIP != NULL)
        {
            pSocketExact = pSocketAnyRemoteIP;
        }
        else if(pSocketListen != NULL)
        {
            pSocketExact = pSocketListen;

            pSocketExact->s.portRemote = pIpStack->pUDPHdr->portSrc;

            if(ILIsIPv6(pSocketExact->s.pLLAdp))
            {
                memcpy(&pSocketExact->s.ipRemote.ipv6, &pIpStack->pIPv6Hdr->ipSrc, sizeof(IPv6));
            }
            else
            {
                pSocketExact->s.ipRemote.ipv4.u32 = pIpStack->pIPv4Hdr->ipSrc.u32;
            }
        }
    }

    // no match to anything, get out.
    if(pSocketExact == NULL)
    {
        return;
    }

    // update our ARP table
    IPSUpdateARPEntry(pIpStack);
    memcpy(&pSocketExact->s.macRemote, &pIpStack->pFrameII->macSrc, sizeof(MACADDR));

    // Receive the UDP input data
    UDPProcessRx(pIpStack, pSocketExact);
}