Browse code

IPv4/6 integration started

Lucian Balaceanu authored on 22/08/2016 14:42:02
Showing 3 changed files
... ...
@@ -74,8 +74,8 @@ static  ip6_node IPv6ranges[IPv6RANGES_SIZE] = {
74 74
     //more to come here soon
75 75
 };
76 76
 
77
-/*
78
-int _compare_ips(str string_ip, char *res)
77
+
78
+int ip6_iptype(str string_ip, char **res)
79 79
 {
80 80
   uint32_t in6_addr[4];
81 81
   char in6_string[INET6_ADDRSTRLEN];
... ...
@@ -92,14 +92,14 @@ int _compare_ips(str string_ip, char *res)
92 92
           ((in6_addr[2] & IPv6ranges[i].sub_mask[2]) == IPv6ranges[i].value[2]) &&
93 93
           ((in6_addr[3] & IPv6ranges[i].sub_mask[3]) == IPv6ranges[i].value[3])) {
94 94
 
95
-          return IPv6ranges[i].ip_type;
95
+          *res = IPv6ranges[i].ip_type;
96 96
       }
97 97
   }
98 98
 
99 99
   return 1;
100 100
 }
101
-*/
102
-char* ip6_iptype(str s, uint32_t *ip) {
101
+
102
+/*char* ip6_iptype(str s, uint32_t *ip) {
103 103
     int i;
104 104
     for (i = 0; i < IPv6RANGES_SIZE; i++) {
105 105
         if (((ip[0] & IPv6ranges[i].sub_mask[0]) == IPv6ranges[i].value[0]) &&
... ...
@@ -111,7 +111,7 @@ char* ip6_iptype(str s, uint32_t *ip) {
111 111
     }
112 112
     return "PUBLIC";
113 113
 }
114
-
114
+*/
115 115
 char* ip4_iptype(uint32_t ip) {
116 116
     int i;
117 117
     for (i = 0; i < IPv4RANGES_SIZE; i++) {
... ...
@@ -47,6 +47,6 @@ typedef struct ip6_node {
47 47
 
48 48
 void ipv6ranges_hton();
49 49
 void ipv4ranges_hton();
50
-char* ip6_iptype(str s, uint32_t *ip);
50
+int ip6_iptype(str string_ip, char **res);
51 51
 
52 52
 #endif
... ...
@@ -87,6 +87,7 @@ static int w_is_ipv6(struct sip_msg*, char*);
87 87
 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
+static int w_detailed_ipv4_type(struct sip_msg* _msg, char* _s,  char *res);
90 91
 static int w_compare_ips(struct sip_msg*, char*, char*);
91 92
 static int w_compare_pure_ips(struct sip_msg*, char*, char*);
92 93
 static int w_is_ip_rfc1918(struct sip_msg*, char*);
... ...
@@ -120,8 +121,10 @@ static cmd_export_t cmds[] =
120 121
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
121 122
   { "ip_type", (cmd_function)w_ip_type, 1, fixup_spve_null, 0,
122 123
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
124
+  { "detailed_ipv4_type", (cmd_function)w_detailed_ipv4_type, 2, fixup_detailed_ipv6_type, 0,
125
+  ANY_ROUTE },
123 126
   { "detailed_ipv6_type", (cmd_function)w_detailed_ipv6_type, 2, fixup_detailed_ipv6_type, 0,
124
-  REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
127
+  ANY_ROUTE },
125 128
   { "compare_ips", (cmd_function)w_compare_ips, 2, fixup_spve_spve, 0,
126 129
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
127 130
   { "compare_pure_ips", (cmd_function)w_compare_pure_ips, 2, fixup_spve_spve, 0,
... ...
@@ -465,13 +468,23 @@ static int w_ip_type(struct sip_msg* _msg, char* _s)
465 468
   }
466 469
 }
467 470
 
471
+static int w_detailed_ipv4_type(struct sip_msg* _msg, char* _s,  char *_dst)
472
+{
473
+    return detailed_ip_type(AF_INET, _msg, _s, _dst);
474
+}
468 475
 
469
-/*! \brief Return the IP type of the given argument (string or pv): 1 = IPv4, 2 = IPv6, 3 = IPv6 refenrece, -1 = invalid IP. */
470 476
 static int w_detailed_ipv6_type(struct sip_msg* _msg, char* _s,  char *_dst)
477
+{
478
+    return detailed_ip_type(AF_INET6, _msg, _s, _dst);
479
+}
480
+
481
+/*! \brief Return the IP type of the given argument (string or pv): 1 = IPv4, 2 = IPv6, 3 = IPv6 refenrece, -1 = invalid IP. */
482
+static int detailed_ip_type(unsigned int _type, struct sip_msg* _msg, char* _s,  char *_dst)
471 483
 {
472 484
   str string;
473 485
   pv_spec_t *dst;
474 486
   pv_value_t val;
487
+  char *res;
475 488
 
476 489
   if (_s == NULL) {
477 490
     LM_ERR("bad parameter\n");
... ...
@@ -486,14 +499,62 @@ static int w_detailed_ipv6_type(struct sip_msg* _msg, char* _s,  char *_dst)
486 499
 
487 500
   LM_ERR("!!!!!!! ip to change is %.*s\n", string.len, string.s);
488 501
   /* make IPv6 from reference */
489
-  if (string.s[0] == '[') {
490
-      string.s++;
491
-      string.len -= 2;
492
-  }
493
-  LM_ERR("!!!!!!! hi 1 \n");
494
-  //
495
-  val.rs.s = "Ana are";
496
-  val.rs.len = 7;
502
+  if (_type == AF_INET) {
503
+      if (!ip4_iptype(string, &res)) {
504
+          LM_ERR("bad ip parameter\n");
505
+          return -1;
506
+      }
507
+  }
508
+  else {
509
+      if (string.s[0] == '[') {
510
+          string.s++;
511
+          string.len -= 2;
512
+      }
513
+      LM_ERR("!!!!!!! hi 1 \n");
514
+      //
515
+      if (!ip6_iptype(string, &res)) {
516
+          LM_ERR("bad ip parameter\n");
517
+          return -1;
518
+      }
519
+  }
520
+  val.rs.s = res;
521
+  val.rs.len = strlen(res);
522
+  val.flags = PV_VAL_STR;
523
+  dst = (pv_spec_t *)_dst;
524
+  dst->setf(_msg, &dst->pvp, (int)EQ_T, &val);
525
+  LM_ERR("!!!!!!! hi 2 \n");
526
+  //return ip6_iptype(string.s, string.len);
527
+  return 1;
528
+}
529
+
530
+/*! \brief Return the IP type of the given argument (string or pv): 1 = IPv4, 2 = IPv6, 3 = IPv6 refenrece, -1 = invalid IP. */
531
+static int w_detailed_ipv4_type(struct sip_msg* _msg, char* _s,  char *_dst)
532
+{
533
+  str string;
534
+  pv_spec_t *dst;
535
+  pv_value_t val;
536
+  char *res;
537
+
538
+  if (_s == NULL) {
539
+    LM_ERR("bad parameter\n");
540
+    return -2;
541
+  }
542
+
543
+  if (fixup_get_svalue(_msg, (gparam_p)_s, &string))
544
+  {
545
+    LM_ERR("cannot print the format for string\n");
546
+    return -3;
547
+  }
548
+
549
+  LM_ERR("!!!!!!! ip to change is %.*s\n", string.len, string.s);
550
+
551
+  if (!ip4_iptype(string, &res)) {
552
+      LM_ERR("bad ip parameter\n");
553
+      return -1;
554
+  }
555
+
556
+  val.rs.s = res;
557
+  val.rs.len = strlen(res);
497 558
   val.flags = PV_VAL_STR;
498 559
   dst = (pv_spec_t *)_dst;
499 560
   dst->setf(_msg, &dst->pvp, (int)EQ_T, &val);