YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/net/sockaddr.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#include "yb/util/net/sockaddr.h"
33
34
#include <stdio.h>
35
#include <string.h>
36
37
#include <string>
38
39
#include <boost/functional/hash.hpp>
40
#include <boost/lexical_cast.hpp>
41
42
#include "yb/gutil/macros.h"
43
#include "yb/gutil/stringprintf.h"
44
#include "yb/util/result.h"
45
#include "yb/util/status_format.h"
46
47
namespace yb {
48
49
using strings::Substitute;
50
51
10.9M
std::string ToString(const Endpoint& endpoint) {
52
10.9M
  return boost::lexical_cast<std::string>(endpoint);
53
10.9M
}
54
55
45.3M
std::size_t hash_value(const IpAddress& address) {
56
45.3M
  size_t seed = 0;
57
58
45.3M
  if (address.is_v4()) {
59
45.2M
    boost::hash_combine(seed, address.to_v4().to_ulong());
60
59.6k
  } else {
61
59.6k
    boost::hash_combine(seed, address.to_v6().to_bytes());
62
59.6k
  }
63
64
45.3M
  return seed;
65
45.3M
}
66
67
45.1M
std::size_t hash_value(const Endpoint& endpoint) {
68
45.1M
  size_t seed = 0;
69
70
45.1M
  boost::hash_combine(seed, hash_value(endpoint.address()));
71
45.1M
  boost::hash_combine(seed, endpoint.port());
72
73
45.1M
  return seed;
74
45.1M
}
75
76
918
Result<Endpoint> ParseEndpoint(const std::string& input, uint16_t default_port) {
77
918
  boost::system::error_code ec;
78
  // First of all we try to parse whole string as address w/o port.
79
  // Important for IPv6 addesses like fe80::1:1.
80
918
  auto address = IpAddress::from_string(input, ec);
81
918
  if (!ec) {
82
3
    return Endpoint(address, default_port);
83
3
  }
84
85
915
  std::string::size_type pos = 0;
86
915
  std::string::size_type address_begin, address_end;
87
88
915
  if (!input.empty() && input[0] == '[') {
89
7
    pos = input.find(']');
90
7
    if (pos != std::string::npos) {
91
6
      address_begin = 1;
92
6
      address_end = pos;
93
6
      if (++pos >= input.size()) {
94
2
        pos = std::string::npos;
95
2
      }
96
1
    } else {
97
1
      return STATUS_SUBSTITUTE(NetworkError, "']' missing in $0", input);
98
1
    }
99
908
  } else {
100
908
    address_begin = 0;
101
908
    pos = input.find(':');
102
908
    address_end = pos != std::string::npos ? pos : input.size();
103
908
  }
104
914
  auto address_str = input.substr(address_begin, address_end - address_begin);
105
914
  address = IpAddress::from_string(address_str, ec);
106
914
  if (ec) {
107
1
    return STATUS_FORMAT(NetworkError, "Failed to parse $0: $1", input, ec);
108
1
  }
109
913
  if (pos == std::string::npos) {
110
2
    return Endpoint(address, default_port);
111
2
  }
112
911
  if (input[pos] != ':') {
113
0
    return STATUS_SUBSTITUTE(NetworkError, "':' missing after ']' in $0", input);
114
0
  }
115
911
  ++pos;
116
911
  if (pos == input.size()) {
117
1
    return STATUS_SUBSTITUTE(NetworkError, "Port not specified in $0", input);
118
1
  }
119
910
  char *end = nullptr;
120
910
  auto port = strtoul(input.c_str() + pos, &end, 10);
121
910
  if (port > 0xffff || end != input.c_str() + input.size()) {
122
0
    return STATUS_SUBSTITUTE(NetworkError, "Invalid port in $0", input);
123
0
  }
124
910
  return Endpoint(address, port);
125
910
}
126
127
} // namespace yb