1 /*
2 Copyright (c) 2007, Adobe Systems, Incorporated
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9 * Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11 
12 * Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in the
14   documentation and/or other materials provided with the distribution.
15 
16 * Neither the name of Adobe Systems, Network Resonance nor the names of its
17   contributors may be used to endorse or promote products derived from
18   this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 
34 
35 #ifndef _transport_addr_h
36 #define _transport_addr_h
37 
38 #include <stdbool.h>
39 #include <sys/types.h>
40 #ifdef WIN32
41 #include <winsock2.h>
42 #include <ws2tcpip.h>
43 #else
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #endif
47 
48 #include "r_types.h"
49 
50 /* Length of a string  hex representation of a MD5 hash */
51 #define MAXIFNAME 33
52 
53 /* Generic transport address
54 
55    This spans both sockaddr_in and sockaddr_in6
56  */
57 typedef struct nr_transport_addr_ {
58   UCHAR ip_version;  /* 4 or 6 */
59 #define NR_IPV4 4
60 #define NR_IPV6 6
61   UCHAR protocol;    /* IPPROTO_TCP, IPPROTO_UDP */
62   union {
63     struct sockaddr_in addr4;
64     struct sockaddr_in6 addr6;
65   } u;
66   char ifname[MAXIFNAME];
67   /* A string version.
68      56 = 5 ("IP6:[") + 39 (ipv6 address) + 2 ("]:") + 5 (port) + 4 (/UDP) + 1 (null) */
69   char as_string[56];
70   char fqdn[256];
71   bool is_proxied;
72   bool tls;
73 } nr_transport_addr;
74 
75 typedef struct nr_transport_addr_mask_ {
76   UINT4 addr;
77   UINT4 mask;
78 } nr_transport_addr_mask;
79 
80 int nr_sockaddr_to_transport_addr(struct sockaddr *saddr, int protocol, int keep, nr_transport_addr *addr);
81 
82 // addresses, ports in local byte order
83 int nr_ip4_port_to_transport_addr(UINT4 ip4, UINT2 port, int protocol, nr_transport_addr *addr);
84 int nr_str_port_to_transport_addr(const char *str, UINT2 port, int protocol, nr_transport_addr *addr);
85 int nr_ip6_port_to_transport_addr(struct in6_addr* addr6, UINT2 port, int protocol, nr_transport_addr *addr);
86 
87 int nr_transport_addr_get_addrstring(const nr_transport_addr *addr, char *str, int maxlen);
88 int nr_transport_addr_get_port(const nr_transport_addr *addr, int *port);
89 int nr_transport_addr_cmp(const nr_transport_addr *addr1,const nr_transport_addr *addr2,int mode);
90 #define NR_TRANSPORT_ADDR_CMP_MODE_VERSION   1
91 #define NR_TRANSPORT_ADDR_CMP_MODE_PROTOCOL  2
92 #define NR_TRANSPORT_ADDR_CMP_MODE_ADDR      3
93 #define NR_TRANSPORT_ADDR_CMP_MODE_ALL       4
94 
95 int nr_transport_addr_is_wildcard(const nr_transport_addr *addr);
96 int nr_transport_addr_is_loopback(const nr_transport_addr *addr);
97 int nr_transport_addr_get_private_addr_range(const nr_transport_addr *addr);
98 int nr_transport_addr_is_link_local(const nr_transport_addr *addr);
99 int nr_transport_addr_is_mac_based(const nr_transport_addr *addr);
100 int nr_transport_addr_is_teredo(const nr_transport_addr *addr);
101 int nr_transport_addr_check_compatibility(const nr_transport_addr *addr1, const nr_transport_addr *addr2);
102 int nr_transport_addr_copy(nr_transport_addr *to, const nr_transport_addr *from);
103 int nr_transport_addr_copy_keep_ifname(nr_transport_addr *to, const nr_transport_addr *from);
104 /* Copies _just_ the address and port (also handles IP version) */
105 int nr_transport_addr_copy_addrport(nr_transport_addr *to, const nr_transport_addr *from);
106 int nr_transport_addr_fmt_addr_string(nr_transport_addr *addr);
107 int nr_transport_addr_fmt_ifname_addr_string(const nr_transport_addr *addr, char *buf, int len);
108 int nr_transport_addr_set_port(nr_transport_addr *addr, int port);
109 int nr_transport_addr_is_reliable_transport(const nr_transport_addr *addr);
110 
111 #endif
112 
113