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); } }
private void ReceiveSink(HTTPSession sender, HTTPMessage msg) { StateData sd = (StateData)sender.StateObject; object Tag = sd.Tag; if (msg.Version == "1.0" || msg.Version == "0.9") { sender.Close(); } else { if (msg.GetTag("Connection").ToUpper() == "CLOSE") { sender.Close(); } } if (OnResponse != null) { OnResponse(this, msg, Tag); } // If I don't set this to null, this holds a strong reference, resulting in // possible memory leaks sender.StateObject = null; lock (TagQueue) { if (TagQueue.Count == 0) { IdleTimeout = true; KeepAliveTimer.Add(GetHashCode(), 10); } } }
private bool ValidateSearchPacket(HTTPMessage msg) { int MX; if (msg.GetTag("MAN") != "\"ssdp:discover\"") { return(false); // { throw (new InvalidSearchPacketException("Invalid MAN")); } } if (msg.DirectiveObj != "*") { return(false); // { throw (new InvalidSearchPacketException("Expected * in RequestLine")); } } if (double.Parse(msg.Version, new CultureInfo("en-US").NumberFormat) < 1.1) { return(false); // { throw (new InvalidSearchPacketException("Version must be at least 1.1")); } } if (int.TryParse(msg.GetTag("MX"), out MX) == false || MX <= 0) { return(false); // { throw (new InvalidSearchPacketException("MX must be a positive integer")); } } return(true); }
/// <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); }
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)); } }