ip_addr.c
4e2fdd79
 /*
  * ip address & address family related functions
7dd0b342
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
6a0f4382
  * This file is part of Kamailio, a free SIP server.
7dd0b342
  *
6a0f4382
  * Kamailio is free software; you can redistribute it and/or modify
7dd0b342
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
6a0f4382
  * Kamailio is distributed in the hope that it will be useful,
7dd0b342
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
4e2fdd79
  */
 
6a0f4382
 /** Kamailio core :: internal ip addresses representation functions.
dc4cdb29
  * @file ip_addr.c
  * @ingroup core
  * Module: @ref core
1d0661db
  */
 
7dd0b342
 
381659ac
 #include <stdlib.h>
4e2fdd79
 #include <stdio.h>
 
 #include "ip_addr.h"
 #include "dprint.h"
e3dccdc9
 #include "mem/mem.h"
dc4cdb29
 #include "resolve.h"
 #include "trim.h"
4e2fdd79
 
 
dc4cdb29
 struct net* mk_new_net(struct ip_addr* ip, struct ip_addr* mask)
4e2fdd79
 {
 	struct net* n;
092bc818
 	int warning;
 	int r;
4e2fdd79
 	
092bc818
 	warning=0;
4e2fdd79
 	if ((ip->af != mask->af) || (ip->len != mask->len)){
36d0ecf4
 		LM_CRIT("trying to use a different mask family"
4e2fdd79
 				" (eg. ipv4/ipv6mask or ipv6/ipv4mask)\n");
 		goto error;
 	}
e3dccdc9
 	n=(struct net*)pkg_malloc(sizeof(struct net));
4e2fdd79
 	if (n==0){ 
36d0ecf4
 		LM_CRIT("memory allocation failure\n");
4e2fdd79
 		goto error;
 	}
 	n->ip=*ip;
 	n->mask=*mask;
092bc818
 	for (r=0; r<n->ip.len/4; r++) { /*ipv4 & ipv6 addresses are multiple of 4*/
 		n->ip.u.addr32[r] &= n->mask.u.addr32[r];
 		if (n->ip.u.addr32[r]!=ip->u.addr32[r]) warning=1;
 	};
 	if (warning){
36d0ecf4
 		LM_WARN("invalid network address/netmask "
092bc818
 					"combination fixed...\n");
 		print_ip("original network address:", ip, "/");
 		print_ip("", mask, "\n");
 		print_ip("fixed    network address:", &(n->ip), "/");
 		print_ip("", &(n->mask), "\n");
 	};
4e2fdd79
 	return n;
 error:
 	return 0;
 }
 
 
 
dc4cdb29
 struct net* mk_new_net_bitlen(struct ip_addr* ip, unsigned int bitlen)
4e2fdd79
 {
092bc818
 	struct ip_addr mask;
4e2fdd79
 	int r;
 	
 	if (bitlen>ip->len*8){
36d0ecf4
 		LM_CRIT("bad bitlen number %d\n", bitlen);
4e2fdd79
 		goto error;
 	}
092bc818
 	memset(&mask,0, sizeof(mask));
 	for (r=0;r<bitlen/8;r++) mask.u.addr[r]=0xff;
 	if (bitlen%8) mask.u.addr[r]=  ~((1<<(8-(bitlen%8)))-1);
 	mask.af=ip->af;
 	mask.len=ip->len;
4e2fdd79
 	
dc4cdb29
 	return mk_new_net(ip, &mask);
4e2fdd79
 error:
 	return 0;
 }
 
 
 
dc4cdb29
 /** fills a net structure from an ip and a mask.
  *
  * This function will not print any error messages or allocate
  * memory (as opposed to mk_new_net() above).
  *
  * @param n - destination net structure
  * @param ip
  * @param mask
  * @return -1 on error (af mismatch), 0 on success
  */
 int mk_net(struct net* n, struct ip_addr* ip, struct ip_addr* mask)
 {
 	int r;
 	
 	if (unlikely((ip->af != mask->af) || (ip->len != mask->len))) {
 		return -1;
 	}
 	n->ip=*ip;
 	n->mask=*mask;
 	/* fix the network part of the mask */
 	for (r=0; r<n->ip.len/4; r++) { /*ipv4 & ipv6 addresses are multiple of 4*/
 		n->ip.u.addr32[r] &= n->mask.u.addr32[r];
 	};
 	return 0;
 }
 
 
 
 /** fills a net structure from an ip and a bitlen.
  *
  * This function will not print any error messages or allocate
  * memory (as opposed to mk_new_net_bitlen() above).
  *
  * @param n - destination net structure
  * @param ip
  * @param bitlen
  * @return -1 on error (af mismatch), 0 on success
  */
 int mk_net_bitlen(struct net* n, struct ip_addr* ip, unsigned int bitlen)
 {
 	struct ip_addr mask;
 	int r;
 	
 	if (unlikely(bitlen>ip->len*8))
 		/* bitlen too big */
 		return -1;
 	memset(&mask,0, sizeof(mask));
 	for (r=0;r<bitlen/8;r++) mask.u.addr[r]=0xff;
 	if (bitlen%8) mask.u.addr[r]=  ~((1<<(8-(bitlen%8)))-1);
 	mask.af=ip->af;
 	mask.len=ip->len;
 	
 	return mk_net(n, ip, &mask);
 }
 
 
 
 /** initializes a net structure from a string.
  * @param dst - net structure that will be filled
  * @param s - string of the form "ip", "ip/mask_len" or "ip/ip_mak".
  * @return -1 on error, 0 on succes
  */
 int mk_net_str(struct net* dst, str* s)
 {
 	struct ip_addr* t;
 	char* p;
 	struct ip_addr ip;
 	str addr;
 	str mask;
 	unsigned int bitlen;
 	
 	/* test for ip only */
 	t = str2ip(s);
 	if (unlikely(t == 0))
 		t = str2ip6(s);
 	if (likely(t))
 		return mk_net_bitlen(dst, t, t->len*8);
 	/* not a simple ip, maybe an ip/netmask pair */
 	p = q_memchr(s->s, '/', s->len);
 	if (likely(p)) {
 		addr.s = s->s;
 		addr.len = (int)(long)(p - s->s);
 		mask.s = p + 1;
 		mask.len = s->len - (addr.len + 1);
 		/* allow '/' enclosed by whitespace */
 		trim_trailing(&addr);
 		trim_leading(&mask);
 		t = str2ip(&addr);
 		if (likely(t)) {
 			/* it can be a number */
 			if (str2int(&mask, &bitlen) == 0)
 				return mk_net_bitlen(dst, t, bitlen);
 			ip = *t;
 			t = str2ip(&mask);
 			if (likely(t))
 				return mk_net(dst, &ip, t);
 			/* error */
 			return -1;
 		}
 		else {
 			t = str2ip6(&addr);
 			if (likely(t)) {
 				/* it can be a number */
 				if (str2int(&mask, &bitlen) == 0)
 					return mk_net_bitlen(dst, t, bitlen);
 				ip = *t;
 				t = str2ip6(&mask);
 				if (likely(t))
 					return mk_net(dst, &ip, t);
 				/* error */
 				return -1;
 			}
 		}
 	}
 	return -1;
 }
 
 
 
9c01c860
 void print_ip(char* p, struct ip_addr* ip, char *s)
4e2fdd79
 {
 	switch(ip->af){
 		case AF_INET:
9c01c860
 			DBG("%s%d.%d.%d.%d%s", (p)?p:"",
 								ip->u.addr[0],
4e2fdd79
 								ip->u.addr[1],
 								ip->u.addr[2],
9c01c860
 								ip->u.addr[3],
 								(s)?s:""
 								);
4e2fdd79
 			break;
 		case AF_INET6:
9c01c860
 			DBG("%s%x:%x:%x:%x:%x:%x:%x:%x%s", (p)?p:"",
 											htons(ip->u.addr16[0]),
4e2fdd79
 											htons(ip->u.addr16[1]),
 											htons(ip->u.addr16[2]),
 											htons(ip->u.addr16[3]),
 											htons(ip->u.addr16[4]),
 											htons(ip->u.addr16[5]),
 											htons(ip->u.addr16[6]),
9c01c860
 											htons(ip->u.addr16[7]),
 											(s)?s:""
4e2fdd79
 				);
 			break;
 		default:
53c7e0f1
 			DBG("print_ip: warning unknown address family %d\n", ip->af);
4e2fdd79
 	}
 }
 
 
 
 void stdout_print_ip(struct ip_addr* ip)
 {
 	switch(ip->af){
 		case AF_INET:
 			printf("%d.%d.%d.%d",	ip->u.addr[0],
 								ip->u.addr[1],
 								ip->u.addr[2],
 								ip->u.addr[3]);
 			break;
 		case AF_INET6:
 			printf("%x:%x:%x:%x:%x:%x:%x:%x",	htons(ip->u.addr16[0]),
 											htons(ip->u.addr16[1]),
 											htons(ip->u.addr16[2]),
 											htons(ip->u.addr16[3]),
 											htons(ip->u.addr16[4]),
 											htons(ip->u.addr16[5]),
 											htons(ip->u.addr16[6]),
 											htons(ip->u.addr16[7])
 				);
 			break;
 		default:
53c7e0f1
 			DBG("print_ip: warning unknown address family %d\n", ip->af);
4e2fdd79
 	}
 }
 
 
 
 void print_net(struct net* net)
 {
 	if (net==0){
36d0ecf4
 		LM_WARN("null pointer\n");
4e2fdd79
 		return;
 	}
9c01c860
 	print_ip("", &net->ip, "/"); print_ip("", &net->mask, "");
4e2fdd79
 }
2ba73117
 
 
 #ifdef USE_MCAST
 
 /* Returns 1 if the given address is a multicast address */
 int is_mcast(struct ip_addr* ip)
 {
 	if (!ip){
36d0ecf4
 		LM_ERR("Invalid parameter value\n");
2ba73117
 		return -1;
 	}
 
 	if (ip->af==AF_INET){
 		return IN_MULTICAST(htonl(ip->u.addr32[0]));
 	} else if (ip->af==AF_INET6){
8a2ca526
 		return IN6_IS_ADDR_MULTICAST((struct in6_addr*)ip->u.addr32);
2ba73117
 	} else {
36d0ecf4
 		LM_ERR("Unsupported protocol family\n");
2ba73117
 		return -1;
 	}
 }
 
 #endif /* USE_MCAST */
10117562
 
b81dff6a
 /** get string for known protocols.
  * @param iproto - protocol number
  * @param utype  - 1 if result is used for URI, or 0
  * @param vtype  - 1 if result is wanted uppercase, or 0 for lowercase
  * @param sproto - the string for the proto
  * @return  0 if it is a valid and supported protocol, negative otherwise
  */
 int get_valid_proto_string(unsigned int iproto, int utype, int vtype,
 		str *sproto)
 {
 	switch(iproto){
 		case PROTO_NONE:
 			return -1;
 		case PROTO_UDP:
 			sproto->len = 3;
 			sproto->s = (vtype)?"UDP":"udp";
 			return 0;
 		case PROTO_TCP:
 			sproto->len = 3;
 			sproto->s = (vtype)?"TCP":"tcp";
 			return 0;
 		case PROTO_TLS:
 			sproto->len = 3;
 			sproto->s = (vtype)?"TLS":"tls";
 			return 0;
 		case PROTO_SCTP:
 			sproto->len = 4;
 			sproto->s = (vtype)?"SCTP":"sctp";
 			return 0;
 		case PROTO_WS:
 		case PROTO_WSS:
 			if(iproto==PROTO_WS || utype) {
 				/* ws-only in SIP URI */
 				sproto->len = 2;
 				sproto->s = (vtype)?"WS":"ws";
 			} else {
 				sproto->len = 3;
 				sproto->s = (vtype)?"WSS":"wss";
 			}
 			return 0;
 		default:
 			return -2;
 	}
 }
10117562
 
 /** get protocol name (asciiz).
  * @param proto - protocol number
  * @return  string with the protocol name or "unknown".
  */
a077f5e1
 char* get_proto_name(unsigned int proto)
10117562
 {
b81dff6a
 	str sproto;
10117562
 	switch(proto){
 		case PROTO_NONE:
 			return "*";
a077f5e1
 		default:
b81dff6a
 			if(get_valid_proto_string(proto, 1, 0, &sproto)<0)
 				return "unknown";
 			return sproto.s;
10117562
 	}
 }
c1a4788c
 
 
 /**
  * match ip address with net address and bitmask
  * - return 0 on match, -1 otherwise
  */
 int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr,
 		int mask)
 {
f429e753
 	unsigned char ci;
 	unsigned char cn;
c1a4788c
 	int i;
 	int mbytes;
 	int mbits;
 
 	if(mask==0)
 		return 0;
 	if(iaddr==NULL || naddr==NULL || mask<0)
 		return -1;
 	if(iaddr->af != naddr->af)
 		return -1;
 
 	if(iaddr->af == AF_INET)
 	{
 		if(mask>32)
 			return -1;
 		if(mask==32)
 		{
 			if(ip_addr_cmp(iaddr, naddr))
 				return 0;
 			return -1;
 		}
 	} else if(iaddr->af ==  AF_INET6) {
 		if(mask>128)
 			return -1;
 
 		if(mask==128)
 		{
 			if(ip_addr_cmp(iaddr, naddr))
 				return 0;
 			return -1;
 		}
 	}
 
 	mbytes = mask / 8;
 	for(i=0; i<mbytes; i++)
 	{
 		if(iaddr->u.addr[i] != naddr->u.addr[i])
 			return -1;
 	}
 	mbits = mask % 8;
 	if(mbits==0)
 		return 0;
f429e753
 	ci = iaddr->u.addr[i] & (~((1 << (8 - mbits)) - 1));
 	cn = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1));
 	if(ci == cn)
c1a4788c
 		return 0;
 	return -1;
 }
c725f1de
 
 int si_get_signaling_data(struct socket_info *si, str **addr, str **port)
 {
 	if(si==NULL)
 		return -1;
 	if(addr) {
 		if(si->useinfo.name.len>0) {
 			*addr = &si->useinfo.name;
 		} else {
 			*addr = &si->address_str;
 		}
 	}
 	if(port) {
 		if(si->useinfo.port_no>0) {
 			*port = &si->useinfo.port_no_str;
 		} else {
 			*port = &si->port_no_str;
 		}
 	}
 	return 0;
 }