Skip to content

General networking knowledge

Created: 2016-10-16 16:05:43 -0700 Modified: 2016-10-16 16:06:50 -0700

  • Video I did on this: https://youtu.be/3j7KGmzKeGA
  • At a high level, a bind address is the address/interface that you tell a service to listen on. You won’t get a response from network traffic via a different address/interface.
    • The available interfaces should show in ifconfig (macOS) or os.networkInterfaces() in Node.js. You won’t see one with your public IP; you’ll just see one with the IP that your router assigned. If you only want to bind to one address and want your service available publicly, you would pick one that your router assigned and also forward ports.
    • You can use netstat to see bind addresses. It’s easiest if you use a unique port so that you can grep the port, e.g. netstat -a | grep 7895 (-a shows socket information).
  • A network interface is a software interface to networking hardware (reference). The hardware could be virtual.

Just by looking at Node.js, we can see how http-server works. http-server . -a 1.2.3.4 will fail with this message:

╰─❯ http-server ./ -a 1.2.3.4
node:events:492
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL: address not available 1.2.3.4:8080
at Server.setupListenHandle [as _listen2] (node:net:1734:21)
at listenInCluster (node:net:1799:12)
at doListen (node:net:1948:7)
at process.processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1778:8)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'EADDRNOTAVAIL',
errno: -49,
syscall: 'listen',
address: '1.2.3.4',
port: 8080
}

The reason is that os.networkInterfaces() doesn’t list 1.2.3.4 for me. It does list these, for example:

lo0: [
{
address: '127.0.0.1',
netmask: '255.0.0.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: true,
cidr: '127.0.0.1/8'
},
{
address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: '00:00:00:00:00:00',
internal: true,
cidr: '::1/128',
scopeid: 0
},
{
address: 'fe80::1',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
internal: true,
cidr: 'fe80::1/64',
scopeid: 1
}
],

Note: to listen on a link-scoped address, you need to specify the scope ID.

  • fe80::1
  • fe80::1%lo0

Note: you are not listening to all addresses on a given interface, just one:

  • `http-server ./ -a ::1 -p 7895
    • curl -i 127.0.0.1:7895
    • curl -i "[::1]:7895"

The network interfaces that print out all have names, e.g.:

  • awdl0
  • en0
  • en1
  • llw0
  • lo0
  • utun0
  • utun1
  • utun2
  • utun3

These all do have a meaning (reference), e.g. lo is typically for loopback (so it’s where you find 127.0.0.1 and ::1), awdl is for Apple Wireless Direct Link, etc.