Compare IP to list of host/network address
<p>I appoligize but am new to C++.  At our organization we recieve a daily black list (much bigger as this is just a snippet) in the following format:</p>

<p>172.44.12.0</p>

<p>198.168.1.5</p>

<p>10.10.0.0</p>

<p>192.168.78.6</p>

<p>192.168.22.22</p>

<p>111.111.0.0</p>

<p>222.222.0.0</p>

<p>12.12.12.12</p>

<p>When I run the program after the code compiles I receive:</p>

<p>1</p>

<p>1</p>

<p>1</p>

<p>1</p>

<p>1</p>

<p>1</p>

<p>1</p>

<p>1</p>

<p>I am using C++ in a Linux/Unix environment.</p>

<p>So far, I am just spitting it out to make sure I have it formatted correctly.  Please be kind I am sure this is considered sloppy programming, I am a noob.</p>

<p>The name of the file is blacklist.txt which contains the IP's listed above for now.  I am only using cout to make sure my variable are defined correctly.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;netinet/in.h&gt;
#include &lt;stdint.h&gt;
#include &lt;arpa/inet.h&gt;

using namespace std;

bool is_match(std::string &amp;hay_stack, std::string &amp;srcip) {
in_addr_t _ip = inet_addr(hay_stack.c_str());
in_addr_t _IP = inet_addr(srcip.c_str());
_ip = ntohl(_ip);
_IP = ntohl(_IP);
uint32_t mask=(_ip &amp; 0x00ffffff == 0) ? 0xff000000 :
(_ip &amp; 0x0000ffff == 0 ? 0xffff0000 : 0);
return ( (_ip &amp; mask) == (_IP &amp; mask) );
}

int main()
{
vector&lt;std::string&gt; lines;
lines.reserve(5000); //Assuming that the file to read can have max 5K lines

string fileName("blacklist.txt");

ifstream file;
file.open(fileName.c_str());

if(!file.is_open())
{
cerr&lt;&lt;"Error opening file : "&lt;&lt;fileName.c_str()&lt;&lt;endl;
return -1;
}

//Read the lines and store it in the vector
string line;
while(getline(file,line))
{
lines.push_back(line);
}

file.close();


//Dump all the lines in output
for(unsigned int i = 0; i &lt; lines.size(); i++)
{
string h = lines[i];
string mi = "10.10.10.10";
cout&lt;&lt;is_match(h,mi)&lt;&lt;endl;
}

return 0;
}
</code></pre>

<p>I am expecting the output to be 10.10.10.10 10.10.0.0 (and some sort of subnet mask here)</p>

<p>Any help is great.</p>
