github-actions[bot] commented on code in PR #33931: URL: https://github.com/apache/doris/pull/33931#discussion_r1573352627
########## cloud/src/common/network_util.cpp: ########## @@ -0,0 +1,238 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "common/network_util.h" + +#include <arpa/inet.h> +#include <butil/endpoint.h> +#include <butil/strings/string_split.h> +#include <ifaddrs.h> +#include <netdb.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/types.h> + +#include <sstream> +#include <vector> + +#include "common/logging.h" + +namespace doris::cloud { + +class CIDR { +public: + CIDR() : address_(0), netmask_(0xffffffff) {} + bool reset(const std::string& cidr_str) { + address_ = 0; + netmask_ = 0xffffffff; + + // check if have mask + std::string cidr_format_str = cidr_str; + int32_t have_mask = cidr_str.find("/"); + if (have_mask == -1) { + cidr_format_str.assign(cidr_str + "/32"); + } + VLOG_DEBUG << "cidr format str: " << cidr_format_str; + + std::vector<std::string> cidr_items; + butil::SplitString(cidr_format_str, '/', &cidr_items); + if (cidr_items.size() != 2) { + LOG(WARNING) << "wrong CIDR format. network=" << cidr_str; + return false; + } + + if (cidr_items[1].empty()) { + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str; + return false; + } + + char* endptr = nullptr; + int32_t mask_length = strtol(cidr_items[1].c_str(), &endptr, 10); + if (errno != 0 && mask_length == 0) { + char errmsg[64]; + // Ignore unused return value + auto ret = strerror_r(errno, errmsg, 64); Review Comment: warning: 'auto ret' can be declared as 'auto *ret' [readability-qualified-auto] ```suggestion auto *ret = strerror_r(errno, errmsg, 64); ``` ########## cloud/src/common/network_util.cpp: ########## @@ -0,0 +1,238 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "common/network_util.h" + +#include <arpa/inet.h> +#include <butil/endpoint.h> +#include <butil/strings/string_split.h> +#include <ifaddrs.h> +#include <netdb.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/types.h> + +#include <sstream> +#include <vector> + +#include "common/logging.h" + +namespace doris::cloud { + +class CIDR { +public: + CIDR() : address_(0), netmask_(0xffffffff) {} + bool reset(const std::string& cidr_str) { + address_ = 0; + netmask_ = 0xffffffff; + + // check if have mask + std::string cidr_format_str = cidr_str; + int32_t have_mask = cidr_str.find("/"); + if (have_mask == -1) { + cidr_format_str.assign(cidr_str + "/32"); + } + VLOG_DEBUG << "cidr format str: " << cidr_format_str; + + std::vector<std::string> cidr_items; + butil::SplitString(cidr_format_str, '/', &cidr_items); + if (cidr_items.size() != 2) { + LOG(WARNING) << "wrong CIDR format. network=" << cidr_str; + return false; + } + + if (cidr_items[1].empty()) { + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str; + return false; + } + + char* endptr = nullptr; + int32_t mask_length = strtol(cidr_items[1].c_str(), &endptr, 10); + if (errno != 0 && mask_length == 0) { + char errmsg[64]; + // Ignore unused return value + auto ret = strerror_r(errno, errmsg, 64); + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str + << ", mask_length=" << mask_length << ", errno=" << errno + << ", errmsg=" << errmsg << ", strerror_r returns=" << ret; + return false; + } + if (mask_length <= 0 || mask_length > 32) { + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str + << ", mask_length=" << mask_length; + return false; + } + + uint32_t address = 0; + if (!ip_to_int(cidr_items[0], &address)) { + LOG(WARNING) << "wrong CIDR IP value. network=" << cidr_str; + return false; + } + address_ = address; + + netmask_ = 0xffffffff; + netmask_ = netmask_ << (32 - mask_length); + return true; + } + bool contains(const std::string& ip) { + uint32_t ip_int = 0; + if (!ip_to_int(ip, &ip_int)) { + return false; + } + if ((address_ & netmask_) == (ip_int & netmask_)) { + return true; + } + return false; + } + +private: + bool ip_to_int(const std::string& ip_str, uint32_t* value) { + struct in_addr addr; + int flag = inet_aton(ip_str.c_str(), &addr); + if (flag == 0) { + return false; + } + *value = ntohl(addr.s_addr); + return true; + } + + uint32_t address_; Review Comment: warning: use default member initializer for 'address_' [modernize-use-default-member-init] cloud/src/common/network_util.cpp:37: ```diff - CIDR() : address_(0), netmask_(0xffffffff) {} + CIDR() : , netmask_(0xffffffff) {} ``` ```suggestion uint32_t address_{0}; ``` ########## cloud/src/common/network_util.cpp: ########## @@ -0,0 +1,238 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "common/network_util.h" Review Comment: warning: 'common/network_util.h' file not found [clang-diagnostic-error] ```cpp #include "common/network_util.h" ^ ``` ########## cloud/src/common/network_util.cpp: ########## @@ -0,0 +1,238 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "common/network_util.h" + +#include <arpa/inet.h> +#include <butil/endpoint.h> +#include <butil/strings/string_split.h> +#include <ifaddrs.h> +#include <netdb.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/types.h> + +#include <sstream> +#include <vector> + +#include "common/logging.h" + +namespace doris::cloud { + +class CIDR { +public: + CIDR() : address_(0), netmask_(0xffffffff) {} + bool reset(const std::string& cidr_str) { + address_ = 0; + netmask_ = 0xffffffff; + + // check if have mask + std::string cidr_format_str = cidr_str; + int32_t have_mask = cidr_str.find("/"); + if (have_mask == -1) { + cidr_format_str.assign(cidr_str + "/32"); + } + VLOG_DEBUG << "cidr format str: " << cidr_format_str; + + std::vector<std::string> cidr_items; + butil::SplitString(cidr_format_str, '/', &cidr_items); + if (cidr_items.size() != 2) { + LOG(WARNING) << "wrong CIDR format. network=" << cidr_str; + return false; + } + + if (cidr_items[1].empty()) { + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str; + return false; + } + + char* endptr = nullptr; + int32_t mask_length = strtol(cidr_items[1].c_str(), &endptr, 10); + if (errno != 0 && mask_length == 0) { + char errmsg[64]; + // Ignore unused return value + auto ret = strerror_r(errno, errmsg, 64); + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str + << ", mask_length=" << mask_length << ", errno=" << errno + << ", errmsg=" << errmsg << ", strerror_r returns=" << ret; + return false; + } + if (mask_length <= 0 || mask_length > 32) { + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str + << ", mask_length=" << mask_length; + return false; + } + + uint32_t address = 0; + if (!ip_to_int(cidr_items[0], &address)) { + LOG(WARNING) << "wrong CIDR IP value. network=" << cidr_str; + return false; + } + address_ = address; + + netmask_ = 0xffffffff; + netmask_ = netmask_ << (32 - mask_length); + return true; + } + bool contains(const std::string& ip) { Review Comment: warning: method 'contains' can be made const [readability-make-member-function-const] ```suggestion bool contains(const std::string& ip) const { ``` ########## cloud/test/network_util_test.cpp: ########## @@ -0,0 +1,48 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "common/network_util.h" Review Comment: warning: 'common/network_util.h' file not found [clang-diagnostic-error] ```cpp #include "common/network_util.h" ^ ``` ########## cloud/src/common/network_util.cpp: ########## @@ -0,0 +1,238 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "common/network_util.h" + +#include <arpa/inet.h> +#include <butil/endpoint.h> +#include <butil/strings/string_split.h> +#include <ifaddrs.h> +#include <netdb.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/types.h> + +#include <sstream> +#include <vector> + +#include "common/logging.h" + +namespace doris::cloud { + +class CIDR { +public: + CIDR() : address_(0), netmask_(0xffffffff) {} + bool reset(const std::string& cidr_str) { + address_ = 0; + netmask_ = 0xffffffff; + + // check if have mask + std::string cidr_format_str = cidr_str; + int32_t have_mask = cidr_str.find("/"); + if (have_mask == -1) { + cidr_format_str.assign(cidr_str + "/32"); + } + VLOG_DEBUG << "cidr format str: " << cidr_format_str; + + std::vector<std::string> cidr_items; + butil::SplitString(cidr_format_str, '/', &cidr_items); + if (cidr_items.size() != 2) { + LOG(WARNING) << "wrong CIDR format. network=" << cidr_str; + return false; + } + + if (cidr_items[1].empty()) { + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str; + return false; + } + + char* endptr = nullptr; + int32_t mask_length = strtol(cidr_items[1].c_str(), &endptr, 10); + if (errno != 0 && mask_length == 0) { + char errmsg[64]; + // Ignore unused return value + auto ret = strerror_r(errno, errmsg, 64); + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str + << ", mask_length=" << mask_length << ", errno=" << errno + << ", errmsg=" << errmsg << ", strerror_r returns=" << ret; + return false; + } + if (mask_length <= 0 || mask_length > 32) { + LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str + << ", mask_length=" << mask_length; + return false; + } + + uint32_t address = 0; + if (!ip_to_int(cidr_items[0], &address)) { + LOG(WARNING) << "wrong CIDR IP value. network=" << cidr_str; + return false; + } + address_ = address; + + netmask_ = 0xffffffff; + netmask_ = netmask_ << (32 - mask_length); + return true; + } + bool contains(const std::string& ip) { + uint32_t ip_int = 0; + if (!ip_to_int(ip, &ip_int)) { + return false; + } + if ((address_ & netmask_) == (ip_int & netmask_)) { + return true; + } + return false; + } + +private: + bool ip_to_int(const std::string& ip_str, uint32_t* value) { + struct in_addr addr; + int flag = inet_aton(ip_str.c_str(), &addr); + if (flag == 0) { + return false; + } + *value = ntohl(addr.s_addr); + return true; + } + + uint32_t address_; + uint32_t netmask_; Review Comment: warning: use default member initializer for 'netmask_' [modernize-use-default-member-init] cloud/src/common/network_util.cpp:37: ```diff - CIDR() : address_(0), netmask_(0xffffffff) {} + CIDR() : address_(0), {} ``` ```suggestion uint32_t netmask_{0xffffffff}; ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org