unused variable is deleted
[sip-router] / ip_addr.h
1 /* $Id$
2  *
3  * ip address family related structures
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License 
25  * along with this program; if not, write to the Free Software 
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28 /*
29  * History:
30  * --------
31  *  2003-02-13  added struct dest_info (andrei)
32  *  2003-04-06  all ports are stored/passed in host byte order now (andrei)
33  *  2006-04-20  comp support in recv_info and dest_info (andrei)
34  *  2006-04-21  added init_dst_from_rcv (andrei)
35  *  2007-06-26  added ip_addr_mk_any() (andrei)
36  */
37
38 #ifndef ip_addr_h
39 #define ip_addr_h
40
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <netdb.h>
46 #include "str.h"
47 #include "compiler_opt.h"
48
49
50 #include "dprint.h"
51
52 enum sip_protos { PROTO_NONE, PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP };
53 #ifdef USE_COMP
54 enum comp_methods { COMP_NONE, COMP_SIGCOMP, COMP_SERGZ };
55 #endif
56
57 struct ip_addr{
58         unsigned int af; /* address family: AF_INET6 or AF_INET */
59         unsigned int len;    /* address len, 16 or 4 */
60         
61         /* 64 bits aligned address */
62         union {
63                 unsigned long  addrl[16/sizeof(long)]; /* long format*/
64                 unsigned int   addr32[4];
65                 unsigned short addr16[8];
66                 unsigned char  addr[16];
67         }u;
68 };
69
70
71
72 struct net{
73         struct ip_addr ip;
74         struct ip_addr mask;
75 };
76
77 union sockaddr_union{
78                 struct sockaddr     s;
79                 struct sockaddr_in  sin;
80         #ifdef USE_IPV6
81                 struct sockaddr_in6 sin6;
82         #endif
83 };
84
85
86
87 enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4,
88                                  SI_IS_ANY=8 };
89
90 struct socket_info{
91         int socket;
92         str name; /* name - eg.: foo.bar or 10.0.0.1 */
93         struct ip_addr address; /* ip address */
94         str address_str;        /* ip address converted to string -- optimization*/
95         str port_no_str; /* port number converted to string -- optimization*/
96         enum si_flags flags; /* SI_IS_IP | SI_IS_LO | SI_IS_MCAST */
97         union sockaddr_union su; 
98         struct socket_info* next;
99         struct socket_info* prev;
100         unsigned short port_no;  /* port number */
101         char proto; /* tcp or udp*/
102 };
103
104
105 struct receive_info{
106         struct ip_addr src_ip;
107         struct ip_addr dst_ip;
108         unsigned short src_port; /* host byte order */
109         unsigned short dst_port; /* host byte order */
110         int proto_reserved1; /* tcp stores the connection id here */
111         int proto_reserved2;
112         union sockaddr_union src_su; /* useful for replies*/
113         struct socket_info* bind_address; /* sock_info structure on which 
114                                                                           the msg was received*/
115         char proto;
116 #ifdef USE_COMP
117         short comp; /* compression */
118 #endif
119         /* no need for dst_su yet */
120 };
121
122
123 struct dest_info{
124         struct socket_info* send_sock;
125         union sockaddr_union to;
126         int id; /* tcp stores the connection id here */ 
127         char proto;
128 #ifdef USE_COMP
129         short comp;
130 #endif
131 };
132
133
134 struct socket_id{
135         char* name;
136         int proto;
137         int port;
138         struct socket_id* next;
139 };
140
141
142
143 /* len of the sockaddr */
144 #ifdef HAVE_SOCKADDR_SA_LEN
145 #define sockaddru_len(su)       ((su).s.sa_len)
146 #else
147 #ifdef USE_IPV6
148 #define sockaddru_len(su)       \
149                         (((su).s.sa_family==AF_INET6)?sizeof(struct sockaddr_in6):\
150                                         sizeof(struct sockaddr_in))
151 #else
152 #define sockaddru_len(su)       sizeof(struct sockaddr_in)
153 #endif /*USE_IPV6*/
154 #endif /* HAVE_SOCKADDR_SA_LEN*/
155         
156 /* inits an ip_addr with the addr. info from a hostent structure
157  * ip = struct ip_addr*
158  * he= struct hostent*
159  */
160 #define hostent2ip_addr(ip, he, addr_no) \
161         do{ \
162                 (ip)->af=(he)->h_addrtype; \
163                 (ip)->len=(he)->h_length;  \
164                 memcpy((ip)->u.addr, (he)->h_addr_list[(addr_no)], (ip)->len); \
165         }while(0)
166         
167
168
169
170 /* gets the protocol family corresponding to a specific address family
171  * ( PF_INET - AF_INET, PF_INET6 - AF_INET6, af for others)
172  */
173 #ifdef USE_IPV6
174 #define AF2PF(af)   (((af)==AF_INET)?PF_INET:((af)==AF_INET6)?PF_INET6:(af))
175 #else
176 #define AF2PF(af)   (((af)==AF_INET)?PF_INET:(af))
177 #endif
178
179
180
181
182 struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask);
183 struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen);
184
185 void print_ip(char* prefix, struct ip_addr* ip, char* suffix);
186 void stdout_print_ip(struct ip_addr* ip);
187 void print_net(struct net* net);
188
189 #ifdef USE_MCAST
190 /* Returns 1 if the given address is a multicast address */
191 int is_mcast(struct ip_addr* ip);
192 #endif /* USE_MCAST */
193
194 /* returns 1 if the given ip address is INADDR_ANY or IN6ADDR_ANY,
195  * 0 otherwise */
196 inline static int ip_addr_any(struct ip_addr* ip)
197 {
198         int r;
199         int l;
200         
201         l=ip->len/4;
202         for (r=0; r<l; r++)
203                 if (ip->u.addr32[r]!=0)
204                         return 0;
205         return 1;
206 }
207
208
209
210 /* creates an ANY ip_addr (filled with 0, af and len properly set) */
211 inline static void ip_addr_mk_any(int af, struct ip_addr* ip)
212 {
213         ip->af=af;
214         if (likely(af==AF_INET)){
215                 ip->len=4;
216                 ip->u.addr32[0]=0;
217         }
218 #ifdef USE_IPV6
219         else{
220                 ip->len=16;
221 #if (defined (ULONG_MAX) && ULONG_MAX > 4294967295) || defined LP64
222                 /* long is 64 bits */
223                 ip->u.addrl[0]=0;
224                 ip->u.addrl[1]=0;
225 #else
226                 ip->u.addr32[0]=0;
227                 ip->u.addr32[1]=0;
228                 ip->u.addr32[2]=0;
229                 ip->u.addr32[3]=0;
230 #endif /* ULONG_MAX */
231         }
232 #endif
233 }
234
235 /* returns 1 if ip & net.mask == net.ip ; 0 otherwise & -1 on error 
236         [ diff. address families ]) */
237 inline static int matchnet(struct ip_addr* ip, struct net* net)
238 {
239         unsigned int r;
240
241         if (ip->af == net->ip.af){
242                 for(r=0; r<ip->len/4; r++){ /* ipv4 & ipv6 addresses are
243                                                                            all multiple of 4*/
244                         if ((ip->u.addr32[r]&net->mask.u.addr32[r])!=
245                                                                                                                  net->ip.u.addr32[r]){
246                                 return 0;
247                         }
248                 }
249                 return 1;
250         };
251         return -1;
252 }
253
254
255
256
257 /* inits an ip_addr pointer from a sockaddr structure*/
258 static inline void sockaddr2ip_addr(struct ip_addr* ip, struct sockaddr* sa)
259 {
260         switch(sa->sa_family){
261         case AF_INET:
262                         ip->af=AF_INET;
263                         ip->len=4;
264                         memcpy(ip->u.addr, &((struct sockaddr_in*)sa)->sin_addr, 4);
265                         break;
266 #ifdef USE_IPV6
267         case AF_INET6:
268                         ip->af=AF_INET6;
269                         ip->len=16;
270                         memcpy(ip->u.addr, &((struct sockaddr_in6*)sa)->sin6_addr, 16);
271                         break;
272 #endif
273         default:
274                         LOG(L_CRIT, "sockaddr2ip_addr: BUG: unknown address family %d\n",
275                                         sa->sa_family);
276         }
277 }
278
279
280
281 /* compare 2 ip_addrs (both args are pointers)*/
282 #define ip_addr_cmp(ip1, ip2) \
283         (((ip1)->af==(ip2)->af)&& \
284                 (memcmp((ip1)->u.addr, (ip2)->u.addr, (ip1)->len)==0))
285
286
287
288 /* compare 2 sockaddr_unions */
289 static inline int su_cmp(union sockaddr_union* s1, union sockaddr_union* s2)
290 {
291         if (s1->s.sa_family!=s2->s.sa_family) return 0;
292         switch(s1->s.sa_family){
293                 case AF_INET:
294                         return (s1->sin.sin_port==s2->sin.sin_port)&&
295                                         (memcmp(&s1->sin.sin_addr, &s2->sin.sin_addr, 4)==0);
296 #ifdef USE_IPV6
297                 case AF_INET6:
298                         return (s1->sin6.sin6_port==s2->sin6.sin6_port)&&
299                                         (memcmp(&s1->sin6.sin6_addr, &s2->sin6.sin6_addr, 16)==0);
300 #endif
301                 default:
302                         LOG(L_CRIT,"su_cmp: BUG: unknown address family %d\n",
303                                                 s1->s.sa_family);
304                         return 0;
305         }
306 }
307
308
309
310 /* gets the port number (host byte order) */
311 static inline unsigned short su_getport(union sockaddr_union* su)
312 {
313         switch(su->s.sa_family){
314                 case AF_INET:
315                         return ntohs(su->sin.sin_port);
316 #ifdef USE_IPV6
317                 case AF_INET6:
318                         return ntohs(su->sin6.sin6_port);
319 #endif
320                 default:
321                         LOG(L_CRIT,"su_get_port: BUG: unknown address family %d\n",
322                                                 su->s.sa_family);
323                         return 0;
324         }
325 }
326
327
328
329 /* sets the port number (host byte order) */
330 static inline void su_setport(union sockaddr_union* su, unsigned short port)
331 {
332         switch(su->s.sa_family){
333                 case AF_INET:
334                         su->sin.sin_port=htons(port);
335                         break;
336 #ifdef USE_IPV6
337                 case AF_INET6:
338                          su->sin6.sin6_port=htons(port);
339                          break;
340 #endif
341                 default:
342                         LOG(L_CRIT,"su_set_port: BUG: unknown address family %d\n",
343                                                 su->s.sa_family);
344         }
345 }
346
347
348
349 /* inits an ip_addr pointer from a sockaddr_union ip address */
350 static inline void su2ip_addr(struct ip_addr* ip, union sockaddr_union* su)
351 {
352         switch(su->s.sa_family){
353         case AF_INET: 
354                                         ip->af=AF_INET;
355                                         ip->len=4;
356                                         memcpy(ip->u.addr, &su->sin.sin_addr, 4);
357                                         break;
358 #ifdef USE_IPV6
359         case AF_INET6:
360                                         ip->af=AF_INET6;
361                                         ip->len=16;
362                                         memcpy(ip->u.addr, &su->sin6.sin6_addr, 16);
363                                         break;
364 #endif
365         default:
366                                         LOG(L_CRIT,"su2ip_addr: BUG: unknown address family %d\n",
367                                                         su->s.sa_family);
368         }
369 }
370
371
372 /* ip_addr2su -> the same as init_su*/
373 #define ip_addr2su init_su
374
375 /* inits a struct sockaddr_union from a struct ip_addr and a port no 
376  * returns 0 if ok, -1 on error (unknown address family)
377  * the port number is in host byte order */
378 static inline int init_su( union sockaddr_union* su,
379                                                         struct ip_addr* ip,
380                                                         unsigned short   port ) 
381 {
382         memset(su, 0, sizeof(union sockaddr_union));/*needed on freebsd*/
383         su->s.sa_family=ip->af;
384         switch(ip->af){
385 #ifdef USE_IPV6
386         case    AF_INET6:
387                 memcpy(&su->sin6.sin6_addr, ip->u.addr, ip->len); 
388                 #ifdef HAVE_SOCKADDR_SA_LEN
389                         su->sin6.sin6_len=sizeof(struct sockaddr_in6);
390                 #endif
391                 su->sin6.sin6_port=htons(port);
392                 break;
393 #endif
394         case AF_INET:
395                 memcpy(&su->sin.sin_addr, ip->u.addr, ip->len);
396                 #ifdef HAVE_SOCKADDR_SA_LEN
397                         su->sin.sin_len=sizeof(struct sockaddr_in);
398                 #endif
399                 su->sin.sin_port=htons(port);
400                 break;
401         default:
402                 LOG(L_CRIT, "init_ss: BUG: unknown address family %d\n", ip->af);
403                 return -1;
404         }
405         return 0;
406 }
407
408
409
410 /* inits a struct sockaddr_union from a struct hostent, an address index in
411  * the hostent structure and a port no. (host byte order)
412  * WARNING: no index overflow  checks!
413  * returns 0 if ok, -1 on error (unknown address family) */
414 static inline int hostent2su( union sockaddr_union* su,
415                                                                 struct hostent* he,
416                                                                 unsigned int idx,
417                                                                 unsigned short   port ) 
418 {
419         memset(su, 0, sizeof(union sockaddr_union)); /*needed on freebsd*/
420         su->s.sa_family=he->h_addrtype;
421         switch(he->h_addrtype){
422 #ifdef USE_IPV6
423         case    AF_INET6:
424                 memcpy(&su->sin6.sin6_addr, he->h_addr_list[idx], he->h_length);
425                 #ifdef HAVE_SOCKADDR_SA_LEN
426                         su->sin6.sin6_len=sizeof(struct sockaddr_in6);
427                 #endif
428                 su->sin6.sin6_port=htons(port);
429                 break;
430 #endif
431         case AF_INET:
432                 memcpy(&su->sin.sin_addr, he->h_addr_list[idx], he->h_length);
433                 #ifdef HAVE_SOCKADDR_SA_LEN
434                         su->sin.sin_len=sizeof(struct sockaddr_in);
435                 #endif
436                 su->sin.sin_port=htons(port);
437                 break;
438         default:
439                 LOG(L_CRIT, "hostent2su: BUG: unknown address family %d\n", 
440                                 he->h_addrtype);
441                 return -1;
442         }
443         return 0;
444 }
445
446 /* maximum size of a str returned by ip_addr2a (including \0) */
447 #define IP_ADDR_MAX_STR_SIZE 40 /* 1234:5678:9012:3456:7890:1234:5678:9012\0 */
448 /* fast ip_addr -> string converter;
449  * it uses an internal buffer
450  */
451 static inline char* ip_addr2a(struct ip_addr* ip)
452 {
453
454         static char buff[IP_ADDR_MAX_STR_SIZE];
455         int offset;
456         register unsigned char a,b,c;
457 #ifdef USE_IPV6
458         register unsigned char d;
459         register unsigned short hex4;
460 #endif
461         int r;
462         #define HEXDIG(x) (((x)>=10)?(x)-10+'A':(x)+'0')
463         
464         
465         offset=0;
466         switch(ip->af){
467         #ifdef USE_IPV6
468                 case AF_INET6:
469                         for(r=0;r<7;r++){
470                                 hex4=ntohs(ip->u.addr16[r]);
471                                 a=hex4>>12;
472                                 b=(hex4>>8)&0xf;
473                                 c=(hex4>>4)&0xf;
474                                 d=hex4&0xf;
475                                 if (a){
476                                         buff[offset]=HEXDIG(a);
477                                         buff[offset+1]=HEXDIG(b);
478                                         buff[offset+2]=HEXDIG(c);
479                                         buff[offset+3]=HEXDIG(d);
480                                         buff[offset+4]=':';
481                                         offset+=5;
482                                 }else if(b){
483                                         buff[offset]=HEXDIG(b);
484                                         buff[offset+1]=HEXDIG(c);
485                                         buff[offset+2]=HEXDIG(d);
486                                         buff[offset+3]=':';
487                                         offset+=4;
488                                 }else if(c){
489                                         buff[offset]=HEXDIG(c);
490                                         buff[offset+1]=HEXDIG(d);
491                                         buff[offset+2]=':';
492                                         offset+=3;
493                                 }else{
494                                         buff[offset]=HEXDIG(d);
495                                         buff[offset+1]=':';
496                                         offset+=2;
497                                 }
498                         }
499                         /* last int16*/
500                         hex4=ntohs(ip->u.addr16[r]);
501                         a=hex4>>12;
502                         b=(hex4>>8)&0xf;
503                         c=(hex4>>4)&0xf;
504                         d=hex4&0xf;
505                         if (a){
506                                 buff[offset]=HEXDIG(a);
507                                 buff[offset+1]=HEXDIG(b);
508                                 buff[offset+2]=HEXDIG(c);
509                                 buff[offset+3]=HEXDIG(d);
510                                 buff[offset+4]=0;
511                         }else if(b){
512                                 buff[offset]=HEXDIG(b);
513                                 buff[offset+1]=HEXDIG(c);
514                                 buff[offset+2]=HEXDIG(d);
515                                 buff[offset+3]=0;
516                         }else if(c){
517                                 buff[offset]=HEXDIG(c);
518                                 buff[offset+1]=HEXDIG(d);
519                                 buff[offset+2]=0;
520                         }else{
521                                 buff[offset]=HEXDIG(d);
522                                 buff[offset+1]=0;
523                         }
524                         break;
525         #endif
526                 case AF_INET:
527                         for(r=0;r<3;r++){
528                                 a=ip->u.addr[r]/100;
529                                 c=ip->u.addr[r]%10;
530                                 b=ip->u.addr[r]%100/10;
531                                 if (a){
532                                         buff[offset]=a+'0';
533                                         buff[offset+1]=b+'0';
534                                         buff[offset+2]=c+'0';
535                                         buff[offset+3]='.';
536                                         offset+=4;
537                                 }else if (b){
538                                         buff[offset]=b+'0';
539                                         buff[offset+1]=c+'0';
540                                         buff[offset+2]='.';
541                                         offset+=3;
542                                 }else{
543                                         buff[offset]=c+'0';
544                                         buff[offset+1]='.';
545                                         offset+=2;
546                                 }
547                         }
548                         /* last number */
549                         a=ip->u.addr[r]/100;
550                         c=ip->u.addr[r]%10;
551                         b=ip->u.addr[r]%100/10;
552                         if (a){
553                                 buff[offset]=a+'0';
554                                 buff[offset+1]=b+'0';
555                                 buff[offset+2]=c+'0';
556                                 buff[offset+3]=0;
557                         }else if (b){
558                                 buff[offset]=b+'0';
559                                 buff[offset+1]=c+'0';
560                                 buff[offset+2]=0;
561                         }else{
562                                 buff[offset]=c+'0';
563                                 buff[offset+1]=0;
564                         }
565                         break;
566                 
567                 default:
568                         LOG(L_CRIT, "BUG: ip_addr2a: unknown address family %d\n",
569                                         ip->af);
570                         return 0;
571         }
572         
573         return buff;
574 }
575
576
577
578 /* converts an ip_addr structure to a hostent, returns pointer to internal
579  * statical structure */
580 static inline struct hostent* ip_addr2he(str* name, struct ip_addr* ip)
581 {
582         static struct hostent he;
583         static char hostname[256];
584         static char* p_aliases[1];
585         static char* p_addr[2];
586         static char address[16];
587         
588         p_aliases[0]=0; /* no aliases*/
589         p_addr[1]=0; /* only one address*/
590         p_addr[0]=address;
591         strncpy(hostname, name->s, (name->len<256)?(name->len)+1:256);
592         if (ip->len>16) return 0;
593         memcpy(address, ip->u.addr, ip->len);
594         
595         he.h_addrtype=ip->af;
596         he.h_length=ip->len;
597         he.h_addr_list=p_addr;
598         he.h_aliases=p_aliases;
599         he.h_name=hostname;
600         return &he;
601 }
602
603
604
605 /* init a dest_info structure */
606 #define init_dest_info(dst) \
607         do{ \
608                 memset((dst), 0, sizeof(struct dest_info)); \
609         } while(0) 
610
611
612
613 /* init a dest_info structure from a recv_info structure */
614 inline static void init_dst_from_rcv(struct dest_info* dst,
615                                                                         struct receive_info* rcv)
616 {
617                 dst->send_sock=rcv->bind_address;
618                 dst->to=rcv->src_su;
619                 dst->id=rcv->proto_reserved1;
620                 dst->proto=rcv->proto;
621 #ifdef USE_COMP
622                 dst->comp=rcv->comp;
623 #endif
624 }
625
626
627 #endif