Example #1
0
		public void MessageIn(byte [] Data)
		{
			short iNameLen = Data[10];
			string strSender = Encoding.ASCII.GetString(Data,11,iNameLen);
			string strMessage;
			bool bAway = false;

			int iTlvNumIdx = 11+iNameLen+2;
			int iNumTLVs = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data,iTlvNumIdx));

			// read through the TLVs
			int iTlvStart = iTlvNumIdx + 2;
			while (iTlvStart < Data.Length)
			{
				TLV tlv = new TLV();
				tlv.type = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data,iTlvStart));
				tlv.length = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data,iTlvStart+2));
				
				if (tlv.type == 0x01)
				{
					// user status
					int iOffset = iTlvStart + 4;
					short iStatus = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data,iOffset));
					bAway = (0x20 & iStatus) != 0;
				}
				if (tlv.type == 0x02)
				{
					int iOffset = iTlvStart + 4;
					while (iOffset < (iTlvStart + 4 + tlv.length))
					{
						TLV subtlv = new TLV();
						subtlv.type = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data,iOffset));
						subtlv.length = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data,iOffset+2));

						if (subtlv.type == 0x101)
						{
							int iMsgOffset = iOffset+8;
							strMessage = Encoding.ASCII.GetString(Data,iMsgOffset,subtlv.length-4);

							OnIMIn(Normalize(strSender),Regex.Replace(strMessage,@"<(.|\n)*?>",string.Empty),bAway);
							return;
						}
						iOffset += 4+subtlv.length;
					}
				}
				
				iTlvStart += 4+tlv.length;
			}
		}
Example #2
0
		public Hashtable GetTLVHash(byte [] byteData, int iDataLen)
		{
			int iIndex = 0;
			Hashtable retVal = new Hashtable();
			byte [] byteTemp = new byte[2];
			
			while (iIndex < iDataLen)
			{
				TLV tlv = new TLV();

				byteTemp[1] = byteData[iIndex++];
				byteTemp[0] = byteData[iIndex++];
				tlv.type = BitConverter.ToInt16(byteTemp,0);

				byteTemp[1] = byteData[iIndex++];
				byteTemp[0] = byteData[iIndex++];
				tlv.length = BitConverter.ToInt16(byteTemp,0);
				
				Array.Copy(byteData,iIndex,tlv.byteData,0,tlv.length);
				iIndex += tlv.length;

				retVal[tlv.type.ToString()] = tlv;
			}

			return retVal;
		}
Example #3
0
        public void OnRecievedData(IAsyncResult ar)
        {
            Socket sock = (Socket)ar.AsyncState;

            try
            {
                int nBytesRead = 0;
                int nBytesRec  = sock.EndReceive(ar);
                if (nBytesRec > 0)
                {
                    do
                    {
                        // build the flap header
                        flap_header fh = new flap_header();
                        fh.asterisk  = (char)m_byBuff[nBytesRead + 0];
                        fh.frametype = (byte)m_byBuff[nBytesRead + 1];

                        byte [] byteTemp = new byte[2];
                        byteTemp[1] = m_byBuff[nBytesRead + 2];
                        byteTemp[0] = m_byBuff[nBytesRead + 3];
                        fh.seqno    = BitConverter.ToInt16(byteTemp, 0);

                        byteTemp[1] = m_byBuff[nBytesRead + 4];
                        byteTemp[0] = m_byBuff[nBytesRead + 5];
                        fh.datalen  = BitConverter.ToInt16(byteTemp, 0);

                        #region do-while-loop
                        // we're talking to the authentication server
                        if (!m_bAuthenticated)
                        {
                            if (fh.frametype == 4)
                            {
                                byteTemp = new byte[fh.datalen];
                                Array.Copy(m_byBuff, nBytesRead + 6, byteTemp, 0, fh.datalen);
                                Hashtable loginInfo = GetTLVHash(byteTemp, fh.datalen);

                                // authentication error
                                if (loginInfo["8"] != null)
                                {
                                    TLV tlv = (TLV)loginInfo[8];
                                    if (OnError != null)
                                    {
                                        OnError("Error Code (" + Encoding.ASCII.GetString(tlv.byteData, 0, 2) + ")");
                                    }
                                    m_socket.Shutdown(SocketShutdown.Both);
                                    m_socket.Close();
                                    m_bTCPConnected = false;
                                }
                                // success!
                                else if (loginInfo["6"] != null)
                                {
                                    // set the BOS info
                                    TLV       tlv     = (TLV)loginInfo["5"];
                                    string    strTemp = Encoding.ASCII.GetString(tlv.byteData, 0, tlv.length);
                                    string [] strData = Regex.Split(strTemp, "(:)");
                                    m_strBOSServer = strData[0];
                                    m_strBOSPort   = strData[2];

                                    // set the auth cookie
                                    TLV cookie = (TLV)loginInfo["6"];
                                    m_authCookie = new byte[cookie.length];
                                    Array.Copy(cookie.byteData, 0, m_authCookie, 0, cookie.length);

                                    // shut down and connect to BOS
                                    m_bAuthenticated = true;
                                    m_bTCPConnected  = false;
                                    m_socket.Shutdown(SocketShutdown.Both);
                                    m_socket.Close();
                                    Connect();
                                }
                            }
                        }
                        else
                        {
                            // SNAC data is always on flap channel 2
                            if (fh.frametype == 2)
                            {
                                byte [] rawData = new byte[fh.datalen];
                                Array.Copy(m_byBuff, nBytesRead + 6, rawData, 0, fh.datalen);

                                // build the snac
                                SNAC snac = new SNAC();
                                snac.family    = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData, 0));
                                snac.subtype   = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData, 2));
                                snac.flags     = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(rawData, 4));
                                snac.requestid = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(rawData, 6));

                                // get the snac data
                                byte [] snacData = new byte[fh.datalen - 10];
                                Array.Copy(rawData, 10, snacData, 0, fh.datalen - 10);

                                // send it on its way
                                DispatchSNACData(snac, snacData);
                            }
                        }
                        #endregion

                        nBytesRead += fh.datalen + FLAP_HEADER_LENGTH;
                    } while (nBytesRead < nBytesRec);

                    SetupRecieveCallback(sock);
                }
                else if (!m_bDCOnPurpose)
                {
                    HandleReconnect();                     // looks like we disconnect, so reconnect
                }
            }
            catch (Exception ex)
            {
                // looks like the connection dropped
                if (!m_bDCOnPurpose)
                {
                    HandleReconnect();
                }
            }
        }