Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: IP Sorting Stuff

  1. #11
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Nice job guys that was it! Looks correct now! Thank you for both of your help... I can't believe it was 1 silly line. Thank you again!

    Here is the full working source


    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <map>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <functional>
    #include <ws2tcpip.h>
    #include <sstream>
    #include <locale>
    #include <iterator>


    #pragma comment(lib, "Ws2_32.lib")


    struct sockaddr_in sa;


    bool StringIPCompare( const std::string& a, const std::string& b )
    {
    static std::string localA;
    static std::string localB;
    static std::string ipPartsA[4];
    static std::string ipPartsB[4];


    localA = a;
    localB = b;
    localA += ".";
    localB += ".";


    for( unsigned int i = 0; i < 4; i++ )
    {
    size_t dotPos = localA.find_first_of( '.' );


    if( dotPos == std::string::npos )
    continue;


    ipPartsA[i] = localA.substr( 0, dotPos );
    localA.erase( 0, dotPos + 1 );


    dotPos = localB.find_first_of( '.' );


    if( dotPos == std::string::npos )
    continue;


    ipPartsB[i] = localB.substr( 0, dotPos );
    localB.erase( 0, dotPos + 1 );
    }


    for( unsigned int i = 0; i < 4; i++ )
    {
    //Add logic here that checks for wildcards or other specialties and handles them accordingly
    //Example: if you want wildcards to always top the list of similar IPs:
    if( ipPartsA[i] == ipPartsB[i] )
    continue;


    if( ipPartsA[i] == "*" )
    return true;


    if( ipPartsB[i] == "*" )
    return false;


    unsigned int uintA;
    unsigned int uintB;


    sscanf_s( ipPartsA[i].c_str(), "%u", &uintA );
    sscanf_s( ipPartsB[i].c_str(), "%u", &uintB );


    if( uintA > uintB ) //If any number we encounter in A's parts is larger than B's equivalent we know A is bigger than B and can return false
    return false;


    else if( uintA < uintB )
    return true;
    }


    return false;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    std::fstream ipFilterInput;
    ipFilterInput.open("ipfilter.cfg", std::ios::in );

    std::fstream ipFilterOutput;
    ipFilterOutput.open("ipfilter2.cfg", std::ios::out );

    std::vector<std::string> ip;
    std::string current;


    std::size_t badIP;

    std::vector<int> octets;
    std::string temp;


    std::vector<std::string> value;


    while(getline(ipFilterInput,current))
    {
    //Delete Ports from IP's with port numbers
    std::string::size_type pos = current.find(":");


    if(pos != std::string::npos)
    current.erase(pos);


    //Validate IP, but don't include wildcards '*'
    if((inet_pton(AF_INET, current.c_str(), &(sa.sin_addr)) != 1))
    {
    badIP = current.find("*");


    if(badIP != std::string::npos)
    {
    //These IP's fail the inet_pton() function because they have a wildcard which technically is invalid
    //However we will exempt this logic for now because we don't want to filter out wildcards
    //printf("A: %s\n", current.c_str());
    ip.push_back(current);
    }
    }
    else
    ip.push_back(current);
    }


    value = ip;


    //Sort Function
    std::sort(value.begin(), value.end(), StringIPCompare);

    //Delete Duplicates
    value.erase(std::unique(value.begin(), value.end()), value.end());


    //Print Results
    copy(value.begin(), value.end(), std::ostream_iterator<std::string> (ipFilterOutput, "\n"));


    ipFilterOutput.close();
    ipFilterInput.close();


    //system("pause");
    return 0;
    }

  2. #12
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,603

    Default

    That was easy code tbh,
    but who cares I HELPED JAMES WOOHOO!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •