- ipv6 support (-DUSE_IPV6)
[sip-router] / ip_addr.c
1 /*
2  * $Id$
3  *
4  *
5  * ip address & address family related functions
6  */
7
8 #include <stdio.h>
9
10 #include "ip_addr.h"
11 #include "dprint.h"
12
13
14 struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask)
15 {
16         struct net* n;
17         
18         if ((ip->af != mask->af) || (ip->len != mask->len)){
19                 LOG(L_CRIT, "ERROR: mk_net: trying to use a different mask family"
20                                 " (eg. ipv4/ipv6mask or ipv6/ipv4mask)\n");
21                 goto error;
22         }
23         n=(struct net*)malloc(sizeof(struct net));
24         if (n==0){ 
25                 LOG(L_CRIT, "ERROR: mk_net: memory allocation failure\n");
26                 goto error;
27         }
28         n->ip=*ip;
29         n->mask=*mask;
30         return n;
31 error:
32         return 0;
33 }
34
35
36
37 struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen)
38 {
39         struct net* n;
40         int r;
41         
42         if (bitlen>ip->len*8){
43                 LOG(L_CRIT, "ERROR: mk_net_bitlen: bad bitlen number %d\n", bitlen);
44                 goto error;
45         }
46         n=(struct net*)malloc(sizeof(struct net));
47         if (n==0){
48                 LOG(L_CRIT, "ERROR: mk_net_bitlen: memory allocation failure\n"); 
49                 goto error;
50         }
51         memset(n,0, sizeof(struct net));
52         n->ip=*ip;
53         for (r=0;r<bitlen/8;r++) n->mask.u.addr[r]=0xff;
54         if (bitlen%8) n->mask.u.addr[r]=  ~((1<<(8-(bitlen%8)))-1);
55         n->mask.af=ip->af;
56         n->mask.len=ip->len;
57         
58         return n;
59 error:
60         return 0;
61 }
62
63
64
65 void print_ip(struct ip_addr* ip)
66 {
67         switch(ip->af){
68                 case AF_INET:
69                         DBG("%d.%d.%d.%d",      ip->u.addr[0],
70                                                                 ip->u.addr[1],
71                                                                 ip->u.addr[2],
72                                                                 ip->u.addr[3]);
73                         break;
74                 case AF_INET6:
75                         DBG("%x:%x:%x:%x:%x:%x:%x:%x",  htons(ip->u.addr16[0]),
76                                                                                         htons(ip->u.addr16[1]),
77                                                                                         htons(ip->u.addr16[2]),
78                                                                                         htons(ip->u.addr16[3]),
79                                                                                         htons(ip->u.addr16[4]),
80                                                                                         htons(ip->u.addr16[5]),
81                                                                                         htons(ip->u.addr16[6]),
82                                                                                         htons(ip->u.addr16[7])
83                                 );
84                         break;
85                 default:
86                         DBG("print_ip: warning unknown adress family %d\n", ip->af);
87         }
88 }
89
90
91
92 void stdout_print_ip(struct ip_addr* ip)
93 {
94         switch(ip->af){
95                 case AF_INET:
96                         printf("%d.%d.%d.%d",   ip->u.addr[0],
97                                                                 ip->u.addr[1],
98                                                                 ip->u.addr[2],
99                                                                 ip->u.addr[3]);
100                         break;
101                 case AF_INET6:
102                         printf("%x:%x:%x:%x:%x:%x:%x:%x",       htons(ip->u.addr16[0]),
103                                                                                         htons(ip->u.addr16[1]),
104                                                                                         htons(ip->u.addr16[2]),
105                                                                                         htons(ip->u.addr16[3]),
106                                                                                         htons(ip->u.addr16[4]),
107                                                                                         htons(ip->u.addr16[5]),
108                                                                                         htons(ip->u.addr16[6]),
109                                                                                         htons(ip->u.addr16[7])
110                                 );
111                         break;
112                 default:
113                         DBG("print_ip: warning unknown adress family %d\n", ip->af);
114         }
115 }
116
117
118
119 void print_net(struct net* net)
120 {
121         if (net==0){
122                 LOG(L_WARN, "ERROR: print net: null pointer\n");
123                 return;
124         }
125         print_ip(&net->ip); DBG("/"); print_ip(&net->mask);
126 }