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