public virtual void ParseDescription(ref UPnPDevice device, string xml) { XmlDocument document = new XmlDocument(); document.LoadXml(xml); #region Get Spec Version XmlNodeList nodes = document.GetElementsByTagName("specVersion"); if (nodes.Count == 1) { nodes = nodes[0].ChildNodes; foreach (XmlNode node in nodes) { switch (node.Name.ToLower()) { case "major": device.Information.SpecVersion.Major = node.InnerText; break; case "minor": device.Information.SpecVersion.Minor = node.InnerText; break; } } } #endregion #region Root Device Information nodes = document.GetElementsByTagName("device"); if (nodes.Count > 0) { nodes = nodes[0].ChildNodes; foreach (XmlNode node in nodes) { switch (node.Name.ToLower()) { case "devicetype": device.Information.DeviceType = node.InnerText; break; case "friendlyname": device.Information.FriendlyName = node.InnerText; break; case "manufacturer": device.Information.Manufacturer = node.InnerText; break; case "manufacturerurl": device.Information.ManufacturerURL = node.InnerText; break; case "modeldescription": device.Information.ModelDescription = node.InnerText; break; case "modelname": device.Information.ModelName = node.InnerText; break; case "udn": device.Information.UDN = node.InnerText; break; case "presentationurl": device.Information.PresentationURL = node.InnerText; break; case "servicelist": XmlNodeList serviceList = node.ChildNodes; foreach (XmlNode xmlService in serviceList) { ServiceBase service = new ServiceBase(); if (device.RootDevice != null) service.Device = device.RootDevice; else service.Device = device; foreach (XmlNode serviceNode in xmlService) { switch (serviceNode.Name.ToLower()) { case "servicetype": service.Information.serviceType = serviceNode.InnerText; break; case "serviceid": service.Information.ServiceId = serviceNode.InnerText; break; case "controlurl": service.Information.ControlURL = serviceNode.InnerText; break; case "eventsuburl": service.Information.EventSubURL = serviceNode.InnerText; break; case "scpdurl": service.Information.SCPDURL = serviceNode.InnerText; break; } } if (device.RootDevice != null) device.RootDevice.Services.Add(service); else device.Services.Add(service); } break; case "devicelist": XmlNodeList devicelist = node.ChildNodes; foreach (XmlNode xmlDevice in devicelist) { UPnPDevice subdevice = new UPnPDevice(); if (device.RootDevice != null) subdevice.RootDevice = device.RootDevice; else subdevice.RootDevice = device; ParseDescription(ref subdevice, xmlDevice.OuterXml); device.SubDevices.Add(subdevice); } break; } } } #endregion }
public UPnPDevice ParseSSDP(string response, EndPoint sender) { UPnPDevice device = new UPnPDevice(); device.Information.Sender = sender; string[] lines = response.Split('\n'); for (int lineNr = 0; lineNr < lines.Length; lineNr++) { int pos = 0; if ((pos = lines[lineNr].IndexOf(':')) != -1) { string key = lines[lineNr].Substring(0, pos++).ToLower(); string value = lines[lineNr].Substring(pos).TrimEnd('\r'); switch (key) { case "st": break; case "usn": device.Information.UDN = value; // USN:uuid:00-17-9A-6C-FD-EF-0100A8C00::upnp:rootdevice if ((pos = value.IndexOf("uuid:")) != -1) { pos += 5; int pos2 = 0; if ((pos2 = value.IndexOf("::", pos)) != -1) { // More values device.Information.UUID = value.Substring(pos, pos2 - pos); } else { // One value device.Information.UUID = value.Substring(pos); } } break; case "location": device.Information.DescriptionURL = value; System.Uri uri = new System.Uri(value); device.Information.URLBase = uri.Host + ":" + uri.Port; break; case "cache-control": if (value.StartsWith("max-age=")) { try { device.Information.MaxAge = int.Parse(value.Replace("max-age=", string.Empty)); } catch { } } break; case "server": device.Information.DeviceVersion = value; break; } } } return device; }
protected virtual void OnUPnPUpdate(object sender, FmdcEventArgs e) { IUPnP upnp = sender as IUPnP; FlowLib.Containers.UPnP.UPnPDevice device = e.Data as FlowLib.Containers.UPnP.UPnPDevice; if (upnp != null && device != null) { switch (e.Action) { case Actions.UPnPRootDeviceFound: //FlowLib.Events.FmdcEventArgs e3 = new FlowLib.Events.FmdcEventArgs(Actions.UPnPDeviceDescription, device.Information.Sender.ToString()); //UpdateBase(this, e3); Success |= Functions.UPnPDevices; break; case Actions.UPnPDeviceUpdated: foreach (ServiceBase service in device.Services) { if (WANIPConnectionService.IsMatching(service)) { #region Retreive internal ip if (this.InternalIP == null) { string routerIP = ((IPEndPoint)device.Information.Sender).Address.ToString(); string[] secRouter = routerIP.Split('.', ':'); System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()); if (hostEntry != null) { System.Net.IPAddress[] collectionOfIPs = hostEntry.AddressList; for (int i = 0; i < collectionOfIPs.Length; i++) { System.Net.IPAddress ip = collectionOfIPs[i]; string[] secIP = ip.ToString().Split('.', ':'); bool found = true; for (int y = 0; y < secRouter.Length - 1; y++) { if (secIP.Length > y) { if (!secRouter[y].Equals(secIP[y])) { found = false; break; } } } if (found) { InternalIP = ip; } } } } #endregion if (InternalIP != null) { wanipService = new WANIPConnectionService(service); #region GetExternalIPAddress try { Progress = Functions.UPnPExternalIp; IPAddress tmpAddress = wanipService.GetExternalIPAddress(this); if (tmpAddress != null) { // Is ExternalIP different then the one we got from router? ExternalIPUPnP = tmpAddress; if (ExternalIP == null) { ExternalIP = ExternalIPUPnP; } Success |= Functions.UPnPExternalIp; } } catch { } #endregion #region AddPortMapping Progress = Functions.UPnPAddMapping; mapping = new WANIPConnectionService.PortMapping( string.Empty, Port, "TCP", Port, InternalIP.ToString(), true, "FlowLibPowered - Connection Detection", /*"FlowLibPowered",*/ 0 ); bool hasAddPortMapping = false; if (hasAddPortMapping = wanipService.AddPortMapping(this, mapping)) { Success |= Functions.UPnPAddMapping; } #endregion #region GetSpecificPortMappingEntry Progress = Functions.UPnPGetMapping; if (hasAddPortMapping) { if (wanipService.GetSpecificPortMappingEntry(this, ref mapping)) { Success |= Functions.UPnPGetMapping; } } #endregion #region DeletePortMapping Progress = Functions.UPnPDeleteMapping; if (hasAddPortMapping) { if (wanipService.DeletePortMapping(this, mapping)) { Success |= Functions.UPnPDeleteMapping; } } #endregion } } Success |= Functions.UPnPIGD; //Progress = Functions.End; } break; } } }