Browse code

Second coming

Lucian Balaceanu authored on 19/08/2016 15:23:48
Showing 3 changed files
... ...
@@ -23,43 +23,33 @@
23 23
  *
24 24
  */
25 25
 
26
+#include "detailed_ip_type.h"
27
+
26 28
 #include <stdio.h>
27 29
 #include <stdlib.h>
28 30
 #include <arpa/inet.h>
29 31
 #include <string.h>
30 32
 
31
-typedef struct ip4_node {
32
-    uint32_t value;
33
-    char *ip_type;
34
-    uint32_t sub_mask;
35
-} ip4_node;
36
-
37
-typedef struct ip6_node {
38
-    uint32_t value[4];
39
-    char *ip_type;
40
-    uint32_t sub_mask[4];
41
-} ip6_node;
42
-
43
-ip4_node IPv4ranges[] = {
44
-        { 0xffffffff ,  "BROADCAST",  0xffffffff },  // 255.255.255.255/32
45
-        { 0xcb007100 ,  "TEST-NET",   0xffffff00 },  // 203.0.113/24
46
-        { 0xc6336400 ,  "TEST-NET",   0xffffff00 },  // 198.51.100/24
47
-        { 0xc0586300 ,  "6TO4-RELAY", 0xffffff00 },  // 192.88.99.0/24
48
-        { 0xc0000200 ,  "TEST-NET",   0xffffff00 },  // 192.0.2/24
49
-        { 0xc0000000 ,  "RESERVED",   0xffffff00 },  // 192.0.0/24
50
-        { 0xc0a80000 ,  "PRIVATE",    0xffff0000 },  // 192.168/16
51
-        { 0xa9fe0000 ,  "LINK-LOCAL", 0xffff0000 },  // 169.254/16
52
-        { 0xc6120000 ,  "RESERVED",   0xfffe0000 },  // 198.18/15
53
-        { 0xac100000 ,  "PRIVATE",    0xfffe0000 },  // 172.16/12
54
-        { 0x64400000 ,  "SHARED",     0xffc00000 },  // 100.64/10
55
-        { 0x7f000000 ,  "LOOPBACK",   0xff000000 },  // 127.0/8
56
-        { 0xa000000 ,   "PRIVATE",    0xff000000 },  // 10/8
57
-        { 0x0 ,         "PRIVATE",    0xff000000 },  // 0/8
58
-        { 0xf0000000 ,  "RESERVED",   0xf0000000 },  // 240/4
59
-        { 0xe0000000 ,  "MULTICAST",  0xf0000000 }  // 224/4
33
+static ip4_node IPv4ranges[IPv4RANGES_SIZE] = {
34
+        { 0xffffffff,  "BROADCAST",  0xffffffff },  // 255.255.255.255/32
35
+        { 0xcb007100,  "TEST-NET",   0xffffff00 },  // 203.0.113/24
36
+        { 0xc6336400,  "TEST-NET",   0xffffff00 },  // 198.51.100/24
37
+        { 0xc0586300,  "6TO4-RELAY", 0xffffff00 },  // 192.88.99.0/24
38
+        { 0xc0000200,  "TEST-NET",   0xffffff00 },  // 192.0.2/24
39
+        { 0xc0000000,  "RESERVED",   0xffffff00 },  // 192.0.0/24
40
+        { 0xc0a80000,  "PRIVATE",    0xffff0000 },  // 192.168/16
41
+        { 0xa9fe0000,  "LINK-LOCAL", 0xffff0000 },  // 169.254/16
42
+        { 0xc6120000,  "RESERVED",   0xfffe0000 },  // 198.18/15
43
+        { 0xac100000,  "PRIVATE",    0xfffe0000 },  // 172.16/12
44
+        { 0x64400000,  "SHARED",     0xffc00000 },  // 100.64/10
45
+        { 0x7f000000,  "LOOPBACK",   0xff000000 },  // 127.0/8
46
+        { 0xa0000000,  "PRIVATE",    0xff000000 },  // 10/8
47
+        { 0x0,         "PRIVATE",    0xff000000 },  // 0/8
48
+        { 0xf0000000,  "RESERVED",   0xf0000000 },  // 240/4
49
+        { 0xe0000000,  "MULTICAST",  0xf0000000 }  // 224/4
60 50
 };
61 51
 
62
-ip6_node IPv6ranges[] = {
52
+static  ip6_node IPv6ranges[IPv6RANGES_SIZE] = {
63 53
     { {0x00000000, 0x00000000, 0x00000000, 0x00000000} , "UNSPECIFIED",         {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} },  //::/128
64 54
     { {0x00000000, 0x00000000, 0x00000000, 0x00000001} , "LOOPBACK",            {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} },  //::1/128
65 55
     { {0x00000000, 0x00000000, 0x0000FFFF, 0x00000000} , "IPV4MAP",             {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000} },  //::FFFF:0:0/96
... ...
@@ -83,9 +73,34 @@ ip6_node IPv6ranges[] = {
83 73
     //more to come here soon
84 74
 };
85 75
 
86
-char* ip6_iptype(uint32_t *ip) {
76
+
77
+int _compare_ips(str string_ip, char *res)
78
+{
79
+  uint32_t in6_addr[4];
80
+  char in6_string[INET6_ADDRSTRLEN];
81
+  int i;
82
+
83
+  memcpy(in6_string, string_ip.s, string_ip.len);
84
+  in6_string[string_ip.len] = '\0';
85
+
86
+  if (inet_pton(AF_INET6, in6_string, in6_addr) != 1)  return 0;
87
+
88
+  for (i = 0; i < IPv6RANGES_SIZE; i++) {
89
+      if (((in6_addr[0] & IPv6ranges[i].sub_mask[0]) == IPv6ranges[i].value[0]) &&
90
+          ((in6_addr[1] & IPv6ranges[i].sub_mask[1]) == IPv6ranges[i].value[1]) &&
91
+          ((in6_addr[2] & IPv6ranges[i].sub_mask[2]) == IPv6ranges[i].value[2]) &&
92
+          ((in6_addr[3] & IPv6ranges[i].sub_mask[3]) == IPv6ranges[i].value[3])) {
93
+
94
+          return IPv6ranges[i].ip_type;
95
+      }
96
+  }
97
+
98
+  return 1;
99
+}
100
+
101
+char* ip6_iptype(str s, uint32_t *ip) {
87 102
     int i;
88
-    for (i = 0; i < 19; i++) {
103
+    for (i = 0; i < IPv6RANGES_SIZE; i++) {
89 104
         if (((ip[0] & IPv6ranges[i].sub_mask[0]) == IPv6ranges[i].value[0]) &&
90 105
             ((ip[1] & IPv6ranges[i].sub_mask[1]) == IPv6ranges[i].value[1]) &&
91 106
             ((ip[2] & IPv6ranges[i].sub_mask[2]) == IPv6ranges[i].value[2]) &&
... ...
@@ -98,7 +113,7 @@ char* ip6_iptype(uint32_t *ip) {
98 113
 
99 114
 char* ip4_iptype(uint32_t ip) {
100 115
     int i;
101
-    for (i = 0; i < 15; i++) {
116
+    for (i = 0; i < IPv4RANGES_SIZE; i++) {
102 117
         if ( (ip & IPv4ranges[i].sub_mask) == IPv4ranges[i].value ) {
103 118
             return IPv4ranges[i].ip_type;
104 119
         }
... ...
@@ -106,41 +121,40 @@ char* ip4_iptype(uint32_t ip) {
106 121
     return "PUBLIC";
107 122
 }
108 123
 
109
-void ipv4ranges_to_network(ip4_node *ip4_node_array, int size) {
124
+void ipv4ranges_hton() {
110 125
     int pos;
111 126
     uint32_t tmp;
112 127
 
113
-    for (pos=0; pos < size; pos++) {
114
-        tmp = ip4_node_array[pos].value;
115
-        ip4_node_array[pos].value = ntohl(tmp);
116
-        tmp = ip4_node_array[pos].sub_mask;
117
-        ip4_node_array[pos].sub_mask = ntohl(tmp);
128
+    for (pos=0; pos < IPv4RANGES_SIZE; pos++) {
129
+        tmp = IPv4ranges[pos].value;
130
+        IPv4ranges[pos].value = ntohl(tmp);
131
+        tmp = IPv4ranges[pos].sub_mask;
132
+        IPv4ranges[pos].sub_mask = ntohl(tmp);
118 133
     }
119 134
 }
120 135
 
121 136
 
122
-void ipv6ranges_to_network(ip6_node *ip6_node_array, int size) {
137
+void ipv6ranges_hton() {
123 138
     int pos;
124 139
     uint32_t tmp;
125 140
 
126
-    for (pos=0; pos < size; pos++) {
127
-        tmp = ip6_node_array[pos].value[0];
128
-        ip6_node_array[pos].value[0] = ntohl(tmp);
129
-        tmp = ip6_node_array[pos].value[1];
130
-        ip6_node_array[pos].value[1] = ntohl(tmp);
131
-        tmp = ip6_node_array[pos].value[2];
132
-        ip6_node_array[pos].value[2] = ntohl(tmp);
133
-        tmp = ip6_node_array[pos].value[3];
134
-        ip6_node_array[pos].value[3] = ntohl(tmp);
135
-
136
-        tmp = ip6_node_array[pos].sub_mask[0];
137
-        ip6_node_array[pos].sub_mask[0] = ntohl(tmp);
138
-        tmp = ip6_node_array[pos].sub_mask[1];
139
-        ip6_node_array[pos].sub_mask[1] = ntohl(tmp);
140
-        tmp = ip6_node_array[pos].sub_mask[2];
141
-        ip6_node_array[pos].sub_mask[2] = ntohl(tmp);
142
-        tmp = ip6_node_array[pos].sub_mask[3];
143
-        ip6_node_array[pos].sub_mask[3] = ntohl(tmp);
141
+    for (pos=0; pos < IPv6RANGES_SIZE; pos++) {
142
+        tmp = IPv6ranges[pos].value[0];
143
+        IPv6ranges[pos].value[0] = ntohl(tmp);
144
+        tmp = IPv6ranges[pos].value[1];
145
+        IPv6ranges[pos].value[1] = ntohl(tmp);
146
+        tmp = IPv6ranges[pos].value[2];
147
+        IPv6ranges[pos].value[2] = ntohl(tmp);
148
+        tmp = IPv6ranges[pos].value[3];
149
+        IPv6ranges[pos].value[3] = ntohl(tmp);
144 150
 
151
+        tmp = IPv6ranges[pos].sub_mask[0];
152
+        IPv6ranges[pos].sub_mask[0] = ntohl(tmp);
153
+        tmp = IPv6ranges[pos].sub_mask[1];
154
+        IPv6ranges[pos].sub_mask[1] = ntohl(tmp);
155
+        tmp = IPv6ranges[pos].sub_mask[2];
156
+        IPv6ranges[pos].sub_mask[2] = ntohl(tmp);
157
+        tmp = IPv6ranges[pos].sub_mask[3];
158
+        IPv6ranges[pos].sub_mask[3] = ntohl(tmp);
145 159
     }
146 160
 }
... ...
@@ -27,6 +27,22 @@
27 27
 #ifndef _IPOPS_DETAILED_IP_TYPE_H
28 28
 #define _IPOPS_DETAILED_IP_TYPE_H
29 29
 
30
+#define IPv4RANGES_SIZE 15
31
+#define IPv6RANGES_SIZE 19
30 32
 
33
+typedef struct ip4_node {
34
+    uint32_t value;
35
+    char *ip_type;
36
+    uint32_t sub_mask;
37
+} ip4_node;
38
+
39
+typedef struct ip6_node {
40
+    uint32_t value[4];
41
+    char *ip_type;
42
+    uint32_t sub_mask[4];
43
+} ip6_node;
44
+
45
+void ipv6ranges_hton();
46
+void ipv4ranges_hton();
31 47
 
32 48
 #endif
... ...
@@ -78,7 +78,7 @@ int _ip_is_in_subnet(char *ip1, size_t len1, enum enum_ip_type ip1_type, char *i
78 78
 /*
79 79
  * Script functions
80 80
  */
81
-static int w_is_ip(struct sip_msg*, char*);
81
+    t w_is_ip(struct sip_msg*, char*);
82 82
 static int w_is_pure_ip(struct sip_msg*, char*);
83 83
 static int w_is_ipv4(struct sip_msg*, char*);
84 84
 static int w_is_ipv6(struct sip_msg*, char*);
... ...
@@ -117,9 +117,7 @@ static cmd_export_t cmds[] =
117 117
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
118 118
   { "ip_type", (cmd_function)w_ip_type, 1, fixup_spve_null, 0,
119 119
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
120
-  { "detailed_ipv4_type", (cmd_function)w_ip_type, 1, fixup_spve_null, 0,
121
-  REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
122
-  { "detailed_ipv6_type", (cmd_function)w_ip_type, 1, fixup_spve_null, 0,
120
+  { "detailed_ipv6_type", (cmd_function)w_detailed_ipv6_type, 2, fixup_spve_null, 0,
123 121
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
124 122
   { "compare_ips", (cmd_function)w_compare_ips, 2, fixup_spve_spve, 0,
125 123
   REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE },
... ...
@@ -152,13 +150,28 @@ struct module_exports exports = {
152 150
   0,                         /*!< exported MI functions */
153 151
   mod_pvs,                   /*!< exported pseudo-variables */
154 152
   0,                         /*!< extra processes */
155
-  0,                         /*!< module initialization function */
153
+  mod_init,                  /*!< module initialization function */
156 154
   (response_function) 0,     /*!< response handling function */
157 155
   0,                         /*!< destroy function */
158 156
   0                          /*!< per-child init function */
159 157
 };
160 158
 
161 159
 
160
+/************* Interface Functions *****************************************/
161
+
162
+/**
163
+ * Initialises the module, i.e. it binds the necessary API functions
164
+ * and registers the fifo commands
165
+ *
166
+ * @return 0 on success, -1 on failure
167
+ */
168
+static int mod_init(void) {
169
+    /* turn detailed_ip_type relevant structures to netowork byte order so no need to
170
+     * transform each ip to host order before comparing */
171
+    ipv4ranges_hton();
172
+    ipv6ranges_hton();
173
+
174
+}
162 175
 /*
163 176
  * Module internal functions
164 177
  */
... ...
@@ -421,7 +434,7 @@ static int w_ip_type(struct sip_msg* _msg, char* _s)
421 434
 
422 435
 
423 436
 /*! \brief Return the IP type of the given argument (string or pv): 1 = IPv4, 2 = IPv6, 3 = IPv6 refenrece, -1 = invalid IP. */
424
-static int w_detailed_ipv6_type(struct sip_msg* _msg, char* _s)
437
+static int w_detailed_ipv6_type(struct sip_msg* _msg, char* _s,  char *res)
425 438
 {
426 439
   str string;
427 440
 
... ...
@@ -442,20 +455,7 @@ static int w_detailed_ipv6_type(struct sip_msg* _msg, char* _s)
442 455
       string.len -= 2;
443 456
   }
444 457
 
445
-  switch (ip_parser_execute(string.s, string.len)) {
446
-    case(ip_type_ipv4):
447
-      return 1;
448
-      break;
449
-    case(ip_type_ipv6):
450
-      return 2;
451
-      break;
452
-    case(ip_type_ipv6_reference):
453
-      return 3;
454
-      break;
455
-    default:
456
-      return -1;
457
-      break;
458
-  }
458
+  return ip6_iptype(string.s, string.len, res);
459 459
 }
460 460
 
461 461
 /*! \brief Return true if both IP's (string or pv) are equal. This function also allows comparing an IPv6 with an IPv6 reference. */