Network services and discovery
Kodi's network layer connects local interfaces to optional services that other devices can use. It provides shared connectivity and socket rules, coordinates servers, publishes service names for local discovery, finds network devices, and wakes sleeping media hosts when access needs them.
Network foundation
Network state
The network foundation presents each platform's interfaces through one common view. It reports connection state and the address data that services need. Higher network services use this view to decide when they can start and whether a remote host is reachable. References: xbmc/…/Network.h• L19-35, xbmc/…/Network.h• L61-102, xbmc/…/Network.cpp• L87-94, xbmc/…/Network.cpp• L163-230
The same foundation supports simple reachability checks. A host can be checked with ICMP or with a short TCP connection to a service port. This gives service startup and wake-on-access one shared way to distinguish an unavailable host from a host whose service is not ready. References: xbmc/…/Network.h• L74-88, xbmc/…/Network.cpp• L291-407, xbmc/…/TestNetwork.cpp• L18-43
Address lookup
The DNS name cache turns host names into reusable IPv4 or IPv6 addresses. It also accepts literal addresses and can retain permanent results supplied by discovery. Address choice favors an address that can reach the target while keeping a fallback for local names. The cache can also use the SMB discovery cache when that provider is active. References: xbmc/…/DNSNameCache.h• L25-76, xbmc/…/DNSNameCache.cpp• L35-100, xbmc/…/DNSNameCache.cpp• L102-192, xbmc/…/TestDNSNameCache.cpp• L21-93
Socket transport
The socket layer gives network features one model for IPv4 and IPv6 addresses and for TCP, UDP, and local sockets. TCP carries sessions such as JSON-RPC control connections. UDP carries datagrams such as event packets and discovery messages. Listener helpers let servers wait on several sockets while shared options keep address reuse and IPv6 behavior consistent across platforms. References: xbmc/…/Socket.h• L30-38, xbmc/…/Socket.h• L43-120, xbmc/…/Socket.h• L217-246, xbmc/…/SocketOptions.h• L13-29
Service orchestration
Service lifecycle
CNetworkServices is the common coordinator for user-facing network services. It starts and stops the web server, JSON-RPC transport, event server, AirPlay, AirTunes, UPnP roles, Zeroconf publication, and SMB WS-Discovery according to settings and available platform support. Settings changes pass through this boundary so a service can validate or restart its active state. References: xbmc/…/NetworkServices.h• L30-104, xbmc/…/NetworkServices.cpp• L174-244, xbmc/…/NetworkServices.cpp• L604-663
When the network becomes available, Kodi starts discovery before the configured services that publish or use it. When the network is lost or the application stops, it removes those services in an orderly way. AirTunes starts before AirPlay because compatible senders depend on that order. References: xbmc/…/NetworkServices.cpp• L536-602, xbmc/…/NetworkServices.cpp• L580-585
The service lifecycle moves from network readiness to discovery, servers, receivers, and clean shutdown.
flowchart TD
A[Network available] --> B[Start service coordinator]
B --> C[Start Zeroconf]
C --> D[Start configured UPnP roles]
D --> E[Start event and JSON-RPC services]
E --> F[Start web service]
F --> G[Start AirTunes and AirPlay]
G --> H[Start SMB discovery]
H --> I[Services ready]
I --> J[Network unavailable or shutdown]
J --> K[Stop services and remove discovery records]
References: xbmc/…/NetworkServices.cpp• L536-602 xbmc/…/NetworkServices.cpp• L1190-1259
Web services
HTTP service
The HTTP service is Kodi's web front door for web interfaces, JSON-RPC over HTTP, images, and virtual-file access. Request handlers map paths to product functions or file data. The service can apply authentication and TLS settings at this public boundary. References: xbmc/…/WebServer.h• L25-49, xbmc/…/IHTTPRequestHandler.h• L85-147, xbmc/…/NetworkServices.cpp• L666-720
The JSON-RPC HTTP endpoint accepts JSON requests through POST or through a request query on GET and HEAD. GET and HEAD clients have read permission, while POST can perform the full JSON-RPC operation set. The endpoint can also return its service description. References: xbmc/…/HTTPJsonRpcHandler.cpp• L23-110, xbmc/…/HTTPJsonRpcHandler.h• L17-73
HTTP also exposes selected virtual-file paths. A file is available only when it exists and belongs to an allowed, shared source. This keeps the web boundary connected to Kodi's virtual file system without exposing arbitrary host paths. References: xbmc/…/HTTPVfsHandler.cpp• L25-103, xbmc/…/TestWebServer.cpp• L671-699
File responses support normal web transfer behavior such as cache validators and byte ranges. Direct helpers that bypass Kodi's normal file path use saved credentials when the URL itself has none. References: xbmc/…/TestWebServer.cpp• L732-1056, xbmc/…/TestWebServer.cpp• L1058-1156
Web request context
HTTP handlers preserve the client's effective scheme as http or https. A trusted reverse proxy can provide that scheme through the first value of X-Forwarded-Proto. WSGI web interfaces receive the same request as a standard CGI and WSGI environment with input, error, and response streams. References: xbmc/…/HTTPRequestHandlerUtils.h• L16-31, xbmc/…/HTTPRequestHandlerUtils.cpp• L16-46, xbmc/…/HTTPPythonWsgiInvoker.cpp• L162-319, xbmc/…/HTTPPythonWsgiInvoker.cpp• L355-494
WebSocket transport
The JSON-RPC TCP service accepts both direct TCP clients and clients that begin with a WebSocket upgrade. After the handshake, WebSocket frames carry the same JSON-RPC messages through a persistent session. The transport handles control frames and fragmented messages before handing complete payloads to JSON-RPC. References: xbmc/…/TCPServer.h• L34-51, xbmc/…/TCPServer.cpp• L262-290, xbmc/…/TCPServer.cpp• L1045-1174
JSON-RPC server
The JSON-RPC server keeps socket receipt separate from request work. The server loop accepts connections and moves input to a worker for each active connection. Idle or half-sent connections do not keep a worker, and a slow peer cannot block announcements or other clients. Shutdown waits for requests that are already running before the application removes the service they reach. References: xbmc/…/TCPServer.h• L77-125, xbmc/…/TCPServer.h• L145-225, xbmc/…/TCPServer.cpp• L180-339, xbmc/…/TCPServer.cpp• L676-935, xbmc/…/TestTCPServer.cpp• L228-395
Remote event control
Event server
The event server accepts UDP packets from remote clients and turns them into Kodi input and control actions. It tracks each client, rebuilds complete messages, and removes clients that stop sending keep-alive traffic. The event protocol is a separate remote-control path from JSON-RPC. References: xbmc/…/EventServer.h• L22-78, xbmc/…/EventServer.cpp• L110-196, xbmc/…/EventServer.cpp• L199-272
Event clients
An event client represents one remote device and its input state. Its messages can describe greetings, buttons, mouse movement, notifications, logs, actions, and pings. The server validates and receives datagrams, then Kodi consumes the completed events through its normal input path. References: xbmc/…/EventClient.h• L104-189, xbmc/…/EventClient.cpp• L97-231, xbmc/…/EventPacket.h• L22-59, xbmc/…/EventPacket.h• L95-180
Media receiver services
AirPlay and AirTunes
AirPlay makes Kodi a network receiver for compatible video, photo, and playback-control clients. AirTunes provides the audio receiver path. Both services use local discovery so a sender can find Kodi without a typed address, and both feed received media or control state into Kodi's normal playback system. References: xbmc/…/AirPlayServer.cpp• L197-229, xbmc/…/AirPlayServer.cpp• L769-997, xbmc/…/AirTunesServer.cpp• L603-671
AirTunes carries audio into Kodi's player path and keeps the active item aligned with sender metadata, cover art, timing, volume, and remote actions. During a stream it can discover the sender's Digital Audio Control Protocol service through Zeroconf. It does not stop shared playback when AirPlay video is still active. References: xbmc/…/AirTunesServer.cpp• L105-219, xbmc/…/AirTunesServer.cpp• L387-425, xbmc/…/AirTunesServer.cpp• L476-569
AirPlay and AirTunes have separate runtime lifecycles but share service settings. Kodi publishes _airplay._tcp for AirPlay and _raop._tcp for AirTunes. References: xbmc/…/NetworkServices.cpp• L757-795, xbmc/…/NetworkServices.cpp• L824-864, xbmc/…/AirTunesServer.cpp• L603-671
UPnP services
UPnP roles
UPnP lets Kodi act as a media client, controller, server, and renderer. The client and controller use devices on the network. The server and renderer publish Kodi capabilities for other devices. References: xbmc/…/UPnP.h• L37-66, xbmc/…/NetworkServices.cpp• L986-1164
The UPnP server exposes Kodi library content and shared files as a browsable media tree. It maps media metadata, artwork, subtitles, and safe network resources into the UPnP model. Library changes update published containers so remote clients can refresh their view. References: xbmc/…/UPnPInternal.cpp• L522-655, xbmc/…/UPnPServer.cpp• L122-232, xbmc/…/UPnPServer.cpp• L778-979
The published file boundary stays inside the configured share. Unsafe object paths and resource requests do not reach the host file system. References: xbmc/…/UPnPServer.cpp• L248-277, xbmc/…/UPnPServer.cpp• L644-649
UPnP media descriptions can expose more than one resource for the same item. Kodi reports a resource's protocol and content type so a client can select a usable form. It may also offer equivalent content-type names for formats such as FLAC and WMA because different clients use different registered spellings. References: xbmc/…/UPnPInternal.cpp• L310-350, xbmc/…/TestUPnPInternal.cpp• L208-268
The server carries media dates through UPnP metadata and restores them when a remote item becomes a Kodi file item. This lets remote media keep a usable date for sorting, including date-only values and full date-time values. References: xbmc/…/UPnPInternal.cpp• L1198-1205, xbmc/…/TestUPnPInternal.cpp• L73-173
UPnP playback
Kodi's UPnP player controls a remote renderer through media descriptions and transport actions. It offers resources that match the renderer's declared protocol support and favors a resource whose address routes through the renderer's network. When the renderer declares no protocol list, Kodi can use the first resource as a fallback. References: xbmc/…/UPnPInternal.cpp• L1332-1401, xbmc/…/UPnPPlayer.cpp• L67-155
Playback state belongs to the file that Kodi asked the renderer to open. Kodi waits for the new file to enter playback before treating a later STOPPED state as the end of that session. Each remote action keeps its own reply state, so a late reply cannot update another action. References: xbmc/…/UPnPPlayer.cpp• L157-234, xbmc/…/UPnPPlayer.cpp• L352-377, xbmc/…/UPnPPlayer.cpp• L435-476, xbmc/…/UPnPPlayerController.h• L127-229
UPnP renderer
Kodi's UPnP renderer accepts remote play, pause, stop, seek, volume, and mute actions. It maps those actions to Kodi playback and updates the UPnP transport state from Kodi's player state. References: xbmc/…/UPnPRenderer.cpp• L242-309, xbmc/…/UPnPRenderer.cpp• L484-611, xbmc/…/UPnPRenderer.cpp• L664-765
Local service discovery
Zeroconf publication
Zeroconf is Kodi's common interface for publishing local services. A service supplies a type, name, port, and optional text records. The interface hides the platform provider and removes or re-announces records when the owning service changes state. References: xbmc/…/Zeroconf.h• L18-67, xbmc/…/Zeroconf.cpp• L67-128
The coordinator publishes the web, JSON-RPC, event, AirPlay, and AirTunes endpoints through this interface. A client can therefore find a service by type and name before it knows Kodi's address or port. References: xbmc/…/NetworkServices.cpp• L704-717, xbmc/…/NetworkServices.cpp• L776-792, xbmc/…/NetworkServices.cpp• L877-885, xbmc/…/EventServer.cpp• L153-157
mDNS browsing
mDNS is a Zeroconf provider that browses services on the local network. It reports services as they appear or disappear and resolves a selected service to an address, port, host name, and text records. Kodi exposes the result as a common Zeroconf service so callers do not depend on the provider. References: xbmc/…/ZeroconfBrowser.h• L23-65, xbmc/…/ZeroconfBrowser.h• L85-140, xbmc/…/ZeroconfBrowserMDNS.cpp• L52-99, xbmc/…/ZeroconfBrowserMDNS.cpp• L129-174
The default browser watches common file-service types such as SMB, FTP, WebDAV, and NFS when those features are enabled. Discovery changes update the virtual zeroconf:// source so the browsing layer can refresh its view. See the virtual file system for the file-browsing relationship. References: xbmc/…/ZeroconfBrowser.cpp• L50-60, xbmc/…/ZeroconfBrowserMDNS.cpp• L69-92
WS-Discovery
WS-Discovery is a separate discovery path for SMB devices. Kodi starts a platform provider when SMB discovery is enabled and keeps its discovered addresses available to the file-system layer. Zeroconf publishes and browses general service types, while WS-Discovery supplies SMB host discovery where that protocol is expected. References: xbmc/…/IWSDiscovery.h• L13-24, xbmc/…/WSDiscoveryWin32.h• L19-65, xbmc/…/SMBWSDiscovery.h• L23-79, xbmc/…/NetworkServices.cpp• L1227-1259
Wake-on-access
Host wake flow
Wake-on-access makes a remote file or UPnP resource usable when its host is asleep. Kodi keeps host and MAC data for known local remotes, links UPnP server identities to those hosts, and limits repeated wake requests with a cooldown. Unknown hosts remain on the normal access path. References: xbmc/…/WakeOnAccess.cpp• L96-179, xbmc/…/WakeOnAccess.cpp• L567-625, xbmc/…/WakeOnAccess.cpp• L708-734
When Kodi is about to access a known host, it resolves the host through the shared DNS or discovery data and checks whether the host already answers. If it does not, Kodi waits for a stable local network, sends a Wake-on-LAN packet, waits for the host to answer, and allows extra time for file or media services to start. References: xbmc/…/WakeOnAccess.cpp• L452-565
The wake path connects remote access to file and media use. External interfaces describes the wider remote-access boundary, while discovered file locations continue through the virtual file system.
sequenceDiagram
participant A as Access request
participant W as Wake-on-access
participant N as Network
participant H as Remote host
A->>W: Request host resource
W->>H: Quick reachability check
alt Host is online
H-->>W: Reply
W-->>A: Continue access
else Host is asleep
W->>N: Wait for stable local network
W->>N: Send Wake-on-LAN packet
N-->>H: Broadcast wake packet
W->>H: Poll for host and services
H-->>W: Host becomes reachable
W-->>A: Continue access
end
References: xbmc/…/WakeOnAccess.cpp• L498-565 xbmc/…/WakeOnAccess.cpp• L643-706
Change checklist
When these files or sections are changed, remember to consider these:
- Keep each service's start, stop, running state, and Zeroconf record aligned in NetworkServices.cpp.
- Keep service type strings, names, ports, and text records aligned between each publisher and its discovery consumer.
- Preserve the JSON-RPC contract across direct TCP framing, WebSocket upgrades, worker shutdown, and announcement delivery.
- Keep HTTP scheme detection aligned with proxy forwarding and every URL returned for a remote file.
- Keep HTTP access checks aligned with shared-source rules and the virtual file system.
- Keep event packet limits, message assembly, client keep-alives, and action mapping as one protocol contract.
- Keep UPnP resource URLs, protocol information, alternate content types, and renderer-network selection aligned as one playback contract.
- Keep UPnP metadata dates aligned between DIDL parsing, Kodi file items, and remote sorting.
- Keep each UPnP action's reply ownership and retirement lifetime aligned so a late reply cannot update another action.
- Keep UPnP published paths inside the same allowed root on every access path.
- Keep AirTunes stream metadata, cover art, timing, volume, and remote-control state aligned with the active player item.
- Keep wake-on-access host identity, discovery results, MAC data, reachability mode, and cooldown state consistent with file and UPnP access.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No connected network interface | Network services cannot start and wake-on-access cannot safely send a wake packet. | Start services only after network availability. Wait for a stable network before waking a host. |
| Web authentication is enabled with no password | The web service does not start. | Validate the authentication settings before start or settings commit. |
| A service port is invalid or already in use | The related server stays unavailable and must not publish a false endpoint. | Validate ports and publish only after a successful bind. |
| A WebSocket handshake is incomplete or invalid | The connection is rejected before JSON-RPC messages are accepted. | Validate the upgrade and WebSocket frame state before handing payloads to JSON-RPC. |
| A peer stops reading while receiving announcements | The peer can block other clients or delay shutdown. | Queue output, cap stalled output, and close the slow connection without blocking the server loop. |
| A request is still running during server shutdown | Removing the service target too early can leave a request using destroyed state. | Wait for active workers after stopping new request work. |
| An event packet is invalid or a client stops sending keep-alives | Remote input is ignored or the client is removed. | Validate packets, assemble complete messages, and require periodic client pings. |
| Zeroconf is unavailable | Local service publication and browsing do not work. | Keep publication behind the common provider and reject dependent startup when required. |
| A UPnP renderer cannot match or route a media resource | Remote playback fails although the media item is valid. | Offer compatible resource descriptions and prefer the renderer's reachable network. |
| A UPnP media date is absent or not parseable | Remote items lose a usable date and date-based sorting becomes unreliable. | Use the UPnP media object's date field and accept only valid date values. |
| A UPnP playback reply arrives after its action is replaced | A late result can update the wrong command or playback state. | Keep one reply delegate per action and retire it only after its wait. |
| A UPnP file path escapes the published root | The request is rejected and the media is not served. | Validate decoded paths and safe resource identifiers against the published root. |
| Wake-on-LAN cannot send or the host never answers | The requested remote resource remains unavailable. | Check reachability first, limit repeated wake attempts, wait for service startup, and return failure on timeout. |
Key files
| File | Why to read it |
|---|---|
| xbmc/…/Network.h | Defines the shared network state, reachability, socket, and service boundary. |
| xbmc/…/DNSNameCache.cpp | Shows shared address lookup and selection for local and remote hosts. |
| xbmc/…/NetworkServices.cpp | Shows service start order, settings changes, and discovery publication. |
| xbmc/…/Socket.h | Defines the shared TCP, UDP, address, and listener model. |
| xbmc/…/WebServer.h | Defines the HTTP service and request-handler boundary. |
| xbmc/…/HTTPJsonRpcHandler.cpp | Shows JSON-RPC access through HTTP and its permission boundary. |
| xbmc/…/HTTPVfsHandler.cpp | Shows how HTTP access is limited to shared virtual-file sources. |
| xbmc/…/HTTPPythonWsgiInvoker.cpp | Shows how web-interface add-ons receive and return HTTP requests. |
| xbmc/…/TCPServer.cpp | Shows direct JSON-RPC connections, WebSocket upgrades, worker isolation, and shutdown. |
| xbmc/…/TestTCPServer.cpp | Shows the guarantees for idle connections, slow peers, concurrent requests, and shutdown. |
| xbmc/…/EventServer.cpp | Shows UDP event receipt, client tracking, and event processing. |
| xbmc/…/AirPlayServer.cpp | Shows the AirPlay receiver boundary and local discovery. |
| xbmc/…/AirTunesServer.cpp | Shows audio delivery, player metadata, DACP discovery, and remote actions. |
| xbmc/…/UPnP.h | Defines the UPnP client, controller, server, and renderer roles. |
| xbmc/…/UPnPInternal.cpp | Shows shared UPnP media metadata, dates, resources, and protocol descriptions. |
| xbmc/…/UPnPServer.cpp | Shows the published library tree, safe resource URLs, and update announcements. |
| xbmc/…/TestUPnPInternal.cpp | Shows the guarantees for UPnP dates, alternate content types, and metadata read-back. |
| xbmc/…/UPnPPlayer.cpp | Shows remote resource choice, transport control, and playback-state reporting. |
| xbmc/…/UPnPPlayerController.h | Defines per-action reply ownership and remote transport state. |
| xbmc/…/UPnPRenderer.cpp | Shows remote control of Kodi playback through UPnP actions. |
| xbmc/…/Zeroconf.h | Defines service publication and provider-independent discovery records. |
| xbmc/…/ZeroconfBrowser.h | Defines discovered service data and resolution. |
| xbmc/…/IWSDiscovery.h | Defines the platform-neutral SMB discovery lifecycle. |
| xbmc/…/WakeOnAccess.cpp | Shows host lookup, MAC discovery, reachability checks, waking, and service-ready waits. |
| docs/external-interfaces.md | Related page for the broader remote-access and external-service boundary. |
| docs/virtual-filesystem.md | Related page for discovered locations and HTTP or UPnP file access. |