core: unified function for returning string of a protocol
[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 /** 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 c;
381         int i;
382         int mbytes;
383         int mbits;
384
385         if(mask==0)
386                 return 0;
387         if(iaddr==NULL || naddr==NULL || mask<0)
388                 return -1;
389         if(iaddr->af != naddr->af)
390                 return -1;
391
392         if(iaddr->af == AF_INET)
393         {
394                 if(mask>32)
395                         return -1;
396                 if(mask==32)
397                 {
398                         if(ip_addr_cmp(iaddr, naddr))
399                                 return 0;
400                         return -1;
401                 }
402         } else if(iaddr->af ==  AF_INET6) {
403                 if(mask>128)
404                         return -1;
405
406                 if(mask==128)
407                 {
408                         if(ip_addr_cmp(iaddr, naddr))
409                                 return 0;
410                         return -1;
411                 }
412         }
413
414         mbytes = mask / 8;
415         for(i=0; i<mbytes; i++)
416         {
417                 if(iaddr->u.addr[i] != naddr->u.addr[i])
418                         return -1;
419         }
420         mbits = mask % 8;
421         if(mbits==0)
422                 return 0;
423         c = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1));
424         if((iaddr->u.addr[i] & c) == c)
425                 return 0;
426         return -1;
427 }
428
429 int si_get_signaling_data(struct socket_info *si, str **addr, str **port)
430 {
431         if(si==NULL)
432                 return -1;
433         if(addr) {
434                 if(si->useinfo.name.len>0) {
435                         *addr = &si->useinfo.name;
436                 } else {
437                         *addr = &si->address_str;
438                 }
439         }
440         if(port) {
441                 if(si->useinfo.port_no>0) {
442                         *port = &si->useinfo.port_no_str;
443                 } else {
444                         *port = &si->port_no_str;
445                 }
446         }
447         return 0;
448 }