69 String IPAddress::to_string (WithPort withport) const |
69 String IPAddress::to_string (WithPort withport) const |
70 { |
70 { |
71 String val; |
71 String val; |
72 |
72 |
73 if (withport == WITH_PORT) |
73 if (withport == WITH_PORT) |
74 val.sprintf ("%u.%u.%u.%u:%u", octet (0), octet (1), octet (2), octet (3), port); |
74 val = sprintf ("%u.%u.%u.%u:%u", octet (0), octet (1), octet (2), octet (3), port); |
75 else |
75 else |
76 val.sprintf ("%u.%u.%u.%u", octet (0), octet (1), octet (2), octet (3)); |
76 val = sprintf ("%u.%u.%u.%u", octet (0), octet (1), octet (2), octet (3)); |
77 |
77 |
78 return val; |
78 return val; |
79 } |
79 } |
80 |
80 |
81 // ----------------------------------------------------------------------------- |
81 // ----------------------------------------------------------------------------- |
143 // |
143 // |
144 IPAddress IPAddress::from_string (String input) |
144 IPAddress IPAddress::from_string (String input) |
145 { |
145 { |
146 unsigned int parts[4]; |
146 unsigned int parts[4]; |
147 int colonpos = input.find (":"); |
147 int colonpos = input.find (":"); |
148 String addressString = colonpos == -1 ? input : input.mid (0, colonpos); |
148 String addressString = colonpos == -1 ? input : mid(input, 0, colonpos); |
149 IPAddress value; |
149 IPAddress value; |
150 |
150 |
151 // Try scanf the IPv4 host first |
151 // Try scanf the IPv4 host first |
152 if (sscanf (addressString, "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3])) |
152 if (sscanf(addressString.data(), "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3])) |
153 { |
153 { |
154 for (int i : range(4)) |
154 for (int i : range(4)) |
155 value.set_octet (i, parts[i]); |
155 value.set_octet (i, parts[i]); |
156 } |
156 } |
157 else |
157 else |
159 // Possibly a hostname, try resolve it |
159 // Possibly a hostname, try resolve it |
160 value = IPAddress::resolve (addressString); |
160 value = IPAddress::resolve (addressString); |
161 } |
161 } |
162 |
162 |
163 if (colonpos != -1) |
163 if (colonpos != -1) |
164 value.port = (unsigned short) input.mid (colonpos + 1, -1).toInt(); |
164 { |
|
165 std::optional<long> opt = to_int(mid(input, colonpos + 1, -1).data()); |
|
166 if (opt.has_value()) |
|
167 { |
|
168 value.port = opt.value(); |
|
169 } |
|
170 } |
165 |
171 |
166 return value; |
172 return value; |
167 } |
173 } |
168 |
174 |
169 // ----------------------------------------------------------------------------- |
175 // ----------------------------------------------------------------------------- |
173 AddrInfo hints; |
179 AddrInfo hints; |
174 AddrInfo* lookup; |
180 AddrInfo* lookup; |
175 memset (&hints, 0, sizeof hints); |
181 memset (&hints, 0, sizeof hints); |
176 hints.ai_family = AF_INET; |
182 hints.ai_family = AF_INET; |
177 |
183 |
178 if (getaddrinfo (node, nullptr, &hints, &lookup) != 0) |
184 if (getaddrinfo(node.data(), nullptr, &hints, &lookup) != 0) |
179 throw StringParseError ("unknown host " + node); |
185 throw StringParseError ("unknown host " + node); |
180 |
186 |
181 IPAddress result; |
187 IPAddress result; |
182 assert (lookup != nullptr); |
188 assert (lookup != nullptr); |
183 sockaddr_in* addr = reinterpret_cast<sockaddr_in*> (lookup[0].ai_addr); |
189 sockaddr_in* addr = reinterpret_cast<sockaddr_in*> (lookup[0].ai_addr); |