1 /*
2  * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org>
3  * Copyright (c) 2006-2007 Sippy Software, Inc., http://www.sippysoft.com
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #ifndef _RTPP_NETWORK_H_
30 #define _RTPP_NETWORK_H_
31 
32 #define	addr2port(sa)	ntohs(satosin(sa)->sin_port)
33 
34 struct cfg;
35 struct timeval;
36 struct sockaddr;
37 struct sockaddr_storage;
38 
39 /* Function prototypes */
40 int ishostseq(const struct sockaddr *, const struct sockaddr *);
41 int ishostnull(const struct sockaddr *);
42 uint16_t getport(const struct sockaddr *);
43 uint16_t getnport(const struct sockaddr *);
44 int isaddrseq(const struct sockaddr *ia1, const struct sockaddr *ia2);
45 int isaddreq(struct sockaddr *ia1, struct sockaddr *ia2);
46 void setport(struct sockaddr *, int);
47 void setanyport(struct sockaddr *);
48 char *addr2char_r(struct sockaddr *, char *buf, int size);
49 char *addrport2char_r(struct sockaddr *, char *buf, int size, char);
50 int resolve(struct sockaddr *, int, const char *, const char *, int);
51 uint16_t rtpp_in_cksum(void *, int);
52 int local4remote(const struct sockaddr *, struct sockaddr_storage *);
53 int extractaddr(const char *, char **, char **, int *);
54 int setbindhost(struct sockaddr *, int, const char *, const char *);
55 
56 /* Some handy/compat macros */
57 #if !defined(AF_LOCAL)
58 #define	AF_LOCAL	AF_UNIX
59 #endif
60 
61 #if !defined(SA_LEN)
62 # define SA_LEN(sa) \
63    (((sa)->sa_family == AF_INET) ? \
64    sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
65 #endif
66 #if !defined(SS_LEN)
67 # define SS_LEN(ss) \
68    (((ss)->ss_family == AF_INET) ? \
69    sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
70 #endif
71 
72 #define AF2STR(af) 	((af) == AF_LOCAL ? "Unix-Domain" : \
73   ((af == AF_INET) ? "IPv4" : ((af == AF_INET6) ? "IPv6" : "Unknown (BUG?!)")))
74 #define SA_AF2STR(sa)	AF2STR((sa)->sa_family)
75 
76 #if !defined(satosin)
77 #define	satosin(sa)	((struct sockaddr_in *)(sa))
78 #endif
79 #if !defined(satosin6)
80 #define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
81 #endif
82 #if !defined(sstosa)
83 #define	sstosa(ss)	((struct sockaddr *)(ss))
84 #endif
85 #if !defined(sstosun)
86 #define sstosun(ss)      ((struct sockaddr_un *)(ss))
87 #endif
88 #if !defined(satoss)
89 #define	satoss(sa)	((struct sockaddr_storage *)(sa))
90 #endif
91 
92 #define	IS_VALID_PORT(p)	((p) > 0 && (p) < 65536)
93 #define	IS_LAST_PORT(p)		((p) == 65535)
94 
95 
96 /*
97  * > len('0000:0000:0000:0000:0000:0000:127.127.127.127')
98  * 45
99  */
100 #define INET6_ADDR_STRLEN 46
101 #define MAX_ADDR_STRLEN INET6_ADDR_STRLEN
102 
103 /*
104  * > len('[0000:0000:0000:0000:0000:0000:127.127.127.127]:65535')
105  * 53
106  * > len('127.127.127.127:65535')
107  * 21
108  */
109 #define MAX_AP_STRBUF 54
110 
111 #endif
112