Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

UpnpLibrary

UPnP (SSDP/UDA) library for .NET 10: device discovery, event subscriptions, SOAP control points, and a complete device server — for UPnP 1.0, 1.1, and 2.0 (including the 2020 revision).

MIT licensed.

Features

Client

  • Discovery — UpnpSearchService: multicast SSDP listening and search requests (ssdp:all, device/service targets), boot-id tracking, description caching. Raises DeviceFound, DeviceUpdated, DeviceExpired, DeviceByeBye, and ServiceFound events; discovered devices are kept in ListedDevices.
  • Eventing — HttpUpnpNotificationListener: TCP + System.IO.Pipelines NOTIFY receiver. Subscribe / renew / unsubscribe via IServiceControlPoint.
  • Control — ServiceControlPoint.GetControlPointAsync(...): SOAP action invocations and state variable queries, with per-spec-version behavior (1.0 / 1.1 / 2.0).

Server

  • UpnpDeviceServer — full device role: serves the description document over HTTP, dispatches control (SOAP) and eventing (SUBSCRIBE/UNSUBSCRIBE) requests to their services, and advertises the device over SSDP.
  • SsdpServer — periodic NOTIFY alive advertisements (IPv4 + IPv6 multicast), answers M-SEARCH with 200 OK, sends NOTIFY byebye on shutdown.
  • UpnpEventServer — subscription management with SID/TIMEOUT handling, NOTIFY delivery, and pruning of subscribers that fail repeatedly (threshold injectable).
  • Icons — icon bytes are registered with SetIcon(icon, data) and served from internal storage; the description document carries only icon metadata, not the data.
  • Spec-compliant subscription validation (UPnP 2.0, Tables 4-4/4-5 and 2020 revision §4.1.1):
    • 412 when CALLBACK is missing/invalid or NT is not upnp:event
    • 400 when SID appears together with NT/CALLBACK (incompatible headers)
    • 412 when a renewal SID is unknown, expired, or belongs to a different service
    • 412 when the delivery URL is not on the same network segment as the subscription URL (private ranges 10/8, 172.16/12, 192.168/16, plus loopback, AutoIP, link-local and ULA for IPv6)

Client usage

await using var service = new UpnpSearchService("MyApp", "1.0");
service.DeviceFound += (s, d) => Console.WriteLine(d.FriendlyName);
await service.StartDiscoveryAsync();

IReadOnlyCollection<UpnpDiscoveryResponse> responses =
    await service.SendDiscoveryRequestAsync("ssdp:all");

UpnpDevice device = service.ListedDevices[udn];
UpnpService svc = device.FindService(new Target("urn:upnp-org:service:Power:1"))!;

IServiceControlPoint cp = await ServiceControlPoint.GetControlPointAsync(svc, "MyApp", "1.0");
ActionResponse resp = await cp.SendActionRequestAsync(new QueryAction("urn:upnp-org:service:Power:1", "PowerState"));

Subscribing to events:

using var listener = new HttpUpnpNotificationListener(networkInterface);
listener.NotificationReceived += (s, e) => Console.WriteLine(e.PropertySet);

UpnpEventSuscriptionResponse sub =
    await cp.SendSubscriptionRequestAsync(listener, TimeSpan.FromHours(1));
// ... later
await cp.SendUnsubscriptionRequestAsync(listener);

Server usage

Uri baseUri = new("http://192.168.1.50:18080/");
UpnpIcon icon = new(new Uri(baseUri, "icon.jpg"), "image/jpeg", 48, 48, 24);
UpnpService power = new("urn:upnp-org:service:Power:1", "urn:upnp-org:serviceId:power",
    new Uri(baseUri, "scpd/PowerSCPD.xml"),
    new Uri(baseUri, "control/power"),
    new Uri(baseUri, "eventing/power"));
UpnpDevice device = new(
    "urn:upnp-org:device:root:1", "My Device", "MyCompany", null, null, "M1", null, null, null,
    "uuid:12345678-1234-1234-1234-123456789abc", null,
    [icon], [power], null, null)
{ IsRootDevice = true };
UpnpDescription description = new(SpecVersion.Upnp20, baseUri, device);

var server = new UpnpDeviceServer(18080, "192.168.1.50", description, HandleAction, scpDefinitions);
server.SetIcon(icon, File.ReadAllBytes("icon.jpg"));
await server.StartAsync();

// Push an event to all subscribers:
await server.Events.SendEventAsync(power.ServiceType, new Dictionary<string, string> { ["PowerState"] = "1" });

// ... later
await server.DisposeAsync();

where HandleAction is an UpnpDeviceServer.ActionHandler:

async Task<ActionResponseContent> HandleAction(UpnpType service, UdaAction action)
{
    // ... do the work ...
    return new ActionResponseContent();            // success
    // return new ActionResponseContent(new UpnpError(402, "Forbidden")); // failure
}

UpnpDeviceServer options: scpDefinitions (SCP XML per service, for argument/state validation), serverHeader, a shared HttpClient, and maxConsecutiveFailures for dead-subscriber pruning.

Notes

  • Pass your own HttpClient to GetControlPointAsync / UpnpDeviceServer when possible; a self-created client is disposed with the object that owns it.
  • Discovered LOCATION URLs are validated (http/https, no loopback/link-local) before fetching.
  • The specs/ folder with the UPnP specification PDFs is for local reference only and is git-ignored — the documents are copyrighted by the UPnP Forum / Open Connectivity Foundation and may not be redistributed.

Development

dotnet build
dotnet test

Tests cover parsing (description/SCP/SSDP/SOAP/event fixtures, including real-world device descriptors), control points, discovery, and end-to-end server behavior (description serving, SOAP dispatch, icon serving, subscription validation, event delivery and pruning).

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages