We’re Officially Live β€” Lifetime Founding Membership Available for a Limited Time
Free preview

IP Addressing

How a packet finds a device: IPv4/IPv6 addresses split into network and host by the subnet mask, the same-subnet-vs-gateway decision, plus DHCP, NAT, and why IoT devices dial out.

24 min read

In this lesson:


Logical addresses that route

A MAC address delivers within one link; an IP address is the logical (layer-3) address that routes a packet across networks to any device on the internet. Two versions:


The subnet mask: network + host

An IP address has two parts: a network portion (which network it's on) and a host portion (which device on that network). The subnet mask says where the split is, the 1-bits are network, the 0-bits are host. CIDR notation writes the count of network bits:

 IP:    192.168.1.42      11000000.10101000.00000001.00101010
 Mask:  255.255.255.0     11111111.11111111.11111111.00000000   = /24
                          └────── network (24 bits) β”€β”€β”€β”€β”€β”€β”˜β””hostβ”˜
 Network: 192.168.1.0     Host range: .1 – .254   (.0 = network, .255 = broadcast)

A /24 gives 256 addresses, 254 usable hosts. The mask is how a device decides whether a destination is local (same network) or remote.


Same subnet, or via the gateway?

Before sending, a device checks: is the destination on my subnet? It ANDs both its own IP and the destination IP with the mask and compares the network parts:

// Is dst on the same subnet as me?
bool same_subnet(uint32_t my_ip, uint32_t dst_ip, uint32_t mask) {
    return (my_ip & mask) == (dst_ip & mask);   // same network bits?
}
flowchart TD
  A[Send to dst IP] --> B{dst & mask == my & mask?}
  B -- yes, same subnet --> C[ARP for dst MAC β†’ send directly]
  B -- no, remote --> D[ARP for gateway MAC β†’ send to router]

This is why a wrong subnet mask breaks things: get it wrong and the device either tries to ARP for a host that isn't local (no reply) or routes local traffic through the gateway unnecessarily.


Special and private addresses

Range Meaning
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 private (RFC 1918), LAN-only, not internet-routable
127.0.0.1 (::1) loopback, the device itself
169.254.0.0/16 (fe80::/10) link-local, auto-assigned; on IPv4 it usually means DHCP failed
255.255.255.255 / subnet broadcast broadcast, all hosts on the segment
0.0.0.0 "this/any", unassigned, or "bind to all interfaces"

DHCP: automatic addressing

Rather than configure every device by hand, DHCP (Dynamic Host Configuration Protocol) hands out addresses automatically. The handshake is DORA:

 Device β†’ Discover  (broadcast: "any DHCP server?")
 Server β†’ Offer     ("here's 192.168.1.42, mask, gateway, DNS")
 Device β†’ Request   ("I'll take it")
 Server β†’ Ack       ("it's yours, lease = N seconds")

The lease must be renewed. An embedded device either runs a DHCP client (convenient, plug-and-play) or uses a static IP (deterministic, no server dependency, common for fixed infrastructure). If DHCP fails, you typically see a 169.254.x.x link-local address, a tell-tale "no DHCP."


NAT: why IoT devices dial out

IPv4 addresses are scarce, so home/office networks use private addresses internally and NAT (Network Address Translation) on the router to share one public IP. The router rewrites the source address/port of outbound packets and remembers the mapping to return replies.

The consequence for IoT: a device behind NAT has no public address, so the cloud cannot initiate a connection to it. The device must connect outbound to a public server and keep that connection open. This is exactly why IoT architectures use a broker/cloud the device dials into (MQTT, next lessons) rather than the cloud reaching in, NAT traversal shapes the whole pattern.

 IoT device (192.168.1.42) ──outbound──▢ NAT router (1 public IP) ──▢ cloud broker
   the cloud can't reach IN; the device reaches OUT and stays connected

Gotchas


TL;DR

This is just the start

Sign up free to track progress on this lesson, get coding problems with the Run button, and unlock the full interview Q&A.

More in Networking & IoT

OSI vs TCP/IP ModelEthernet Basics
TCP vs UDP
Sockets
MQTT
CoAP
IP Addressing | EmbeddedPrep.io