Skip to content

Commit dd05390

Browse files
authored
Merge pull request #420 from sshanks-kx/ip
improve .z.a/.z.h/.Q.host/.Q.addr details
2 parents 822c31a + 0dcc16d commit dd05390

File tree

4 files changed

+154
-78
lines changed

4 files changed

+154
-78
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)256 vs .Q.addr`localhost
139-
127 0 0 1
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)
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: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,39 @@ q).z.a
7777
-1408172030i
7878
```
7979

80-
Note its relationship to [`.z.h`](#zh-host) for the hostname, converted to an int using [`.Q.addr`](dotq.md#host-ip-to-hostname).
81-
```q
82-
q).Q.addr .z.h
83-
-1408172030i
84-
```
85-
86-
It can be split into components as follows:
87-
88-
```q
89-
q)"i"$0x0 vs .z.a
90-
172 17 0 2i
91-
```
92-
93-
When invoked inside a `.z.p*` callback via a TCP/IP connection, it is the IP address of the client session, not the current session. For example, connecting from a remote machine:
94-
95-
```q
96-
q)h:hopen myhost:1234
97-
q)h"\"i\"$0x0 vs .z.a"
98-
192 168 65 1i
99-
```
100-
or from same machine:
101-
```q
102-
q)h:hopen 1234
103-
q)h"\"i\"$0x0 vs .z.a"
104-
127 0 0 1i
105-
```
106-
107-
When invoked via a Unix Domain Socket, it is 0.
108-
```q
109-
q)h:hopen `:unix://1234
110-
q)h".z.a"
111-
0i
112-
```
80+
The dotted-decimal string representation can be obtained from an integer using [`vs`](vs.md#integer-based-ip-address).
81+
82+
The return value depends on whether it is invoked in an IPC callback or not.
83+
84+
* **Not invoking in an IPC callback**
85+
<br>Returns the current IP address associated with the hostname.
86+
<br>This pre-populated value is equivalent to passing [`.z.h`](#zh-host) to [`.Q.addr`](dotq.md#host-ip-to-hostname) to find the IP address of the current host.
87+
```q
88+
q).z.a
89+
-1408172030i
90+
q).Q.addr .z.h
91+
-1408172030i
92+
```
93+
* **Invoking in an IPC callback**
94+
<br>When invoked inside a `.z.p*` callback via a TCP/IP connection, it is the IP address of the client session, not the current session.
95+
For example, connecting from a remote machine:
96+
```q
97+
q)h:hopen myhost:1234
98+
q)h"\"i\"$0x0 vs .z.a"
99+
192 168 65 1i
100+
```
101+
or from same machine:
102+
```q
103+
q)h:hopen 1234
104+
q)h"\"i\"$0x0 vs .z.a"
105+
127 0 0 1i
106+
```
107+
When invoked via a Unix Domain Socket, it is 0.
108+
```q
109+
q)h:hopen `:unix://1234
110+
q)h".z.a"
111+
0i
112+
```
113113

114114
:fontawesome-solid-hand-point-right:
115115
[`.z.h`](#zh-host) (host), [`.Q.host`](dotq.md#host-ip-to-hostname) (IP to hostname)
@@ -351,33 +351,26 @@ q).z.h
351351
`demo.kx.com
352352
```
353353

354-
On Linux this should return the same as the shell command `hostname`. If you require a fully qualified domain name, and the `hostname` command returns a hostname only (with no domain name), this should be resolved by your system administrators. Often this can be traced to the ordering of entries in `/etc/hosts`, e.g.
354+
If you require a fully qualified domain name, and the command returns a hostname only (with no domain name), this should be resolved by your system administrators.
355355

356-
Non-working `/etc/host` looks like :
356+
**Linux**
357357

358-
```txt
359-
127.0.0.1 localhost.localdomain localhost
360-
192.168.1.1 myhost.mydomain.com myhost
361-
```
358+
On Linux this should return the same value as the shell command `hostname`.
359+
Linux stores the current hostname in `/proc/sys/kernel/hostname`. The operating system reads the hostname from `/etc/hostname`.
362360

363-
Working one has this ordering :
361+
**MacOS**
364362

365-
```txt
366-
127.0.0.1 localhost.localdomain localhost
367-
192.168.1.1 myhost myhost.mydomain.com
368-
```
363+
On MacOS this should return the same value as `sysctl kern.hostname` or `scutil --get HostName`.
369364

370-
One solution seems to be to flip around the entries, i.e. so the entries should be
365+
**Microsoft Windows**
371366

372-
```txt
373-
ip hostname fqdn
367+
On Windows this should return the same value as `hostname` via cmd.exe or Powershell,
368+
which typically gets the hostname from the registry entry `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName`.
369+
The `req query` command can be used to retrieve the current value.
374370
```
375-
376-
A workaround from within kdb+ is
377-
378-
```q
379-
q).Q.host .z.a
371+
reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName
380372
```
373+
The value can also be viewed and altered via the Control Panel.
381374

382375
:fontawesome-solid-hand-point-right:
383376
[`.z.a`](#za-ip-address) (IP address), [`.Q.addr`](dotq.md#addr-iphost-as-int) (IP/host as int)

docs/ref/tok.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ q)"D"$"2147483648"
8383

8484
Short converts to 0Nh instead of +/-0Wh
8585

86-
## :fontawesome-solid-sitemap: Iteration
86+
## Iteration
8787

8888
Tok is a near-[atomic function](../basics/atomic.md).
8989
Implicit recursion stops at strings, not atoms.
@@ -153,17 +153,24 @@ q)"b"$" ",.Q.an
153153
```q
154154
q)"I"$"192.168.1.34" /an IP address as an int
155155
-1062731486i
156-
q)"NT"$\:"123456123987654" / since V3.4
157-
0D12:34:56.123987654
158-
12:34:56.123
159156
```
160157

161158
:fontawesome-solid-book:
162-
[`.Q.addr`](dotq.md#addr-iphost-as-int),
163-
[`.Q.host`](dotq.md#host-ip-to-hostname)
159+
[`.Q.addr`](dotq.md#addr-iphost-as-int) (IP/host as int),
160+
[`.Q.host`](dotq.md#host-ip-to-hostname) (IP to hostname)
161+
162+
## Timestamps
163+
164+
### Raw numeric timestamps
164165

166+
```q
167+
q)"N"$"123456123987654"
168+
0D12:34:56.123987654
169+
q)"T"$"123456123987654"
170+
12:34:56.123
171+
```
165172

166-
## Unix timestamps
173+
### Unix timestamps
167174

168175
(from seconds since Unix epoch), string with 9…11 digits:
169176

docs/ref/vs.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,20 @@ q)0x0 vs 2413e
152152
0x4516d000
153153
q)0x0 vs 2413f
154154
0x40a2da0000000000
155-
q)"."sv string"h"$0x0 vs .z.a / ip address string from .z.a
156-
"192.168.1.213"
157155
```
158156

157+
#### Integer based IP address
158+
159+
Base-256 is used to encode IP addresses to integers. The following example converts the current IP address reported by [`.z.a`](dotz.md#za-ip-address)
160+
```q
161+
q)"i"$0x0 vs .z.a
162+
192 168 0 3i
163+
```
164+
The commonly written dotted-decimal notation can be produced from the list of longs using [`string`](string.md) and [`sv`](sv.md#strings).
165+
```q
166+
q)"." sv string "i"$0x0 vs .z.a
167+
"192.168.0.3"
168+
```
159169

160170
### Base-x representation
161171

0 commit comments

Comments
 (0)