- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)
1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,450 +0,0 @@ |
1 |
-/* |
|
2 |
- * ip address & address family related functions |
|
3 |
- * |
|
4 |
- * Copyright (C) 2001-2003 FhG Fokus |
|
5 |
- * |
|
6 |
- * This file is part of Kamailio, a free SIP server. |
|
7 |
- * |
|
8 |
- * Kamailio is free software; you can redistribute it and/or modify |
|
9 |
- * it under the terms of the GNU General Public License as published by |
|
10 |
- * the Free Software Foundation; either version 2 of the License, or |
|
11 |
- * (at your option) any later version |
|
12 |
- * |
|
13 |
- * Kamailio is distributed in the hope that it will be useful, |
|
14 |
- * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
- * GNU General Public License for more details. |
|
17 |
- * |
|
18 |
- * You should have received a copy of the GNU General Public License |
|
19 |
- * along with this program; if not, write to the Free Software |
|
20 |
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
21 |
- */ |
|
22 |
- |
|
23 |
-/** Kamailio core :: internal ip addresses representation functions. |
|
24 |
- * @file ip_addr.c |
|
25 |
- * @ingroup core |
|
26 |
- * Module: @ref core |
|
27 |
- */ |
|
28 |
- |
|
29 |
- |
|
30 |
-#include <stdlib.h> |
|
31 |
-#include <stdio.h> |
|
32 |
- |
|
33 |
-#include "ip_addr.h" |
|
34 |
-#include "dprint.h" |
|
35 |
-#include "mem/mem.h" |
|
36 |
-#include "resolve.h" |
|
37 |
-#include "trim.h" |
|
38 |
- |
|
39 |
- |
|
40 |
-struct net* mk_new_net(struct ip_addr* ip, struct ip_addr* mask) |
|
41 |
-{ |
|
42 |
- struct net* n; |
|
43 |
- int warning; |
|
44 |
- int r; |
|
45 |
- |
|
46 |
- warning=0; |
|
47 |
- if ((ip->af != mask->af) || (ip->len != mask->len)){ |
|
48 |
- LM_CRIT("trying to use a different mask family" |
|
49 |
- " (eg. ipv4/ipv6mask or ipv6/ipv4mask)\n"); |
|
50 |
- goto error; |
|
51 |
- } |
|
52 |
- n=(struct net*)pkg_malloc(sizeof(struct net)); |
|
53 |
- if (n==0){ |
|
54 |
- LM_CRIT("memory allocation failure\n"); |
|
55 |
- goto error; |
|
56 |
- } |
|
57 |
- n->ip=*ip; |
|
58 |
- n->mask=*mask; |
|
59 |
- for (r=0; r<n->ip.len/4; r++) { /*ipv4 & ipv6 addresses are multiple of 4*/ |
|
60 |
- n->ip.u.addr32[r] &= n->mask.u.addr32[r]; |
|
61 |
- if (n->ip.u.addr32[r]!=ip->u.addr32[r]) warning=1; |
|
62 |
- }; |
|
63 |
- if (warning){ |
|
64 |
- LM_WARN("invalid network address/netmask " |
|
65 |
- "combination fixed...\n"); |
|
66 |
- print_ip("original network address:", ip, "/"); |
|
67 |
- print_ip("", mask, "\n"); |
|
68 |
- print_ip("fixed network address:", &(n->ip), "/"); |
|
69 |
- print_ip("", &(n->mask), "\n"); |
|
70 |
- }; |
|
71 |
- return n; |
|
72 |
-error: |
|
73 |
- return 0; |
|
74 |
-} |
|
75 |
- |
|
76 |
- |
|
77 |
- |
|
78 |
-struct net* mk_new_net_bitlen(struct ip_addr* ip, unsigned int bitlen) |
|
79 |
-{ |
|
80 |
- struct ip_addr mask; |
|
81 |
- int r; |
|
82 |
- |
|
83 |
- if (bitlen>ip->len*8){ |
|
84 |
- LM_CRIT("bad bitlen number %d\n", bitlen); |
|
85 |
- goto error; |
|
86 |
- } |
|
87 |
- memset(&mask,0, sizeof(mask)); |
|
88 |
- for (r=0;r<bitlen/8;r++) mask.u.addr[r]=0xff; |
|
89 |
- if (bitlen%8) mask.u.addr[r]= ~((1<<(8-(bitlen%8)))-1); |
|
90 |
- mask.af=ip->af; |
|
91 |
- mask.len=ip->len; |
|
92 |
- |
|
93 |
- return mk_new_net(ip, &mask); |
|
94 |
-error: |
|
95 |
- return 0; |
|
96 |
-} |
|
97 |
- |
|
98 |
- |
|
99 |
- |
|
100 |
-/** fills a net structure from an ip and a mask. |
|
101 |
- * |
|
102 |
- * This function will not print any error messages or allocate |
|
103 |
- * memory (as opposed to mk_new_net() above). |
|
104 |
- * |
|
105 |
- * @param n - destination net structure |
|
106 |
- * @param ip |
|
107 |
- * @param mask |
|
108 |
- * @return -1 on error (af mismatch), 0 on success |
|
109 |
- */ |
|
110 |
-int mk_net(struct net* n, struct ip_addr* ip, struct ip_addr* mask) |
|
111 |
-{ |
|
112 |
- int r; |
|
113 |
- |
|
114 |
- if (unlikely((ip->af != mask->af) || (ip->len != mask->len))) { |
|
115 |
- return -1; |
|
116 |
- } |
|
117 |
- n->ip=*ip; |
|
118 |
- n->mask=*mask; |
|
119 |
- /* fix the network part of the mask */ |
|
120 |
- for (r=0; r<n->ip.len/4; r++) { /*ipv4 & ipv6 addresses are multiple of 4*/ |
|
121 |
- n->ip.u.addr32[r] &= n->mask.u.addr32[r]; |
|
122 |
- }; |
|
123 |
- return 0; |
|
124 |
-} |
|
125 |
- |
|
126 |
- |
|
127 |
- |
|
128 |
-/** fills a net structure from an ip and a bitlen. |
|
129 |
- * |
|
130 |
- * This function will not print any error messages or allocate |
|
131 |
- * memory (as opposed to mk_new_net_bitlen() above). |
|
132 |
- * |
|
133 |
- * @param n - destination net structure |
|
134 |
- * @param ip |
|
135 |
- * @param bitlen |
|
136 |
- * @return -1 on error (af mismatch), 0 on success |
|
137 |
- */ |
|
138 |
-int mk_net_bitlen(struct net* n, struct ip_addr* ip, unsigned int bitlen) |
|
139 |
-{ |
|
140 |
- struct ip_addr mask; |
|
141 |
- int r; |
|
142 |
- |
|
143 |
- if (unlikely(bitlen>ip->len*8)) |
|
144 |
- /* bitlen too big */ |
|
145 |
- return -1; |
|
146 |
- memset(&mask,0, sizeof(mask)); |
|
147 |
- for (r=0;r<bitlen/8;r++) mask.u.addr[r]=0xff; |
|
148 |
- if (bitlen%8) mask.u.addr[r]= ~((1<<(8-(bitlen%8)))-1); |
|
149 |
- mask.af=ip->af; |
|
150 |
- mask.len=ip->len; |
|
151 |
- |
|
152 |
- return mk_net(n, ip, &mask); |
|
153 |
-} |
|
154 |
- |
|
155 |
- |
|
156 |
- |
|
157 |
-/** initializes a net structure from a string. |
|
158 |
- * @param dst - net structure that will be filled |
|
159 |
- * @param s - string of the form "ip", "ip/mask_len" or "ip/ip_mak". |
|
160 |
- * @return -1 on error, 0 on succes |
|
161 |
- */ |
|
162 |
-int mk_net_str(struct net* dst, str* s) |
|
163 |
-{ |
|
164 |
- struct ip_addr* t; |
|
165 |
- char* p; |
|
166 |
- struct ip_addr ip; |
|
167 |
- str addr; |
|
168 |
- str mask; |
|
169 |
- unsigned int bitlen; |
|
170 |
- |
|
171 |
- /* test for ip only */ |
|
172 |
- t = str2ip(s); |
|
173 |
- if (unlikely(t == 0)) |
|
174 |
- t = str2ip6(s); |
|
175 |
- if (likely(t)) |
|
176 |
- return mk_net_bitlen(dst, t, t->len*8); |
|
177 |
- /* not a simple ip, maybe an ip/netmask pair */ |
|
178 |
- p = q_memchr(s->s, '/', s->len); |
|
179 |
- if (likely(p)) { |
|
180 |
- addr.s = s->s; |
|
181 |
- addr.len = (int)(long)(p - s->s); |
|
182 |
- mask.s = p + 1; |
|
183 |
- mask.len = s->len - (addr.len + 1); |
|
184 |
- /* allow '/' enclosed by whitespace */ |
|
185 |
- trim_trailing(&addr); |
|
186 |
- trim_leading(&mask); |
|
187 |
- t = str2ip(&addr); |
|
188 |
- if (likely(t)) { |
|
189 |
- /* it can be a number */ |
|
190 |
- if (str2int(&mask, &bitlen) == 0) |
|
191 |
- return mk_net_bitlen(dst, t, bitlen); |
|
192 |
- ip = *t; |
|
193 |
- t = str2ip(&mask); |
|
194 |
- if (likely(t)) |
|
195 |
- return mk_net(dst, &ip, t); |
|
196 |
- /* error */ |
|
197 |
- return -1; |
|
198 |
- } |
|
199 |
- else { |
|
200 |
- t = str2ip6(&addr); |
|
201 |
- if (likely(t)) { |
|
202 |
- /* it can be a number */ |
|
203 |
- if (str2int(&mask, &bitlen) == 0) |
|
204 |
- return mk_net_bitlen(dst, t, bitlen); |
|
205 |
- ip = *t; |
|
206 |
- t = str2ip6(&mask); |
|
207 |
- if (likely(t)) |
|
208 |
- return mk_net(dst, &ip, t); |
|
209 |
- /* error */ |
|
210 |
- return -1; |
|
211 |
- } |
|
212 |
- } |
|
213 |
- } |
|
214 |
- return -1; |
|
215 |
-} |
|
216 |
- |
|
217 |
- |
|
218 |
- |
|
219 |
-void print_ip(char* p, struct ip_addr* ip, char *s) |
|
220 |
-{ |
|
221 |
- switch(ip->af){ |
|
222 |
- case AF_INET: |
|
223 |
- DBG("%s%d.%d.%d.%d%s", (p)?p:"", |
|
224 |
- ip->u.addr[0], |
|
225 |
- ip->u.addr[1], |
|
226 |
- ip->u.addr[2], |
|
227 |
- ip->u.addr[3], |
|
228 |
- (s)?s:"" |
|
229 |
- ); |
|
230 |
- break; |
|
231 |
- case AF_INET6: |
|
232 |
- DBG("%s%x:%x:%x:%x:%x:%x:%x:%x%s", (p)?p:"", |
|
233 |
- htons(ip->u.addr16[0]), |
|
234 |
- htons(ip->u.addr16[1]), |
|
235 |
- htons(ip->u.addr16[2]), |
|
236 |
- htons(ip->u.addr16[3]), |
|
237 |
- htons(ip->u.addr16[4]), |
|
238 |
- htons(ip->u.addr16[5]), |
|
239 |
- htons(ip->u.addr16[6]), |
|
240 |
- htons(ip->u.addr16[7]), |
|
241 |
- (s)?s:"" |
|
242 |
- ); |
|
243 |
- break; |
|
244 |
- default: |
|
245 |
- DBG("print_ip: warning unknown address family %d\n", ip->af); |
|
246 |
- } |
|
247 |
-} |
|
248 |
- |
|
249 |
- |
|
250 |
- |
|
251 |
-void stdout_print_ip(struct ip_addr* ip) |
|
252 |
-{ |
|
253 |
- switch(ip->af){ |
|
254 |
- case AF_INET: |
|
255 |
- printf("%d.%d.%d.%d", ip->u.addr[0], |
|
256 |
- ip->u.addr[1], |
|
257 |
- ip->u.addr[2], |
|
258 |
- ip->u.addr[3]); |
|
259 |
- break; |
|
260 |
- case AF_INET6: |
|
261 |
- printf("%x:%x:%x:%x:%x:%x:%x:%x", htons(ip->u.addr16[0]), |
|
262 |
- htons(ip->u.addr16[1]), |
|
263 |
- htons(ip->u.addr16[2]), |
|
264 |
- htons(ip->u.addr16[3]), |
|
265 |
- htons(ip->u.addr16[4]), |
|
266 |
- htons(ip->u.addr16[5]), |
|
267 |
- htons(ip->u.addr16[6]), |
|
268 |
- htons(ip->u.addr16[7]) |
|
269 |
- ); |
|
270 |
- break; |
|
271 |
- default: |
|
272 |
- DBG("print_ip: warning unknown address family %d\n", ip->af); |
|
273 |
- } |
|
274 |
-} |
|
275 |
- |
|
276 |
- |
|
277 |
- |
|
278 |
-void print_net(struct net* net) |
|
279 |
-{ |
|
280 |
- if (net==0){ |
|
281 |
- LM_WARN("null pointer\n"); |
|
282 |
- return; |
|
283 |
- } |
|
284 |
- print_ip("", &net->ip, "/"); print_ip("", &net->mask, ""); |
|
285 |
-} |
|
286 |
- |
|
287 |
- |
|
288 |
-#ifdef USE_MCAST |
|
289 |
- |
|
290 |
-/* Returns 1 if the given address is a multicast address */ |
|
291 |
-int is_mcast(struct ip_addr* ip) |
|
292 |
-{ |
|
293 |
- if (!ip){ |
|
294 |
- LM_ERR("Invalid parameter value\n"); |
|
295 |
- return -1; |
|
296 |
- } |
|
297 |
- |
|
298 |
- if (ip->af==AF_INET){ |
|
299 |
- return IN_MULTICAST(htonl(ip->u.addr32[0])); |
|
300 |
- } else if (ip->af==AF_INET6){ |
|
301 |
- return IN6_IS_ADDR_MULTICAST((struct in6_addr*)ip->u.addr32); |
|
302 |
- } else { |
|
303 |
- LM_ERR("Unsupported protocol family\n"); |
|
304 |
- return -1; |
|
305 |
- } |
|
306 |
-} |
|
307 |
- |
|
308 |
-#endif /* USE_MCAST */ |
|
309 |
- |
|
310 |
-/** get string for known protocols. |
|
311 |
- * @param iproto - protocol number |
|
312 |
- * @param utype - 1 if result is used for URI, or 0 |
|
313 |
- * @param vtype - 1 if result is wanted uppercase, or 0 for lowercase |
|
314 |
- * @param sproto - the string for the proto |
|
315 |
- * @return 0 if it is a valid and supported protocol, negative otherwise |
|
316 |
- */ |
|
317 |
-int get_valid_proto_string(unsigned int iproto, int utype, int vtype, |
|
318 |
- str *sproto) |
|
319 |
-{ |
|
320 |
- switch(iproto){ |
|
321 |
- case PROTO_NONE: |
|
322 |
- return -1; |
|
323 |
- case PROTO_UDP: |
|
324 |
- sproto->len = 3; |
|
325 |
- sproto->s = (vtype)?"UDP":"udp"; |
|
326 |
- return 0; |
|
327 |
- case PROTO_TCP: |
|
328 |
- sproto->len = 3; |
|
329 |
- sproto->s = (vtype)?"TCP":"tcp"; |
|
330 |
- return 0; |
|
331 |
- case PROTO_TLS: |
|
332 |
- sproto->len = 3; |
|
333 |
- sproto->s = (vtype)?"TLS":"tls"; |
|
334 |
- return 0; |
|
335 |
- case PROTO_SCTP: |
|
336 |
- sproto->len = 4; |
|
337 |
- sproto->s = (vtype)?"SCTP":"sctp"; |
|
338 |
- return 0; |
|
339 |
- case PROTO_WS: |
|
340 |
- case PROTO_WSS: |
|
341 |
- if(iproto==PROTO_WS || utype) { |
|
342 |
- /* ws-only in SIP URI */ |
|
343 |
- sproto->len = 2; |
|
344 |
- sproto->s = (vtype)?"WS":"ws"; |
|
345 |
- } else { |
|
346 |
- sproto->len = 3; |
|
347 |
- sproto->s = (vtype)?"WSS":"wss"; |
|
348 |
- } |
|
349 |
- return 0; |
|
350 |
- default: |
|
351 |
- return -2; |
|
352 |
- } |
|
353 |
-} |
|
354 |
- |
|
355 |
-/** get protocol name (asciiz). |
|
356 |
- * @param proto - protocol number |
|
357 |
- * @return string with the protocol name or "unknown". |
|
358 |
- */ |
|
359 |
-char* get_proto_name(unsigned int proto) |
|
360 |
-{ |
|
361 |
- str sproto; |
|
362 |
- switch(proto){ |
|
363 |
- case PROTO_NONE: |
|
364 |
- return "*"; |
|
365 |
- default: |
|
366 |
- if(get_valid_proto_string(proto, 1, 0, &sproto)<0) |
|
367 |
- return "unknown"; |
|
368 |
- return sproto.s; |
|
369 |
- } |
|
370 |
-} |
|
371 |
- |
|
372 |
- |
|
373 |
-/** |
|
374 |
- * match ip address with net address and bitmask |
|
375 |
- * - return 0 on match, -1 otherwise |
|
376 |
- */ |
|
377 |
-int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr, |
|
378 |
- int mask) |
|
379 |
-{ |
|
380 |
- unsigned char ci; |
|
381 |
- unsigned char cn; |
|
382 |
- int i; |
|
383 |
- int mbytes; |
|
384 |
- int mbits; |
|
385 |
- |
|
386 |
- if(mask==0) |
|
387 |
- return 0; |
|
388 |
- if(iaddr==NULL || naddr==NULL || mask<0) |
|
389 |
- return -1; |
|
390 |
- if(iaddr->af != naddr->af) |
|
391 |
- return -1; |
|
392 |
- |
|
393 |
- if(iaddr->af == AF_INET) |
|
394 |
- { |
|
395 |
- if(mask>32) |
|
396 |
- return -1; |
|
397 |
- if(mask==32) |
|
398 |
- { |
|
399 |
- if(ip_addr_cmp(iaddr, naddr)) |
|
400 |
- return 0; |
|
401 |
- return -1; |
|
402 |
- } |
|
403 |
- } else if(iaddr->af == AF_INET6) { |
|
404 |
- if(mask>128) |
|
405 |
- return -1; |
|
406 |
- |
|
407 |
- if(mask==128) |
|
408 |
- { |
|
409 |
- if(ip_addr_cmp(iaddr, naddr)) |
|
410 |
- return 0; |
|
411 |
- return -1; |
|
412 |
- } |
|
413 |
- } |
|
414 |
- |
|
415 |
- mbytes = mask / 8; |
|
416 |
- for(i=0; i<mbytes; i++) |
|
417 |
- { |
|
418 |
- if(iaddr->u.addr[i] != naddr->u.addr[i]) |
|
419 |
- return -1; |
|
420 |
- } |
|
421 |
- mbits = mask % 8; |
|
422 |
- if(mbits==0) |
|
423 |
- return 0; |
|
424 |
- ci = iaddr->u.addr[i] & (~((1 << (8 - mbits)) - 1)); |
|
425 |
- cn = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1)); |
|
426 |
- if(ci == cn) |
|
427 |
- return 0; |
|
428 |
- return -1; |
|
429 |
-} |
|
430 |
- |
|
431 |
-int si_get_signaling_data(struct socket_info *si, str **addr, str **port) |
|
432 |
-{ |
|
433 |
- if(si==NULL) |
|
434 |
- return -1; |
|
435 |
- if(addr) { |
|
436 |
- if(si->useinfo.name.len>0) { |
|
437 |
- *addr = &si->useinfo.name; |
|
438 |
- } else { |
|
439 |
- *addr = &si->address_str; |
|
440 |
- } |
|
441 |
- } |
|
442 |
- if(port) { |
|
443 |
- if(si->useinfo.port_no>0) { |
|
444 |
- *port = &si->useinfo.port_no_str; |
|
445 |
- } else { |
|
446 |
- *port = &si->port_no_str; |
|
447 |
- } |
|
448 |
- } |
|
449 |
- return 0; |
|
450 |
-} |
- reported by Kyle Kurz <kkurz@digium.com> for permissions module
... | ... |
@@ -377,7 +377,8 @@ char* get_proto_name(unsigned int proto) |
377 | 377 |
int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr, |
378 | 378 |
int mask) |
379 | 379 |
{ |
380 |
- unsigned char c; |
|
380 |
+ unsigned char ci; |
|
381 |
+ unsigned char cn; |
|
381 | 382 |
int i; |
382 | 383 |
int mbytes; |
383 | 384 |
int mbits; |
... | ... |
@@ -420,8 +421,9 @@ int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr, |
420 | 421 |
mbits = mask % 8; |
421 | 422 |
if(mbits==0) |
422 | 423 |
return 0; |
423 |
- c = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1)); |
|
424 |
- if((iaddr->u.addr[i] & c) == c) |
|
424 |
+ ci = iaddr->u.addr[i] & (~((1 << (8 - mbits)) - 1)); |
|
425 |
+ cn = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1)); |
|
426 |
+ if(ci == cn) |
|
425 | 427 |
return 0; |
426 | 428 |
return -1; |
427 | 429 |
} |
- returns variants for uppercase and lowecase, representation for uri or
bare proto: udp, tcp, tls, sctp, ws and wss
- in uri, PROTO_WS and PROTO_WSS are represented as transport=ws
... | ... |
@@ -307,7 +307,50 @@ int is_mcast(struct ip_addr* ip) |
307 | 307 |
|
308 | 308 |
#endif /* USE_MCAST */ |
309 | 309 |
|
310 |
- |
|
310 |
+/** get string for known protocols. |
|
311 |
+ * @param iproto - protocol number |
|
312 |
+ * @param utype - 1 if result is used for URI, or 0 |
|
313 |
+ * @param vtype - 1 if result is wanted uppercase, or 0 for lowercase |
|
314 |
+ * @param sproto - the string for the proto |
|
315 |
+ * @return 0 if it is a valid and supported protocol, negative otherwise |
|
316 |
+ */ |
|
317 |
+int get_valid_proto_string(unsigned int iproto, int utype, int vtype, |
|
318 |
+ str *sproto) |
|
319 |
+{ |
|
320 |
+ switch(iproto){ |
|
321 |
+ case PROTO_NONE: |
|
322 |
+ return -1; |
|
323 |
+ case PROTO_UDP: |
|
324 |
+ sproto->len = 3; |
|
325 |
+ sproto->s = (vtype)?"UDP":"udp"; |
|
326 |
+ return 0; |
|
327 |
+ case PROTO_TCP: |
|
328 |
+ sproto->len = 3; |
|
329 |
+ sproto->s = (vtype)?"TCP":"tcp"; |
|
330 |
+ return 0; |
|
331 |
+ case PROTO_TLS: |
|
332 |
+ sproto->len = 3; |
|
333 |
+ sproto->s = (vtype)?"TLS":"tls"; |
|
334 |
+ return 0; |
|
335 |
+ case PROTO_SCTP: |
|
336 |
+ sproto->len = 4; |
|
337 |
+ sproto->s = (vtype)?"SCTP":"sctp"; |
|
338 |
+ return 0; |
|
339 |
+ case PROTO_WS: |
|
340 |
+ case PROTO_WSS: |
|
341 |
+ if(iproto==PROTO_WS || utype) { |
|
342 |
+ /* ws-only in SIP URI */ |
|
343 |
+ sproto->len = 2; |
|
344 |
+ sproto->s = (vtype)?"WS":"ws"; |
|
345 |
+ } else { |
|
346 |
+ sproto->len = 3; |
|
347 |
+ sproto->s = (vtype)?"WSS":"wss"; |
|
348 |
+ } |
|
349 |
+ return 0; |
|
350 |
+ default: |
|
351 |
+ return -2; |
|
352 |
+ } |
|
353 |
+} |
|
311 | 354 |
|
312 | 355 |
/** get protocol name (asciiz). |
313 | 356 |
* @param proto - protocol number |
... | ... |
@@ -315,22 +358,14 @@ int is_mcast(struct ip_addr* ip) |
315 | 358 |
*/ |
316 | 359 |
char* get_proto_name(unsigned int proto) |
317 | 360 |
{ |
361 |
+ str sproto; |
|
318 | 362 |
switch(proto){ |
319 | 363 |
case PROTO_NONE: |
320 | 364 |
return "*"; |
321 |
- case PROTO_UDP: |
|
322 |
- return "udp"; |
|
323 |
- case PROTO_TCP: |
|
324 |
- return "tcp"; |
|
325 |
- case PROTO_TLS: |
|
326 |
- return "tls"; |
|
327 |
- case PROTO_SCTP: |
|
328 |
- return "sctp"; |
|
329 |
- case PROTO_WS: |
|
330 |
- case PROTO_WSS: |
|
331 |
- return "ws"; |
|
332 | 365 |
default: |
333 |
- return "unknown"; |
|
366 |
+ if(get_valid_proto_string(proto, 1, 0, &sproto)<0) |
|
367 |
+ return "unknown"; |
|
368 |
+ return sproto.s; |
|
334 | 369 |
} |
335 | 370 |
} |
336 | 371 |
|
... | ... |
@@ -10,11 +10,6 @@ |
10 | 10 |
* the Free Software Foundation; either version 2 of the License, or |
11 | 11 |
* (at your option) any later version |
12 | 12 |
* |
13 |
- * For a license to use the ser software under conditions |
|
14 |
- * other than those described here, or to purchase support for this |
|
15 |
- * software, please contact iptel.org by e-mail at the following addresses: |
|
16 |
- * info@iptel.org |
|
17 |
- * |
|
18 | 13 |
* Kamailio is distributed in the hope that it will be useful, |
19 | 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
... | ... |
@@ -1,14 +1,11 @@ |
1 | 1 |
/* |
2 |
- * $Id$ |
|
3 |
- * |
|
4 |
- * |
|
5 | 2 |
* ip address & address family related functions |
6 | 3 |
* |
7 | 4 |
* Copyright (C) 2001-2003 FhG Fokus |
8 | 5 |
* |
9 |
- * This file is part of ser, a free SIP server. |
|
6 |
+ * This file is part of Kamailio, a free SIP server. |
|
10 | 7 |
* |
11 |
- * ser is free software; you can redistribute it and/or modify |
|
8 |
+ * Kamailio is free software; you can redistribute it and/or modify |
|
12 | 9 |
* it under the terms of the GNU General Public License as published by |
13 | 10 |
* the Free Software Foundation; either version 2 of the License, or |
14 | 11 |
* (at your option) any later version |
... | ... |
@@ -18,7 +15,7 @@ |
18 | 15 |
* software, please contact iptel.org by e-mail at the following addresses: |
19 | 16 |
* info@iptel.org |
20 | 17 |
* |
21 |
- * ser is distributed in the hope that it will be useful, |
|
18 |
+ * Kamailio is distributed in the hope that it will be useful, |
|
22 | 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24 | 21 |
* GNU General Public License for more details. |
... | ... |
@@ -27,14 +24,8 @@ |
27 | 24 |
* along with this program; if not, write to the Free Software |
28 | 25 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
29 | 26 |
*/ |
30 |
-/* |
|
31 |
- * History: |
|
32 |
- * -------- |
|
33 |
- * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free |
|
34 |
- * 2004-10-01 mk_net fixes bad network addresses now (andrei) |
|
35 |
- */ |
|
36 | 27 |
|
37 |
-/** inernal ip addresses representation functions. |
|
28 |
+/** Kamailio core :: internal ip addresses representation functions. |
|
38 | 29 |
* @file ip_addr.c |
39 | 30 |
* @ingroup core |
40 | 31 |
* Module: @ref core |
... | ... |
@@ -59,13 +59,13 @@ struct net* mk_new_net(struct ip_addr* ip, struct ip_addr* mask) |
59 | 59 |
|
60 | 60 |
warning=0; |
61 | 61 |
if ((ip->af != mask->af) || (ip->len != mask->len)){ |
62 |
- LOG(L_CRIT, "ERROR: mk_net: trying to use a different mask family" |
|
62 |
+ LM_CRIT("trying to use a different mask family" |
|
63 | 63 |
" (eg. ipv4/ipv6mask or ipv6/ipv4mask)\n"); |
64 | 64 |
goto error; |
65 | 65 |
} |
66 | 66 |
n=(struct net*)pkg_malloc(sizeof(struct net)); |
67 | 67 |
if (n==0){ |
68 |
- LOG(L_CRIT, "ERROR: mk_net: memory allocation failure\n"); |
|
68 |
+ LM_CRIT("memory allocation failure\n"); |
|
69 | 69 |
goto error; |
70 | 70 |
} |
71 | 71 |
n->ip=*ip; |
... | ... |
@@ -75,7 +75,7 @@ struct net* mk_new_net(struct ip_addr* ip, struct ip_addr* mask) |
75 | 75 |
if (n->ip.u.addr32[r]!=ip->u.addr32[r]) warning=1; |
76 | 76 |
}; |
77 | 77 |
if (warning){ |
78 |
- LOG(L_WARN, "WARNING: mk_net: invalid network address/netmask " |
|
78 |
+ LM_WARN("invalid network address/netmask " |
|
79 | 79 |
"combination fixed...\n"); |
80 | 80 |
print_ip("original network address:", ip, "/"); |
81 | 81 |
print_ip("", mask, "\n"); |
... | ... |
@@ -95,7 +95,7 @@ struct net* mk_new_net_bitlen(struct ip_addr* ip, unsigned int bitlen) |
95 | 95 |
int r; |
96 | 96 |
|
97 | 97 |
if (bitlen>ip->len*8){ |
98 |
- LOG(L_CRIT, "ERROR: mk_net_bitlen: bad bitlen number %d\n", bitlen); |
|
98 |
+ LM_CRIT("bad bitlen number %d\n", bitlen); |
|
99 | 99 |
goto error; |
100 | 100 |
} |
101 | 101 |
memset(&mask,0, sizeof(mask)); |
... | ... |
@@ -292,7 +292,7 @@ void stdout_print_ip(struct ip_addr* ip) |
292 | 292 |
void print_net(struct net* net) |
293 | 293 |
{ |
294 | 294 |
if (net==0){ |
295 |
- LOG(L_WARN, "ERROR: print net: null pointer\n"); |
|
295 |
+ LM_WARN("null pointer\n"); |
|
296 | 296 |
return; |
297 | 297 |
} |
298 | 298 |
print_ip("", &net->ip, "/"); print_ip("", &net->mask, ""); |
... | ... |
@@ -305,7 +305,7 @@ void print_net(struct net* net) |
305 | 305 |
int is_mcast(struct ip_addr* ip) |
306 | 306 |
{ |
307 | 307 |
if (!ip){ |
308 |
- LOG(L_ERR, "ERROR: is_mcast: Invalid parameter value\n"); |
|
308 |
+ LM_ERR("Invalid parameter value\n"); |
|
309 | 309 |
return -1; |
310 | 310 |
} |
311 | 311 |
|
... | ... |
@@ -314,7 +314,7 @@ int is_mcast(struct ip_addr* ip) |
314 | 314 |
} else if (ip->af==AF_INET6){ |
315 | 315 |
return IN6_IS_ADDR_MULTICAST((struct in6_addr*)ip->u.addr32); |
316 | 316 |
} else { |
317 |
- LOG(L_ERR, "ERROR: is_mcast: Unsupported protocol family\n"); |
|
317 |
+ LM_ERR("Unsupported protocol family\n"); |
|
318 | 318 |
return -1; |
319 | 319 |
} |
320 | 320 |
} |
... | ... |
@@ -404,3 +404,24 @@ int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr, |
404 | 404 |
return 0; |
405 | 405 |
return -1; |
406 | 406 |
} |
407 |
+ |
|
408 |
+int si_get_signaling_data(struct socket_info *si, str **addr, str **port) |
|
409 |
+{ |
|
410 |
+ if(si==NULL) |
|
411 |
+ return -1; |
|
412 |
+ if(addr) { |
|
413 |
+ if(si->useinfo.name.len>0) { |
|
414 |
+ *addr = &si->useinfo.name; |
|
415 |
+ } else { |
|
416 |
+ *addr = &si->address_str; |
|
417 |
+ } |
|
418 |
+ } |
|
419 |
+ if(port) { |
|
420 |
+ if(si->useinfo.port_no>0) { |
|
421 |
+ *port = &si->useinfo.port_no_str; |
|
422 |
+ } else { |
|
423 |
+ *port = &si->port_no_str; |
|
424 |
+ } |
|
425 |
+ } |
|
426 |
+ return 0; |
|
427 |
+} |
... | ... |
@@ -25,7 +25,7 @@ |
25 | 25 |
* |
26 | 26 |
* You should have received a copy of the GNU General Public License |
27 | 27 |
* along with this program; if not, write to the Free Software |
28 |
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
28 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
29 | 29 |
*/ |
30 | 30 |
/* |
31 | 31 |
* History: |
* Make IPv6 the default in the core and affected modules
* it has been default switched on since a long time, and was introduced in 2002
* even on embedded systems one probably want now proper IPv6 support
* there was an issue in cygwin in 2008, but IPv6 is there also available since v1.7
* remove over 160 #ifdefs, cleanup the code a lot and removes many of rarely
tested alternative code paths to ease support of the codebase
* note for gentoo maintainer: please review your packages, they will maybe not
work now correctly anymore if somebody specified -ipv6 in the use flags
... | ... |
@@ -184,10 +184,8 @@ int mk_net_str(struct net* dst, str* s) |
184 | 184 |
|
185 | 185 |
/* test for ip only */ |
186 | 186 |
t = str2ip(s); |
187 |
-#ifdef USE_IPV6 |
|
188 | 187 |
if (unlikely(t == 0)) |
189 | 188 |
t = str2ip6(s); |
190 |
-#endif /* USE_IPV6 */ |
|
191 | 189 |
if (likely(t)) |
192 | 190 |
return mk_net_bitlen(dst, t, t->len*8); |
193 | 191 |
/* not a simple ip, maybe an ip/netmask pair */ |
... | ... |
@@ -212,7 +210,6 @@ int mk_net_str(struct net* dst, str* s) |
212 | 210 |
/* error */ |
213 | 211 |
return -1; |
214 | 212 |
} |
215 |
-#ifdef USE_IPV6 |
|
216 | 213 |
else { |
217 | 214 |
t = str2ip6(&addr); |
218 | 215 |
if (likely(t)) { |
... | ... |
@@ -227,7 +224,6 @@ int mk_net_str(struct net* dst, str* s) |
227 | 224 |
return -1; |
228 | 225 |
} |
229 | 226 |
} |
230 |
-#endif /* USE_IPV6 */ |
|
231 | 227 |
} |
232 | 228 |
return -1; |
233 | 229 |
} |
... | ... |
@@ -246,7 +242,6 @@ void print_ip(char* p, struct ip_addr* ip, char *s) |
246 | 242 |
(s)?s:"" |
247 | 243 |
); |
248 | 244 |
break; |
249 |
-#ifdef USE_IPV6 |
|
250 | 245 |
case AF_INET6: |
251 | 246 |
DBG("%s%x:%x:%x:%x:%x:%x:%x:%x%s", (p)?p:"", |
252 | 247 |
htons(ip->u.addr16[0]), |
... | ... |
@@ -260,7 +255,6 @@ void print_ip(char* p, struct ip_addr* ip, char *s) |
260 | 255 |
(s)?s:"" |
261 | 256 |
); |
262 | 257 |
break; |
263 |
-#endif /* USE_IPV6 */ |
|
264 | 258 |
default: |
265 | 259 |
DBG("print_ip: warning unknown address family %d\n", ip->af); |
266 | 260 |
} |
... | ... |
@@ -277,7 +271,6 @@ void stdout_print_ip(struct ip_addr* ip) |
277 | 271 |
ip->u.addr[2], |
278 | 272 |
ip->u.addr[3]); |
279 | 273 |
break; |
280 |
-#ifdef USE_IPV6 |
|
281 | 274 |
case AF_INET6: |
282 | 275 |
printf("%x:%x:%x:%x:%x:%x:%x:%x", htons(ip->u.addr16[0]), |
283 | 276 |
htons(ip->u.addr16[1]), |
... | ... |
@@ -289,7 +282,6 @@ void stdout_print_ip(struct ip_addr* ip) |
289 | 282 |
htons(ip->u.addr16[7]) |
290 | 283 |
); |
291 | 284 |
break; |
292 |
-#endif /* USE_IPV6 */ |
|
293 | 285 |
default: |
294 | 286 |
DBG("print_ip: warning unknown address family %d\n", ip->af); |
295 | 287 |
} |
... | ... |
@@ -319,10 +311,8 @@ int is_mcast(struct ip_addr* ip) |
319 | 311 |
|
320 | 312 |
if (ip->af==AF_INET){ |
321 | 313 |
return IN_MULTICAST(htonl(ip->u.addr32[0])); |
322 |
-#ifdef USE_IPV6 |
|
323 | 314 |
} else if (ip->af==AF_INET6){ |
324 | 315 |
return IN6_IS_ADDR_MULTICAST((struct in6_addr*)ip->u.addr32); |
325 |
-#endif /* USE_IPV6 */ |
|
326 | 316 |
} else { |
327 | 317 |
LOG(L_ERR, "ERROR: is_mcast: Unsupported protocol family\n"); |
328 | 318 |
return -1; |
- It should always be ";transport=ws" with WebSocket
- Bound to have missed something and lots of testing required.
... | ... |
@@ -354,3 +354,60 @@ char* get_proto_name(unsigned int proto) |
354 | 354 |
return "unknown"; |
355 | 355 |
} |
356 | 356 |
} |
357 |
+ |
|
358 |
+ |
|
359 |
+/** |
|
360 |
+ * match ip address with net address and bitmask |
|
361 |
+ * - return 0 on match, -1 otherwise |
|
362 |
+ */ |
|
363 |
+int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr, |
|
364 |
+ int mask) |
|
365 |
+{ |
|
366 |
+ unsigned char c; |
|
367 |
+ int i; |
|
368 |
+ int mbytes; |
|
369 |
+ int mbits; |
|
370 |
+ |
|
371 |
+ if(mask==0) |
|
372 |
+ return 0; |
|
373 |
+ if(iaddr==NULL || naddr==NULL || mask<0) |
|
374 |
+ return -1; |
|
375 |
+ if(iaddr->af != naddr->af) |
|
376 |
+ return -1; |
|
377 |
+ |
|
378 |
+ if(iaddr->af == AF_INET) |
|
379 |
+ { |
|
380 |
+ if(mask>32) |
|
381 |
+ return -1; |
|
382 |
+ if(mask==32) |
|
383 |
+ { |
|
384 |
+ if(ip_addr_cmp(iaddr, naddr)) |
|
385 |
+ return 0; |
|
386 |
+ return -1; |
|
387 |
+ } |
|
388 |
+ } else if(iaddr->af == AF_INET6) { |
|
389 |
+ if(mask>128) |
|
390 |
+ return -1; |
|
391 |
+ |
|
392 |
+ if(mask==128) |
|
393 |
+ { |
|
394 |
+ if(ip_addr_cmp(iaddr, naddr)) |
|
395 |
+ return 0; |
|
396 |
+ return -1; |
|
397 |
+ } |
|
398 |
+ } |
|
399 |
+ |
|
400 |
+ mbytes = mask / 8; |
|
401 |
+ for(i=0; i<mbytes; i++) |
|
402 |
+ { |
|
403 |
+ if(iaddr->u.addr[i] != naddr->u.addr[i]) |
|
404 |
+ return -1; |
|
405 |
+ } |
|
406 |
+ mbits = mask % 8; |
|
407 |
+ if(mbits==0) |
|
408 |
+ return 0; |
|
409 |
+ c = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1)); |
|
410 |
+ if((iaddr->u.addr[i] & c) == c) |
|
411 |
+ return 0; |
|
412 |
+ return -1; |
|
413 |
+} |
... | ... |
@@ -337,7 +337,7 @@ int is_mcast(struct ip_addr* ip) |
337 | 337 |
* @param proto - protocol number |
338 | 338 |
* @return string with the protocol name or "unknown". |
339 | 339 |
*/ |
340 |
-char* proto2a(enum sip_protos proto) |
|
340 |
+char* get_proto_name(unsigned int proto) |
|
341 | 341 |
{ |
342 | 342 |
switch(proto){ |
343 | 343 |
case PROTO_NONE: |
... | ... |
@@ -350,8 +350,7 @@ char* proto2a(enum sip_protos proto) |
350 | 350 |
return "tls"; |
351 | 351 |
case PROTO_SCTP: |
352 | 352 |
return "sctp"; |
353 |
- case PROTO_OTHER: |
|
354 |
- return "other"; |
|
353 |
+ default: |
|
354 |
+ return "unknown"; |
|
355 | 355 |
} |
356 |
- return "unknown"; |
|
357 | 356 |
} |
Added a new function to ip_addr.h, for converting the internal
protocol number to an ascii name (proto2a(...)).
... | ... |
@@ -330,3 +330,26 @@ int is_mcast(struct ip_addr* ip) |
330 | 330 |
} |
331 | 331 |
|
332 | 332 |
#endif /* USE_MCAST */ |
333 |
+ |
|
334 |
+ |
|
335 |
+ |
|
336 |
+/** get protocol name (asciiz). |
|
337 |
+ * @param proto - protocol number |
|
338 |
+ * @return string with the protocol name or "unknown". |
|
339 |
+ */ |
|
340 |
+char* proto2a(enum sip_protos proto) |
|
341 |
+{ |
|
342 |
+ switch(proto){ |
|
343 |
+ case PROTO_NONE: |
|
344 |
+ return "*"; |
|
345 |
+ case PROTO_UDP: |
|