private void ReceiveSink2(AsyncSocket sender, Byte[] buffer, int HeadPointer, int BufferSize, int BytesRead, IPEndPoint source, IPEndPoint remote)
        {
            HTTPMessage msg = HTTPMessage.ParseByteArray(buffer, 0, BufferSize);
            if (msg.Directive != "NOTIFY")
            {
                sender.BufferBeginPointer = BufferSize;
                return;
            }

            string USN = msg.GetTag("USN");
            string UDN;
            if (USN.IndexOf("::") != -1)
            {
                UDN = USN.Substring(0, USN.IndexOf("::"));
            }
            else
            {
                UDN = USN;
            }
            UDN = UDN.Substring(5);

            if (msg.GetTag("NTS").Trim() == "ssdp:alive")
            {
                if (TestDevice.GetDevice(UDN) != null)
                {
                    NotifyTable[msg.GetTag("NT")] = DateTime.Now;
                }
            }

            sender.BufferBeginPointer = BufferSize;
        }
        public override void Start(UPnPDevice device)
        {
            UPnPDevice d = device;
            while (d.ParentDevice != null)
            {
                d = d.ParentDevice;
            }
            TestDevice = d;

            ASocket = new AsyncSocket(4096);
            ASocket.Attach(new IPEndPoint(TestDevice.InterfaceToHost, 0), System.Net.Sockets.ProtocolType.Udp);
            ASocket.SetTTL(4);
            ASocket.AddMembership((IPEndPoint)ASocket.LocalEndPoint, IPAddress.Parse("239.255.255.250"));
            ASocket.OnReceive += new AsyncSocket.OnReceiveHandler(ReceiveSink);
            ASocket.Begin();

            ASocket2 = new AsyncSocket(4096);
            ASocket2.Attach(new IPEndPoint(TestDevice.InterfaceToHost, 1900), System.Net.Sockets.ProtocolType.Udp);
            ASocket2.SetTTL(2);
            ASocket2.AddMembership((IPEndPoint)ASocket.LocalEndPoint, IPAddress.Parse("239.255.255.250"));
            ASocket2.OnReceive += new AsyncSocket.OnReceiveHandler(ReceiveSink2);

            Validate_MSEARCH_RESPONSETIME();
            Validate_NOTIFY();
            Validate_DISCOVERY();

            UPnPTestStates RetState = UPnPTestStates.Pass;
            if (NOTIFY == UPnPTestStates.Failed || DISCOVERY == UPnPTestStates.Failed)
            {
                RetState = UPnPTestStates.Failed;
            }
            else
            {
                if (NOTIFY == UPnPTestStates.Warn || DISCOVERY == UPnPTestStates.Warn || MX == UPnPTestStates.Warn)
                {
                    RetState = UPnPTestStates.Warn;
                }
            }

            state = RetState;
        }
        private void MSEARCHSink(AsyncSocket sender, Byte[] buffer, int HeadPointer, int BufferSize, int BytesRead, IPEndPoint source, IPEndPoint remote)
        {
            HTTPMessage msg = HTTPMessage.ParseByteArray(buffer, HeadPointer, BufferSize);
            DText P = new DText();

            string USN = msg.GetTag("USN");
            string UDN;
            if (USN.IndexOf("::") != -1)
            {
                UDN = USN.Substring(0, USN.IndexOf("::"));
            }
            else
            {
                UDN = USN;
            }
            UDN = UDN.Substring(5);

            sender.BufferBeginPointer = BufferSize;
            if (TestDevice.GetDevice(UDN) == null)
            {
                return;
            }

            lock (MSEARCHTable)
            {
                this.sample += "\r\n" + msg.GetTag("ST");
                MSEARCHTable[msg.GetTag("ST").Trim()] = "";
            }
        }
        private void ReceiveSink(AsyncSocket sender, Byte[] buffer, int HeadPointer, int BufferSize, int BytesRead, IPEndPoint source, IPEndPoint remote)
        {
            DateTime EndTime = DateTime.Now;
            DText P = new DText();
            HTTPMessage msg = HTTPMessage.ParseByteArray(buffer, 0, BufferSize);

            string USN = msg.GetTag("USN");
            string UDN = USN;
            if (USN.IndexOf("::") != -1)
            {
                UDN = USN.Substring(0, USN.IndexOf("::"));
            }

            UDN = UDN.Substring(5);
            sender.BufferBeginPointer = BufferSize;
            if (UDN != TestDevice.UniqueDeviceName) return;

            string cc = msg.GetTag("Cache-Control");
            P.ATTRMARK = "=";
            P[0] = cc;
            cc = P[2].Trim();
            this.Cache = int.Parse(cc);

            DPA.AddDataPoint(EndTime.Subtract(StartTime));

            MRE.Set();
        }
Example #5
0
		private void HandleDisconnect(AsyncSocket sender)
		{
			this.Connected = false;
            lock (this)
            {
                MainSocket = null;
            }
			if (this.NeedToWaitToClose==true)
			{
				if (UserStream==null)
				{
					SocketStream.Flush();
					Headers.BodyBuffer = SocketStream.ToArray();
					if (OpenSource.Utilities.EventLogger.Enabled)
					{
						OpenSource.Utilities.EventLogger.Log(this,System.Diagnostics.EventLogEntryType.Information,Headers.StringPacket);
					}
					OnSniffPacketEvent.Fire(this,Headers);
					OnReceiveEvent.Fire(this,Headers);					
				}
				DONE_ReadingPacket();
			}

			if (PostStream!=null)
			{
				OnStreamDoneEvent.Fire(this,PostStream);
			}
			Info infoObj;
			infoObj.CurrentStreamObject = null;
			lock(StateQueue)
			{
				if (StateQueue.Count>0)
				{
					infoObj = (Info)StateQueue.Peek();
				}
			}
			if (infoObj.CurrentStreamObject!=null)
			{
				OnStreamDoneEvent.Fire(this,infoObj.CurrentStreamObject);
			}

			OnClosedEvent.Fire(this);
		}
 private void BadMSEARCHSink(AsyncSocket sender, Byte[] buffer, int HeadPointer, int BufferSize, int BytesRead, IPEndPoint source, IPEndPoint remote)
 {
     HTTPMessage msg = HTTPMessage.ParseByteArray(buffer, HeadPointer, BufferSize);
     if (remote.Address.ToString() == TestDevice.RemoteEndPoint.Address.ToString())
     {
         lock (MSEARCHTable)
         {
             MSEARCHTable[remote.Address.ToString()] = msg;
         }
     }
 }
Example #7
0
		/// <summary>
		/// Creates a new HTTPSession
		/// </summary>
		/// <param name="Local">Source IPEndPoint to use</param>
		/// <param name="Remote">Remote IPEndPoint to connect to</param>
		/// <param name="CreateCallback">Succussful callback</param>
		/// <param name="CreateFailedCallback">Failed callback</param>
		/// <param name="State">StateObject</param>
		public HTTPSession(IPEndPoint Local, IPEndPoint Remote, SessionHandler CreateCallback, SessionHandler CreateFailedCallback, Object State)
		{
			OpenSource.Utilities.InstanceTracker.Add(this);
			local_ep = Local;
			
			OnCreateSession += CreateCallback;
			OnCreateFailed += CreateFailedCallback;

			StateObject = State;

			MainSocket = new AsyncSocket(4096);
			MainSocket.Attach(Local,ProtocolType.Tcp);
			MainSocket.OnConnect += new AsyncSocket.ConnectHandler(HandleConnect);
			MainSocket.OnConnectFailed += new AsyncSocket.ConnectHandler(HandleConnectFailed);
			MainSocket.OnDisconnect += new AsyncSocket.ConnectHandler(HandleDisconnect);
			MainSocket.OnSendReady += new AsyncSocket.OnSendReadyHandler(HandleReady);

			MainSocket.Connect(Remote);
		}
Example #8
0
		public HTTPSession(IPEndPoint Local, Socket TheSocket)
		{
			/* Eveything calls this */

			OpenSource.Utilities.InstanceTracker.Add(this);

			this.Connected = TheSocket.Connected;
			local_ep = Local;

			MainSocket = new AsyncSocket(4096);
			MainSocket.Attach(TheSocket);
			MainSocket.OnReceive += new AsyncSocket.OnReceiveHandler(HandleReceive);
			MainSocket.OnDisconnect += new AsyncSocket.ConnectHandler(HandleDisconnect);
			MainSocket.OnSendReady += new AsyncSocket.OnSendReadyHandler(HandleReady);
			/* This is New */
			//MainSocket.BufferReadLength = 1;
			MainSocket.BufferReadLength = 1024;
		}
Example #9
0
		/// <summary>
		/// Close the session
		/// </summary>
		public void Close()
		{
			try
			{
				if (MainSocket!=null)
				{
					MainSocket.Close();	
				}
			}
			catch(Exception ex)
			{
                OpenSource.Utilities.EventLogger.Log(ex);
            }
			MainSocket = null;
		}
Example #10
0
		private void HandleReceive(AsyncSocket sender, Byte[] buffer, int BeginPointer, int BufferSize, int BytesRead, IPEndPoint source, IPEndPoint remote)
		{

			if (BytesRead!=0)
			{
				OnSniffEvent.Fire(buffer,BufferSize-BytesRead, BytesRead);
			}
		

			if (this.FinishedHeader==false)
			{
				if (BufferSize<4)
				{
					sender.BufferReadLength = 1;
					sender.BufferBeginPointer = 0;
					return;
				}
				/* This is New */
				for(int i=4;i<BufferSize-4;++i)
				{
					if ((buffer[i-4]==13)&&(buffer[i-3]==10)&&
						(buffer[i-2]==13)&&(buffer[i-1]==10))
					{
						BufferSize = i;
						break;
					}
				}


				if ((buffer[BufferSize-4]==13)&&(buffer[BufferSize-3]==10)&&
					(buffer[BufferSize-2]==13)&&(buffer[BufferSize-1]==10))
				{
					// End Of Headers
					Headers = HTTPMessage.ParseByteArray(buffer,0,BufferSize);
					

					if (Headers.StatusCode!=-1)
					{
						if (Headers.StatusCode>=100 && Headers.StatusCode<=199)
						{
							//Informational
							if (OpenSource.Utilities.EventLogger.Enabled)
							{
								OpenSource.Utilities.EventLogger.Log(this,System.Diagnostics.EventLogEntryType.Information,"<<IGNORING>>\r\n" + Headers.StringPacket);
							}
//							OnHeaderEvent.Fire(this,Headers, UserStream);
							OnSniffPacketEvent.Fire(this,Headers);
//							OnReceiveEvent.Fire(this, Headers);
//							DONE_ReadingPacket();
							BeginHeader(BufferSize);
							return;
						}

						if (Headers.StatusCode==204 || Headers.StatusCode==304)
						{
							//No Body or No Change
							if (OpenSource.Utilities.EventLogger.Enabled)
							{
								OpenSource.Utilities.EventLogger.Log(this,System.Diagnostics.EventLogEntryType.Information,Headers.StringPacket);
							}
							OnHeaderEvent.Fire(this,Headers, UserStream);
							OnSniffPacketEvent.Fire(this,Headers);
							OnReceiveEvent.Fire(this, Headers);
							DONE_ReadingPacket();
							BeginHeader(BufferSize);
							return;
						}
					}
					else
					{
						SET_START_OF_REQUEST();
					}

					FinishedHeader = true;
					if (Headers.GetTag("Content-Length")=="")
					{
						if (Headers.GetTag("Transfer-Encoding").ToUpper()=="CHUNKED")
						{
							IsChunked = true;
						}
						else
						{
							if (Headers.StatusCode!=-1)
							{
								this.NeedToWaitToClose = true;
							}
						}
					}
					else
					{
						if (Headers.GetTag("Transfer-Encoding").ToUpper()=="CHUNKED")
						{
							IsChunked = true;
						}
						else
						{
							BodySize = int.Parse(Headers.GetTag("Content-Length"));
						}
					}
					if (Headers.GetTag("Connection").ToUpper()=="CLOSE")
					{
						this.ConnectionCloseSpecified=true;
					}
					
					if (!IsChunked && NeedToWaitToClose && !ConnectionCloseSpecified &&!IsLegacy && Headers.Version!="1.0")
					{
						NeedToWaitToClose = false;
						BodySize = 0;
					}

					OnHeaderEvent.Fire(this,Headers, UserStream);
					if (this.NeedToWaitToClose==true)
					{
						sender.BufferBeginPointer = BufferSize;
						sender.BufferReadLength = 4096;
					}
					else
					{
						if (IsChunked==true)
						{
							// Chunked
							BeginChunk(BufferSize);
						}
						else
						{
							// Normal
							if (BodySize==0)
							{
								// Already have the packet
								if (OpenSource.Utilities.EventLogger.Enabled)
								{
									OpenSource.Utilities.EventLogger.Log(this,System.Diagnostics.EventLogEntryType.Information,Headers.StringPacket);
								}
								OnSniffPacketEvent.Fire(this,Headers);
								OnReceiveEvent.Fire(this, Headers);
								if (UserStream!=null)
								{
									UserStream.Flush();
									OnStreamDoneEvent.Fire(this,UserStream);
								}
								DONE_ReadingPacket();
								BeginHeader(BufferSize);
							}
							else
							{
								if (BodySize<=4096)
								{
									sender.BufferBeginPointer = BufferSize;
									sender.BufferReadLength = BodySize;
								}
								else
								{
									sender.BufferBeginPointer = BufferSize;
									sender.BufferReadLength = 4096;
								}
							}
						} // End Normal Else Clause
					} // End Non HTTP/1.0 Else Clause
					return;
				} // End of Processing Header
				else
				{
					// Need to read more of the header
					sender.BufferBeginPointer = 0;
					sender.BufferReadLength = 1;
				}
				return;
			} // End of If FinishedHeader
			

			// Have some body data
			if (IsChunked == false)
			{
				/* This is New */
				if (this.NeedToWaitToClose==false)
				{
					if (BufferSize>BodySize) BufferSize = BodySize;
				}

				// Normal Data
				if (UserStream!=null)
				{
					UserStream.Write(buffer,0,BufferSize);
				}
				else
				{
//					OpenSource.Utilities.EventLogger.Log(this,System.Diagnostics.EventLogEntryType.SuccessAudit,"NONChunk Data: " + BufferSize.ToString() + " bytes");
//					OpenSource.Utilities.EventLogger.Log(this,System.Diagnostics.EventLogEntryType.SuccessAudit,"Writing["+SocketStream.GetHashCode().ToString()+"]: " + U.GetString(buffer,0,BufferSize));

					SocketStream.Write(buffer,0,BufferSize);
				}

				if (this.NeedToWaitToClose==false)
				{
					BodySize -= BufferSize;
					if (BodySize>0)
					{
						// More To Read
						sender.BufferBeginPointer  = BufferSize;
						if (BodySize < 4096)
						{
							sender.BufferReadLength = BodySize;
						}
						else
						{
							sender.BufferReadLength = 4096;
						}
					}
					else
					{
						// Finished Reading
						if (UserStream==null)
						{
							SocketStream.Flush();
							Headers.BodyBuffer = SocketStream.ToArray();
							if (OpenSource.Utilities.EventLogger.Enabled)
							{
								OpenSource.Utilities.EventLogger.Log(this,System.Diagnostics.EventLogEntryType.Information,Headers.StringPacket);
							}
							OnSniffPacketEvent.Fire(this,Headers);
							OnReceiveEvent.Fire(this, Headers);
						}
						else
						{
							UserStream.Flush();
							OnStreamDoneEvent.Fire(this,UserStream);
						}
						DONE_ReadingPacket();
						BeginHeader(BufferSize);
					}
				}
				else
				{
					// HTTP/1.0 Socket
					sender.BufferReadLength = 4096;
					sender.BufferBeginPointer = BufferSize;
				}
			}
			else
			{
				// Chunked Data
				ProcessChunk(buffer, BufferSize);
			}
		}
Example #11
0
		private void HandleConnect(AsyncSocket sender)
		{
			MainSocket.OnReceive += new AsyncSocket.OnReceiveHandler(HandleReceive);
			/* This is New */
			//MainSocket.BufferReadLength = 1;
			MainSocket.BufferReadLength = 1024;
			
			this.Connected = true;
			OnCreateSessionEvent.Fire(this);
			MainSocket.Begin();
		}
Example #12
0
		private void HandleConnectFailed(AsyncSocket sender)
		{
			OnCreateFailedEvent.Fire(this);
		}
 /// <summary>
 /// Close the session
 /// </summary>
 public void Close()
 {
     try
     {
         if (MainSocket!=null)
         {
             MainSocket.Close();
         }
     }
     catch(Exception)
     {
     }
     MainSocket = null;
 }