Skip to content

Commit f2ac74b

Browse files
authored
[mono] Move networking code to debugger component (#82785)
The debugger is the only component left in Mono using the networking code, move the code there so we can avoid compiling it into the main runtime and also simplify it a bit.
1 parent e7926d6 commit f2ac74b

File tree

10 files changed

+257
-391
lines changed

10 files changed

+257
-391
lines changed

src/mono/mono/component/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ list(APPEND components
2424
set(${MONO_DEBUGGER_COMPONENT_NAME}-sources
2525
${MONO_COMPONENT_PATH}/debugger.c
2626
${MONO_COMPONENT_PATH}/debugger.h
27+
${MONO_COMPONENT_PATH}/debugger-networking.c
28+
${MONO_COMPONENT_PATH}/debugger-networking.h
2729
${MONO_COMPONENT_PATH}/debugger-agent.c
2830
${MONO_COMPONENT_PATH}/debugger-agent.h
2931
${MONO_COMPONENT_PATH}/debugger-engine.c

src/mono/mono/component/debugger-agent.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@
8181
#include <mono/utils/mono-stack-unwinding.h>
8282
#include <mono/utils/mono-time.h>
8383
#include <mono/utils/mono-threads.h>
84-
#include <mono/utils/networking.h>
8584
#include <mono/utils/mono-proclib.h>
8685
#include <mono/utils/w32api.h>
8786
#include <mono/utils/mono-logger-internals.h>
8887
#include <mono/utils/mono-proclib.h>
8988

9089
#include <mono/component/debugger-state-machine.h>
9190
#include "debugger-agent.h"
91+
#include "debugger-networking.h"
9292
#include <mono/mini/mini.h>
9393
#include <mono/mini/seq-points.h>
9494
#include <mono/mini/aot-runtime.h>
@@ -1047,7 +1047,7 @@ socket_transport_connect (const char *address)
10471047
listen_fd = INVALID_SOCKET;
10481048

10491049
MONO_ENTER_GC_UNSAFE;
1050-
mono_networking_init();
1050+
mono_debugger_networking_init ();
10511051
MONO_EXIT_GC_UNSAFE;
10521052

10531053
if (host) {
@@ -1060,7 +1060,7 @@ socket_transport_connect (const char *address)
10601060
for (size_t i = 0; i < sizeof(hints) / sizeof(int); i++) {
10611061
/* Obtain address(es) matching host/port */
10621062
MONO_ENTER_GC_UNSAFE;
1063-
s = mono_get_address_info (host, port, hints[i], &result);
1063+
s = mono_debugger_get_address_info (host, port, hints[i], &result);
10641064
MONO_EXIT_GC_UNSAFE;
10651065
if (s == 0)
10661066
break;
@@ -1111,7 +1111,7 @@ socket_transport_connect (const char *address)
11111111
int n = 1;
11121112

11131113
MONO_ENTER_GC_UNSAFE;
1114-
mono_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port);
1114+
mono_debugger_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port);
11151115
MONO_EXIT_GC_UNSAFE;
11161116

11171117
sfd = socket (rp->family, rp->socktype, rp->protocol);
@@ -1133,7 +1133,7 @@ socket_transport_connect (const char *address)
11331133
}
11341134

11351135
MONO_ENTER_GC_UNSAFE;
1136-
mono_free_address_info (result);
1136+
mono_debugger_free_address_info (result);
11371137
MONO_EXIT_GC_UNSAFE;
11381138
}
11391139

@@ -1176,7 +1176,7 @@ socket_transport_connect (const char *address)
11761176
socklen_t sock_len;
11771177

11781178
MONO_ENTER_GC_UNSAFE;
1179-
mono_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port);
1179+
mono_debugger_socket_address_init (&sockaddr, &sock_len, rp->family, &rp->address, port);
11801180
MONO_EXIT_GC_UNSAFE;
11811181

11821182
sfd = socket (rp->family, rp->socktype,
@@ -1213,7 +1213,7 @@ socket_transport_connect (const char *address)
12131213
conn_fd = sfd;
12141214

12151215
MONO_ENTER_GC_UNSAFE;
1216-
mono_free_address_info (result);
1216+
mono_debugger_free_address_info (result);
12171217
MONO_EXIT_GC_UNSAFE;
12181218
}
12191219

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
/**
2+
* \file
3+
* Portable networking functions
4+
*
5+
* Author:
6+
* Rodrigo Kumpera ([email protected])
7+
*
8+
* (C) 2015 Xamarin
9+
*/
10+
11+
#include <mono/utils/mono-threads-coop.h>
12+
#include <glib.h>
13+
14+
#ifdef HAVE_NETDB_H
15+
#include <netdb.h>
16+
#endif
17+
#ifdef HAVE_SYS_IOCTL_H
18+
#include <sys/ioctl.h>
19+
#endif
20+
#ifdef HAVE_SYS_SOCKET_H
21+
#include <sys/socket.h>
22+
#endif
23+
#ifdef HAVE_NET_IF_H
24+
#include <net/if.h>
25+
#endif
26+
#ifdef HAVE_UNISTD_H
27+
#include <unistd.h>
28+
#endif
29+
30+
#include "debugger-networking.h"
31+
32+
33+
void
34+
mono_debugger_networking_init (void)
35+
{
36+
#ifdef HOST_WIN32
37+
WSADATA wsadata;
38+
int err;
39+
40+
err = WSAStartup (2 /* 2.0 */, &wsadata);
41+
if(err)
42+
g_error ("%s: Couldn't initialise networking", __func__);
43+
#endif
44+
}
45+
46+
void
47+
mono_debugger_networking_shutdown (void)
48+
{
49+
#ifdef HOST_WIN32
50+
WSACleanup ();
51+
#endif
52+
}
53+
54+
55+
/* port in host order, address in network order */
56+
void
57+
mono_debugger_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port)
58+
{
59+
memset (sa, 0, sizeof (MonoSocketAddress));
60+
if (family == AF_INET) {
61+
*len = sizeof (struct sockaddr_in);
62+
63+
sa->v4.sin_family = AF_INET;
64+
sa->v4.sin_addr = *(struct in_addr*)address;
65+
sa->v4.sin_port = htons (GINT_TO_UINT16 (port));
66+
#if HAVE_SOCKADDR_IN_SIN_LEN
67+
sa->v4.sin_len = sizeof (*len);
68+
#endif
69+
#ifdef HAVE_STRUCT_SOCKADDR_IN6
70+
} else if (family == AF_INET6) {
71+
*len = sizeof (struct sockaddr_in6);
72+
73+
sa->v6.sin6_family = AF_INET6;
74+
sa->v6.sin6_addr = *(struct in6_addr*)address;
75+
sa->v6.sin6_port = htons (GINT_TO_UINT16 (port));
76+
#if HAVE_SOCKADDR_IN6_SIN_LEN
77+
sa->v6.sin6_len = sizeof (*len);
78+
#endif
79+
#endif
80+
} else {
81+
g_error ("Cannot handle address family %d", family);
82+
}
83+
}
84+
85+
86+
void
87+
mono_debugger_free_address_info (MonoAddressInfo *ai)
88+
{
89+
MonoAddressEntry *cur = ai->entries, *next;
90+
while (cur) {
91+
next = cur->next;
92+
g_free ((void*)cur->canonical_name);
93+
g_free (cur);
94+
cur = next;
95+
}
96+
g_strfreev (ai->aliases);
97+
g_free (ai);
98+
}
99+
100+
101+
#if !defined (HAVE_GETADDRINFO) && (defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2))
102+
static void
103+
add_hostent (MonoAddressInfo *info, int flags, struct hostent *h)
104+
{
105+
MonoAddressEntry *cur, *prev = info->entries;
106+
int idx = 0;
107+
108+
if (!h)
109+
return;
110+
111+
if (!info->aliases)
112+
info->aliases = g_strdupv (h->h_aliases);
113+
114+
while (h->h_addr_list [idx]) {
115+
cur = g_new0 (MonoAddressEntry, 1);
116+
if (prev)
117+
prev->next = cur;
118+
else
119+
info->entries = cur;
120+
121+
if (flags & MONO_HINT_CANONICAL_NAME && h->h_name)
122+
cur->canonical_name = g_strdup (h->h_name);
123+
124+
cur->family = h->h_addrtype;
125+
cur->socktype = SOCK_STREAM;
126+
cur->protocol = 0; /* Zero means the default stream protocol */
127+
cur->address_len = h->h_length;
128+
memcpy (&cur->address, h->h_addr_list [idx], h->h_length);
129+
130+
prev = cur;
131+
++idx;
132+
}
133+
}
134+
#endif /* !defined (HAVE_GETADDRINFO) && (defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2)) */
135+
136+
137+
int
138+
mono_debugger_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result)
139+
{
140+
#if defined (HAVE_GETADDRINFO) /* modern posix networking code */
141+
char service_name [16];
142+
struct addrinfo hints, *res = NULL, *info;
143+
MonoAddressEntry *cur = NULL, *prev = NULL;
144+
MonoAddressInfo *addr_info;
145+
int ret;
146+
147+
memset (&hints, 0, sizeof (struct addrinfo));
148+
*result = NULL;
149+
150+
hints.ai_family = PF_UNSPEC;
151+
if (flags & MONO_HINT_IPV4)
152+
hints.ai_family = PF_INET;
153+
else if (flags & MONO_HINT_IPV6)
154+
hints.ai_family = PF_INET6;
155+
156+
hints.ai_socktype = SOCK_STREAM;
157+
158+
if (flags & MONO_HINT_CANONICAL_NAME)
159+
hints.ai_flags = AI_CANONNAME;
160+
if (flags & MONO_HINT_NUMERIC_HOST)
161+
hints.ai_flags |= AI_NUMERICHOST;
162+
163+
/* Some ancient libc don't define AI_ADDRCONFIG */
164+
#ifdef AI_ADDRCONFIG
165+
if (flags & MONO_HINT_CONFIGURED_ONLY)
166+
hints.ai_flags |= AI_ADDRCONFIG;
167+
#endif
168+
sprintf (service_name, "%d", port);
169+
170+
MONO_ENTER_GC_SAFE;
171+
ret = getaddrinfo (hostname, service_name, &hints, &info);
172+
MONO_EXIT_GC_SAFE;
173+
174+
if (ret)
175+
return 1; /* FIXME propagate the error */
176+
177+
res = info;
178+
*result = addr_info = g_new0 (MonoAddressInfo, 1);
179+
180+
while (res) {
181+
cur = g_new0 (MonoAddressEntry, 1);
182+
cur->family = res->ai_family;
183+
cur->socktype = res->ai_socktype;
184+
cur->protocol = res->ai_protocol;
185+
if (cur->family == PF_INET) {
186+
cur->address_len = sizeof (struct in_addr);
187+
cur->address.v4 = ((struct sockaddr_in*)res->ai_addr)->sin_addr;
188+
#ifdef HAVE_STRUCT_SOCKADDR_IN6
189+
} else if (cur->family == PF_INET6) {
190+
cur->address_len = sizeof (struct in6_addr);
191+
cur->address.v6 = ((struct sockaddr_in6*)res->ai_addr)->sin6_addr;
192+
#endif
193+
} else {
194+
g_warning ("Cannot handle address family %d", cur->family);
195+
res = res->ai_next;
196+
g_free (cur);
197+
continue;
198+
}
199+
200+
if (res->ai_canonname)
201+
cur->canonical_name = g_strdup (res->ai_canonname);
202+
203+
if (prev)
204+
prev->next = cur;
205+
else
206+
addr_info->entries = cur;
207+
208+
prev = cur;
209+
res = res->ai_next;
210+
}
211+
212+
freeaddrinfo (info);
213+
return 0;
214+
215+
#elif defined (HAVE_GETHOSTBYNAME) || defined (HAVE_GETHOSTBYNAME2) /* fallback networking code that relies on old BSD apis or whatever else is available */
216+
MonoAddressInfo *addr_info;
217+
addr_info = g_new0 (MonoAddressInfo, 1);
218+
219+
#ifdef HAVE_GETHOSTBYNAME2
220+
if (flags & MONO_HINT_IPV6 || flags & MONO_HINT_UNSPECIFIED)
221+
add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET6));
222+
if (flags & MONO_HINT_IPV4 || flags & MONO_HINT_UNSPECIFIED)
223+
add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET));
224+
#else
225+
add_hostent (addr_info, flags, gethostbyname (hostname))
226+
#endif
227+
228+
if (!addr_info->entries) {
229+
*result = NULL;
230+
mono_debugger_free_address_info (addr_info);
231+
return 1;
232+
}
233+
234+
*result = addr_info;
235+
return 0;
236+
237+
#else
238+
g_error ("No networking implementation available");
239+
return 1;
240+
#endif /* defined (HAVE_GETADDRINFO) */
241+
}

src/mono/mono/utils/networking.h renamed to src/mono/mono/component/debugger-networking.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*/
1010

1111

12-
#ifndef __MONO_NETWORKING_H__
13-
#define __MONO_NETWORKING_H__
12+
#ifndef __MONO_DEBUGGER_NETWORKING_H__
13+
#define __MONO_DEBUGGER_NETWORKING_H__
1414

1515
#include <config.h>
1616
#include <glib.h>
@@ -77,18 +77,14 @@ typedef union {
7777
} MonoSocketAddress;
7878

7979
/* This only supports IPV4 / IPV6 and tcp */
80-
MONO_COMPONENT_API int mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **res);
80+
int mono_debugger_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **res);
8181

82-
MONO_COMPONENT_API void mono_free_address_info (MonoAddressInfo *ai);
82+
void mono_debugger_free_address_info (MonoAddressInfo *ai);
8383

84-
MONO_COMPONENT_API void mono_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port);
84+
void mono_debugger_socket_address_init (MonoSocketAddress *sa, socklen_t *len, int family, const void *address, int port);
8585

86-
#ifndef HAVE_INET_PTON
87-
int inet_pton (int family, const char *address, void *inaddrp);
88-
#endif
89-
90-
MONO_COMPONENT_API void mono_networking_init (void);
91-
void mono_networking_shutdown (void);
86+
void mono_debugger_networking_init (void);
87+
void mono_debugger_networking_shutdown (void);
9288

9389

9490
#endif

src/mono/mono/utils/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,6 @@ set(utils_common_sources
166166
mono-conc-hashtable.c
167167
json.h
168168
json.c
169-
networking.c
170-
networking-posix.c
171-
networking-fallback.c
172-
networking-missing.c
173-
networking-windows.c
174-
networking.h
175169
mono-rand.c
176170
mono-rand-windows.c
177171
mono-rand.h

0 commit comments

Comments
 (0)