1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,146 @@ |
1 |
+/* |
|
2 |
+ * $Id$ |
|
3 |
+ * |
|
4 |
+ * Functions that operate on IP addresses |
|
5 |
+ * |
|
6 |
+ * Copyright (C) 2012 Hugh Waite (crocodile-rcs.com) |
|
7 |
+ * |
|
8 |
+ * This file is part of Kamailio, a free SIP server. |
|
9 |
+ * |
|
10 |
+ * Kamailio is free software; you can redistribute it and/or modify |
|
11 |
+ * it under the terms of the GNU General Public License as published by |
|
12 |
+ * the Free Software Foundation; either version 2 of the License, or |
|
13 |
+ * (at your option) any later version |
|
14 |
+ * |
|
15 |
+ * Kamailio is distributed in the hope that it will be useful, |
|
16 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 |
+ * GNU General Public License for more details. |
|
19 |
+ * |
|
20 |
+ * You should have received a copy of the GNU General Public License |
|
21 |
+ * along with this program; if not, write to the Free Software |
|
22 |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
+ * |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+#include <stdio.h> |
|
27 |
+#include <stdlib.h> |
|
28 |
+#include <arpa/inet.h> |
|
29 |
+#include <string.h> |
|
30 |
+ |
|
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 |
|
60 |
+}; |
|
61 |
+ |
|
62 |
+ip6_node IPv6ranges[] = { |
|
63 |
+ { {0x00000000, 0x00000000, 0x00000000, 0x00000000} , "UNSPECIFIED", {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} }, //::/128 |
|
64 |
+ { {0x00000000, 0x00000000, 0x00000000, 0x00000001} , "LOOPBACK", {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} }, //::1/128 |
|
65 |
+ { {0x00000000, 0x00000000, 0x0000FFFF, 0x00000000} , "IPV4MAP", {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000} }, //::FFFF:0:0/96 |
|
66 |
+ { {0x01000000, 0x00000000, 0x00000000, 0x00000000} , "DISCARD", {0xFFFFFFFF, 0xFFFFFF00, 0x00000000, 0x00000000} }, //0100::/64 |
|
67 |
+ { {0x20010002, 0x00000000, 0x00000000, 0x00000000} , "BMWG", {0xFFFFFFFF, 0xFF000000, 0x00000000, 0x00000000} }, //2001:0002::/48 |
|
68 |
+ { {0x20010000, 0x00000000, 0x00000000, 0x00000000} , "TEREDO", {0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000} }, //2001::/32 |
|
69 |
+ { {0x20010DB8, 0x00000000, 0x00000000, 0x00000000} , "DOCUMENTATION", {0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000} }, //2001:DB8::/32 |
|
70 |
+ { {0x20010010, 0x00000000, 0x00000000, 0x00000000} , "ORCHID", {0xFFFFFFF0, 0x00000000, 0x00000000, 0x00000000} }, //2001:10::/28 |
|
71 |
+ { {0x20020000, 0x00000000, 0x00000000, 0x00000000} , "6TO4", {0xFFFF0000, 0x00000000, 0x00000000, 0x00000000} }, //2002::/16 |
|
72 |
+ { {0xFE800000, 0x00000000, 0x00000000, 0x00000000} , "LINK-LOCAL-UNICAST", {0xFFC00000, 0x00000000, 0x00000000, 0x00000000} }, //FE80::/10 |
|
73 |
+ { {0xFEC00000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED", {0xFFC00000, 0x00000000, 0x00000000, 0x00000000} }, //FEC0::/10 |
|
74 |
+ { {0xFE000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED", {0xFF800000, 0x00000000, 0x00000000, 0x00000000} }, //FE00::/9 |
|
75 |
+ { {0x00000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED", {0xFF000000, 0x00000000, 0x00000000, 0x00000000} }, //::/8 |
|
76 |
+ { {0x01000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED", {0xFF000000, 0x00000000, 0x00000000, 0x00000000} }, //0100::/8 |
|
77 |
+ { {0xFF000000, 0x00000000, 0x00000000, 0x00000000} , "MULTICAST", {0xFF000000, 0x00000000, 0x00000000, 0x00000000} }, //FF00::/8 |
|
78 |
+ { {0x02000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED", {0xFE000000, 0x00000000, 0x00000000, 0x00000000} }, //0200::/7 |
|
79 |
+ { {0xFC000000, 0x00000000, 0x00000000, 0x00000000} , "UNIQUE-LOCAL-UNICAST",{0xFE000000, 0x00000000, 0x00000000, 0x00000000} }, //FC00::/7 |
|
80 |
+ { {0x04000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED", {0xFC000000, 0x00000000, 0x00000000, 0x00000000} }, //400::/6 |
|
81 |
+ { {0xF8000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED", {0xFC000000, 0x00000000, 0x00000000, 0x00000000} }, //F800::/6 |
|
82 |
+ { {0x08000000, 0x00000000, 0x00000000, 0x00000000} , "RESERVED", {0xF8000000, 0x00000000, 0x00000000, 0x00000000} } //0800::/5 |
|
83 |
+ //more to come here soon |
|
84 |
+}; |
|
85 |
+ |
|
86 |
+char* ip6_iptype(uint32_t *ip) { |
|
87 |
+ int i; |
|
88 |
+ for (i = 0; i < 19; i++) { |
|
89 |
+ if (((ip[0] & IPv6ranges[i].sub_mask[0]) == IPv6ranges[i].value[0]) && |
|
90 |
+ ((ip[1] & IPv6ranges[i].sub_mask[1]) == IPv6ranges[i].value[1]) && |
|
91 |
+ ((ip[2] & IPv6ranges[i].sub_mask[2]) == IPv6ranges[i].value[2]) && |
|
92 |
+ ((ip[3] & IPv6ranges[i].sub_mask[3]) == IPv6ranges[i].value[3])) { |
|
93 |
+ return IPv6ranges[i].ip_type; |
|
94 |
+ } |
|
95 |
+ } |
|
96 |
+ return "PUBLIC"; |
|
97 |
+} |
|
98 |
+ |
|
99 |
+char* ip4_iptype(uint32_t ip) { |
|
100 |
+ int i; |
|
101 |
+ for (i = 0; i < 15; i++) { |
|
102 |
+ if ( (ip & IPv4ranges[i].sub_mask) == IPv4ranges[i].value ) { |
|
103 |
+ return IPv4ranges[i].ip_type; |
|
104 |
+ } |
|
105 |
+ } |
|
106 |
+ return "PUBLIC"; |
|
107 |
+} |
|
108 |
+ |
|
109 |
+void ipv4ranges_to_network(ip4_node *ip4_node_array, int size) { |
|
110 |
+ int pos; |
|
111 |
+ uint32_t tmp; |
|
112 |
+ |
|
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); |
|
118 |
+ } |
|
119 |
+} |
|
120 |
+ |
|
121 |
+ |
|
122 |
+void ipv6ranges_to_network(ip6_node *ip6_node_array, int size) { |
|
123 |
+ int pos; |
|
124 |
+ uint32_t tmp; |
|
125 |
+ |
|
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); |
|
144 |
+ |
|
145 |
+ } |
|
146 |
+} |
0 | 147 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,32 @@ |
1 |
+/* |
|
2 |
+ * $Id$ |
|
3 |
+ * |
|
4 |
+ * Functions that operate on IP addresses |
|
5 |
+ * |
|
6 |
+ * Copyright (C) 2012 Hugh Waite (crocodile-rcs.com) |
|
7 |
+ * |
|
8 |
+ * This file is part of Kamailio, a free SIP server. |
|
9 |
+ * |
|
10 |
+ * Kamailio is free software; you can redistribute it and/or modify |
|
11 |
+ * it under the terms of the GNU General Public License as published by |
|
12 |
+ * the Free Software Foundation; either version 2 of the License, or |
|
13 |
+ * (at your option) any later version |
|
14 |
+ * |
|
15 |
+ * Kamailio is distributed in the hope that it will be useful, |
|
16 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 |
+ * GNU General Public License for more details. |
|
19 |
+ * |
|
20 |
+ * You should have received a copy of the GNU General Public License |
|
21 |
+ * along with this program; if not, write to the Free Software |
|
22 |
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
+ * |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+ |
|
27 |
+#ifndef _IPOPS_DETAILED_IP_TYPE_H |
|
28 |
+#define _IPOPS_DETAILED_IP_TYPE_H |
|
29 |
+ |
|
30 |
+ |
|
31 |
+ |
|
32 |
+#endif |
... | ... |
@@ -84,6 +84,7 @@ static int w_is_ipv4(struct sip_msg*, char*); |
84 | 84 |
static int w_is_ipv6(struct sip_msg*, char*); |
85 | 85 |
static int w_is_ipv6_reference(struct sip_msg*, char*); |
86 | 86 |
static int w_ip_type(struct sip_msg*, char*); |
87 |
+static int w_detailed_ipv6_type(struct sip_msg*, char*); |
|
87 | 88 |
static int w_compare_ips(struct sip_msg*, char*, char*); |
88 | 89 |
static int w_compare_pure_ips(struct sip_msg*, char*, char*); |
89 | 90 |
static int w_is_ip_rfc1918(struct sip_msg*, char*); |
... | ... |
@@ -116,6 +117,10 @@ static cmd_export_t cmds[] = |
116 | 117 |
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE }, |
117 | 118 |
{ "ip_type", (cmd_function)w_ip_type, 1, fixup_spve_null, 0, |
118 | 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, |
|
123 |
+ REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE }, |
|
119 | 124 |
{ "compare_ips", (cmd_function)w_compare_ips, 2, fixup_spve_spve, 0, |
120 | 125 |
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE }, |
121 | 126 |
{ "compare_pure_ips", (cmd_function)w_compare_pure_ips, 2, fixup_spve_spve, 0, |
... | ... |
@@ -415,6 +420,44 @@ static int w_ip_type(struct sip_msg* _msg, char* _s) |
415 | 420 |
} |
416 | 421 |
|
417 | 422 |
|
423 |
+/*! \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) |
|
425 |
+{ |
|
426 |
+ str string; |
|
427 |
+ |
|
428 |
+ if (_s == NULL) { |
|
429 |
+ LM_ERR("bad parameter\n"); |
|
430 |
+ return -2; |
|
431 |
+ } |
|
432 |
+ |
|
433 |
+ if (fixup_get_svalue(_msg, (gparam_p)_s, &string)) |
|
434 |
+ { |
|
435 |
+ LM_ERR("cannot print the format for string\n"); |
|
436 |
+ return -3; |
|
437 |
+ } |
|
438 |
+ |
|
439 |
+ /* make IPv6 from reference */ |
|
440 |
+ if (string.s[0] == '[') { |
|
441 |
+ string.s++; |
|
442 |
+ string.len -= 2; |
|
443 |
+ } |
|
444 |
+ |
|
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 |
+ } |
|
459 |
+} |
|
460 |
+ |
|
418 | 461 |
/*! \brief Return true if both IP's (string or pv) are equal. This function also allows comparing an IPv6 with an IPv6 reference. */ |
419 | 462 |
static int w_compare_ips(struct sip_msg* _msg, char* _s1, char* _s2) |
420 | 463 |
{ |