18dffb485Schristos /* Operations on network stuff. 2*5ba1f45fSchristos Copyright (C) 2018-2024 Free Software Foundation, Inc. 38dffb485Schristos 48dffb485Schristos This file is part of GDB. 58dffb485Schristos 68dffb485Schristos This program is free software; you can redistribute it and/or modify 78dffb485Schristos it under the terms of the GNU General Public License as published by 88dffb485Schristos the Free Software Foundation; either version 3 of the License, or 98dffb485Schristos (at your option) any later version. 108dffb485Schristos 118dffb485Schristos This program is distributed in the hope that it will be useful, 128dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 138dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 148dffb485Schristos GNU General Public License for more details. 158dffb485Schristos 168dffb485Schristos You should have received a copy of the GNU General Public License 178dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 188dffb485Schristos 198dffb485Schristos #ifndef COMMON_NETSTUFF_H 208dffb485Schristos #define COMMON_NETSTUFF_H 218dffb485Schristos 228dffb485Schristos #include <string> 238dffb485Schristos 248dffb485Schristos /* Like NI_MAXHOST/NI_MAXSERV, but enough for numeric forms. */ 258dffb485Schristos #define GDB_NI_MAX_ADDR 64 268dffb485Schristos #define GDB_NI_MAX_PORT 16 278dffb485Schristos 288dffb485Schristos /* Helper class to guarantee that we always call 'freeaddrinfo'. */ 298dffb485Schristos 308dffb485Schristos class scoped_free_addrinfo 318dffb485Schristos { 328dffb485Schristos public: 338dffb485Schristos /* Default constructor. */ 348dffb485Schristos explicit scoped_free_addrinfo (struct addrinfo *ainfo) 358dffb485Schristos : m_res (ainfo) 368dffb485Schristos { 378dffb485Schristos } 388dffb485Schristos 398dffb485Schristos /* Destructor responsible for free'ing M_RES by calling 408dffb485Schristos 'freeaddrinfo'. */ 418dffb485Schristos ~scoped_free_addrinfo (); 428dffb485Schristos 438dffb485Schristos DISABLE_COPY_AND_ASSIGN (scoped_free_addrinfo); 448dffb485Schristos 458dffb485Schristos private: 468dffb485Schristos /* The addrinfo resource. */ 478dffb485Schristos struct addrinfo *m_res; 488dffb485Schristos }; 498dffb485Schristos 508dffb485Schristos /* The struct we return after parsing the connection spec. */ 518dffb485Schristos 528dffb485Schristos struct parsed_connection_spec 538dffb485Schristos { 548dffb485Schristos /* The hostname. */ 558dffb485Schristos std::string host_str; 568dffb485Schristos 578dffb485Schristos /* The port, if any. */ 588dffb485Schristos std::string port_str; 598dffb485Schristos }; 608dffb485Schristos 618dffb485Schristos 628dffb485Schristos /* Parse SPEC (which is a string in the form of "ADDR:PORT") and 638dffb485Schristos return a 'parsed_connection_spec' structure with the proper fields 648dffb485Schristos filled in. Also adjust HINT accordingly. */ 658dffb485Schristos extern parsed_connection_spec 668dffb485Schristos parse_connection_spec_without_prefix (std::string spec, 678dffb485Schristos struct addrinfo *hint); 688dffb485Schristos 698dffb485Schristos /* Parse SPEC (which is a string in the form of 708dffb485Schristos "[tcp[6]:|udp[6]:]ADDR:PORT") and return a 'parsed_connection_spec' 718dffb485Schristos structure with the proper fields filled in. Also adjust HINT 728dffb485Schristos accordingly. */ 738dffb485Schristos extern parsed_connection_spec parse_connection_spec (const char *spec, 748dffb485Schristos struct addrinfo *hint); 758dffb485Schristos 768dffb485Schristos #endif /* COMMON_NETSTUFF_H */ 77