forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipv4_address.cpp
More file actions
174 lines (145 loc) · 3.51 KB
/
Copy pathipv4_address.cpp
File metadata and controls
174 lines (145 loc) · 3.51 KB
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/net/ipv4_address.hpp>
namespace
{
namespace local
{
constexpr bool is_digit(char c)
{
return c >= '0' && c <= '9';
}
constexpr std::uint8_t digit_value(char c)
{
return static_cast<std::uint8_t>(c - '0');
}
}
}
std::optional<cppcoro::net::ipv4_address>
cppcoro::net::ipv4_address::from_string(std::string_view string) noexcept
{
if (string.empty()) return std::nullopt;
if (!local::is_digit(string[0]))
{
return std::nullopt;
}
const auto length = string.length();
std::uint8_t partValues[4];
if (string[0] == '0' && length > 1)
{
if (local::is_digit(string[1]))
{
// Octal format (not supported)
return std::nullopt;
}
else if (string[1] == 'x')
{
// Hexadecimal format (not supported)
return std::nullopt;
}
}
// Parse the first integer.
// Could be a single 32-bit integer or first integer in a dotted decimal string.
std::size_t pos = 0;
{
constexpr std::uint32_t maxValue = 0xFFFFFFFFu / 10;
constexpr std::uint32_t maxDigit = 0xFFFFFFFFu % 10;
std::uint32_t partValue = local::digit_value(string[pos]);
++pos;
while (pos < length && local::is_digit(string[pos]))
{
const auto digitValue = local::digit_value(string[pos]);
++pos;
// Check if this digit would overflow the 32-bit integer
if (partValue > maxValue || (partValue == maxValue && digitValue > maxDigit))
{
return std::nullopt;
}
partValue = (partValue * 10) + digitValue;
}
if (pos == length)
{
// A single-integer string
return ipv4_address{ partValue };
}
else if (partValue > 255)
{
// Not a valid first component of dotted decimal
return std::nullopt;
}
partValues[0] = static_cast<std::uint8_t>(partValue);
}
for (int part = 1; part < 4; ++part)
{
if ((pos + 1) >= length || string[pos] != '.' || !local::is_digit(string[pos + 1]))
{
return std::nullopt;
}
// Skip the '.'
++pos;
// Check for an octal format (not yet supported)
const bool isPartOctal =
(pos + 1) < length &&
string[pos] == '0' &&
local::is_digit(string[pos + 1]);
if (isPartOctal)
{
return std::nullopt;
}
std::uint32_t partValue = local::digit_value(string[pos]);
++pos;
if (pos < length && local::is_digit(string[pos]))
{
partValue = (partValue * 10) + local::digit_value(string[pos]);
++pos;
if (pos < length && local::is_digit(string[pos]))
{
partValue = (partValue * 10) + local::digit_value(string[pos]);
if (partValue > 255)
{
return std::nullopt;
}
++pos;
}
}
partValues[part] = static_cast<std::uint8_t>(partValue);
}
if (pos < length)
{
// Extra chars after end of a valid IPv4 string
return std::nullopt;
}
return ipv4_address{ partValues };
}
std::string cppcoro::net::ipv4_address::to_string() const
{
// Buffer is large enough to hold larges ip address
// "xxx.xxx.xxx.xxx"
char buffer[15];
char* c = &buffer[0];
for (int i = 0; i < 4; ++i)
{
if (i > 0)
{
*c++ = '.';
}
if (m_bytes[i] >= 100)
{
*c++ = '0' + (m_bytes[i] / 100);
*c++ = '0' + (m_bytes[i] % 100) / 10;
*c++ = '0' + (m_bytes[i] % 10);
}
else if (m_bytes[i] >= 10)
{
*c++ = '0' + (m_bytes[i] / 10);
*c++ = '0' + (m_bytes[i] % 10);
}
else
{
*c++ = '0' + m_bytes[i];
}
}
return std::string{ &buffer[0], c };
}