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
14 ******************************************************************************/
15 #ifndef NETWORKHANDLER_H_
16 #define NETWORKHANDLER_H_
19 // platform.h includes sys/socket.h
20 #include <netinet/in.h>
25 #include <cygwin/version.h>
27 #if CYGWIN_VERSION_DLL_MAJOR >= 1007
35 // For legacy (e.g. Solaris 6) systems.
36 #ifndef INET_ADDRSTRLEN
37 #define INET_ADDRSTRLEN 16
39 #ifndef INET6_ADDRSTRLEN
40 #define INET6_ADDRSTRLEN 46
43 #define NI_MAXHOST 1025
46 typedef enum { ipv0 = -1, ipv4 = 0, ipv6 } NetworkFamily;
52 #if defined(LINUX) || defined(CYGWIN17)
56 class HCNetworkHandler;
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;
85 class IPv4Address : public IPAddress {
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.
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);
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;
113 char m_host_str[NI_MAXHOST]; // DNS name.
114 char m_addr_str[INET_ADDRSTRLEN]; // Address in numeric format.
117 #if defined(LINUX) || defined(CYGWIN17)
118 class IPv6Address : public IPAddress {
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.
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);
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;
146 char m_host_str[NI_MAXHOST]; // DNS name.
147 char m_addr_str[INET6_ADDRSTRLEN]; // Address in numeric format.
149 #endif // LINUX || CYGWIN17
151 class NetworkHandler {
154 NetworkHandler(const NetworkFamily& p_family);
155 NetworkHandler(const char *p_addr);
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; }
161 static int socket(const NetworkFamily& p_family);
163 NetworkHandler(const NetworkHandler& p_handler);
164 NetworkHandler& operator=(const NetworkHandler& p_handler);
166 NetworkFamily m_family;
169 class HCNetworkHandler : public NetworkHandler {
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(); }
188 HCNetworkHandler(const HCNetworkHandler& p_handler);
189 HCNetworkHandler& operator=(const HCNetworkHandler& p_handler);
191 IPAddress *m_mc_addr;
192 IPAddress *m_local_addr;
195 #endif // NETWORKHANDLER_H_