Skip to content

Commit 0dcc16d

Browse files
committed
.Q.addr/.Q.host details improved
1 parent 6ab0a98 commit 0dcc16d

File tree

2 files changed

+84
-18
lines changed

2 files changed

+84
-18
lines changed

docs/ref/dotq.md

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,61 @@ q).Q.addmonths[2006.10.29;4]
126126
.Q.addr x
127127
```
128128

129-
Where `x` is a hostname or IP address as a symbol atom, returns the IP address as an integer (same format as [`.z.a`](dotz.md#za-ip-address))
129+
Where `x` is a hostname or IP address as a symbol atom, returns the IP address as an integer.
130130

131+
The dotted-decimal string representation can be obtained from an integer using [`vs`](vs.md#integer-based-ip-address).
132+
133+
If the symbol represents a standard IPv4 dotted decimal notation, it returns the IP as integer without any DNS look-ups required.
131134
```q
132-
q).Q.addr`localhost
135+
q).Q.addr`$"127.0.0.1"
133136
2130706433i
134-
q).Q.host .Q.addr`localhost
135-
`localhost
137+
```
138+
139+
When given a host name, the underlying operating system will govern how the look-up occurs. The IP address will be returned as an integer.
140+
141+
```q
136142
q).Q.addr`localhost
137143
2130706433i
138-
q)"i"$0x0 vs .Q.addr`localhost
139-
127 0 0 1i
144+
```
145+
If the host cannot be resolved, -1 will be returned
146+
```q
147+
q).Q.addr`blah
148+
-1i
140149
```
141150

142-
:fontawesome-regular-hand-point-right:
143-
[`.Q.host`](#host-ip-to-hostname) (IP to hostname)
144-
<br>
145-
:fontawesome-solid-book-open:
146-
[`vs`](vs.md#byte-representation) (Byte representation)
151+
Each underlying operating system deals with IP to hostname (and vice-versa) in different ways.
152+
153+
**Linux**
154+
155+
Consults `/etc/nsswitch.conf` to find the `host` entry (consult the man page on `/etc/nsswitch.conf` for further details).
156+
This configuration and look-up order can be different for each distribution. For example, an entry may exist for the following:
157+
158+
* `hosts` consults `/etc/hosts` which can contain multiple hostnames for a given IP
159+
* `dns` use DNS resolver (`/etc/resolv.conf`)
160+
161+
Return first entry found to match the provided IP.
162+
163+
**MacOS**
164+
165+
Returns first entry found when consulting the following:
166+
167+
* `/etc/hosts` which can contain multiple hostnames for a given IP
168+
* consults system settings
169+
* consults [mDNSResponder](https://github.com/apple-oss-distributions/mDNSResponder)
170+
171+
**Microsoft Windows**
172+
These can be adjusted by system settings or policies, but typical order is:
173+
174+
* check Windows DNS Client Service (dnscache) for recent query result
175+
* check hosts file `C:\Windows\System32\drivers\etc\hosts`
176+
* reverse dns
177+
* optionally configured services, for example NetBIOS/LLMNR/WINS
147178

179+
If multiple addresses available, it uses a [prefix policy table](https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/configure-ipv6-in-windows)
180+
and dynamically adjusts preference based on interface reachability and past success.
181+
182+
:fontawesome-regular-hand-point-right:
183+
[`.Q.host`](#host-ip-to-hostname) (IP to hostname), [`.z.h`](dotz.md#zh-host) (host), [`.z.a`](dotz.md#za-ip-address) (IP address), [`vs`](vs.md#byte-representation) (Byte representation)
148184

149185
## `b6` (bicameral-alphanums)
150186

@@ -1160,21 +1196,51 @@ The server then decides whether to gzip the returned payload, which is uncompres
11601196
Where `x` is an IP address as an int atom, returns its hostname as a symbol atom.
11611197

11621198
```q
1163-
q).Q.host .Q.addr`localhost
1199+
q).Q.host 2130706433i
11641200
`localhost
1165-
q).Q.addr`localhost
1166-
2130706433i
1201+
```
11671202

1203+
The operator [`$`](tok.md#ip-address) (tok) can be used to convert an IP address in dotted-decimal string representation to an integer
1204+
```q
11681205
q)"I"$"104.130.139.23"
11691206
1753385751i
11701207
q).Q.host "I"$"104.130.139.23"
11711208
`netbox.com
1172-
q).Q.addr `netbox.com
1173-
1753385751i
1209+
```
1210+
1211+
Each underlying operating system deals with resolving a hostname to IP (and vice-versa) in different ways, reference [`.Q.addr`](#addr-iphost-as-int) for details.
1212+
1213+
When the resolving leads to consulting a DNS server, the DNS server can also have rules on which IP (or the sort order of IPs) it can return when multiple IPs associated with one host.
1214+
Therefore performing an IP lookup from a given hostname, then using the resuling IP to get its hostname, can return a different hostname.
1215+
For example:
1216+
1217+
```q
1218+
q).Q.host .Q.addr `$"www.yahoo.co.uk"
1219+
`a7de0457831fd11f7.awsglobalaccelerator.com / alternative hostname for IP
1220+
```
1221+
1222+
When using `/etc/hosts` on MacOS/Linux, the order in which multiple hosts are associated with an IP will effect the value returned.
1223+
For example, `/etc/hosts` with the entry
1224+
```bash
1225+
172.17.0.4 test1 test2
1226+
```
1227+
will cause 172.17.0.4 to be resolved to test1
1228+
```q
1229+
q).Q.host "I"$"172.17.0.4"
1230+
`test1
1231+
```
1232+
but `/etc/hosts` with the machine names in a different order
1233+
```bash
1234+
172.17.0.4 test2 test1
1235+
```
1236+
will cause 172.17.0.4 to be resolved to test2
1237+
```q
1238+
q).Q.host "I"$"172.17.0.4"
1239+
`test2
11741240
```
11751241

11761242
:fontawesome-regular-hand-point-right:
1177-
[`.Q.addr`](#addr-iphost-as-int) (IP/host as int), [`$`](tok.md#ip-address) tok (IP address as int)
1243+
[`.Q.addr`](#addr-iphost-as-int) (IP/host as int), [`.z.h`](dotz.md#zh-host) (host), [`.z.a`](dotz.md#za-ip-address) (IP address)
11781244

11791245

11801246
## `hp` (HTTP post)

docs/ref/dotz.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ q).z.a
7777
-1408172030i
7878
```
7979

80-
The dotted-decimal string representation can be obtained using [`vs`](vs.md#integer-based-ip-address).
80+
The dotted-decimal string representation can be obtained from an integer using [`vs`](vs.md#integer-based-ip-address).
8181

8282
The return value depends on whether it is invoked in an IPC callback or not.
8383

0 commit comments

Comments
 (0)