Skip to content

Commit a36f803

Browse files
committed
Fixed ip_representation comparison method
1 parent 5568e5a commit a36f803

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

src/http_utils.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -506,20 +506,37 @@ ip_representation::ip_representation(const std::string& ip)
506506

507507
bool ip_representation::operator <(const ip_representation& b) const
508508
{
509-
int VAL = 16;
510-
if(this->ip_version == http_utils::IPV4 && this->ip_version == b.ip_version)
509+
long this_score = 0;
510+
long b_score = 0;
511+
for (int i = 0; i < 16; i++)
511512
{
512-
VAL = this->ip_version;
513+
if (i == 10 || i == 11) continue;
514+
515+
if (CHECK_BIT(this->mask, i) && CHECK_BIT(b.mask, i))
516+
{
517+
this_score += (16 - i) * this->pieces[i];
518+
b_score += (16 - i) * b.pieces[i];
519+
}
520+
}
521+
522+
if (this_score == b_score &&
523+
((this->pieces[10] == 0x00 || this->pieces[10] == 0xFF) && (b.pieces[10] == 0x00 || b.pieces[10] == 0xFF)) &&
524+
((this->pieces[11] == 0x00 || this->pieces[11] == 0xFF) && (b.pieces[11] == 0x00 || b.pieces[11] == 0xFF))
525+
)
526+
{
527+
return false;
513528
}
514-
for(int i = 16 - VAL; i < 16; i++)
529+
530+
for (int i = 10; i < 12; i++)
515531
{
516-
if(CHECK_BIT(this->mask,i) &&
517-
CHECK_BIT(b.mask,i) &&
518-
this->pieces[i] < b.pieces[i]
519-
)
520-
return true;
532+
if (CHECK_BIT(this->mask, i) && CHECK_BIT(b.mask, i))
533+
{
534+
this_score += (16 - i) * this->pieces[i];
535+
b_score += (16 - i) * b.pieces[i];
536+
}
521537
}
522-
return false;
538+
539+
return this_score < b_score;
523540
}
524541

525542
char* load_file (const char *filename)

test/unit/http_utils_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,46 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, load_file_invalid)
476476
LT_CHECK_THROW(http::load_file("test_content_invalid"));
477477
LT_END_AUTO_TEST(load_file_invalid)
478478

479+
LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation_less_than)
480+
LT_CHECK_EQ(http::ip_representation("127.0.0.1") < http::ip_representation("127.0.0.2"), true);
481+
LT_CHECK_EQ(http::ip_representation("128.0.0.1") < http::ip_representation("127.0.0.2"), false);
482+
LT_CHECK_EQ(http::ip_representation("127.0.0.2") < http::ip_representation("127.0.0.1"), false);
483+
LT_CHECK_EQ(http::ip_representation("127.0.0.1") < http::ip_representation("127.0.0.1"), false);
484+
LT_CHECK_EQ(http::ip_representation("127.0.0.1") < http::ip_representation("127.0.0.1"), false);
485+
486+
LT_CHECK_EQ(http::ip_representation("2001:db8::ff00:42:8329") < http::ip_representation("2001:db8::ff00:42:8329"), false);
487+
LT_CHECK_EQ(http::ip_representation("2001:db8::ff00:42:8330") < http::ip_representation("2001:db8::ff00:42:8329"), false);
488+
LT_CHECK_EQ(http::ip_representation("2001:db8::ff00:42:8329") < http::ip_representation("2001:db8::ff00:42:8330"), true);
489+
LT_CHECK_EQ(http::ip_representation("2002:db8::ff00:42:8329") < http::ip_representation("2001:db8::ff00:42:8330"), false);
490+
491+
LT_CHECK_EQ(http::ip_representation("::192.0.2.128") < http::ip_representation("::192.0.2.128"), false);
492+
LT_CHECK_EQ(http::ip_representation("::192.0.2.129") < http::ip_representation("::192.0.2.128"), false);
493+
LT_CHECK_EQ(http::ip_representation("::192.0.2.128") < http::ip_representation("::192.0.2.129"), true);
494+
495+
LT_CHECK_EQ(http::ip_representation("::ffff:192.0.2.128") < http::ip_representation("::ffff:192.0.2.128"), false);
496+
LT_CHECK_EQ(http::ip_representation("::ffff:192.0.2.129") < http::ip_representation("::ffff:192.0.2.128"), false);
497+
LT_CHECK_EQ(http::ip_representation("::ffff:192.0.2.128") < http::ip_representation("::ffff:192.0.2.129"), true);
498+
499+
LT_CHECK_EQ(http::ip_representation("::ffff:192.0.2.128") < http::ip_representation("::192.0.2.128"), false);
500+
LT_CHECK_EQ(http::ip_representation("::ffff:192.0.2.128") < http::ip_representation("::192.0.2.128"), false);
501+
LT_CHECK_EQ(http::ip_representation("::192.0.2.128") < http::ip_representation("::ffff:192.0.2.129"), true);
502+
LT_END_AUTO_TEST(ip_representation_less_than)
503+
504+
LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation_less_than_with_masks)
505+
LT_CHECK_EQ(http::ip_representation("127.0.*.*") < http::ip_representation("127.0.0.1"), false);
506+
LT_CHECK_EQ(http::ip_representation("127.0.0.1") < http::ip_representation("127.0.*.*"), false);
507+
LT_CHECK_EQ(http::ip_representation("127.0.0.*") < http::ip_representation("127.0.*.*"), false);
508+
LT_CHECK_EQ(http::ip_representation("127.0.*.1") < http::ip_representation("127.0.0.1"), false);
509+
LT_CHECK_EQ(http::ip_representation("127.0.0.1") < http::ip_representation("127.0.*.1"), false);
510+
LT_CHECK_EQ(http::ip_representation("127.1.0.1") < http::ip_representation("127.0.*.1"), false);
511+
LT_CHECK_EQ(http::ip_representation("127.0.*.1") < http::ip_representation("127.1.0.1"), true);
512+
LT_CHECK_EQ(http::ip_representation("127.1.*.1") < http::ip_representation("127.0.*.1"), false);
513+
LT_CHECK_EQ(http::ip_representation("127.0.*.1") < http::ip_representation("127.1.*.1"), true);
514+
515+
LT_CHECK_EQ(http::ip_representation("2001:db8::ff00:42:*") < http::ip_representation("2001:db8::ff00:42:8329"), false);
516+
LT_CHECK_EQ(http::ip_representation("2001:db8::ff00:42:8329") < http::ip_representation("2001:db8::ff00:42:*"), false);
517+
LT_END_AUTO_TEST(ip_representation_less_than_with_masks)
518+
479519
LT_BEGIN_AUTO_TEST_ENV()
480520
AUTORUN_TESTS()
481521
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)