News:

How did you even find this place?

Main Menu

[C#] Packet Buffer & Parser

Started by Smarter, December 06, 2006, 07:49:20 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Smarter

I'm attempting to convert:
Static strBuffer As String
Dim strTemp As String, lngLen As Long
Winsock1.GetData strTemp, vbString
strBuffer = strBuffer & strTemp
While Len(strBuffer) > 4
    lngLen = Val("&H" & Login.StrToHex(StrReverse(Mid(strBuffer, 3, 2))))
    If Len(strBuffer) < lngLen Then Exit Sub
    Login.ParsePacket Left(strBuffer, lngLen)
    strBuffer = Mid(strBuffer, lngLen + 1)
Wend


and
Private Buffer As String

Public Function InsertDWORDArray(Data() As Long)
Dim i As Integer
For i = LBound(Data) To UBound(Data) Step 1
    Buffer = Buffer & MakeDWORD(Data(i))
Next i
End Function

Public Function InsertDWORD(Data As Long)
Buffer = Buffer & MakeDWORD(Data)
End Function

Public Function InsertData(Data As String)
Buffer = Buffer & Data
End Function

Public Function InsertWORD(Data As Integer)
Buffer = Buffer & MakeWORD(Data)
End Function

Public Function InsertBYTE(Data As Integer)
Buffer = Buffer & Chr(Data)
End Function

Public Sub InsertBytes(Data As String)
Dim i As Long, Enqueueer As String
For i = 1 To Len(Data) Step 3
    Enqueueer = Enqueueer & Chr(Val("&h0" & Mid(Data, i, 2)))
Next i
Buffer = Buffer & Enqueueer
End Sub

Public Function InsertNTString(Data As String)
Buffer = Buffer & Data & Chr(0)
End Function

Public Function InsertNonNTString(Data As String)
Buffer = Buffer & Data
End Function

Function MakeDWORD(Value As Long) As String
Dim Result As String * 4
CopyMemory ByVal Result, Value, 4
MakeDWORD = Result
End Function

Function MakeWORD(Value As Integer) As String
Dim Result As String * 2
CopyMemory ByVal Result, Value, 2
MakeWORD = Result
End Function

Public Function Clear()
Buffer = ""
End Function

Public Function SendPacket(PacketID As Byte)
Dim PacketData As String, Packet As String
If Form1.Winsock1.State <> sckConnected Then: Exit Function
Form1.Winsock1.SendData Chr(&HFF) & Chr(PacketID) & MakeWORD(Len(Buffer) + 4) & Buffer
Clear
End Function






From VB > C#, It's proving quite the challange for me, as I am still learning C#, any help would be greatly appreciated.

rabbit

Maybe if you just used the original one...

Smarter


Chavo

He means there are other C# Packet Buffer's already around.  I wouldn't recommend copying them though, you are more likely to learn about them if you port it from another language that forces you to learn it.  The best solution would be to write it from scratch, but something tells me thats not the solution you want if you are trying to convert VB to C# ;)

Smarter

Well, both actually, I want to learn through conversion, and i just "want it to work" heh. Could you link me ot the C# pbuffer you speak of?

MyndFyre

http://www.jinxbot.net/mbncsutil/ is the link to the full documentation.  MBNCSUtil is licensed under the modified BSD license.

Because MBNCSUtil is also designed to be a generalized networking library, there are several classes of interest.  The most basic class is the DataBuffer, and there is a specialized Battle.net version of it called BncsPacket.

For reading incoming data, there is a generalized DataReader packet, and a specialized Battle.net-aware version called BncsReader.

You can try to reach me on AIM for support of this, but I don't have time to write your code for you.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Smarter

I've had MBNCSUtil, since I started learning C#, and it seems and I was told that it is hard to use.

Smarter

        private void sckBNET_DataArrival(long bytesTotal, object sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
        {
            BncsPacket BNCSPacket = new BncsPacket();
            BncsReader BNCSRead = new BncsReader();
            BNCSPacket.Count();
        }

After playing with it a bit, it seems to make some sense, but how do you get the data arriving into the buffer, and use it, etc. Is there a C# Bot source out that I could learn this from?

MyndFyre

Well, first of all, if you've ever seen my talks about Battle.net programming and .NET network programming in general, you'll know that I do not advocate the use of the ActiveX Winsock control in .NET programs.  With that said, I'm not going to correct the "code sample" you've provided.

I will point out that if you're using C#, the code you provided will cause 3 compiler errors: the BncsPacket() and BncsReader() default constructors are marked Obsolete.  The third is that Count is a property, not a method, and should not be referenced with trailing parentheses (it is referred to as BNCSPacket.Count, not BNCSPacket.Count()).

MBNCSUtil is organized much in the same way that the .NET Framework is, and uses many of the same naming conventions, so anyone familiar with the .NET Framework Class Library should generally understand what functions mean.

Because I chose to follow the .NET Framework's naming conventions, though, some of the Battle.net naming conventions are lost.  For example, what is commonly thought of as "AddDWORD()" or "ReadDWORD()" would be AddInt32() in BncsPacket, or ReadInt32() in BncsReader.

For instance, when you're reading data from the server:

sckBnet.Read(buf, 0, 4, SocketFlags.None);
int len = BitConverter.ToInt16(buf, 2) - 4;
byte[] data = new byte[len];
if (len < 0)
{
  sckBnet.Read(data, 0, len, SocketFlags.None);
}
HandlePacket(buf[1], data, len);

That code reads one packet of data from a Battle.net connection (assuming you have a Socket named sckBnet and a byte[] named buf of arbitrary length >= 4), and then calls HandlePacket with the packet ID, length, and any data that may have been included.

HandlePacket demonstrates how easy it is to use a DataReader.

void HandlePacket(byte id, byte[] data, int len)
{
  DataReader rdr = new DataReader(data);
  switch (id)
  {
    case 0x50: // SID_AUTH_INFO
      int logonType =  rdr.ReadInt32();
      int serverToken = rdr.ReadInt32();
      int udpToken = rdr.ReadInt32();
      DateTime mpqTime = DateTime.FromFileTime(rdr.ReadInt64());
      string verFileName = rdr.ReadCString();
      string vercheckStr = rdr.ReadCString();
  // do something with all of that.
      break;
  }
}
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Smarter

Did I ever mention I love you, and about 20 minutes after I posted that I started talking with a C# developer friend of mine who told me the only help he would give me is to use TCPClient();, but Socket you say to use, so i'll be sure to use that! Thanks so much, if i ever actually finish my program, you will be credited everywhere possible! Ok, and one other thing, so MBNCSUtil, is just a buffer, so I still have to make a Parser? Or does that have a sexy part for that too, cause in VB that was the part I really blew with, parsing the data... I could make the packets up, etc, but couldn't parse it.. always had to get help.

Smarter

Gah see, i always don't know the simplest thing, i've been at it for 2 days now, cannot figure out how to get the data into the Buffer array.. >.<.... also Socket doesn't have a .Read method ...? I must be doing something wrong here...! >.<

MyndFyre

Quote from: Smarter on December 08, 2006, 09:35:32 AM
Gah see, i always don't know the simplest thing, i've been at it for 2 days now, cannot figure out how to get the data into the Buffer array.. >.<.... also Socket doesn't have a .Read method ...? I must be doing something wrong here...! >.<
My bad, I was thinking of a NetworkStream.  Socket's equivalent call is Receive where I wrote Read.

Socket, NetworkStream, and TcpClient are all valid classes to use.

DataReader/BncsReader is the parser class.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Smarter

Well, i figured all that out, and made the socket use a ASync connection, but now my problem is implementing BNLS into my project... it confuses me... I got the data to buffer and all that great stuff, the project just needs it's 0x51 C>S ... using BNLS, any help on that? lol I hate asking for help >.<.

Warrior

Interesting topic, comes at a good time when I'm working on a C# Battle.net client.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Smarter

public void OnDataReceived(IAsyncResult asyn)
        {
            int len = BitConverter.ToInt16(buffer, 2) - 4;
            byte[] data = new byte[len];
            if (len < 0)
            {
                // sckBnet.Read(data, 0, len, SocketFlags.None);
                sckBNET.Receive(data, 0, len, SocketFlags.None);
            }
            HandlePacket(buffer[1], data, len);

            //end receive...
            int iRx = 0;
            iRx = sckBNET.EndReceive(asyn);

            WaitForData();
        }


I get an error stating: byte[] data = new byte[len]; - Arithmetic operation resulted in an overflow., help! lol