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