Exemple #1
0
static void ackClient(IPaddress clientAddr) {
  IPXHeader regHeader;
  UDPpacket regPacket;
  Bits result;

  SDLNet_Write16(0xffff, regHeader.checkSum);
  SDLNet_Write16(sizeof(regHeader), regHeader.length);
	
  SDLNet_Write32(0, regHeader.dest.network);
  PackIP(clientAddr, &regHeader.dest.addr.byIP);
  SDLNet_Write16(0x2, regHeader.dest.socket);

  SDLNet_Write32(1, regHeader.src.network);
  PackIP(ipxServerIp, &regHeader.src.addr.byIP);
  SDLNet_Write16(0x2, regHeader.src.socket);
  regHeader.transControl = 0;

  regPacket.data = (Uint8 *)&regHeader;
  regPacket.len = sizeof(regHeader);
  regPacket.maxlen = sizeof(regHeader);
  regPacket.address = clientAddr;
  // Send registration string to client.  If client doesn't get this, client will not be registered
  result = SDLNet_UDP_Send(ipxServerSocket,-1,&regPacket);

}
__checkReturn bool IsBlockedIp( __in PackedIP const f_ip )
{
    static PackedIP const ipLocalhost( PackIP( 127, 0, 0, 1 ) );

    return ( ipLocalhost == f_ip ) ||
        ( BlockedIP() == f_ip );
}
__checkReturn size_t LoadCache(
                               __inout IPAddressMap & f_cache,
                               __in_z char const * const f_szFileName )
{
    FILE *finf = OpenFile( f_szFileName );
    if( !finf )
    {
        // some error, abort
        return 0;
    }

    size_t  nItems( 0 );
    size_t const knLineLength( 8192 );
    char szLine[ knLineLength + 1 ];
    while( fgets( szLine, knLineLength, finf ) )
    {
        if( IsComment( szLine ) )
        {
            continue;
        }
        
        int ip1;
        int ip2;
        int ip3;
        int ip4;
        if( !ReadIP( szLine, ip1, ip2, ip3, ip4 ))
        {
            continue;
        }
        
        char const szDelimiters[] = "\t ";
        char *next_token = 0;
        char const *pTok = strtok_s( szLine, szDelimiters, &next_token );
        if( pTok )
        {
            PackedIP const recordIP( PackIP( static_cast<char >(ip1 & 0xff), static_cast<char >(ip2 & 0xff), static_cast<char >(ip3 & 0xff), static_cast<char >(ip4 & 0xff) ) );

            bool const bIsBlockedIp( IsBlockedIp( recordIP ) );

            PackedIP const ip( bIsBlockedIp
                ? BlockedIP()
                : recordIP );

            for( pTok = strtok_s( 0, szDelimiters, &next_token );
                pTok;
                pTok = strtok_s( 0, szDelimiters, &next_token ) )
            {
                if( IsComment( pTok ) )
                {
                    break;
                }

                std::string const sHostName( CleanHostName( pTok ) );

                // skip blanks
                if( sHostName.empty() )
                {
                    continue;
                }

                // skip localhost
                if( sHostName == "localhost" )
                {
                    continue;
                }

                bool bFound( false );
                if( !bIsBlockedIp )
                {
                    // Perform search
                    IPAddressMap::const_iterator const itLower( f_cache.lower_bound( ip ) );
                    IPAddressMap::const_iterator const itUpper( f_cache.upper_bound( ip ) );

                    for( IPAddressMap::const_iterator itItem( itLower );
                        itUpper != itItem;
                        ++itItem )
                    {
                        if( itItem->second == sHostName )
                        {
                            bFound = true;
                            break;
                        }
                    }
                }
				else
				{
					// Blocked IP with no .
					if( std::string::npos == sHostName.find("."))
					{
						continue;
					}
				}


                if( !bFound )
                {
                    (void)f_cache.insert(
                        IPAddressMap::value_type( ip, sHostName ) );
                    ++nItems;
                }
            }
        }
    }

    fclose( finf );

    return nItems;
}
__checkReturn PackedIP BlockedIP()
{
    static PackedIP const ipZero( PackIP( 0, 0, 0, 0 ) );

    return ipZero;
}