Commit | Line | Data |
---|---|---|
d44e3c4f | 1 | /****************************************************************************** |
2 | * Copyright (c) 2000-2016 Ericsson Telecom AB | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Balasko, Jeno | |
10 | * Beres, Szabolcs | |
11 | * Kovacs, Ferenc | |
12 | * Raduly, Csaba | |
13 | * | |
14 | ******************************************************************************/ | |
970ed795 EL |
15 | #ifndef NETWORKHANDLER_H_ |
16 | #define NETWORKHANDLER_H_ | |
17 | ||
18 | #include "platform.h" | |
19 | // platform.h includes sys/socket.h | |
20 | #include <netinet/in.h> | |
21 | #include <netdb.h> | |
22 | #include <string.h> | |
23 | ||
24 | #ifdef WIN32 | |
25 | #include <cygwin/version.h> | |
26 | ||
27 | #if CYGWIN_VERSION_DLL_MAJOR >= 1007 | |
28 | #define CYGWIN17 | |
29 | #else | |
30 | #define CYGWIN15 | |
31 | #endif | |
32 | ||
33 | #endif | |
34 | ||
35 | // For legacy (e.g. Solaris 6) systems. | |
36 | #ifndef INET_ADDRSTRLEN | |
37 | #define INET_ADDRSTRLEN 16 | |
38 | #endif | |
39 | #ifndef INET6_ADDRSTRLEN | |
40 | #define INET6_ADDRSTRLEN 46 | |
41 | #endif | |
42 | #ifndef NI_MAXHOST | |
43 | #define NI_MAXHOST 1025 | |
44 | #endif | |
45 | ||
46 | typedef enum { ipv0 = -1, ipv4 = 0, ipv6 } NetworkFamily; | |
47 | ||
48 | class Text_Buf; | |
49 | ||
50 | class IPAddress; | |
51 | class IPv4Address; | |
52 | #if defined(LINUX) || defined(CYGWIN17) | |
53 | class IPV6Address; | |
54 | #endif | |
55 | class NetworkHandler; | |
56 | class HCNetworkHandler; | |
57 | ||
58 | class IPAddress { | |
59 | public: | |
60 | virtual ~IPAddress() = 0; | |
61 | static IPAddress *create_addr(const NetworkFamily& p_family); | |
62 | static IPAddress *create_addr(const char *p_addr); | |
63 | // Always return something. | |
64 | virtual const char *get_host_str() const = 0; | |
65 | virtual const char *get_addr_str() const = 0; | |
66 | virtual bool operator==(const IPAddress& p_addr) const = 0; | |
67 | virtual bool operator!=(const IPAddress& p_addr) const = 0; | |
68 | virtual IPAddress& operator=(const IPAddress& p_addr) = 0; | |
69 | // Encode and decode the address and the corresponding port for internal | |
70 | // communication. Used by connected ports. | |
71 | virtual void push_raw(Text_Buf& p_buf) const = 0; | |
72 | virtual void pull_raw(Text_Buf& p_buf) = 0; | |
73 | virtual void clean_up() = 0; | |
74 | virtual int accept(int p_sockfd) = 0; | |
75 | // Return the current address and port to which the socket is bound to. | |
76 | virtual int getsockname(int p_sockfd) = 0; | |
77 | virtual unsigned short get_port() const = 0; | |
78 | virtual void set_port(unsigned short p_port) = 0; | |
79 | virtual bool set_addr(const char *p_addr, unsigned short p_port = 0) = 0; | |
80 | virtual const struct sockaddr *get_addr() const = 0; | |
81 | virtual socklen_type get_addr_len() const = 0; | |
82 | virtual bool is_local() const = 0; | |
83 | }; | |
84 | ||
85 | class IPv4Address : public IPAddress { | |
86 | public: | |
87 | IPv4Address(); | |
88 | // Does DNS lookup. | |
89 | IPv4Address(const char *p_addr, unsigned short p_port = 0 /* Any port. */); | |
90 | //IPv4Address(const IPv4Address& p_addr) = default; | |
91 | //There are no pointers, so the compiler generated copy is OK. | |
92 | ~IPv4Address(); | |
93 | ||
94 | bool operator==(const IPAddress& p_addr) const; | |
95 | bool operator!=(const IPAddress& p_addr) const; | |
96 | IPAddress& operator=(const IPAddress& p_addr); | |
97 | void push_raw(Text_Buf& p_buf) const; | |
98 | void pull_raw(Text_Buf& p_buf); | |
99 | void clean_up(); | |
100 | int accept(int p_sockfd); | |
101 | int getsockname(int p_sockfd); | |
102 | inline unsigned short get_port() const { return ntohs(m_addr.sin_port); } | |
103 | inline void set_port(unsigned short p_port) { m_addr.sin_port = htons(p_port); } | |
104 | bool set_addr(const char *p_addr, unsigned short p_port = 0); | |
105 | inline const struct sockaddr *get_addr() const { return (const struct sockaddr *)&m_addr; } | |
106 | inline socklen_type get_addr_len() const { return sizeof(m_addr); } | |
107 | inline const char *get_host_str() const { return strlen(m_host_str) > 0 ? m_host_str : m_addr_str; } | |
108 | inline const char *get_addr_str() const { return strlen(m_addr_str) > 0 ? m_addr_str : m_host_str; } | |
109 | static bool is_valid(const char *p_addr); | |
110 | bool is_local() const; | |
111 | private: | |
112 | sockaddr_in m_addr; | |
113 | char m_host_str[NI_MAXHOST]; // DNS name. | |
114 | char m_addr_str[INET_ADDRSTRLEN]; // Address in numeric format. | |
115 | }; | |
116 | ||
117 | #if defined(LINUX) || defined(CYGWIN17) | |
118 | class IPv6Address : public IPAddress { | |
119 | public: | |
120 | IPv6Address(); | |
121 | // Does DNS lookup. | |
122 | IPv6Address(const char *p_addr, unsigned short p_port = 0 /* Any port. */); | |
123 | //IPv6Address(const IPv6Address& p_addr) = default; | |
124 | //There are no pointers, so the compiler generated copy is OK. | |
125 | ~IPv6Address(); | |
126 | ||
127 | bool operator==(const IPAddress& p_addr) const; | |
128 | bool operator!=(const IPAddress& p_addr) const; | |
129 | IPAddress& operator=(const IPAddress& p_addr); | |
130 | void push_raw(Text_Buf& p_buf) const; | |
131 | void pull_raw(Text_Buf& p_buf); | |
132 | void clean_up(); | |
133 | int accept(int p_sockfd); | |
134 | int getsockname(int p_sockfd); | |
135 | inline unsigned short get_port() const { return ntohs(m_addr.sin6_port); } | |
136 | inline void set_port(unsigned short p_port) { m_addr.sin6_port = htons(p_port); } | |
137 | bool set_addr(const char *p_addr, unsigned short p_port = 0); | |
138 | inline const struct sockaddr *get_addr() const { return (const struct sockaddr *)&m_addr; } | |
139 | inline socklen_type get_addr_len() const { return sizeof(m_addr); } | |
140 | inline const char *get_host_str() const { return strlen(m_host_str) > 0 ? m_host_str : m_addr_str; } | |
141 | const char *get_addr_str() const; | |
142 | static bool is_valid(const char *p_addr); | |
143 | bool is_local() const; | |
144 | private: | |
145 | sockaddr_in6 m_addr; | |
146 | char m_host_str[NI_MAXHOST]; // DNS name. | |
147 | char m_addr_str[INET6_ADDRSTRLEN]; // Address in numeric format. | |
148 | }; | |
149 | #endif // LINUX || CYGWIN17 | |
150 | ||
151 | class NetworkHandler { | |
152 | public: | |
153 | NetworkHandler(); | |
154 | NetworkHandler(const NetworkFamily& p_family); | |
155 | NetworkHandler(const char *p_addr); | |
156 | ||
157 | inline void set_family(const NetworkFamily& p_family) { m_family = p_family; } | |
158 | void set_family(const char *p_addr); | |
159 | inline const NetworkFamily& get_family() const { return m_family; } | |
160 | int socket(); | |
161 | static int socket(const NetworkFamily& p_family); | |
162 | private: | |
163 | NetworkHandler(const NetworkHandler& p_handler); | |
164 | NetworkHandler& operator=(const NetworkHandler& p_handler); | |
165 | protected: | |
166 | NetworkFamily m_family; | |
167 | }; | |
168 | ||
169 | class HCNetworkHandler : public NetworkHandler { | |
170 | public: | |
171 | HCNetworkHandler(); | |
172 | ~HCNetworkHandler(); | |
173 | ||
174 | bool set_local_addr(const char *p_addr, unsigned short p_port = 0 /* Any port. */); | |
175 | bool set_mc_addr(const char *p_addr, unsigned short p_port = 0 /* Any port. */); | |
176 | int getsockname_local_addr(int p_sockfd); | |
177 | int bind_local_addr(int p_sockfd) const; | |
178 | int connect_to_mc(int p_sockfd) const; | |
179 | inline const char *get_mc_host_str() const { return m_mc_addr->get_host_str(); } | |
180 | inline const char *get_mc_addr_str() const { return m_mc_addr->get_addr_str(); } | |
181 | inline const char *get_local_host_str() const { return m_local_addr->get_host_str(); } | |
182 | inline const char *get_local_addr_str() const { return m_local_addr->get_addr_str(); } | |
183 | inline IPAddress *get_mc_addr() const { return m_mc_addr; } | |
184 | inline IPAddress *get_local_addr() const { return m_local_addr; } | |
185 | inline unsigned short get_mc_port() const { return m_mc_addr->get_port(); } | |
186 | inline unsigned short get_local_port() const { return m_local_addr->get_port(); } | |
187 | private: | |
188 | HCNetworkHandler(const HCNetworkHandler& p_handler); | |
189 | HCNetworkHandler& operator=(const HCNetworkHandler& p_handler); | |
190 | ||
191 | IPAddress *m_mc_addr; | |
192 | IPAddress *m_local_addr; | |
193 | }; | |
194 | ||
195 | #endif // NETWORKHANDLER_H_ |