private void HandleAsyncSearch(SSDPSession sender, HTTPMessage msg)
        {
            DText  text   = new DText();
            string tag    = msg.GetTag("Location");
            int    maxAge = 0;
            string str2   = msg.GetTag("Cache-Control").Trim();

            if (str2 != "")
            {
                text.ATTRMARK = ",";
                text.MULTMARK = "=";
                text[0]       = str2;
                for (int i = 1; i <= text.DCOUNT(); i++)
                {
                    if (text[i, 1].Trim().ToUpper() == "MAX-AGE")
                    {
                        maxAge = int.Parse(text[i, 2].Trim());
                        break;
                    }
                }
            }
            str2 = msg.GetTag("USN");
            string uSN          = str2.Substring(str2.IndexOf(":") + 1);
            string searchTarget = msg.GetTag("ST");

            if (uSN.IndexOf("::") != -1)
            {
                uSN = uSN.Substring(0, uSN.IndexOf("::"));
            }
            EventLogger.Log(this, EventLogEntryType.SuccessAudit, msg.RemoteEndPoint.ToString());
            if (this.OnSearch != null)
            {
                this.OnSearch(msg.RemoteEndPoint, msg.LocalEndPoint, new Uri(tag), uSN, searchTarget, maxAge);
            }
        }
        public static string GetURNPrefix(string urn)
        {
            DText text = new DText();

            text.ATTRMARK = ":";
            text[0]       = urn;
            int length = text[text.DCOUNT()].Length;

            return(urn.Substring(0, urn.Length - length));
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        /// <param name="version">Version Number</param>
        /// <param name="Service_ID">USN, if blank a new GUID will be created for you</param>
        /// <param name="Service_Type">The Type</param>
        /// <param name="IsStandard">True if standard, false if domain specific</param>
        public ServiceDescription(double version, String Service_ID, String Service_Type, bool IsStandard)
        {
            IsStandardService = IsStandard;
            if (Service_ID == "")
            {
                ServiceID = Guid.NewGuid().ToString();
            }
            else
            {
                ServiceID = Service_ID;
            }
            ServiceType = Service_Type;

            SCPDURL    = "{" + Service_ID + "}scpd.xml";
            ControlURL = "{" + Service_ID + "}control";
            EventURL   = "{" + Service_ID + "}event";

            if (version == 0)
            {
                Major = 1;
                Minor = 0;
            }
            else
            {
                DText TempNum = new DText();
                Major = int.Parse(TempNum.FIELD(version.ToString(), ".", 1));
                if (TempNum.DCOUNT(version.ToString(), ".") == 2)
                {
                    Minor = int.Parse(TempNum.FIELD(version.ToString(), ".", 2));
                }
                else
                {
                    Minor = 0;
                }
            }
        }
Exemple #4
0
        public static HTTPMessage ParseByteArray(byte[] buffer, int indx, int count)
        {
            byte[]      buffer2;
            HTTPMessage message = new HTTPMessage();
            string      str     = new UTF8Encoding().GetString(buffer, indx, count);
            DText       text    = new DText();
            int         index   = str.IndexOf("\r\n\r\n");

            str           = str.Substring(0, index);
            text.ATTRMARK = "\r\n";
            text.MULTMARK = ":";
            text[0]       = str;
            string str3  = text[1];
            DText  text2 = new DText();

            text2.ATTRMARK = " ";
            text2.MULTMARK = "/";
            text2[0]       = str3;
            if (str3.ToUpper().StartsWith("HTTP/"))
            {
                message.ResponseCode = int.Parse(text2[2]);
                int startIndex = str3.IndexOf(" ");
                startIndex           = str3.IndexOf(" ", (int)(startIndex + 1));
                message.ResponseData = UnEscapeString(str3.Substring(startIndex));
                try
                {
                    message.Version = text2[1, 2];
                }
                catch (Exception)
                {
                    message.Version = "0.9";
                }
            }
            else
            {
                message.Directive = text2[1];
                string theString = str3.Substring(str3.LastIndexOf(" ") + 1);
                if (!theString.ToUpper().StartsWith("HTTP/"))
                {
                    message.Version      = "0.9";
                    message.DirectiveObj = UnEscapeString(theString);
                }
                else
                {
                    message.Version = theString.Substring(theString.IndexOf("/") + 1);
                    int num3 = str3.IndexOf(" ") + 1;
                    message.DirectiveObj = UnEscapeString(str3.Substring(num3, ((str3.Length - num3) - theString.Length) - 1));
                }
            }
            string tagName = "";
            string tagData = "";

            for (int i = 2; i <= text.DCOUNT(); i++)
            {
                if ((tagName != "") && text[i, 1].StartsWith(" "))
                {
                    tagData = text[i, 1].Substring(1);
                }
                else
                {
                    tagName = text[i, 1];
                    tagData = "";
                    for (int j = 2; j <= text.DCOUNT(i); j++)
                    {
                        if (tagData == "")
                        {
                            tagData = text[i, j];
                        }
                        else
                        {
                            tagData = tagData + text.MULTMARK + text[i, j];
                        }
                    }
                }
                message.AppendTag(tagName, tagData);
            }
            int length = 0;

            if (message.HasTag("Content-Length"))
            {
                try
                {
                    length = int.Parse(message.GetTag("Content-Length"));
                }
                catch (Exception)
                {
                    length = -1;
                }
            }
            else
            {
                length = -1;
            }
            if (length > 0)
            {
                buffer2 = new byte[length];
                if (((index + 4) + length) <= count)
                {
                    Array.Copy(buffer, index + 4, buffer2, 0, length);
                    message.DataBuffer = buffer2;
                }
            }
            switch (length)
            {
            case -1:
                buffer2 = new byte[count - (index + 4)];
                Array.Copy(buffer, index + 4, buffer2, 0, buffer2.Length);
                message.DataBuffer = buffer2;
                break;

            case 0:
                message.DataBuffer = new byte[0];
                break;
            }
            return(message);
        }
Exemple #5
0
        private void ProcessPacket(HTTPMessage msg, IPEndPoint src)
        {
            if (this.OnSniffPacket != null)
            {
                this.OnSniffPacket(src, null, msg);
            }
            DText text = new DText();

            text.ATTRMARK = "::";
            bool   isAlive = false;
            string tag     = msg.GetTag("USN");

            text[0] = tag;
            string uSN = text[1];

            uSN = uSN.Substring(uSN.IndexOf(":") + 1);
            string sT     = text[2];
            int    maxAge = 0;

            if (msg.GetTag("NTS").ToUpper() == "SSDP:ALIVE")
            {
                isAlive = true;
                string str5 = msg.GetTag("Cache-Control").Trim();
                if (str5 != "")
                {
                    text.ATTRMARK = ",";
                    text.MULTMARK = "=";
                    text[0]       = str5;
                    for (int i = 1; i <= text.DCOUNT(); i++)
                    {
                        if (text[i, 1].Trim().ToUpper() == "MAX-AGE")
                        {
                            maxAge = int.Parse(text[i, 2].Trim());
                            break;
                        }
                    }
                }
            }
            else
            {
                isAlive = false;
            }
            if ((msg.Directive == "NOTIFY") && (this.OnNotify != null))
            {
                Uri uri;
                try
                {
                    uri = new Uri(msg.GetTag("Location"));
                }
                catch (Exception)
                {
                    uri = null;
                }
                this.OnNotify(src, msg.LocalEndPoint, uri, isAlive, uSN, sT, maxAge, msg);
            }
            if (msg.Directive == "M-SEARCH")
            {
                try
                {
                    this.ValidateSearchPacket(msg);
                }
                catch (InvalidSearchPacketException)
                {
                    return;
                }
                if (this.OnSearch != null)
                {
                    int          maxValue = int.Parse(msg.GetTag("MX"));
                    SearchStruct struct2  = new SearchStruct();
                    struct2.ST     = msg.GetTag("ST");
                    struct2.Source = src;
                    struct2.Local  = this.LocalIPEndPoint;
                    this.SearchTimer.Add(struct2, this.RandomGenerator.Next(0, maxValue));
                }
            }
        }
        private static ItemCollection ParseComplexType_SequenceChoice(XmlTextReader X)
        {
            bool           flag      = false;
            ItemCollection items     = null;
            string         localName = X.LocalName;
            DText          text      = new DText();

            text.ATTRMARK = ":";
            if (X.LocalName == "choice")
            {
                items = new Choice();
            }
            else
            {
                items = new Sequence();
            }
            if (X.HasAttributes)
            {
                for (int i = 0; i < X.AttributeCount; i++)
                {
                    X.MoveToAttribute(i);
                    switch (X.LocalName)
                    {
                    case "minOccurs":
                        items.MinOccurs = X.Value;
                        break;

                    case "maxOccurs":
                        items.MaxOccurs = X.Value;
                        break;
                    }
                }
                X.MoveToElement();
            }
            X.Read();
            do
            {
                switch (X.NodeType)
                {
                case XmlNodeType.Element:
                {
                    string str = X.LocalName;
                    if (str != null)
                    {
                        str = string.IsInterned(str);
                        if (str == "group")
                        {
                            if (X.HasAttributes)
                            {
                                for (int j = 0; j < X.AttributeCount; j++)
                                {
                                    string str5;
                                    X.MoveToAttribute(j);
                                    if (((str5 = X.LocalName) != null) && (string.IsInterned(str5) == "ref"))
                                    {
                                        string str2 = X.Value;
                                    }
                                }
                                X.MoveToElement();
                            }
                        }
                        else
                        {
                            if ((str == "sequence") || (str == "choice"))
                            {
                                items.AddCollection(ParseComplexType_SequenceChoice(X));
                                break;
                            }
                            if (str == "element")
                            {
                                items.AddContentItem(new Element());
                                if (X.HasAttributes)
                                {
                                    for (int k = 0; k < X.AttributeCount; k++)
                                    {
                                        X.MoveToAttribute(k);
                                        switch (X.LocalName)
                                        {
                                        case "name":
                                            items.CurrentItem.Name = X.Value;
                                            break;

                                        case "type":
                                            text[0] = X.Value;
                                            if (text.DCOUNT() == 1)
                                            {
                                                items.CurrentItem.Type   = X.Value;
                                                items.CurrentItem.TypeNS = X.LookupNamespace("");
                                            }
                                            else
                                            {
                                                items.CurrentItem.Type   = text[2];
                                                items.CurrentItem.TypeNS = X.LookupNamespace(text[1]);
                                            }
                                            break;

                                        case "minOccurs":
                                            items.CurrentItem.MinOccurs = X.Value;
                                            break;

                                        case "maxOccurs":
                                            items.CurrentItem.MaxOccurs = X.Value;
                                            break;
                                        }
                                    }
                                    X.MoveToElement();
                                }
                                break;
                            }
                            if (str == "attribute")
                            {
                            }
                        }
                    }
                    break;
                }

                case XmlNodeType.EndElement:
                    if (X.LocalName == localName)
                    {
                        flag = true;
                    }
                    break;
                }
            }while (!flag && X.Read());
            return(items);
        }
        private static UPnPComplexType ParseComplexType(XmlTextReader X, UPnPComplexType RetVal)
        {
            string localName = X.LocalName;
            int    num       = 0;
            bool   flag      = false;
            DText  text      = new DText();

            text.ATTRMARK = ":";
            RetVal.AddContainer(new GenericContainer());
            do
            {
                switch (X.NodeType)
                {
                case XmlNodeType.Element:
                {
                    string str = X.LocalName;
                    if (str != null)
                    {
                        str = string.IsInterned(str);
                        if ((str == "complexType") || (str == "group"))
                        {
                            num++;
                            if (X.HasAttributes)
                            {
                                for (int i = 0; i < X.AttributeCount; i++)
                                {
                                    X.MoveToAttribute(i);
                                    if (X.Name == "name")
                                    {
                                        text[0] = X.Value;
                                        if (text.DCOUNT() == 1)
                                        {
                                            RetVal.LocalName = X.Value;
                                            RetVal.NameSpace = X.LookupNamespace("");
                                        }
                                        else
                                        {
                                            RetVal.LocalName = text[2];
                                            RetVal.NameSpace = X.LookupNamespace(text[1]);
                                        }
                                    }
                                    else if (X.Name == "ref")
                                    {
                                    }
                                }
                                X.MoveToElement();
                            }
                        }
                        else
                        {
                            if ((str == "sequence") || (str == "choice"))
                            {
                                RetVal.CurrentContainer.AddCollection(ParseComplexType_SequenceChoice(X));
                                break;
                            }
                            if (str == "complexContent")
                            {
                                RetVal.AddContainer(new ComplexContent());
                                break;
                            }
                            if (str == "simpleContent")
                            {
                                RetVal.AddContainer(new SimpleContent());
                                break;
                            }
                            if (str == "restriction")
                            {
                                Restriction restriction = new Restriction();
                                if (RetVal.CurrentContainer.GetType() == typeof(ComplexContent))
                                {
                                    ((ComplexContent)RetVal.CurrentContainer).RestExt = restriction;
                                }
                                else if (RetVal.CurrentContainer.GetType() == typeof(SimpleContent))
                                {
                                    ((SimpleContent)RetVal.CurrentContainer).RestExt = restriction;
                                }
                                if (X.HasAttributes)
                                {
                                    for (int j = 0; j < X.AttributeCount; j++)
                                    {
                                        X.MoveToAttribute(j);
                                        if (X.Name == "base")
                                        {
                                            text[0] = X.Value;
                                            if (text.DCOUNT() == 1)
                                            {
                                                restriction.baseType   = X.Value;
                                                restriction.baseTypeNS = X.LookupNamespace("");
                                            }
                                            else
                                            {
                                                restriction.baseType   = text[2];
                                                restriction.baseTypeNS = X.LookupNamespace(text[1]);
                                            }
                                        }
                                    }
                                    X.MoveToElement();
                                }
                                break;
                            }
                        }
                    }
                    break;
                }

                case XmlNodeType.EndElement:
                    if (X.LocalName == localName)
                    {
                        num--;
                        if (num == 0)
                        {
                            flag = true;
                        }
                    }
                    break;
                }
            }while (!flag && X.Read());
            return(RetVal);
        }