private void DispatchSNACData(SNAC snac, byte [] byteData) { string strSnacID = string.Format("{0:X2}:{1:X2}", snac.family, snac.subtype); if (OnSNAC != null) { OnSNAC(strSnacID); } switch (strSnacID) { // error! case "02:01": break; // message in case "04:07": MessageIn(byteData); break; // buddy signed on case "03:0B": UserSignedOn(byteData); break; // signed off case "03:0C": UserSignedOff(byteData); break; #region login cases // login procedure case "01:03": byte [] outData = { 0, 1, 0, 3, 0, 2, 0, 1, 0, 3, 0, 1, 0, 21, 0, 1, 0, 4, 0, 1, 0, 6, 0, 1, 0, 9, 0, 1, 0, 10, 0, 1 }; SendSnacPacket(GetSNACHeader(0x1, 0x17, 0x0, 0x17), outData); break; // login procedure case "01:18": //SNAC(04,02) byte [] outData3 = { 0, 0, 0, 0, 0, 3, 0x1f, 0x40, 3, 0xe7, 3, 0xef, 0, 0, 0, 0 }; SendSnacPacket(GetSNACHeader(0x04, 0x02, 0x00, 0x02), outData3); // SNAC (02,04) byte [] outData2 = { 0x00, 0x05, 0x00, 0x30, 0x09, 0x46, 0x13, 0x49, 0x4C, 0x7F, 0x11, 0xD1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00, 0x09, 0x46, 0x13, 0x44, 0x4C, 0x7F, 0x11, 0xD1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00, 0x09, 0x46, 0x13, 0x4E, 0x4C, 0x7F, 0x11, 0xD1, 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00 }; SendSnacPacket(GetSNACHeader(0x02, 0x04, 0x00, 0x04), outData2); // SNAC (03,04) byte [] newData1 = { 0x09, 0x31, 0x36, 0x31, 0x39, 0x36, 0x32, 0x33, 0x39, 0x34 }; SendSnacPacket(GetSNACHeader(0x03, 0x04, 0x00, 0x04), newData1); // SNAC (01,1E) byte [] outData4 = { 0x00, 0x06, 0x00, 0x04, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x25, 0xC0, 0xA8, 0x01, 0x64, 0x00, 0x00, 0x0C, 0xE0, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xAB, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xBA, 0xAD, 0xDE, 0x00, 0x00 }; SendSnacPacket(GetSNACHeader(0x01, 0x1e, 0x00, 0x1e), outData4); // SNAC (01,02) Client Ready byte [] newData3 = { 0x00, 0x01, 0x00, 0x03, 0x01, 0x10, 0x02, 0x8A, 0x00, 0x02, 0x00, 0x01, 0x01, 0x01, 0x02, 0x8A, 0x00, 0x03, 0x00, 0x01, 0x01, 0x10, 0x02, 0x8A, 0x00, 0x15, 0x00, 0x01, 0x01, 0x10, 0x02, 0x8A, 0x00, 0x04, 0x00, 0x01, 0x01, 0x10, 0x02, 0x8A, 0x00, 0x06, 0x00, 0x01, 0x01, 0x10, 0x02, 0x8A, 0x00, 0x09, 0x00, 0x01, 0x01, 0x10, 0x02, 0x8A, 0x00, 0x0A, 0x00, 0x01, 0x01, 0x10, 0x02, 0x8A }; SendSnacPacket(GetSNACHeader(0x01, 0x2, 0x00, 0x2), newData3); if (OnSignedOn != null) { OnSignedOn(); } break; #endregion default: break; } }
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(); } } }