Browse code

Solve IPv6 ranges issues

Lucian Balaceanu authored on 25/08/2016 10:34:11
Showing 2 changed files
... ...
@@ -55,32 +55,33 @@ static  ip6_node IPv6ranges[IPv6RANGES_SIZE] = {
55 55
     { {0x00000000, 0x00000000, 0x00000000, 0x00000000} , "UNSPECIFIED",         {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} },  //::/128
56 56
     { {0x00000000, 0x00000000, 0x00000000, 0x00000001} , "LOOPBACK",            {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} },  //::1/128
57 57
     { {0x00000000, 0x00000000, 0x0000FFFF, 0x00000000} , "IPV4MAP",             {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000} },  //::FFFF:0:0/96
58
-    { {0x01000000, 0x00000000, 0x00000000, 0x00000000} , "DISCARD",             {0xFFFFFFFF, 0xFFFFFF00, 0x00000000, 0x00000000} },  //0100::/64
59
-    { {0x20010002, 0x00000000, 0x00000000, 0x00000000} , "BMWG",                {0xFFFFFFFF, 0xFF000000, 0x00000000, 0x00000000} },  //2001:0002::/48
58
+    { {0x01000000, 0x00000000, 0x00000000, 0x00000000} , "DISCARD",             {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000} },  //0100::/64
59
+    { {0x20010002, 0x00000000, 0x00000000, 0x00000000} , "BMWG",                {0xFFFFFFFF, 0xFFFF0000, 0x00000000, 0x00000000} },  //2001:0002::/48
60 60
     { {0x20010000, 0x00000000, 0x00000000, 0x00000000} , "TEREDO",              {0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000} },  //2001::/32
61 61
     { {0x20010DB8, 0x00000000, 0x00000000, 0x00000000} , "DOCUMENTATION",       {0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000} },  //2001:DB8::/32
62 62
     { {0x20010010, 0x00000000, 0x00000000, 0x00000000} , "ORCHID",              {0xFFFFFFF0, 0x00000000, 0x00000000, 0x00000000} },  //2001:10::/28
63 63
     { {0x20020000, 0x00000000, 0x00000000, 0x00000000} , "6TO4",                {0xFFFF0000, 0x00000000, 0x00000000, 0x00000000} },  //2002::/16
64
-    { {0xFE800000, 0x00000000, 0x00000000, 0x00000000} , "LINK-LOCAL-UNICAST",  {0xFFC00000, 0x00000000, 0x00000000, 0x00000000} },  //FE80::/10
65 64
     { {0xFEC00000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFFC00000, 0x00000000, 0x00000000, 0x00000000} },  //FEC0::/10
66
-    { {0xFE800000, 0x00000000, 0x00000000, 0x00000000} , "LINK-LOCAL-UNICAST",  {0xFFc00000, 0x00000000, 0x00000000, 0x00000000} },  // FE80::/10
67
-    { {0xFEC00000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFFc00000, 0x00000000, 0x00000000, 0x00000000} },  // FEC0::/10
65
+    { {0xFE800000, 0x00000000, 0x00000000, 0x00000000} , "LINK-LOCAL-UNICAST",  {0xFFc00000, 0x00000000, 0x00000000, 0x00000000} },  //FE80::/10
68 66
     { {0xFE000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFF800000, 0x00000000, 0x00000000, 0x00000000} },  //FE00::/9
69
-    { {0xFE000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFF800000, 0x00000000, 0x00000000, 0x00000000} },  // FE00::/9
70 67
     { {0x00000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFF000000, 0x00000000, 0x00000000, 0x00000000} },  //::/8
71 68
     { {0x01000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFF000000, 0x00000000, 0x00000000, 0x00000000} },  //0100::/8
72 69
     { {0xFF000000, 0x00000000, 0x00000000, 0x00000000} , "MULTICAST",           {0xFF000000, 0x00000000, 0x00000000, 0x00000000} },  //FF00::/8
73
-    { {0xFF000000, 0x00000000, 0x00000000, 0x00000000} , "MULTICAST",           {0xFF000000, 0x00000000, 0x00000000, 0x00000000} },  // FF00::/8
74 70
     { {0x02000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFE000000, 0x00000000, 0x00000000, 0x00000000} },  //0200::/7
75 71
     { {0xFC000000, 0x00000000, 0x00000000, 0x00000000} , "UNIQUE-LOCAL-UNICAST",{0xFE000000, 0x00000000, 0x00000000, 0x00000000} },  //FC00::/7
76
-    { {0xFC000000, 0x00000000, 0x00000000, 0x00000000} , "UNIQUE-LOCAL-UNICAST",{0xFE000000, 0x00000000, 0x00000000, 0x00000000} },  // FC00::/7
77 72
     { {0x04000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFC000000, 0x00000000, 0x00000000, 0x00000000} },  //400::/6
78 73
     { {0xF8000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFC000000, 0x00000000, 0x00000000, 0x00000000} },  //F800::/6
79
-    { {0xF8000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xFC000000, 0x00000000, 0x00000000, 0x00000000} },  // F800::/6
80 74
     { {0x08000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xF8000000, 0x00000000, 0x00000000, 0x00000000} },  //0800::/5
81
-    { {0xF0000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xF8000000, 0x00000000, 0x00000000, 0x00000000} },  // F000::/5
82
-    { {0xE0000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xF0000000, 0x00000000, 0x00000000, 0x00000000} },  // E000::/4
83
-    { {0xC0000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xE0000000, 0x00000000, 0x00000000, 0x00000000} }   // C000::/3
75
+    { {0xF0000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xF8000000, 0x00000000, 0x00000000, 0x00000000} },  //F000::/5
76
+    { {0xE0000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xF0000000, 0x00000000, 0x00000000, 0x00000000} },  //E000::/4
77
+    { {0x10000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xF0000000, 0x00000000, 0x00000000, 0x00000000} },  //1000::/4
78
+    { {0xC0000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xE0000000, 0x00000000, 0x00000000, 0x00000000} },  //C000::/3
79
+    { {0x20000000, 0x00000000, 0x00000000, 0x00000000} , "GLOBAL-UNICAST",      {0xE0000000, 0x00000000, 0x00000000, 0x00000000} },  //2000::/3
80
+    { {0x40000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xE0000000, 0x00000000, 0x00000000, 0x00000000} },  //4000::/3
81
+    { {0x60000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xE0000000, 0x00000000, 0x00000000, 0x00000000} },  //6000::/3
82
+    { {0x80000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xE0000000, 0x00000000, 0x00000000, 0x00000000} },  //8000::/3
83
+    { {0xA0000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED",            {0xE0000000, 0x00000000, 0x00000000, 0x00000000} }   //A000::/3
84
+
84 85
 };
85 86
 
86 87
 
... ...
@@ -90,6 +91,9 @@ int ip6_iptype(str string_ip, char **res)
90 91
   char in6_string[INET6_ADDRSTRLEN];
91 92
   int i;
92 93
 
94
+  if (string_ip.len >= INET6_ADDRSTRLEN)
95
+        return 0;
96
+
93 97
   memcpy(in6_string, string_ip.s, string_ip.len);
94 98
   in6_string[string_ip.len] = '\0';
95 99
 
... ...
@@ -114,6 +118,9 @@ int ip4_iptype(str string_ip, char **res)
114 118
   char in4_string[INET_ADDRSTRLEN];
115 119
   int i;
116 120
 
121
+  if (string_ip.len >= INET_ADDRSTRLEN)
122
+      return 0;
123
+
117 124
   memcpy(in4_string, string_ip.s, string_ip.len);
118 125
   in4_string[string_ip.len] = '\0';
119 126
 
... ...
@@ -88,6 +88,7 @@ static int w_is_ipv6_reference(struct sip_msg*, char*);
88 88
 static int w_ip_type(struct sip_msg*, char*);
89 89
 static int w_detailed_ipv6_type(struct sip_msg* _msg, char* _s,  char *res);
90 90
 static int w_detailed_ipv4_type(struct sip_msg* _msg, char* _s,  char *res);
91
+static int w_detailed_ip_type(struct sip_msg* _msg, char* _s,  char *res);
91 92
 static int w_compare_ips(struct sip_msg*, char*, char*);
92 93
 static int w_compare_pure_ips(struct sip_msg*, char*, char*);
93 94
 static int w_is_ip_rfc1918(struct sip_msg*, char*);
... ...
@@ -126,6 +127,8 @@ static cmd_export_t cmds[] =
126 127
   fixup_detailed_ip_type, fixup_free_detailed_ip_type, ANY_ROUTE },
127 128
   { "detailed_ipv6_type", (cmd_function)w_detailed_ipv6_type, 2,
128 129
   fixup_detailed_ip_type, fixup_free_detailed_ip_type, ANY_ROUTE },
130
+  { "detailed_ip_type", (cmd_function)w_detailed_ip_type, 2,
131
+  fixup_detailed_ip_type, fixup_free_detailed_ip_type, ANY_ROUTE },
129 132
   { "compare_ips", (cmd_function)w_compare_ips, 2, fixup_spve_spve, 0,
130 133
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
131 134
   { "compare_pure_ips", (cmd_function)w_compare_pure_ips, 2, fixup_spve_spve, 0,
... ...
@@ -484,12 +487,18 @@ static int w_ip_type(struct sip_msg* _msg, char* _s)
484 487
 
485 488
 static int w_detailed_ipv4_type(struct sip_msg* _msg, char* _s,  char *_dst)
486 489
 {
487
-    return _detailed_ip_type(AF_INET, _msg, _s, _dst);
490
+    return _detailed_ip_type(ip_type_ipv4, _msg, _s, _dst);
488 491
 }
489 492
 
490 493
 static int w_detailed_ipv6_type(struct sip_msg* _msg, char* _s,  char *_dst)
491 494
 {
492
-    return _detailed_ip_type(AF_INET6, _msg, _s, _dst);
495
+    return _detailed_ip_type(ip_type_ipv6, _msg, _s, _dst);
496
+}
497
+
498
+static int w_detailed_ip_type(struct sip_msg* _msg, char* _s,  char *_dst)
499
+{
500
+    /* `ip_type_error` should read `unknown type` */
501
+    return _detailed_ip_type(ip_type_error, _msg, _s, _dst);
493 502
 }
494 503
 
495 504
 /*! \brief Return the IP type of the given argument (string or pv): 1 = IPv4, 2 = IPv6, 3 = IPv6 refenrece, -1 = invalid IP. */
... ...
@@ -499,6 +508,7 @@ static int _detailed_ip_type(unsigned int _type, struct sip_msg* _msg, char* _s,
499 508
   pv_spec_t *dst;
500 509
   pv_value_t val;
501 510
   char *res;
511
+  unsigned int assumed_type;
502 512
 
503 513
   if (_s == NULL) {
504 514
     LM_ERR("bad parameter\n");
... ...
@@ -511,15 +521,18 @@ static int _detailed_ip_type(unsigned int _type, struct sip_msg* _msg, char* _s,
511 521
     return -3;
512 522
   }
513 523
 
514
-  switch (_type) {
515
-      case AF_INET:
524
+  assumed_type = (ip_type_error == _type)? ip_parser_execute(string.s, string.len) : _type;
525
+
526
+  switch (assumed_type) {
527
+      case ip_type_ipv4:
516 528
           if (!ip4_iptype(string, &res)) {
517 529
               LM_ERR("bad ip parameter\n");
518 530
               return -1;
519 531
           }
520 532
           break;
521
-      case AF_INET6:
522
-          /* make IPv6 from reference if needed */
533
+      case ip_type_ipv6_reference:
534
+      case ip_type_ipv6:
535
+          /* consider this reference */
523 536
           if (string.s[0] == '[') {
524 537
               string.s++;
525 538
               string.len -= 2;