Implement IPv6 support for GDB/gdbserver
[deliverable/binutils-gdb.git] / gdb / common / netstuff.c
CommitLineData
c7ab0aef
SDJ
1/* Operations on network stuff.
2 Copyright (C) 2018 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#include "common-defs.h"
20#include "netstuff.h"
21#include <algorithm>
22
23#ifdef USE_WIN32API
24#include <winsock2.h>
25#include <wspiapi.h>
26#else
27#include <netinet/in.h>
28#include <arpa/inet.h>
29#include <netdb.h>
30#include <sys/socket.h>
31#include <netinet/tcp.h>
32#endif
33
34/* See common/netstuff.h. */
35
36scoped_free_addrinfo::~scoped_free_addrinfo ()
37{
38 freeaddrinfo (m_res);
39}
40
41/* See common/netstuff.h. */
42
43parsed_connection_spec
44parse_connection_spec_without_prefix (std::string spec, struct addrinfo *hint)
45{
46 parsed_connection_spec ret;
47 size_t last_colon_pos = 0;
48 /* We're dealing with IPv6 if:
49
50 - ai_family is AF_INET6, or
51 - ai_family is not AF_INET, and
52 - spec[0] is '[', or
53 - the number of ':' on spec is greater than 1. */
54 bool is_ipv6 = (hint->ai_family == AF_INET6
55 || (hint->ai_family != AF_INET
56 && (spec[0] == '['
57 || std::count (spec.begin (),
58 spec.end (), ':') > 1)));
59
60 if (is_ipv6)
61 {
62 if (spec[0] == '[')
63 {
64 /* IPv6 addresses can be written as '[ADDR]:PORT', and we
65 support this notation. */
66 size_t close_bracket_pos = spec.find_first_of (']');
67
68 if (close_bracket_pos == std::string::npos)
69 error (_("Missing close bracket in hostname '%s'"),
70 spec.c_str ());
71
72 hint->ai_family = AF_INET6;
73
74 const char c = spec[close_bracket_pos + 1];
75
76 if (c == '\0')
77 last_colon_pos = std::string::npos;
78 else if (c != ':')
79 error (_("Invalid cruft after close bracket in '%s'"),
80 spec.c_str ());
81
82 /* Erase both '[' and ']'. */
83 spec.erase (0, 1);
84 spec.erase (close_bracket_pos - 1, 1);
85 }
86 else if (spec.find_first_of (']') != std::string::npos)
87 error (_("Missing open bracket in hostname '%s'"),
88 spec.c_str ());
89 }
90
91 if (last_colon_pos == 0)
92 last_colon_pos = spec.find_last_of (':');
93
94 /* The length of the hostname part. */
95 size_t host_len;
96
97 if (last_colon_pos != std::string::npos)
98 {
99 /* The user has provided a port. */
100 host_len = last_colon_pos;
101 ret.port_str = spec.substr (last_colon_pos + 1);
102 }
103 else
104 host_len = spec.size ();
105
106 ret.host_str = spec.substr (0, host_len);
107
108 /* Default hostname is localhost. */
109 if (ret.host_str.empty ())
110 ret.host_str = "localhost";
111
112 return ret;
113}
114
115/* See common/netstuff.h. */
116
117parsed_connection_spec
118parse_connection_spec (const char *spec, struct addrinfo *hint)
119{
120 /* Struct to hold the association between valid prefixes, their
121 family and socktype. */
122 struct host_prefix
123 {
124 /* The prefix. */
125 const char *prefix;
126
127 /* The 'ai_family'. */
128 int family;
129
130 /* The 'ai_socktype'. */
131 int socktype;
132 };
133 static const struct host_prefix prefixes[] =
134 {
135 { "udp:", AF_UNSPEC, SOCK_DGRAM },
136 { "tcp:", AF_UNSPEC, SOCK_STREAM },
137 { "udp4:", AF_INET, SOCK_DGRAM },
138 { "tcp4:", AF_INET, SOCK_STREAM },
139 { "udp6:", AF_INET6, SOCK_DGRAM },
140 { "tcp6:", AF_INET6, SOCK_STREAM },
141 };
142
143 for (const host_prefix prefix : prefixes)
144 if (startswith (spec, prefix.prefix))
145 {
146 spec += strlen (prefix.prefix);
147 hint->ai_family = prefix.family;
148 hint->ai_socktype = prefix.socktype;
149 hint->ai_protocol
150 = hint->ai_socktype == SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
151 break;
152 }
153
154 return parse_connection_spec_without_prefix (spec, hint);
155}
This page took 0.029348 seconds and 4 git commands to generate.