You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

comm_util.cc 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "ps/comm/comm_util.h"
  17. #include <arpa/inet.h>
  18. #include <cstdio>
  19. #include <cstdlib>
  20. #include <cstring>
  21. #include <functional>
  22. #include <regex>
  23. namespace mindspore {
  24. namespace ps {
  25. namespace comm {
  26. bool CommUtil::CheckIpWithRegex(const std::string &ip) {
  27. std::regex pattern("((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");
  28. std::smatch res;
  29. if (regex_match(ip, res, pattern)) {
  30. return true;
  31. }
  32. return false;
  33. }
  34. void CommUtil::CheckIp(const std::string &ip) {
  35. if (!CheckIpWithRegex(ip)) {
  36. MS_LOG(EXCEPTION) << "Server address" << ip << " illegal!";
  37. }
  38. int64_t uAddr = inet_addr(ip.c_str());
  39. if (INADDR_NONE == uAddr) {
  40. MS_LOG(EXCEPTION) << "Server address illegal, inet_addr converting failed!";
  41. }
  42. }
  43. } // namespace comm
  44. } // namespace ps
  45. } // namespace mindspore