0ca959ff54d5331a8627be1e58997884b4b1b752
[sip-router] / ip_addr.c
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
311
312 /** get protocol name (asciiz).
313  * @param proto - protocol number
314  * @return  string with the protocol name or "unknown".
315  */
316 char* get_proto_name(unsigned int proto)
317 {
318         switch(proto){
319                 case PROTO_NONE:
320                         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                 default:
333                         return "unknown";
334         }
335 }
336
337
338 /**
339  * match ip address with net address and bitmask
340  * - return 0 on match, -1 otherwise
341  */
342 int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr,
343                 int mask)
344 {
345         unsigned char c;
346         int i;
347         int mbytes;
348         int mbits;
349
350         if(mask==0)
351                 return 0;
352         if(iaddr==NULL || naddr==NULL || mask<0)
353                 return -1;
354         if(iaddr->af != naddr->af)
355                 return -1;
356
357         if(iaddr->af == AF_INET)
358         {
359                 if(mask>32)
360                         return -1;
361                 if(mask==32)
362                 {
363                         if(ip_addr_cmp(iaddr, naddr))
364                                 return 0;
365                         return -1;
366                 }
367         } else if(iaddr->af ==  AF_INET6) {
368                 if(mask>128)
369                         return -1;
370
371                 if(mask==128)
372                 {
373                         if(ip_addr_cmp(iaddr, naddr))
374                                 return 0;
375                         return -1;
376                 }
377         }
378
379         mbytes = mask / 8;
380         for(i=0; i<mbytes; i++)
381         {
382                 if(iaddr->u.addr[i] != naddr->u.addr[i])
383                         return -1;
384         }
385         mbits = mask % 8;
386         if(mbits==0)
387                 return 0;
388         c = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1));
389         if((iaddr->u.addr[i] & c) == c)
390                 return 0;
391         return -1;
392 }
393
394 int si_get_signaling_data(struct socket_info *si, str **addr, str **port)
395 {
396         if(si==NULL)
397                 return -1;
398         if(addr) {
399                 if(si->useinfo.name.len>0) {
400                         *addr = &si->useinfo.name;
401                 } else {
402                         *addr = &si->address_str;
403                 }
404         }
405         if(port) {
406                 if(si->useinfo.port_no>0) {
407                         *port = &si->useinfo.port_no_str;
408                 } else {
409                         *port = &si->port_no_str;
410                 }
411         }
412         return 0;
413 }