NetworkHandler.cc 13.1 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
balaskoa's avatar
balaskoa committed
2
 * Copyright (c) 2000-2018 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
 *
 * Contributors:
 *   Balasko, Jeno
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "NetworkHandler.hh"

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>

#include "memory.h"
#include "../core/Textbuf.hh"

IPAddress::~IPAddress() { }

NetworkHandler::NetworkHandler() : m_family(ipv4) { }

void NetworkHandler::set_family(const char *p_addr)
{
  if (!p_addr || IPv4Address::is_valid(p_addr)) m_family = ipv4;
#if defined(LINUX) || defined(CYGWIN17)
  else if (IPv6Address::is_valid(p_addr)) m_family = ipv6;
#endif
  else m_family = ipv0;
}

int NetworkHandler::socket()
{
  return socket(m_family);
}

int NetworkHandler::socket(const NetworkFamily& p_family)
{
#if defined(LINUX) || defined(CYGWIN17)
  return ::socket(p_family == ipv4 ? AF_INET : AF_INET6, SOCK_STREAM, 0);
#else
  (void)p_family;
  return ::socket(AF_INET, SOCK_STREAM, 0);
#endif
}

HCNetworkHandler::HCNetworkHandler() : m_mc_addr(NULL), m_local_addr(NULL) { }

HCNetworkHandler::~HCNetworkHandler()
{
  delete m_mc_addr;
  delete m_local_addr;
}

bool HCNetworkHandler::set_local_addr(const char *p_addr, unsigned short p_port)
{
  if (!p_addr) return false;
  switch (m_family) {
  case ipv4: m_local_addr = new IPv4Address(p_addr, p_port); break;
#if defined(LINUX) || defined(CYGWIN17)
  case ipv6: m_local_addr = new IPv6Address(p_addr, p_port); break;
#endif
  default:
    break;
  }
  return m_local_addr != NULL;
}

bool HCNetworkHandler::set_mc_addr(const char *p_addr, unsigned short p_port)
{
  if (!p_addr) return false;
  switch (m_family) {
  case ipv4: m_mc_addr = new IPv4Address(p_addr, p_port); break;
#if defined(LINUX) || defined(CYGWIN17)
  case ipv6: m_mc_addr = new IPv6Address(p_addr, p_port); break;
#endif
  default:
    break;
  }
  return m_mc_addr != NULL;
}

int HCNetworkHandler::getsockname_local_addr(int p_sockfd)
{
  if (m_local_addr != NULL) delete m_local_addr;
  switch (m_family) {
  case ipv4: m_local_addr = new IPv4Address(); break;
#if defined(LINUX) || defined(CYGWIN17)
  case ipv6: m_local_addr = new IPv6Address(); break;
#endif
  default: return -1;
  }
  return m_local_addr->getsockname(p_sockfd);
}

int HCNetworkHandler::bind_local_addr(int p_sockfd) const
{
  m_local_addr->set_port(0);
  return ::bind(p_sockfd, m_local_addr->get_addr(), m_local_addr->get_addr_len());
}

#ifdef SOLARIS
#define CAST_AWAY_CONST_FOR_SOLARIS6 (sockaddr *)
#else
#define CAST_AWAY_CONST_FOR_SOLARIS6
#endif

int HCNetworkHandler::connect_to_mc(int p_sockfd) const
{
  return ::connect(p_sockfd, CAST_AWAY_CONST_FOR_SOLARIS6 m_mc_addr->get_addr(),
    m_mc_addr->get_addr_len());
}

IPAddress *IPAddress::create_addr(const NetworkFamily& p_family)
{
  switch (p_family) {
  case ipv4:
    return new IPv4Address();
#if defined(LINUX) || defined(CYGWIN17)
  case ipv6:
    return new IPv6Address();
#endif
  default:
    return NULL;
  }
}

IPAddress *IPAddress::create_addr(const char *p_addr)
{
  if (p_addr == NULL)
    return NULL;
  if (IPv4Address::is_valid(p_addr))
    return new IPv4Address(p_addr);
#if defined(LINUX) || defined(CYGWIN17)
  else if (IPv6Address::is_valid(p_addr))
    return new IPv6Address(p_addr);
#endif
  else
    return NULL;
}

IPv4Address::IPv4Address()
{
  clean_up();
  m_addr.sin_family = AF_INET;
  m_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  m_addr.sin_port = htons(0);
}

IPv4Address::IPv4Address(const char *p_addr, unsigned short p_port)
{
  set_addr(p_addr, p_port);
}

IPv4Address::~IPv4Address()
{
  clean_up();
}

bool IPv4Address::is_valid(const char *p_addr)
{
  if (p_addr == NULL) {
    return false;
  } else {
    IPv4Address addr;
    return addr.set_addr(p_addr);
  }
}

bool IPv4Address::is_local() const
{
  // Or: (ntohl(m_addr.sin_addr.s_addr) & 0xff000000u) == 0x7f000000u.
  return m_addr.sin_addr.s_addr == inet_addr("127.0.0.1");
}

bool IPv4Address::set_addr(const char *p_addr, unsigned short p_port)
{
  clean_up();
  if (p_addr != NULL) {
    struct hostent *hptr = gethostbyname(p_addr);
191
    if (hptr != NULL && static_cast<size_t>(hptr->h_length) == sizeof(struct in_addr)) {
Elemer Lelik's avatar
Elemer Lelik committed
192
193
194
      m_addr.sin_family = AF_INET;
      m_addr.sin_port = htons(p_port);
      memset(m_addr.sin_zero, 0, sizeof(m_addr.sin_zero));
195
      memcpy(&m_addr.sin_addr, hptr->h_addr_list[0], static_cast<size_t>(hptr->h_length));
Elemer Lelik's avatar
Elemer Lelik committed
196
197
198
199
200
201
202
203
204
205
206
207
      strncpy(m_addr_str, inet_ntoa(m_addr.sin_addr), sizeof(m_addr_str));
      strncpy(m_host_str, hptr->h_name, sizeof(m_host_str));
      return true;
    }
  }
  return false;
}

int IPv4Address::accept(int p_sockfd)
{
  clean_up();
  socklen_type addrlen = sizeof(m_addr);
Kristof Szabados's avatar
Kristof Szabados committed
208
  int fd = ::accept(p_sockfd, reinterpret_cast<struct sockaddr *>(&m_addr), &addrlen);
Elemer Lelik's avatar
Elemer Lelik committed
209
210
211
212
  if (fd >= 0) {
    strncpy(m_addr_str, inet_ntoa(m_addr.sin_addr), sizeof(m_addr_str));
    if (m_addr.sin_addr.s_addr != htonl(INADDR_ANY)) {
      struct hostent *hptr =
Kristof Szabados's avatar
Kristof Szabados committed
213
        gethostbyaddr(reinterpret_cast<const char *>(&m_addr.sin_addr),
Elemer Lelik's avatar
Elemer Lelik committed
214
                      sizeof(m_addr.sin_addr), m_addr.sin_family);
215
      if (hptr != NULL && static_cast<size_t>(hptr->h_length) == sizeof(struct in_addr)) {
Elemer Lelik's avatar
Elemer Lelik committed
216
217
218
219
220
221
222
223
224
225
226
        strncpy(m_host_str, hptr->h_name, sizeof(m_host_str));
      }
    }
  }
  return fd;
}

int IPv4Address::getsockname(int p_sockfd)
{
  clean_up();
  socklen_type addrlen = sizeof(m_addr);
Kristof Szabados's avatar
Kristof Szabados committed
227
  int s = ::getsockname(p_sockfd, reinterpret_cast<struct sockaddr *>(&m_addr), &addrlen);
Elemer Lelik's avatar
Elemer Lelik committed
228
229
230
231
  if (s >= 0) {
    strncpy(m_addr_str, inet_ntoa(m_addr.sin_addr), sizeof(m_addr_str));
    if (m_addr.sin_addr.s_addr != htonl(INADDR_ANY)) {
      struct hostent *hptr =
Kristof Szabados's avatar
Kristof Szabados committed
232
        gethostbyaddr(reinterpret_cast<const char *>(&m_addr.sin_addr),
Elemer Lelik's avatar
Elemer Lelik committed
233
                      sizeof(m_addr.sin_addr), m_addr.sin_family);
234
      if (hptr != NULL && static_cast<size_t>(hptr->h_length) == sizeof(struct in_addr)) {
Elemer Lelik's avatar
Elemer Lelik committed
235
236
237
238
239
240
241
242
243
        strncpy(m_host_str, hptr->h_name, sizeof(m_host_str));
      }
    }
  }
  return s;
}

bool IPv4Address::operator==(const IPAddress& p_addr) const
{
244
  return memcmp(&m_addr.sin_addr, &((static_cast<const IPv4Address&>(p_addr)).m_addr.sin_addr), sizeof(m_addr.sin_addr)) == 0;
Elemer Lelik's avatar
Elemer Lelik committed
245
246
247
248
249
250
251
252
253
254
}

bool IPv4Address::operator!=(const IPAddress& p_addr) const
{
  return !(*this == p_addr);
}

IPAddress& IPv4Address::operator=(const IPAddress& p_addr)
{
  clean_up();
255
256
257
  memcpy(&m_addr, &(static_cast<const IPv4Address&>(p_addr)).m_addr, sizeof(m_addr));
  strncpy(m_host_str, (static_cast<const IPv4Address&>(p_addr)).m_host_str, sizeof(m_host_str));
  strncpy(m_addr_str, (static_cast<const IPv4Address&>(p_addr)).m_addr_str, sizeof(m_addr_str));
Elemer Lelik's avatar
Elemer Lelik committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  return *this;
}

void IPv4Address::push_raw(Text_Buf& p_buf) const
{
  p_buf.push_raw(sizeof(m_addr.sin_family), &m_addr.sin_family);
  p_buf.push_raw(sizeof(m_addr.sin_port), &m_addr.sin_port);
  p_buf.push_raw(sizeof(m_addr.sin_addr), &m_addr.sin_addr);
  p_buf.push_raw(sizeof(m_addr.sin_zero), &m_addr.sin_zero);
}

void IPv4Address::pull_raw(Text_Buf& p_buf)
{
  clean_up();
  p_buf.pull_raw(sizeof(m_addr.sin_family), &m_addr.sin_family);
  p_buf.pull_raw(sizeof(m_addr.sin_port), &m_addr.sin_port);
  p_buf.pull_raw(sizeof(m_addr.sin_addr), &m_addr.sin_addr);
  p_buf.pull_raw(sizeof(m_addr.sin_zero), &m_addr.sin_zero);
}

void IPv4Address::clean_up()
{
  memset(&m_addr, 0, sizeof(m_addr));
  memset(m_host_str, 0, sizeof(m_host_str));
  memset(m_addr_str, 0, sizeof(m_addr_str));
}

#if defined(LINUX) || defined(CYGWIN17)
IPv6Address::IPv6Address()
{
  clean_up();
  m_addr.sin6_family = AF_INET6;
  m_addr.sin6_addr = in6addr_any;
  m_addr.sin6_port = htons(0);
}

IPv6Address::IPv6Address(const char *p_addr, unsigned short p_port)
{
  set_addr(p_addr, p_port);
}

IPv6Address::~IPv6Address()
{
  clean_up();
}

const char *IPv6Address::get_addr_str() const
{
  if (strlen(m_addr_str) > 0) {
    // The host name should always contain the interface's name for link-local addresses.
    return (strlen(m_host_str) > 0 && strstr(m_host_str, "%") != NULL) ? m_host_str : m_addr_str;
  } else {
    return m_host_str;
  }
}

bool IPv6Address::set_addr(const char *p_addr, unsigned short p_port)
{
  clean_up();
  struct addrinfo hints, *res;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
  hints.ai_family = AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;  // Any.
  char p_port_str[6];
  memset(p_port_str, 0, sizeof(p_port_str));
  snprintf(p_port_str, sizeof(p_port_str), "%u", p_port);
  int s = getaddrinfo(p_addr, p_port_str, &hints, &res);
  if (s == 0) {
328
    struct sockaddr_in6 *addr = static_cast<struct sockaddr_in6 *>(static_cast<void*>(res->ai_addr));
Elemer Lelik's avatar
Elemer Lelik committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    // The (void*) shuts up the "cast increases required alignment" warning.
    // Hopefully, the res->ai_addr points to a properly aligned sockaddr_in6
    // and we won't have problems like these if we decide to support Sparc:
    // http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=340384
    memcpy(&m_addr, addr, sizeof(m_addr));
    m_addr.sin6_port = htons(p_port);
    inet_ntop(res->ai_family, &(addr->sin6_addr), m_addr_str, sizeof(m_addr_str));
    strncpy(m_host_str, res->ai_canonname, sizeof(m_host_str));
    freeaddrinfo(res);
    return true;
  }
  return false;
}

int IPv6Address::accept(int p_sockfd)
{
  clean_up();
  socklen_type addrlen = sizeof(m_addr);
Kristof Szabados's avatar
Kristof Szabados committed
347
  int fd = ::accept(p_sockfd, reinterpret_cast<struct sockaddr *>(&m_addr), &addrlen);
Elemer Lelik's avatar
Elemer Lelik committed
348
349
350
351
352
  if (fd >= 0) {
    if (!inet_ntop(AF_INET6, &m_addr.sin6_addr, m_addr_str, sizeof(m_addr_str))) {
      fprintf(stderr, "IPv6Address::accept(): Unable to convert IPv6 address "
              "from binary to text form: %s\n", strerror(errno));
    }
Kristof Szabados's avatar
Kristof Szabados committed
353
    int s = getnameinfo(reinterpret_cast<struct sockaddr *>(&m_addr), sizeof(m_addr),
Elemer Lelik's avatar
Elemer Lelik committed
354
355
356
357
358
359
360
361
362
363
364
365
366
                        m_host_str, sizeof(m_host_str), NULL, 0, 0);
    if (s != 0) {
      fprintf(stderr, "IPv6Address::accept(): Address to name translation "
              "failed: %s\n", gai_strerror(s));
    }
  }
  return fd;
}

int IPv6Address::getsockname(int p_sockfd)
{
  clean_up();
  socklen_type addrlen = sizeof(m_addr);
Kristof Szabados's avatar
Kristof Szabados committed
367
  int s1 = ::getsockname(p_sockfd, reinterpret_cast<struct sockaddr *>(&m_addr), &addrlen);
Elemer Lelik's avatar
Elemer Lelik committed
368
369
370
371
372
  if (s1 >= 0) {
    if (!inet_ntop(AF_INET6, &m_addr.sin6_addr, m_addr_str, sizeof(m_addr_str))) {
      fprintf(stderr, "IPv6Address::getsockname(): Unable to convert IPv6 "
              "address from binary to text form: %s\n", strerror(errno));
    }
Kristof Szabados's avatar
Kristof Szabados committed
373
    int s2 = getnameinfo(reinterpret_cast<struct sockaddr *>(&m_addr), sizeof(m_addr),
Elemer Lelik's avatar
Elemer Lelik committed
374
375
376
377
378
379
380
381
382
383
384
                         m_host_str, sizeof(m_host_str), NULL, 0, 0);
    if (s2 != 0) {
      fprintf(stderr, "IPv6Address::getsockname(): Address to name "
              "translation failed: %s\n", gai_strerror(s2));
    }
  }
  return s1;
}

bool IPv6Address::operator==(const IPAddress& p_addr) const
{
385
  return memcmp(&m_addr.sin6_addr, &((static_cast<const IPv6Address&>(p_addr)).m_addr.sin6_addr), sizeof(m_addr.sin6_addr)) == 0;
Elemer Lelik's avatar
Elemer Lelik committed
386
387
388
389
390
391
392
393
394
395
}

bool IPv6Address::operator!=(const IPAddress& p_addr) const
{
  return !(*this == p_addr);
}

IPAddress& IPv6Address::operator=(const IPAddress& p_addr)
{
  clean_up();
396
397
398
  memcpy(&m_addr, &(static_cast<const IPv6Address&>(p_addr)).m_addr, sizeof(m_addr));
  strncpy(m_host_str, (static_cast<const IPv6Address&>(p_addr)).m_host_str, sizeof(m_host_str));
  strncpy(m_addr_str, (static_cast<const IPv6Address&>(p_addr)).m_addr_str, sizeof(m_addr_str));
Elemer Lelik's avatar
Elemer Lelik committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  return *this;
}

void IPv6Address::push_raw(Text_Buf& p_buf) const
{
  p_buf.push_raw(sizeof(m_addr.sin6_family), &m_addr.sin6_family);
  p_buf.push_raw(sizeof(m_addr.sin6_port), &m_addr.sin6_port);
  p_buf.push_raw(sizeof(m_addr.sin6_flowinfo), &m_addr.sin6_flowinfo);
  p_buf.push_raw(sizeof(m_addr.sin6_addr), &m_addr.sin6_addr);
  p_buf.push_raw(sizeof(m_addr.sin6_scope_id), &m_addr.sin6_scope_id);
}

void IPv6Address::pull_raw(Text_Buf& p_buf)
{
  clean_up();
  p_buf.pull_raw(sizeof(m_addr.sin6_family), &m_addr.sin6_family);
  p_buf.pull_raw(sizeof(m_addr.sin6_port), &m_addr.sin6_port);
  p_buf.pull_raw(sizeof(m_addr.sin6_flowinfo), &m_addr.sin6_flowinfo);
  p_buf.pull_raw(sizeof(m_addr.sin6_addr), &m_addr.sin6_addr);
  p_buf.pull_raw(sizeof(m_addr.sin6_scope_id), &m_addr.sin6_scope_id);
}

void IPv6Address::clean_up()
{
  memset(&m_addr, 0, sizeof(m_addr));
  memset(m_host_str, 0, sizeof(m_host_str));
  memset(m_addr_str, 0, sizeof(m_addr_str));
}

bool IPv6Address::is_local() const
{
  const unsigned char localhost_bytes[] =
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  const unsigned char mapped_ipv4_localhost[] =
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1 };

  return (memcmp(m_addr.sin6_addr.s6_addr, localhost_bytes, 16) == 0
          || memcmp(m_addr.sin6_addr.s6_addr, mapped_ipv4_localhost, 16) == 0);
}

bool IPv6Address::is_valid(const char *p_addr)
{
  if (p_addr == NULL) {
    return false;
  } else {
    IPv6Address addr;
    return addr.set_addr(p_addr);
  }
}
#endif // LINUX || CYGWIN17