2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-10-05 01:17:07 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/file/udp_options.h>
|
2016-10-05 01:17:07 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <iterator>
|
2016-10-05 01:17:07 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/flags/flag.h>
|
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
#include <absl/strings/numbers.h>
|
|
|
|
#include <absl/strings/str_split.h>
|
2016-10-05 01:17:07 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/kv_pairs/kv_pairs.h>
|
|
|
|
|
|
|
|
ABSL_FLAG(std::string,
|
|
|
|
udp_interface_address,
|
|
|
|
"",
|
|
|
|
"IP address of the interface over which to receive UDP unicast"
|
|
|
|
" or multicast streams");
|
2016-10-05 01:17:07 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum FieldType {
|
|
|
|
kUnknownField = 0,
|
2018-06-07 18:20:12 +00:00
|
|
|
kBufferSizeField,
|
2016-10-05 01:17:07 +00:00
|
|
|
kInterfaceAddressField,
|
2018-03-06 18:09:28 +00:00
|
|
|
kMulticastSourceField,
|
2018-06-07 18:20:12 +00:00
|
|
|
kReuseField,
|
2016-10-05 01:17:07 +00:00
|
|
|
kTimeoutField,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FieldNameToTypeMapping {
|
|
|
|
const char* field_name;
|
|
|
|
FieldType field_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
|
2018-06-07 18:20:12 +00:00
|
|
|
{"buffer_size", kBufferSizeField},
|
2016-10-05 01:17:07 +00:00
|
|
|
{"interface", kInterfaceAddressField},
|
2018-06-07 18:20:12 +00:00
|
|
|
{"reuse", kReuseField},
|
2018-03-06 18:09:28 +00:00
|
|
|
{"source", kMulticastSourceField},
|
2016-10-05 01:17:07 +00:00
|
|
|
{"timeout", kTimeoutField},
|
|
|
|
};
|
|
|
|
|
|
|
|
FieldType GetFieldType(const std::string& field_name) {
|
2023-12-01 17:32:19 +00:00
|
|
|
for (size_t idx = 0; idx < std::size(kFieldNameTypeMappings); ++idx) {
|
2016-10-05 01:17:07 +00:00
|
|
|
if (field_name == kFieldNameTypeMappings[idx].field_name)
|
|
|
|
return kFieldNameTypeMappings[idx].field_type;
|
|
|
|
}
|
|
|
|
return kUnknownField;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
bool StringToAddressAndPort(std::string_view addr_and_port,
|
2016-10-05 01:17:07 +00:00
|
|
|
std::string* addr,
|
|
|
|
uint16_t* port) {
|
|
|
|
DCHECK(addr);
|
|
|
|
DCHECK(port);
|
|
|
|
|
|
|
|
const size_t colon_pos = addr_and_port.find(':');
|
2023-12-01 17:32:19 +00:00
|
|
|
if (colon_pos == std::string_view::npos) {
|
2016-10-05 01:17:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-12-01 17:32:19 +00:00
|
|
|
*addr = addr_and_port.substr(0, colon_pos);
|
|
|
|
|
|
|
|
// NOTE: SimpleAtoi will not take a uint16_t. So we check the bounds of the
|
|
|
|
// value and then cast to uint16_t.
|
|
|
|
uint32_t port_value;
|
|
|
|
if (!absl::SimpleAtoi(addr_and_port.substr(colon_pos + 1), &port_value) ||
|
2016-10-05 01:17:07 +00:00
|
|
|
(port_value > 65535)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-12-01 17:32:19 +00:00
|
|
|
*port = static_cast<uint16_t>(port_value);
|
2016-10-05 01:17:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<UdpOptions> UdpOptions::ParseFromString(
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string_view udp_url) {
|
2016-10-05 01:17:07 +00:00
|
|
|
std::unique_ptr<UdpOptions> options(new UdpOptions);
|
|
|
|
|
|
|
|
const size_t question_mark_pos = udp_url.find('?');
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string_view address_str = udp_url.substr(0, question_mark_pos);
|
2016-10-05 01:17:07 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
if (question_mark_pos != std::string_view::npos) {
|
|
|
|
std::string_view options_str = udp_url.substr(question_mark_pos + 1);
|
|
|
|
std::vector<KVPair> kv_pairs = SplitStringIntoKeyValuePairs(options_str);
|
2016-10-05 01:17:07 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
for (const auto& pair : kv_pairs) {
|
2016-10-05 01:17:07 +00:00
|
|
|
switch (GetFieldType(pair.first)) {
|
2018-06-07 18:20:12 +00:00
|
|
|
case kBufferSizeField:
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!absl::SimpleAtoi(pair.second, &options->buffer_size_)) {
|
2018-06-07 18:20:12 +00:00
|
|
|
LOG(ERROR) << "Invalid udp option for buffer_size field "
|
|
|
|
<< pair.second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kInterfaceAddressField:
|
|
|
|
options->interface_address_ = pair.second;
|
|
|
|
break;
|
|
|
|
case kMulticastSourceField:
|
|
|
|
options->source_address_ = pair.second;
|
|
|
|
options->is_source_specific_multicast_ = true;
|
|
|
|
break;
|
2016-10-05 01:17:07 +00:00
|
|
|
case kReuseField: {
|
|
|
|
int reuse_value = 0;
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!absl::SimpleAtoi(pair.second, &reuse_value)) {
|
2016-10-05 01:17:07 +00:00
|
|
|
LOG(ERROR) << "Invalid udp option for reuse field " << pair.second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
options->reuse_ = reuse_value > 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTimeoutField:
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!absl::SimpleAtoi(pair.second, &options->timeout_us_)) {
|
2017-07-10 18:26:22 +00:00
|
|
|
LOG(ERROR) << "Invalid udp option for timeout field "
|
|
|
|
<< pair.second;
|
2016-10-05 01:17:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Unknown field in udp options (\"" << pair.first
|
|
|
|
<< "\").";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!absl::GetFlag(FLAGS_udp_interface_address).empty()) {
|
2016-10-05 01:17:07 +00:00
|
|
|
LOG(WARNING) << "--udp_interface_address is deprecated. Consider switching "
|
|
|
|
"to udp options instead, something like "
|
|
|
|
"udp:://ip:port?interface=interface_ip.";
|
2023-12-01 17:32:19 +00:00
|
|
|
options->interface_address_ = absl::GetFlag(FLAGS_udp_interface_address);
|
2016-10-05 01:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!StringToAddressAndPort(address_str, &options->address_,
|
|
|
|
&options->port_)) {
|
|
|
|
LOG(ERROR) << "Malformed address:port UDP url " << address_str;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shaka
|