reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   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
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  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
  328
//===-- SocketAddress.cpp ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Note: This file is used on Darwin by debugserver, so it needs to remain as
//       self contained as possible, and devoid of references to LLVM unless 
//       there is compelling reason.
//
//===----------------------------------------------------------------------===//

#if defined(_MSC_VER)
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif

#include "lldb/Host/SocketAddress.h"
#include <stddef.h>
#include <stdio.h>

#if !defined(_WIN32)
#include <arpa/inet.h>
#endif

#include <assert.h>
#include <string.h>

#include "lldb/Host/PosixApi.h"

// WindowsXP needs an inet_ntop implementation
#ifdef _WIN32

#ifndef INET6_ADDRSTRLEN // might not be defined in older Windows SDKs
#define INET6_ADDRSTRLEN 46
#endif

// TODO: implement shortened form "::" for runs of zeros
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) {
  if (size == 0) {
    return nullptr;
  }

  switch (af) {
  case AF_INET: {
    {
      const char *formatted = inet_ntoa(*static_cast<const in_addr *>(src));
      if (formatted && strlen(formatted) < static_cast<size_t>(size)) {
        return ::strcpy(dst, formatted);
      }
    }
    return nullptr;
  case AF_INET6: {
    char tmp[INET6_ADDRSTRLEN] = {0};
    const uint16_t *src16 = static_cast<const uint16_t *>(src);
    int full_size = ::snprintf(
        tmp, sizeof(tmp), "%x:%x:%x:%x:%x:%x:%x:%x", ntohs(src16[0]),
        ntohs(src16[1]), ntohs(src16[2]), ntohs(src16[3]), ntohs(src16[4]),
        ntohs(src16[5]), ntohs(src16[6]), ntohs(src16[7]));
    if (full_size < static_cast<int>(size)) {
      return ::strcpy(dst, tmp);
    }
    return nullptr;
  }
  }
  }
  return nullptr;
}
#endif

using namespace lldb_private;

// SocketAddress constructor
SocketAddress::SocketAddress() { Clear(); }

SocketAddress::SocketAddress(const struct sockaddr &s) { m_socket_addr.sa = s; }

SocketAddress::SocketAddress(const struct sockaddr_in &s) {
  m_socket_addr.sa_ipv4 = s;
}

SocketAddress::SocketAddress(const struct sockaddr_in6 &s) {
  m_socket_addr.sa_ipv6 = s;
}

SocketAddress::SocketAddress(const struct sockaddr_storage &s) {
  m_socket_addr.sa_storage = s;
}

SocketAddress::SocketAddress(const struct addrinfo *addr_info) {
  *this = addr_info;
}

// Destructor
SocketAddress::~SocketAddress() {}

void SocketAddress::Clear() {
  memset(&m_socket_addr, 0, sizeof(m_socket_addr));
}

bool SocketAddress::IsValid() const { return GetLength() != 0; }

static socklen_t GetFamilyLength(sa_family_t family) {
  switch (family) {
  case AF_INET:
    return sizeof(struct sockaddr_in);
  case AF_INET6:
    return sizeof(struct sockaddr_in6);
  }
  assert(0 && "Unsupported address family");
  return 0;
}

socklen_t SocketAddress::GetLength() const {
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
  return m_socket_addr.sa.sa_len;
#else
  return GetFamilyLength(GetFamily());
#endif
}

socklen_t SocketAddress::GetMaxLength() { return sizeof(sockaddr_t); }

sa_family_t SocketAddress::GetFamily() const {
  return m_socket_addr.sa.sa_family;
}

void SocketAddress::SetFamily(sa_family_t family) {
  m_socket_addr.sa.sa_family = family;
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
  m_socket_addr.sa.sa_len = GetFamilyLength(family);
#endif
}

std::string SocketAddress::GetIPAddress() const {
  char str[INET6_ADDRSTRLEN] = {0};
  switch (GetFamily()) {
  case AF_INET:
    if (inet_ntop(GetFamily(), &m_socket_addr.sa_ipv4.sin_addr, str,
                  sizeof(str)))
      return str;
    break;
  case AF_INET6:
    if (inet_ntop(GetFamily(), &m_socket_addr.sa_ipv6.sin6_addr, str,
                  sizeof(str)))
      return str;
    break;
  }
  return "";
}

uint16_t SocketAddress::GetPort() const {
  switch (GetFamily()) {
  case AF_INET:
    return ntohs(m_socket_addr.sa_ipv4.sin_port);
  case AF_INET6:
    return ntohs(m_socket_addr.sa_ipv6.sin6_port);
  }
  return 0;
}

bool SocketAddress::SetPort(uint16_t port) {
  switch (GetFamily()) {
  case AF_INET:
    m_socket_addr.sa_ipv4.sin_port = htons(port);
    return true;

  case AF_INET6:
    m_socket_addr.sa_ipv6.sin6_port = htons(port);
    return true;
  }
  return false;
}

// SocketAddress assignment operator
const SocketAddress &SocketAddress::operator=(const SocketAddress &rhs) {
  if (this != &rhs)
    m_socket_addr = rhs.m_socket_addr;
  return *this;
}

const SocketAddress &SocketAddress::
operator=(const struct addrinfo *addr_info) {
  Clear();
  if (addr_info && addr_info->ai_addr && addr_info->ai_addrlen > 0 &&
      size_t(addr_info->ai_addrlen) <= sizeof m_socket_addr) {
    ::memcpy(&m_socket_addr, addr_info->ai_addr, addr_info->ai_addrlen);
  }
  return *this;
}

const SocketAddress &SocketAddress::operator=(const struct sockaddr &s) {
  m_socket_addr.sa = s;
  return *this;
}

const SocketAddress &SocketAddress::operator=(const struct sockaddr_in &s) {
  m_socket_addr.sa_ipv4 = s;
  return *this;
}

const SocketAddress &SocketAddress::operator=(const struct sockaddr_in6 &s) {
  m_socket_addr.sa_ipv6 = s;
  return *this;
}

const SocketAddress &SocketAddress::
operator=(const struct sockaddr_storage &s) {
  m_socket_addr.sa_storage = s;
  return *this;
}

bool SocketAddress::getaddrinfo(const char *host, const char *service,
                                int ai_family, int ai_socktype, int ai_protocol,
                                int ai_flags) {
  Clear();

  auto addresses = GetAddressInfo(host, service, ai_family, ai_socktype,
                                  ai_protocol, ai_flags);
  if (!addresses.empty())
    *this = addresses[0];
  return IsValid();
}

std::vector<SocketAddress>
SocketAddress::GetAddressInfo(const char *hostname, const char *servname,
                              int ai_family, int ai_socktype, int ai_protocol,
                              int ai_flags) {
  std::vector<SocketAddress> addr_list;

  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = ai_family;
  hints.ai_socktype = ai_socktype;
  hints.ai_protocol = ai_protocol;
  hints.ai_flags = ai_flags;

  struct addrinfo *service_info_list = nullptr;
  int err = ::getaddrinfo(hostname, servname, &hints, &service_info_list);
  if (err == 0 && service_info_list) {
    for (struct addrinfo *service_ptr = service_info_list;
         service_ptr != nullptr; service_ptr = service_ptr->ai_next) {
      addr_list.emplace_back(SocketAddress(service_ptr));
    }
  }

  if (service_info_list)
    ::freeaddrinfo(service_info_list);
  return addr_list;
}

bool SocketAddress::SetToLocalhost(sa_family_t family, uint16_t port) {
  switch (family) {
  case AF_INET:
    SetFamily(AF_INET);
    if (SetPort(port)) {
      m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
      return true;
    }
    break;

  case AF_INET6:
    SetFamily(AF_INET6);
    if (SetPort(port)) {
      m_socket_addr.sa_ipv6.sin6_addr = in6addr_loopback;
      return true;
    }
    break;
  }
  Clear();
  return false;
}

bool SocketAddress::SetToAnyAddress(sa_family_t family, uint16_t port) {
  switch (family) {
  case AF_INET:
    SetFamily(AF_INET);
    if (SetPort(port)) {
      m_socket_addr.sa_ipv4.sin_addr.s_addr = htonl(INADDR_ANY);
      return true;
    }
    break;

  case AF_INET6:
    SetFamily(AF_INET6);
    if (SetPort(port)) {
      m_socket_addr.sa_ipv6.sin6_addr = in6addr_any;
      return true;
    }
    break;
  }
  Clear();
  return false;
}

bool SocketAddress::IsAnyAddr() const {
  return (GetFamily() == AF_INET)
             ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_ANY)
             : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_any, 16);
}

bool SocketAddress::IsLocalhost() const {
  return (GetFamily() == AF_INET)
             ? m_socket_addr.sa_ipv4.sin_addr.s_addr == htonl(INADDR_LOOPBACK)
             : 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr, &in6addr_loopback,
                           16);
}

bool SocketAddress::operator==(const SocketAddress &rhs) const {
  if (GetFamily() != rhs.GetFamily())
    return false;
  if (GetLength() != rhs.GetLength())
    return false;
  switch (GetFamily()) {
  case AF_INET:
    return m_socket_addr.sa_ipv4.sin_addr.s_addr ==
           rhs.m_socket_addr.sa_ipv4.sin_addr.s_addr;
  case AF_INET6:
    return 0 == memcmp(&m_socket_addr.sa_ipv6.sin6_addr,
                       &rhs.m_socket_addr.sa_ipv6.sin6_addr, 16);
  }
  return false;
}

bool SocketAddress::operator!=(const SocketAddress &rhs) const {
  return !(*this == rhs);
}