Add documentation, screenshots and changelog for 1.0.0
README with install, quick start, keys, command reference and troubleshooting; docs/how-it-works.md explains the scan in detail. The screenshots show made-up devices only. The help line now drops hints that do not fit instead of cutting one in half, and the timeline no longer prints a new device's address twice. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e34ba75b7b
commit
52781ac4a8
9 files changed
+428
-83
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 194 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 151 KiB |
@@ -0,0 +1,143 @@
|
||||
# How nscan scans
|
||||
|
||||
The short version: nscan never waits for a single device. It sends a probe to every
|
||||
address at once, collects whatever answers on the side, and lets the kernel find the
|
||||
devices that do not answer. This page explains each step, where the time goes, and what
|
||||
the approach cannot do.
|
||||
|
||||
## Why a naive scan is slow
|
||||
|
||||
`ping` sends one packet and waits up to a second for the reply before it moves on. Most
|
||||
addresses in a network are empty, so most of that time is spent waiting for nothing. A
|
||||
/24 has 254 addresses, which comes to over four minutes. Even 32 pings in parallel take
|
||||
eight seconds, and the result still misses every device whose firewall drops pings.
|
||||
|
||||
## The sweep
|
||||
|
||||
### Sending and receiving are separate
|
||||
|
||||
nscan opens a single ICMP socket. One goroutine writes an echo request to every address
|
||||
in the range. A second goroutine reads every reply that arrives, in whatever order.
|
||||
|
||||
Each request carries its own send time in the payload (`internal/scanner/icmp.go`), so a
|
||||
reply is enough to compute the round-trip time. nscan keeps no timer and no state per
|
||||
address. [ZMap](https://zmap.io) and [masscan](https://github.com/robertdavidgraham/masscan)
|
||||
are built on the same idea.
|
||||
|
||||
The socket is an *unprivileged* ICMP socket (`SOCK_DGRAM` with `IPPROTO_ICMP`), which
|
||||
macOS allows for every user and Linux for the groups in `net.ipv4.ping_group_range`. So
|
||||
nscan needs no root and no setuid bit.
|
||||
|
||||
### About 2500 packets per second
|
||||
|
||||
After every eight packets, nscan pauses for 3 ms. A /24 is sent in about 0.1 s, a /20
|
||||
(4094 addresses) in about 1.6 s.
|
||||
|
||||
Sending faster would not help. Before the first packet to an address can leave, the
|
||||
kernel has to find its MAC address with an ARP request, and the room it has for packets
|
||||
waiting on ARP is limited. Flood it and the kernel drops packets, and devices go missing
|
||||
from the result.
|
||||
|
||||
### ARP finds the quiet devices
|
||||
|
||||
This is the actual trick. Every packet to an address on the local segment makes the
|
||||
kernel send an ARP request: *who has 192.168.1.23?* Every device has to answer that,
|
||||
even one whose firewall drops all pings, or it could not talk to anyone on the network.
|
||||
|
||||
While the sweep runs, nscan reads the kernel's ARP table every 600 ms:
|
||||
|
||||
| System | Source |
|
||||
|--------|-----------------------------------------------------------------|
|
||||
| macOS | the routing table via `sysctl` (`NET_RT_FLAGS`, `RTF_LLINFO`), falling back to `arp -an` |
|
||||
| Linux | `/proc/net/arp` |
|
||||
|
||||
Every complete entry is a device, with its MAC address. nscan never builds an ARP packet
|
||||
itself, which would take a raw socket and root. It lets the kernel do that work.
|
||||
|
||||
### Fixed waits instead of per-address timeouts
|
||||
|
||||
```text
|
||||
0.0 s round 1 echo request to every address ~0.1 s
|
||||
wait late replies, ARP table polling 0.75 s
|
||||
round 2 only the addresses still silent ~0.1 s
|
||||
wait late replies, ARP table polling 1.5 s
|
||||
~2.5 s sweep done
|
||||
```
|
||||
|
||||
The second round catches packets lost on Wi-Fi, and devices that were asleep and missed
|
||||
their first ARP request. Both waits are fixed, so empty addresses cost nothing extra:
|
||||
a /24 with 3 devices takes as long as one with 200. `-wait` sets the second wait; the first
|
||||
is half of it.
|
||||
|
||||
Without any ICMP permission, nscan sends one UDP datagram to port 9 of each address
|
||||
instead. That still triggers ARP, so devices are found, only without response times.
|
||||
|
||||
## Outside the local segment
|
||||
|
||||
ARP only reaches the local segment, and routers and firewalls often drop ICMP. For
|
||||
addresses in a routed range (`-r 10.20.0.0/24` from somewhere else) that stayed silent,
|
||||
nscan tries TCP connects to ports 80, 443, 22, 445, 139 and 62078, up to 256 hosts at a
|
||||
time, with 800 ms per host. A refused connection counts too: the host had to be there
|
||||
to refuse it.
|
||||
|
||||
## Names
|
||||
|
||||
As soon as the sweep is done, three lookups run at the same time:
|
||||
|
||||
| Source | How |
|
||||
|-------------|---------------------------------------------------------------------|
|
||||
| mDNS | a unicast `PTR` query for the reverse name to port 5353 of each device |
|
||||
| NetBIOS | a node status request (`NBSTAT`) to UDP port 137 of each device |
|
||||
| Reverse DNS | the system resolver, 32 lookups at a time |
|
||||
|
||||
mDNS and NetBIOS work like the sweep: all queries go out of one socket at once, and the
|
||||
answers are collected until a shared deadline of 1.5 s. The interface shows the first
|
||||
name it has, preferring a custom name, then mDNS, NetBIOS and DNS. Suffixes such as
|
||||
`.local`, `.lan` or `.fritz.box` are dropped.
|
||||
|
||||
## Bonjour services
|
||||
|
||||
A DNS-SD browse starts in the background together with the sweep. It asks for
|
||||
`_services._dns-sd._udp.local` to learn every service type on the network, then for the
|
||||
instances of each type, and resolves them to addresses.
|
||||
|
||||
The queries go to the mDNS multicast group from an ephemeral port instead of port 5353.
|
||||
Responders then answer by unicast ([RFC 6762, section 6.7](https://www.rfc-editor.org/rfc/rfc6762#section-6.7)),
|
||||
so nscan does not compete with the system's mDNS daemon for the port.
|
||||
|
||||
## Models
|
||||
|
||||
Apple devices publish a `_device-info._tcp` record with their model, e.g.
|
||||
`model=MacBookAir10,1`. For every device with an mDNS or service name, nscan sends a
|
||||
unicast `TXT` query for `<name>._device-info._tcp.local.` and waits up to one second.
|
||||
|
||||
This is what still identifies a device behind a private MAC address. Such an address has
|
||||
the *locally administered* bit set, carries no vendor prefix, and changes per Wi-Fi
|
||||
network. The model does not change.
|
||||
|
||||
## Where the time goes
|
||||
|
||||
For a /24 on a home network:
|
||||
|
||||
| Phase | Time | Runs |
|
||||
|----------------------|-----------------|------------------------------|
|
||||
| Ping and ARP | about 2.5 s | first |
|
||||
| Bonjour | 3.5 s | in parallel from the start |
|
||||
| Names | 1.5 s, rarely up to 3 s | after the sweep |
|
||||
| Models | up to 1 s | after the names |
|
||||
|
||||
A full scan is done in about five seconds. The list fills up much sooner, though: every
|
||||
device is sent to the interface the moment it is found, so most appear within the first
|
||||
second.
|
||||
|
||||
## Trade-offs
|
||||
|
||||
- **Stale ARP entries.** The kernel keeps ARP entries for a while after a device left
|
||||
(macOS: up to 20 minutes). A device found only that way (*via arp* in the details) can
|
||||
appear online for that long.
|
||||
- **Missing a reply is possible.** A device that is asleep through both rounds and every
|
||||
ARP retry is reported missing. That is why nscan only reports a device as gone after it
|
||||
missed two scans in a row.
|
||||
- **Load.** 2500 small packets per second is nothing for a wired network and fine for
|
||||
Wi-Fi, but a scan of a large range still sends one packet to every address in it. Only
|
||||
scan networks you are allowed to scan.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 422 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 216 KiB |
Reference in new issue
Block a user