indentation problem in the generated code
[deliverable/titan.core.git] / common / NetworkHandler.hh
CommitLineData
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
46typedef enum { ipv0 = -1, ipv4 = 0, ipv6 } NetworkFamily;
47
48class Text_Buf;
49
50class IPAddress;
51class IPv4Address;
52#if defined(LINUX) || defined(CYGWIN17)
53class IPV6Address;
54#endif
55class NetworkHandler;
56class HCNetworkHandler;
57
58class IPAddress {
59public:
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
85class IPv4Address : public IPAddress {
86public:
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;
111private:
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)
118class IPv6Address : public IPAddress {
119public:
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;
144private:
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
151class NetworkHandler {
152public:
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);
162private:
163 NetworkHandler(const NetworkHandler& p_handler);
164 NetworkHandler& operator=(const NetworkHandler& p_handler);
165protected:
166 NetworkFamily m_family;
167};
168
169class HCNetworkHandler : public NetworkHandler {
170public:
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(); }
187private:
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_
This page took 0.031541 seconds and 5 git commands to generate.