1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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 #ifndef NETWORKHANDLER_H_
9 #define NETWORKHANDLER_H_
12 // platform.h includes sys/socket.h
13 #include <netinet/in.h>
18 #include <cygwin/version.h>
20 #if CYGWIN_VERSION_DLL_MAJOR >= 1007
28 // For legacy (e.g. Solaris 6) systems.
29 #ifndef INET_ADDRSTRLEN
30 #define INET_ADDRSTRLEN 16
32 #ifndef INET6_ADDRSTRLEN
33 #define INET6_ADDRSTRLEN 46
36 #define NI_MAXHOST 1025
39 typedef enum { ipv0 = -1, ipv4 = 0, ipv6 } NetworkFamily;
45 #if defined(LINUX) || defined(CYGWIN17)
49 class HCNetworkHandler;
53 virtual ~IPAddress() = 0;
54 static IPAddress *create_addr(const NetworkFamily& p_family);
55 static IPAddress *create_addr(const char *p_addr);
56 // Always return something.
57 virtual const char *get_host_str() const = 0;
58 virtual const char *get_addr_str() const = 0;
59 virtual bool operator==(const IPAddress& p_addr) const = 0;
60 virtual bool operator!=(const IPAddress& p_addr) const = 0;
61 virtual IPAddress& operator=(const IPAddress& p_addr) = 0;
62 // Encode and decode the address and the corresponding port for internal
63 // communication. Used by connected ports.
64 virtual void push_raw(Text_Buf& p_buf) const = 0;
65 virtual void pull_raw(Text_Buf& p_buf) = 0;
66 virtual void clean_up() = 0;
67 virtual int accept(int p_sockfd) = 0;
68 // Return the current address and port to which the socket is bound to.
69 virtual int getsockname(int p_sockfd) = 0;
70 virtual unsigned short get_port() const = 0;
71 virtual void set_port(unsigned short p_port) = 0;
72 virtual bool set_addr(const char *p_addr, unsigned short p_port = 0) = 0;
73 virtual const struct sockaddr *get_addr() const = 0;
74 virtual socklen_type get_addr_len() const = 0;
75 virtual bool is_local() const = 0;
78 class IPv4Address : public IPAddress {
82 IPv4Address(const char *p_addr, unsigned short p_port = 0 /* Any port. */);
83 //IPv4Address(const IPv4Address& p_addr) = default;
84 //There are no pointers, so the compiler generated copy is OK.
87 bool operator==(const IPAddress& p_addr) const;
88 bool operator!=(const IPAddress& p_addr) const;
89 IPAddress& operator=(const IPAddress& p_addr);
90 void push_raw(Text_Buf& p_buf) const;
91 void pull_raw(Text_Buf& p_buf);
93 int accept(int p_sockfd);
94 int getsockname(int p_sockfd);
95 inline unsigned short get_port() const { return ntohs(m_addr.sin_port); }
96 inline void set_port(unsigned short p_port) { m_addr.sin_port = htons(p_port); }
97 bool set_addr(const char *p_addr, unsigned short p_port = 0);
98 inline const struct sockaddr *get_addr() const { return (const struct sockaddr *)&m_addr; }
99 inline socklen_type get_addr_len() const { return sizeof(m_addr); }
100 inline const char *get_host_str() const { return strlen(m_host_str) > 0 ? m_host_str : m_addr_str; }
101 inline const char *get_addr_str() const { return strlen(m_addr_str) > 0 ? m_addr_str : m_host_str; }
102 static bool is_valid(const char *p_addr);
103 bool is_local() const;
106 char m_host_str[NI_MAXHOST]; // DNS name.
107 char m_addr_str[INET_ADDRSTRLEN]; // Address in numeric format.
110 #if defined(LINUX) || defined(CYGWIN17)
111 class IPv6Address : public IPAddress {
115 IPv6Address(const char *p_addr, unsigned short p_port = 0 /* Any port. */);
116 //IPv6Address(const IPv6Address& p_addr) = default;
117 //There are no pointers, so the compiler generated copy is OK.
120 bool operator==(const IPAddress& p_addr) const;
121 bool operator!=(const IPAddress& p_addr) const;
122 IPAddress& operator=(const IPAddress& p_addr);
123 void push_raw(Text_Buf& p_buf) const;
124 void pull_raw(Text_Buf& p_buf);
126 int accept(int p_sockfd);
127 int getsockname(int p_sockfd);
128 inline unsigned short get_port() const { return ntohs(m_addr.sin6_port); }
129 inline void set_port(unsigned short p_port) { m_addr.sin6_port = htons(p_port); }
130 bool set_addr(const char *p_addr, unsigned short p_port = 0);
131 inline const struct sockaddr *get_addr() const { return (const struct sockaddr *)&m_addr; }
132 inline socklen_type get_addr_len() const { return sizeof(m_addr); }
133 inline const char *get_host_str() const { return strlen(m_host_str) > 0 ? m_host_str : m_addr_str; }
134 const char *get_addr_str() const;
135 static bool is_valid(const char *p_addr);
136 bool is_local() const;
139 char m_host_str[NI_MAXHOST]; // DNS name.
140 char m_addr_str[INET6_ADDRSTRLEN]; // Address in numeric format.
142 #endif // LINUX || CYGWIN17
144 class NetworkHandler {
147 NetworkHandler(const NetworkFamily& p_family);
148 NetworkHandler(const char *p_addr);
150 inline void set_family(const NetworkFamily& p_family) { m_family = p_family; }
151 void set_family(const char *p_addr);
152 inline const NetworkFamily& get_family() const { return m_family; }
154 static int socket(const NetworkFamily& p_family);
156 NetworkHandler(const NetworkHandler& p_handler);
157 NetworkHandler& operator=(const NetworkHandler& p_handler);
159 NetworkFamily m_family;
162 class HCNetworkHandler : public NetworkHandler {
167 bool set_local_addr(const char *p_addr, unsigned short p_port = 0 /* Any port. */);
168 bool set_mc_addr(const char *p_addr, unsigned short p_port = 0 /* Any port. */);
169 int getsockname_local_addr(int p_sockfd);
170 int bind_local_addr(int p_sockfd) const;
171 int connect_to_mc(int p_sockfd) const;
172 inline const char *get_mc_host_str() const { return m_mc_addr->get_host_str(); }
173 inline const char *get_mc_addr_str() const { return m_mc_addr->get_addr_str(); }
174 inline const char *get_local_host_str() const { return m_local_addr->get_host_str(); }
175 inline const char *get_local_addr_str() const { return m_local_addr->get_addr_str(); }
176 inline IPAddress *get_mc_addr() const { return m_mc_addr; }
177 inline IPAddress *get_local_addr() const { return m_local_addr; }
178 inline unsigned short get_mc_port() const { return m_mc_addr->get_port(); }
179 inline unsigned short get_local_port() const { return m_local_addr->get_port(); }
181 HCNetworkHandler(const HCNetworkHandler& p_handler);
182 HCNetworkHandler& operator=(const HCNetworkHandler& p_handler);
184 IPAddress *m_mc_addr;
185 IPAddress *m_local_addr;
188 #endif // NETWORKHANDLER_H_