xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/netstuff.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Operations on network stuff.
2*6881a400Schristos    Copyright (C) 2018-2023 Free Software Foundation, Inc.
37d62b00eSchristos 
47d62b00eSchristos    This file is part of GDB.
57d62b00eSchristos 
67d62b00eSchristos    This program is free software; you can redistribute it and/or modify
77d62b00eSchristos    it under the terms of the GNU General Public License as published by
87d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
97d62b00eSchristos    (at your option) any later version.
107d62b00eSchristos 
117d62b00eSchristos    This program is distributed in the hope that it will be useful,
127d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
137d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147d62b00eSchristos    GNU General Public License for more details.
157d62b00eSchristos 
167d62b00eSchristos    You should have received a copy of the GNU General Public License
177d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
187d62b00eSchristos 
197d62b00eSchristos #ifndef COMMON_NETSTUFF_H
207d62b00eSchristos #define COMMON_NETSTUFF_H
217d62b00eSchristos 
227d62b00eSchristos #include <string>
237d62b00eSchristos 
247d62b00eSchristos /* Like NI_MAXHOST/NI_MAXSERV, but enough for numeric forms.  */
257d62b00eSchristos #define GDB_NI_MAX_ADDR 64
267d62b00eSchristos #define GDB_NI_MAX_PORT 16
277d62b00eSchristos 
287d62b00eSchristos /* Helper class to guarantee that we always call 'freeaddrinfo'.  */
297d62b00eSchristos 
307d62b00eSchristos class scoped_free_addrinfo
317d62b00eSchristos {
327d62b00eSchristos public:
337d62b00eSchristos   /* Default constructor.  */
347d62b00eSchristos   explicit scoped_free_addrinfo (struct addrinfo *ainfo)
357d62b00eSchristos     : m_res (ainfo)
367d62b00eSchristos   {
377d62b00eSchristos   }
387d62b00eSchristos 
397d62b00eSchristos   /* Destructor responsible for free'ing M_RES by calling
407d62b00eSchristos      'freeaddrinfo'.  */
417d62b00eSchristos   ~scoped_free_addrinfo ();
427d62b00eSchristos 
437d62b00eSchristos   DISABLE_COPY_AND_ASSIGN (scoped_free_addrinfo);
447d62b00eSchristos 
457d62b00eSchristos private:
467d62b00eSchristos   /* The addrinfo resource.  */
477d62b00eSchristos   struct addrinfo *m_res;
487d62b00eSchristos };
497d62b00eSchristos 
507d62b00eSchristos /* The struct we return after parsing the connection spec.  */
517d62b00eSchristos 
527d62b00eSchristos struct parsed_connection_spec
537d62b00eSchristos {
547d62b00eSchristos   /* The hostname.  */
557d62b00eSchristos   std::string host_str;
567d62b00eSchristos 
577d62b00eSchristos   /* The port, if any.  */
587d62b00eSchristos   std::string port_str;
597d62b00eSchristos };
607d62b00eSchristos 
617d62b00eSchristos 
627d62b00eSchristos /* Parse SPEC (which is a string in the form of "ADDR:PORT") and
637d62b00eSchristos    return a 'parsed_connection_spec' structure with the proper fields
647d62b00eSchristos    filled in.  Also adjust HINT accordingly.  */
657d62b00eSchristos extern parsed_connection_spec
667d62b00eSchristos   parse_connection_spec_without_prefix (std::string spec,
677d62b00eSchristos 					struct addrinfo *hint);
687d62b00eSchristos 
697d62b00eSchristos /* Parse SPEC (which is a string in the form of
707d62b00eSchristos    "[tcp[6]:|udp[6]:]ADDR:PORT") and return a 'parsed_connection_spec'
717d62b00eSchristos    structure with the proper fields filled in.  Also adjust HINT
727d62b00eSchristos    accordingly.  */
737d62b00eSchristos extern parsed_connection_spec parse_connection_spec (const char *spec,
747d62b00eSchristos 						     struct addrinfo *hint);
757d62b00eSchristos 
767d62b00eSchristos #endif /* COMMON_NETSTUFF_H */
77