Browse code

Further integration

Lucian Balaceanu authored on 22/08/2016 15:18:45
Showing 3 changed files
... ...
@@ -99,6 +99,26 @@ int ip6_iptype(str string_ip, char **res)
99 99
   return 1;
100 100
 }
101 101
 
102
+int ip4_iptype(str string_ip, char **res)
103
+{
104
+  uint32_t in4_addr;
105
+  char in4_string[INET_ADDRSTRLEN];
106
+  int i;
107
+
108
+  memcpy(in4_string, string_ip.s, string_ip.len);
109
+  in4_string[string_ip.len] = '\0';
110
+
111
+  if (inet_pton(AF_INET, in4_string, &in4_addr) != 1)  return 0;
112
+
113
+  *res = "PUBLIC";
114
+  for (i = 0; i < IPv4RANGES_SIZE; i++) {
115
+      if ( (in4_addr & IPv4ranges[i].sub_mask) == IPv4ranges[i].value ) {
116
+          *res = IPv4ranges[i].ip_type;
117
+      }
118
+  }
119
+  return 1;
120
+}
121
+
102 122
 /*char* ip6_iptype(str s, uint32_t *ip) {
103 123
     int i;
104 124
     for (i = 0; i < IPv6RANGES_SIZE; i++) {
... ...
@@ -111,7 +131,7 @@ int ip6_iptype(str string_ip, char **res)
111 131
     }
112 132
     return "PUBLIC";
113 133
 }
114
-*/
134
+
115 135
 char* ip4_iptype(uint32_t ip) {
116 136
     int i;
117 137
     for (i = 0; i < IPv4RANGES_SIZE; i++) {
... ...
@@ -121,6 +141,7 @@ char* ip4_iptype(uint32_t ip) {
121 141
     }
122 142
     return "PUBLIC";
123 143
 }
144
+*/
124 145
 
125 146
 void ipv4ranges_hton() {
126 147
     int pos;
... ...
@@ -48,5 +48,6 @@ typedef struct ip6_node {
48 48
 void ipv6ranges_hton();
49 49
 void ipv4ranges_hton();
50 50
 int ip6_iptype(str string_ip, char **res);
51
+int ip4_iptype(str string_ip, char **res);
51 52
 
52 53
 #endif
... ...
@@ -75,7 +75,7 @@ MODULE_VERSION
75 75
  */
76 76
 int _compare_ips(char*, size_t, enum enum_ip_type, char*, size_t, enum enum_ip_type);
77 77
 int _ip_is_in_subnet(char *ip1, size_t len1, enum enum_ip_type ip1_type, char *ip2, size_t len2, enum enum_ip_type ip2_type, int netmask);
78
-
78
+static int _detailed_ip_type(unsigned int _type, struct sip_msg* _msg, char* _s,  char *_dst);
79 79
 
80 80
 /*
81 81
  * Script functions
... ...
@@ -470,16 +470,16 @@ static int w_ip_type(struct sip_msg* _msg, char* _s)
470 470
 
471 471
 static int w_detailed_ipv4_type(struct sip_msg* _msg, char* _s,  char *_dst)
472 472
 {
473
-    return detailed_ip_type(AF_INET, _msg, _s, _dst);
473
+    return _detailed_ip_type(AF_INET, _msg, _s, _dst);
474 474
 }
475 475
 
476 476
 static int w_detailed_ipv6_type(struct sip_msg* _msg, char* _s,  char *_dst)
477 477
 {
478
-    return detailed_ip_type(AF_INET6, _msg, _s, _dst);
478
+    return _detailed_ip_type(AF_INET6, _msg, _s, _dst);
479 479
 }
480 480
 
481 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)
482
+static int _detailed_ip_type(unsigned int _type, struct sip_msg* _msg, char* _s,  char *_dst)
483 483
 {
484 484
   str string;
485 485
   pv_spec_t *dst;
... ...
@@ -498,59 +498,27 @@ static int detailed_ip_type(unsigned int _type, struct sip_msg* _msg, char* _s,
498 498
   }
499 499
 
500 500
   LM_ERR("!!!!!!! ip to change is %.*s\n", string.len, string.s);
501
-  /* make IPv6 from reference */
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");
501
+  switch (_type) {
502
+      case AF_INET:
503
+          if (!ip4_iptype(string, &res)) {
504
+              LM_ERR("bad ip parameter\n");
505
+              return -1;
506
+          }
507
+          break;
508
+      case AF_INET6:
509
+          /* make IPv6 from reference if needed */
510
+          if (string.s[0] == '[') {
511
+              string.s++;
512
+              string.len -= 2;
513
+          }
514
+          LM_ERR("!!!!!!! hi 1 \n");
515
+          if (!ip6_iptype(string, &res)) {
516
+              LM_ERR("bad ip parameter\n");
517
+              return -1;
518
+          }
519
+          break;
520
+      default:
517 521
           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 522
   }
555 523
 
556 524
   val.rs.s = res;
... ...
@@ -559,7 +527,6 @@ static int w_detailed_ipv4_type(struct sip_msg* _msg, char* _s,  char *_dst)
559 527
   dst = (pv_spec_t *)_dst;
560 528
   dst->setf(_msg, &dst->pvp, (int)EQ_T, &val);
561 529
   LM_ERR("!!!!!!! hi 2 \n");
562
-  //return ip6_iptype(string.s, string.len);
563 530
   return 1;
564 531
 }
565 532