Skip to content

Commit f367ee5

Browse files
committed
FEAT: it's again possible to read dns:// to resolve a hostname
``` >> read dns:// == "Oldes-Aero" ``` It also fixes a bug introduce in rebol#66, that resolving domain name from ip was not possible at all. Now it's again working properly: ``` >> read dns://rebol.com == 162.216.18.225 >> read dns://162.216.18.225 == "rebol.com" ``` Related to: Oldes/Rebol-issues#1935
1 parent 088b509 commit f367ee5

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

src/core/p-dns.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,22 @@
6565
}
6666

6767
arg = Obj_Value(spec, STD_PORT_SPEC_NET_HOST);
68-
69-
if (IS_TUPLE(arg) && Scan_Tuple(VAL_BIN(arg), (REBCNT)LEN_BYTES(VAL_BIN(arg)), &tmp)) {
68+
if (IS_NONE(arg)) {
69+
// case: read dns://
70+
sock->data = NULL;
71+
}
72+
else if (IS_TUPLE(arg)) {
7073
SET_FLAG(sock->modes, RST_REVERSE);
71-
memcpy(&sock->net.remote_ip, VAL_TUPLE(&tmp), 4);
74+
memcpy(&sock->net.remote_ip, VAL_TUPLE(arg), 4);
7275
}
7376
else if (IS_STRING(arg)) {
74-
sock->data = VAL_BIN(arg);
77+
if (Scan_Tuple(VAL_BIN(arg), (REBCNT)LEN_BYTES(VAL_BIN(arg)), &tmp)) {
78+
SET_FLAG(sock->modes, RST_REVERSE);
79+
memcpy(&sock->net.remote_ip, VAL_TUPLE(&tmp), 4);
80+
}
81+
else {
82+
sock->data = VAL_BIN(arg);
83+
}
7584
}
7685
else Trap_Port(RE_INVALID_SPEC, port, -10);
7786

src/os/dev-dns.c

+27-7
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,24 @@ extern HWND Event_Handle;
106106
#endif
107107

108108
host = OS_Make(MAXGETHOSTSTRUCT); // be sure to free it
109+
sock->net.host_info = host; // stores allocated buffer, deallocated on close or on error
109110

110111
#ifdef HAS_ASYNC_DNS
111-
if (!GET_FLAG(sock->modes, RST_REVERSE)) // hostname lookup
112-
handle = WSAAsyncGetHostByName(Event_Handle, WM_DNS, sock->data, host, MAXGETHOSTSTRUCT);
113-
else
112+
// WINDOWS version
113+
if (!GET_FLAG(sock->modes, RST_REVERSE)) {// hostname lookup
114+
if (sock->data == NULL) {
115+
DWORD dwSize = MAXGETHOSTSTRUCT;
116+
if (GetComputerNameExA(ComputerNameDnsFullyQualified, host, &dwSize)) {
117+
((REBYTE*)host)[dwSize] = 0;
118+
sock->data = host;
119+
SET_FLAG(sock->modes, RST_REVERSE);
120+
SET_FLAG(sock->flags, RRF_DONE);
121+
return DR_DONE;
122+
}
123+
goto error;
124+
}
125+
handle = WSAAsyncGetHostByName(Event_Handle, WM_DNS, cs_cast(sock->data), host, MAXGETHOSTSTRUCT);
126+
} else
114127
handle = WSAAsyncGetHostByAddr(Event_Handle, WM_DNS, (char*)&(sock->net.remote_ip), 4, AF_INET, host, MAXGETHOSTSTRUCT);
115128

116129
if (handle != 0) {
@@ -119,27 +132,34 @@ extern HWND Event_Handle;
119132
return DR_PEND; // keep it on pending list
120133
}
121134
#else
135+
// POSIX version
122136
// Use old-style blocking DNS (mainly for testing purposes):
123137
if (GET_FLAG(sock->modes, RST_REVERSE)) {
124138
he = gethostbyaddr((char*)&sock->net.remote_ip, 4, AF_INET);
125139
if (he) {
126-
sock->net.host_info = host; //???
127140
sock->data = he->h_name;
128141
SET_FLAG(sock->flags, RRF_DONE);
129142
return DR_DONE;
130143
}
131144
}
145+
else if (sock->data == NULL) {
146+
if(0 == gethostname(host, MAXGETHOSTSTRUCT)) {
147+
sock->data = host;
148+
SET_FLAG(sock->modes, RST_REVERSE);
149+
SET_FLAG(sock->flags, RRF_DONE);
150+
return DR_DONE;
151+
}
152+
}
132153
else {
133154
he = gethostbyname(sock->data);
134155
if (he) {
135-
sock->net.host_info = host; // ?? who deallocs?
136156
COPY_MEM((char*)&(sock->net.remote_ip), (char *)(*he->h_addr_list), 4); //he->h_length);
137157
SET_FLAG(sock->flags, RRF_DONE);
138158
return DR_DONE;
139159
}
140160
}
141161
#endif
142-
162+
error:
143163
OS_Free(host);
144164
sock->net.host_info = 0;
145165

@@ -178,7 +198,7 @@ extern HWND Event_Handle;
178198
if (!req->error) { // success!
179199
host = (HOSTENT*)req->net.host_info;
180200
if (GET_FLAG(req->modes, RST_REVERSE))
181-
req->data = host->h_name;
201+
req->data = (REBYTE*)host->h_name;
182202
else
183203
COPY_MEM((char*)&(req->net.remote_ip), (char *)(*host->h_addr_list), 4); //he->h_length);
184204
Signal_Device(req, EVT_READ);

src/tests/units/port-test.r3

+11
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,15 @@ if "true" <> get-env "CONTINUOUS_INTEGRATION" [
130130
===end-group===
131131
]
132132

133+
134+
===start-group=== "DNS"
135+
;@@ https://github.com/Oldes/Rebol-issues/issues/1935
136+
--test-- "read dns://"
137+
--assert string? try [probe read dns://] ;- no crash!
138+
--test-- "read dns://8.8.8.8"
139+
--assert "dns.google" = try [probe read dns://8.8.8.8]
140+
--test-- "read dns://google.com"
141+
--assert tuple? try [read dns://google.com]
142+
===end-group===
143+
133144
~~~end-file~~~

0 commit comments

Comments
 (0)