How networkconfiguration/dhcpcd works
Project Type and Purpose
• dhcpcd is a system‐level C daemon that implements a DHCPv4, DHCPv6, and IPv4LL (ZeroConf) client for UNIX‐like operating systems. It runs in the background, configures network interfaces, manages IP leases, and invokes scripts/hooks on state changes. It also exposes a control socket for external tools (e.g., dhcpcd-ui) and co‐exists with other resolvers via openresolv.
Core Directory Analysis
• src/: the principal C source files. Contains:
• common.c/.h – shared utilities
• eloop.c/.h – the event‐loop core
• dhcp*.c/.h – DHCPv4 client logic
• dhcp6.c/.h – DHCPv6 client logic
• ipv4*.c/.h, ipv6*.c/.h – IP address management, SLAAC, IPv4LL
• privsep-*.c/.h – privilege separation modules
• control.c/.h – control socket interface
• script.c/.h & hooks/ – hook script runner and shipped hook templates
• bpf.c/.h, route.c/.h, arp.c/.h – low‐level kernel interface (netlink/BPF)
• config parsing, logging, DUID, lease database, etc.
• compat/: portability shims for various OS functions (arc4random, crypt, strlcpy, etc.).
• hooks/: packaged hook scripts (DNS config, hostname, DHCP‐specific tasks).
• tests/: unit and integration tests for cryptographic primitives, event loop, etc.
• Build files (Makefile, configure, BUILDING.md): standard autotools/Makefile.inc integration.
Key Architectural Components
a. Core Daemon Process ("dhcpcd")
• Initialization & Configuration Loader
• Event Loop (eloop)
• Interface Manager
• DHCPv4 & DHCPv6 Protocol Engines
• IPv4LL (ZeroConf) & SLAAC Engine
• Privilege Separation Daemon (child process)
• Control Socket Server (UNIX domain socket)
• Script/Hook Executor
• Lease Database & DUID Storage
• Logging/Error Handling
b. Compat Layer
• Provides uniform APIs for cryptography, random, string, and other OS‐specific calls.
c. Hooks Subsystem
• External shell/perl scripts triggered on interface events.
d. openresolv Integration
• Invoked by hooks to update /etc/resolv.conf, coexisting with other daemons.
e. External Clients
• GUI or CLI clients (e.g., dhcpcd-ui) connecting over the control socket.
Interactions & Data Flows
• Daemon starts, reads /etc/dhcpcd.conf → initializes eloop → enumerates network interfaces.
• For each interface:
• Binds raw sockets or netlink/BPF
• Launches DHCPv4/DHCPv6 or IPv4LL state machine
• Receives packets → processes leases → updates OS interface via netlink/ioctl
• On state transitions (bound, deconfigured, renewed):
• Event loop triggers hook scripts → hooks call openresolv or modify system files
• Control socket:
• External client → sends commands (status, renew, release) → daemon responds.
• Privilege Separation:
• Sensitive operations (raw sockets, file writes) handled in a less‐privileged child.
Architectural Patterns & Principles
• Modular C code organized by protocol and functionality.
• Event‐driven design (single‐threaded eloop).
• Privilege separation to minimize attack surface.
• Clear separation between core logic and external scripts (hooks).
• Portability layer (compat/) insulating OS differences.