File: socket_address_posix.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (99 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/impl/socket_address_posix.h"

#include <cstring>
#include <vector>

#include "util/osp_logging.h"

namespace openscreen {

SocketAddressPosix::SocketAddressPosix(const struct sockaddr& address) {
  if (address.sa_family == AF_INET) {
    memcpy(&internal_address_, &address, sizeof(struct sockaddr_in));
    RecomputeEndpoint(IPAddress::Version::kV4);
  } else if (address.sa_family == AF_INET6) {
    memcpy(&internal_address_, &address, sizeof(struct sockaddr_in6));
    RecomputeEndpoint(IPAddress::Version::kV6);
  } else {
    // Not IPv4 or IPv6.
    OSP_NOTREACHED();
  }
}

SocketAddressPosix::SocketAddressPosix(const IPEndpoint& endpoint)
    : endpoint_(endpoint) {
  if (endpoint.address.IsV4()) {
    internal_address_.v4.sin_family = AF_INET;
    internal_address_.v4.sin_port = htons(endpoint.port);
    endpoint.address.CopyToV4(
        reinterpret_cast<uint8_t*>(&internal_address_.v4.sin_addr.s_addr));
  } else {
    OSP_DCHECK(endpoint.address.IsV6());

    internal_address_.v6.sin6_family = AF_INET6;
    internal_address_.v6.sin6_flowinfo = 0;
    internal_address_.v6.sin6_scope_id = 0;
    internal_address_.v6.sin6_port = htons(endpoint.port);
    endpoint.address.CopyToV6(
        reinterpret_cast<uint8_t*>(&internal_address_.v6.sin6_addr));
  }
}

struct sockaddr* SocketAddressPosix::address() {
  switch (version()) {
    case IPAddress::Version::kV4:
      return reinterpret_cast<struct sockaddr*>(&internal_address_.v4);
    case IPAddress::Version::kV6:
      return reinterpret_cast<struct sockaddr*>(&internal_address_.v6);
    default:
      OSP_NOTREACHED();
  }
}

const struct sockaddr* SocketAddressPosix::address() const {
  switch (version()) {
    case IPAddress::Version::kV4:
      return reinterpret_cast<const struct sockaddr*>(&internal_address_.v4);
    case IPAddress::Version::kV6:
      return reinterpret_cast<const struct sockaddr*>(&internal_address_.v6);
    default:
      OSP_NOTREACHED();
  }
}

socklen_t SocketAddressPosix::size() const {
  switch (version()) {
    case IPAddress::Version::kV4:
      return sizeof(struct sockaddr_in);
    case IPAddress::Version::kV6:
      return sizeof(struct sockaddr_in6);
    default:
      OSP_NOTREACHED();
  }
}

void SocketAddressPosix::RecomputeEndpoint() {
  RecomputeEndpoint(endpoint_.address.version());
}

void SocketAddressPosix::RecomputeEndpoint(IPAddress::Version version) {
  switch (version) {
    case IPAddress::Version::kV4:
      endpoint_.address = IPAddress(IPAddress::Version::kV4,
                                    reinterpret_cast<const uint8_t*>(
                                        &internal_address_.v4.sin_addr.s_addr));
      endpoint_.port = ntohs(internal_address_.v4.sin_port);
      break;
    case IPAddress::Version::kV6:
      endpoint_.address = IPAddress(IPAddress::Version::kV6,
                                    internal_address_.v6.sin6_addr.s6_addr);
      endpoint_.port = ntohs(internal_address_.v6.sin6_port);
      break;
  }
}

}  // namespace openscreen