Menu

#2 Code to block IP ranges, for LAN sharing

open
nobody
None
5
2015-02-19
2008-07-27
Anonymous
No

The list would look like this

ip_block = "... deny"
ip_block = "192.168.. allow"
ip_block = "10...* allow"

[code snips]

    int numIPs = vectorSize / 2;
    for( int i=0; i<numIPs; i++ ) {

        char *addressString = *( blockListVector->getElement( 2 * i ) );
        char *denyString = *( blockListVector->getElement( 2 * i + 1 ) );

        int numRead = sscanf(addressString, "%3[^.].%3[^.].%3[^.].%3[^ ]", ip1, ip2, ip3, ip4);

        if( numRead == 4 ) {
            BlockListEntry *entry = new BlockListEntry();

            entry->ip1 = entry->ip2 = entry->ip3 = entry->ip4 = -1; // * is -1

            if (ip1[0] != '*') entry->ip1 = atoi(ip1);
            if (ip2[0] != '*') entry->ip2 = atoi(ip2);
            if (ip3[0] != '*') entry->ip3 = atoi(ip3);
            if (ip4[0] != '*') entry->ip4 = atoi(ip4);

            if (!strncasecmp(denyString, "deny", 4)) entry->deny = true;
            else if (!strncasecmp(denyString, "allow", 5)) entry->deny = false;
            else {
                delete [] addressString; // skip this entry
                delete [] denyString;
                printf( "blockList settings file not properly formatted\n" );
                continue;
                }
            mBlockList->push_back( entry );
            }
        else {
            printf( "blockList settings file not properly formatted\n" );
            }

        delete [] addressString;
        delete [] denyString;
        }

checkBlockList( char *inAddress ) {

BlockListEntry *entry;
int listSize = mBlockList->size();
int ip1, ip2, ip3, ip4;
char x = false; // allow all by default

int numRead = sscanf(inAddress, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
if( numRead == 4 ) {
    // always go through whole list so they can do
    // combinations of allow deny
    for( int i=0; i < listSize; i++ ) {
        entry = *( mBlockList->getElement( i ) );
        if(entry->ip1 == ip1 || entry->ip1 == -1)
            if(entry->ip2 == ip2 || entry->ip2 == -1)
                if(entry->ip3 == ip3 || entry->ip3 == -1)
                    if(entry->ip4 == ip4 || entry->ip4 == -1) {
                        x = entry->deny;
                        }
        }

    }

return x; // deny
}

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.