common.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  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. #ifndef SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
  17. #define SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
  18. #include <sys/cdefs.h>
  19. #include <sys/socket.h>
  20. #include <unistd.h>
  21. #include <string>
  22. #include <android/multinetwork.h>
  23. enum class ApiMode {
  24. EXPLICIT,
  25. PROCESS,
  26. };
  27. struct Arguments {
  28. Arguments() : nethandle(NETWORK_UNSPECIFIED),
  29. api_mode(ApiMode::EXPLICIT),
  30. family(AF_UNSPEC),
  31. arg1(nullptr) {}
  32. ~Arguments();
  33. bool parseArguments(int argc, const char* argv[]);
  34. net_handle_t nethandle;
  35. ApiMode api_mode;
  36. sa_family_t family;
  37. const char* arg1;
  38. };
  39. void printUsage(const char *progname);
  40. // If port is non-zero returns strings of the form "192.0.2.1:port" or
  41. // "[2001:db8::1]:port", else it returns the bare IP string literal.
  42. std::string inetSockaddrToString(const sockaddr* sa);
  43. struct FdAutoCloser {
  44. FdAutoCloser() : fd(-1) {}
  45. /* not explicit */ FdAutoCloser(int fd) : fd(fd) {}
  46. ~FdAutoCloser() {
  47. if (fd > -1) {
  48. close(fd);
  49. }
  50. fd = -1;
  51. }
  52. int fd;
  53. };
  54. #endif // SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_