Esempio n. 1
0
        private void OnReceiveSink2(byte[] buffer, IPEndPoint remote, IPEndPoint local)
        {
            HTTPMessage msg;

            try
            {
                msg = HTTPMessage.ParseByteArray(buffer, 0, buffer.Length);
            }
            catch (Exception ex)
            {
                EventLogger.Log(ex);
                msg              = new HTTPMessage();
                msg.Directive    = "---";
                msg.DirectiveObj = "---";
                msg.BodyBuffer   = buffer;
            }
            msg.LocalEndPoint  = local;
            msg.RemoteEndPoint = remote;

            DText parser = new DText();

            String Location = msg.GetTag("Location");
            int    MaxAge   = 0;
            String ma       = msg.GetTag("Cache-Control").Trim();

            if (ma != "")
            {
                parser.ATTRMARK = ",";
                parser.MULTMARK = "=";
                parser[0]       = ma;
                for (int i = 1; i <= parser.DCOUNT(); ++i)
                {
                    if (parser[i, 1].Trim().ToUpper() == "MAX-AGE")
                    {
                        MaxAge = int.Parse(parser[i, 2].Trim());
                        break;
                    }
                }
            }
            ma = msg.GetTag("USN");
            String USN = ma.Substring(ma.IndexOf(":") + 1);
            String ST  = msg.GetTag("ST");

            if (USN.IndexOf("::") != -1)
            {
                USN = USN.Substring(0, USN.IndexOf("::"));
            }
            EventLogger.Log(this, EventLogEntryType.SuccessAudit, msg.RemoteEndPoint.ToString());
            if (OnSearch != null)
            {
                OnSearch(msg.RemoteEndPoint, msg.LocalEndPoint, new Uri(Location), USN, ST, MaxAge);
            }
        }
Esempio n. 2
0
        private static UPnPComplexType ParseComplexType(XmlTextReader X, UPnPComplexType RetVal)
        {
            string elementName = X.LocalName;
            int    count       = 0;
            bool   done        = false;
            DText  P           = new DText();

            P.ATTRMARK = ":";

            RetVal.AddContainer(new GenericContainer());

            do
            {
                switch (X.NodeType)
                {
                case XmlNodeType.Element:
                    switch (X.LocalName)
                    {
                    case "complexType":
                    case "group":
                        ++count;
                        if (X.HasAttributes)
                        {
                            for (int i = 0; i < X.AttributeCount; i++)
                            {
                                X.MoveToAttribute(i);
                                if (X.Name == "name")
                                {
                                    P[0] = X.Value;
                                    if (P.DCOUNT() == 1)
                                    {
                                        RetVal.LocalName = X.Value;
                                        RetVal.NameSpace = X.LookupNamespace("");
                                    }
                                    else
                                    {
                                        RetVal.LocalName = P[2];
                                        RetVal.NameSpace = X.LookupNamespace(P[1]);
                                    }
                                }
                                else if (X.Name == "ref")
                                {
                                    // NOP
                                }
                            }
                            X.MoveToElement();
                        }
                        break;

                    case "sequence":
                    case "choice":
                        RetVal.CurrentContainer.AddCollection(ParseComplexType_SequenceChoice(X));
                        //ParseComplexType_Sequence(X,RetVal);
                        break;

                    case "complexContent":
                        RetVal.AddContainer(new ComplexContent());
                        break;

                    case "simpleContent":
                        RetVal.AddContainer(new SimpleContent());
                        break;

                    case "restriction":
                        Restriction r = new Restriction();
                        if (RetVal.CurrentContainer.GetType() == typeof(ComplexContent))
                        {
                            ((ComplexContent)RetVal.CurrentContainer).RestExt = r;
                        }
                        else if (RetVal.CurrentContainer.GetType() == typeof(SimpleContent))
                        {
                            ((SimpleContent)RetVal.CurrentContainer).RestExt = r;
                        }
                        if (X.HasAttributes)
                        {
                            for (int i = 0; i < X.AttributeCount; i++)
                            {
                                X.MoveToAttribute(i);
                                if (X.Name == "base")
                                {
                                    P[0] = X.Value;
                                    if (P.DCOUNT() == 1)
                                    {
                                        r.baseType   = X.Value;
                                        r.baseTypeNS = X.LookupNamespace("");
                                    }
                                    else
                                    {
                                        r.baseType   = P[2];
                                        r.baseTypeNS = X.LookupNamespace(P[1]);
                                    }
                                }
                            }
                            X.MoveToElement();
                        }
                        break;
                    }
                    break;

                case XmlNodeType.EndElement:
                    if (X.LocalName == elementName)
                    {
                        --count;
                        if (count == 0)
                        {
                            done = true;
                        }
                    }
                    break;

                case XmlNodeType.Text:
                    break;
                }
            }while(!done && X.Read());
            return(RetVal);
        }
Esempio n. 3
0
        private static ItemCollection ParseComplexType_SequenceChoice(XmlTextReader X)
        {
            bool           done = false;
            ItemCollection RetVal;
            string         elementName = X.LocalName;
            DText          p           = new DText();

            p.ATTRMARK = ":";

            if (X.LocalName == "choice")
            {
                RetVal = new Choice();
            }
            else
            {
                RetVal = new Sequence();
            }

            if (X.HasAttributes)
            {
                for (int i = 0; i < X.AttributeCount; i++)
                {
                    X.MoveToAttribute(i);
                    switch (X.LocalName)
                    {
                    case "minOccurs":
                        RetVal.MinOccurs = X.Value;
                        break;

                    case "maxOccurs":
                        RetVal.MaxOccurs = X.Value;
                        break;
                    }
                }
                X.MoveToElement();
            }
            X.Read();


            do
            {
                switch (X.NodeType)
                {
                case XmlNodeType.Element:
                    switch (X.LocalName)
                    {
                    case "group":
                        if (X.HasAttributes)
                        {
                            for (int i = 0; i < X.AttributeCount; i++)
                            {
                                X.MoveToAttribute(i);
                                switch (X.LocalName)
                                {
                                case "ref":
                                    break;
                                }
                            }
                            X.MoveToElement();
                        }
                        break;

                    case "sequence":
                    case "choice":
                        RetVal.AddCollection(ParseComplexType_SequenceChoice(X));
                        break;

                    case "element":
                        RetVal.AddContentItem(new Element());
                        if (X.HasAttributes)
                        {
                            for (int i = 0; i < X.AttributeCount; i++)
                            {
                                X.MoveToAttribute(i);
                                switch (X.LocalName)
                                {
                                case "name":
                                    RetVal.CurrentItem.Name = X.Value;
                                    break;

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

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

                                case "maxOccurs":
                                    RetVal.CurrentItem.MaxOccurs = X.Value;
                                    break;
                                }
                            }
                            X.MoveToElement();
                        }
                        break;

                    case "attribute":
                        break;
                    }
                    break;

                case XmlNodeType.EndElement:
                    if (X.LocalName == elementName)
                    {
                        done = true;
                    }
                    break;

                case XmlNodeType.Text:
                    break;
                }
            }while(!done && X.Read());

            return(RetVal);
        }
Esempio n. 4
0
        /// <summary>
        /// Parses a Byte Array at a specific location, and builds a Packet.
        /// </summary>
        /// <param name="buffer">The Array of Bytes</param>
        /// <param name="indx">The Start Index</param>
        /// <param name="count">The number of Bytes to process</param>
        /// <returns></returns>
        static public HTTPMessage ParseByteArray(byte[] buffer, int indx, int count)
        {
            HTTPMessage  TheMessage = new HTTPMessage();
            UTF8Encoding UTF8       = new UTF8Encoding();
            String       TempData   = UTF8.GetString(buffer, indx, count);
            DText        parser     = new DText();
            String       TempString;

            int idx = TempData.IndexOf("\r\n\r\n", StringComparison.Ordinal);

            if (idx < 0)
            {
                return(null);
            }
            TempData = TempData.Substring(0, idx);

            parser.ATTRMARK = "\r\n";
            parser.MULTMARK = ":";
            parser[0]       = TempData;
            string CurrentLine = parser[1];

            DText HdrParser = new DText();

            HdrParser.ATTRMARK = " ";
            HdrParser.MULTMARK = "/";
            HdrParser[0]       = CurrentLine;

            if (CurrentLine.ToUpper().StartsWith("HTTP/"))
            {
                TheMessage.ResponseCode = int.Parse(HdrParser[2]);
                int s1 = CurrentLine.IndexOf(" ", StringComparison.Ordinal);
                s1 = CurrentLine.IndexOf(" ", s1 + 1, StringComparison.Ordinal);
                TheMessage.ResponseData = UnEscapeString(CurrentLine.Substring(s1));
                try
                {
                    TheMessage.Version = HdrParser[1, 2];
                }
                catch (Exception ex)
                {
                    EventLogger.Log(ex);
                    TheMessage.Version = "0.9";
                }
            }
            else
            {
                TheMessage.Directive = HdrParser[1];
                TempString           = CurrentLine.Substring(CurrentLine.LastIndexOf(" ", StringComparison.Ordinal) + 1);
                if (TempString.ToUpper().StartsWith("HTTP/") == false)
                {
                    TheMessage.Version      = "0.9";
                    TheMessage.DirectiveObj = UnEscapeString(TempString);
                }
                else
                {
                    TheMessage.Version = TempString.Substring(TempString.IndexOf("/", StringComparison.Ordinal) + 1);
                    int fs = CurrentLine.IndexOf(" ", StringComparison.Ordinal) + 1;
                    TheMessage.DirectiveObj = UnEscapeString(CurrentLine.Substring(
                                                                 fs,
                                                                 CurrentLine.Length - fs - TempString.Length - 1));
                }
            }
            String Tag = "";

            for (int line = 2; line <= parser.DCOUNT(); ++line)
            {
                String TagData;
                if (Tag != "" && parser[line, 1].StartsWith(" "))
                {
                    TagData = parser[line, 1].Substring(1);
                }
                else
                {
                    Tag     = parser[line, 1];
                    TagData = "";
                    for (int i = 2; i <= parser.DCOUNT(line); ++i)
                    {
                        if (TagData == "")
                        {
                            TagData = parser[line, i];
                        }
                        else
                        {
                            TagData = TagData + parser.MULTMARK + parser[line, i];
                        }
                    }
                }
                TheMessage.AppendTag(Tag, TagData);
            }
            int cl;

            if (TheMessage.HasTag("Content-Length"))
            {
                try
                {
                    cl = int.Parse(TheMessage.GetTag("Content-Length"));
                }
                catch (Exception ex)
                {
                    EventLogger.Log(ex);
                    cl = -1;
                }
            }
            else
            {
                cl = -1;
            }

            byte[] tbuffer;
            if (cl > 0)
            {
                tbuffer = new byte[cl];
                if ((idx + 4 + cl) > count)
                {
                    // NOP
                }
                else
                {
                    Array.Copy(buffer, idx + 4, tbuffer, 0, cl);
                    TheMessage.DataBuffer = tbuffer;
                }
            }
            if (cl == -1)
            {
                tbuffer = new Byte[count - (idx + 4)];
                Array.Copy(buffer, idx + 4, tbuffer, 0, tbuffer.Length);
                TheMessage.DataBuffer = tbuffer;
            }
            if (cl == 0)
            {
                TheMessage.DataBuffer = new byte[0];
            }
            return(TheMessage);
        }
Esempio n. 5
0
        private void ProcessPacket(HTTPMessage msg, IPEndPoint src, IPEndPoint local)
        {
            if (OnSniffPacket != null)
            {
                OnSniffPacket(src, null, msg);
            }

            DText parser = new DText();

            parser.ATTRMARK = "::";

            bool   Alive = false;
            String UDN   = msg.GetTag("USN");

            parser[0] = UDN;
            String USN = parser[1];

            USN = USN.Substring(USN.IndexOf(":", StringComparison.Ordinal) + 1);
            String ST     = parser[2];
            int    MaxAge = 0;

            String NTS = msg.GetTag("NTS").ToUpper();

            if (NTS == "SSDP:ALIVE")
            {
                Alive = true;
                String ma = msg.GetTag("Cache-Control").Trim();
                if (ma != "")
                {
                    parser.ATTRMARK = ",";
                    parser.MULTMARK = "=";
                    parser[0]       = ma;
                    for (int i = 1; i <= parser.DCOUNT(); ++i)
                    {
                        if (parser[i, 1].Trim().ToUpper() == "MAX-AGE")
                        {
                            MaxAge = int.Parse(parser[i, 2].Trim());
                            break;
                        }
                    }
                }
            }

            if (msg.Directive == "NOTIFY" && OnNotify != null)
            {
                Uri    locuri   = null;
                string location = msg.GetTag("Location");
                if (location != null && location.Length > 0)
                {
                    try
                    {
                        locuri = new Uri(location);
                    }
                    catch (Exception ex)
                    {
                        EventLogger.Log(ex);
                    }
                }
                OnNotify(src, msg.LocalEndPoint, locuri, Alive, USN, ST, MaxAge, msg);
            }
            else if (msg.Directive == "M-SEARCH" && OnSearch != null)
            {
                if (ValidateSearchPacket(msg) == false)
                {
                    return;
                }
                int          MaxTimer   = int.Parse(msg.GetTag("MX"));
                SearchStruct SearchData = new SearchStruct();
                SearchData.ST     = msg.GetTag("ST");
                SearchData.Source = src;
                SearchData.Local  = local;
                SearchTimer.Add(SearchData, RandomGenerator.Next(0, MaxTimer));
            }
        }