Titan Core Initial Contribution
[deliverable/titan.core.git] / common / NetworkHandler.hh
CommitLineData
970ed795
EL
1///////////////////////////////////////////////////////////////////////////////
2// Copyright (c) 2000-2014 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_
10
11#include "platform.h"
12// platform.h includes sys/socket.h
13#include <netinet/in.h>
14#include <netdb.h>
15#include <string.h>
16
17#ifdef WIN32
18#include <cygwin/version.h>
19
20#if CYGWIN_VERSION_DLL_MAJOR >= 1007
21#define CYGWIN17
22#else
23#define CYGWIN15
24#endif
25
26#endif
27
28// For legacy (e.g. Solaris 6) systems.
29#ifndef INET_ADDRSTRLEN
30#define INET_ADDRSTRLEN 16
31#endif
32#ifndef INET6_ADDRSTRLEN
33#define INET6_ADDRSTRLEN 46
34#endif
35#ifndef NI_MAXHOST
36#define NI_MAXHOST 1025
37#endif
38
39typedef enum { ipv0 = -1, ipv4 = 0, ipv6 } NetworkFamily;
40
41class Text_Buf;
42
43class IPAddress;
44class IPv4Address;
45#if defined(LINUX) || defined(CYGWIN17)
46class IPV6Address;
47#endif
48class NetworkHandler;
49class HCNetworkHandler;
50
51class IPAddress {
52public:
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;
76};
77
78class IPv4Address : public IPAddress {
79public:
80 IPv4Address();
81 // Does DNS lookup.
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.
85 ~IPv4Address();
86
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);
92 void clean_up();
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;
104private:
105 sockaddr_in m_addr;
106 char m_host_str[NI_MAXHOST]; // DNS name.
107 char m_addr_str[INET_ADDRSTRLEN]; // Address in numeric format.
108};
109
110#if defined(LINUX) || defined(CYGWIN17)
111class IPv6Address : public IPAddress {
112public:
113 IPv6Address();
114 // Does DNS lookup.
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.
118 ~IPv6Address();
119
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);
125 void clean_up();
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;
137private:
138 sockaddr_in6 m_addr;
139 char m_host_str[NI_MAXHOST]; // DNS name.
140 char m_addr_str[INET6_ADDRSTRLEN]; // Address in numeric format.
141};
142#endif // LINUX || CYGWIN17
143
144class NetworkHandler {
145public:
146 NetworkHandler();
147 NetworkHandler(const NetworkFamily& p_family);
148 NetworkHandler(const char *p_addr);
149
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; }
153 int socket();
154 static int socket(const NetworkFamily& p_family);
155private:
156 NetworkHandler(const NetworkHandler& p_handler);
157 NetworkHandler& operator=(const NetworkHandler& p_handler);
158protected:
159 NetworkFamily m_family;
160};
161
162class HCNetworkHandler : public NetworkHandler {
163public:
164 HCNetworkHandler();
165 ~HCNetworkHandler();
166
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(); }
180private:
181 HCNetworkHandler(const HCNetworkHandler& p_handler);
182 HCNetworkHandler& operator=(const HCNetworkHandler& p_handler);
183
184 IPAddress *m_mc_addr;
185 IPAddress *m_local_addr;
186};
187
188#endif // NETWORKHANDLER_H_
This page took 0.031529 seconds and 5 git commands to generate.