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); } }
private void ValidateSearchPacket(HTTPMessage msg) { if (msg.GetTag("MAN") != "\"ssdp:discover\"") { throw new InvalidSearchPacketException("Invalid MAN"); } if (msg.DirectiveObj != "*") { throw new InvalidSearchPacketException("Expected * in RequestLine"); } if (double.Parse(msg.Version, new CultureInfo("en-US").NumberFormat) < 1.1) { throw new InvalidSearchPacketException("Version must be at least 1.1"); } int num = 0; string tag = msg.GetTag("MX"); if (tag == "") { throw new InvalidSearchPacketException("Missing MX"); } try { num = int.Parse(tag); } catch (Exception) { throw new InvalidSearchPacketException("MX must be an integer"); } if (num <= 0) { throw new InvalidSearchPacketException("MX must be a positive integer"); } }
private void ReceiveSink(HTTPSession sender, HTTPMessage msg) { StateData stateObject = (StateData)sender.StateObject; object tag = stateObject.Tag; if ((msg.Version == "1.0") || (msg.Version == "0.9")) { sender.Close(); } else if (msg.GetTag("Connection").ToUpper() == "CLOSE") { sender.Close(); } if (this.OnResponse != null) { this.OnResponse(this, msg, tag); } sender.StateObject = null; lock (this.TagQueue) { if (this.TagQueue.Count == 0) { this.IdleTimeout = true; KeepAliveTimer.Add(this.GetHashCode(), 10); } } }
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); }
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)); } } }