netstuff.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Operations on network stuff.
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_NETSTUFF_H
  15. #define COMMON_NETSTUFF_H
  16. #include <string>
  17. /* Like NI_MAXHOST/NI_MAXSERV, but enough for numeric forms. */
  18. #define GDB_NI_MAX_ADDR 64
  19. #define GDB_NI_MAX_PORT 16
  20. /* Helper class to guarantee that we always call 'freeaddrinfo'. */
  21. class scoped_free_addrinfo
  22. {
  23. public:
  24. /* Default constructor. */
  25. explicit scoped_free_addrinfo (struct addrinfo *ainfo)
  26. : m_res (ainfo)
  27. {
  28. }
  29. /* Destructor responsible for free'ing M_RES by calling
  30. 'freeaddrinfo'. */
  31. ~scoped_free_addrinfo ();
  32. DISABLE_COPY_AND_ASSIGN (scoped_free_addrinfo);
  33. private:
  34. /* The addrinfo resource. */
  35. struct addrinfo *m_res;
  36. };
  37. /* The struct we return after parsing the connection spec. */
  38. struct parsed_connection_spec
  39. {
  40. /* The hostname. */
  41. std::string host_str;
  42. /* The port, if any. */
  43. std::string port_str;
  44. };
  45. /* Parse SPEC (which is a string in the form of "ADDR:PORT") and
  46. return a 'parsed_connection_spec' structure with the proper fields
  47. filled in. Also adjust HINT accordingly. */
  48. extern parsed_connection_spec
  49. parse_connection_spec_without_prefix (std::string spec,
  50. struct addrinfo *hint);
  51. /* Parse SPEC (which is a string in the form of
  52. "[tcp[6]:|udp[6]:]ADDR:PORT") and return a 'parsed_connection_spec'
  53. structure with the proper fields filled in. Also adjust HINT
  54. accordingly. */
  55. extern parsed_connection_spec parse_connection_spec (const char *spec,
  56. struct addrinfo *hint);
  57. #endif /* COMMON_NETSTUFF_H */