Valhalla Legends Archive

Programming => Battle.net Bot Development => Topic started by: PhoeNix_FasT on August 15, 2007, 01:10 PM

Title: 0x50 with C# Problem
Post by: PhoeNix_FasT on August 15, 2007, 01:10 PM
Im new to C# (learning it for 2 Month now) and i tried the simpliest thing : Sending 0x50 and receive an answer.
For this i use MBNCSUtil ( using MBNCSUtil; )
This is what i wrote

            IPHostEntry ip = Dns.GetHostByName("useast.battle.net");
            IPAddress[] ips = ip.AddressList;
            IPAddress battleNetIP = ips[0];

            richTextBox1.AppendText("Address of useast.battle.net : " + battleNetIP + "\n");

            m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipEnd = new IPEndPoint(battleNetIP, battleNetPort);
            try
            {
                m_clientSocket.Connect(ipEnd);
            }
            catch (Exception exp)
            {
                richTextBox1.AppendText("Exception : " + exp.Message + "\n");
                return;
            }

            if(m_clientSocket.Connected)
                richTextBox1.AppendText("Connected\n");

            BncsPacket pck0x50 = new BncsPacket((byte)0x50);
            pck0x50.InsertInt32(0);             
            pck0x50.InsertDwordString("IX86"); 
            pck0x50.InsertDwordString("D2XP"); 
            pck0x50.InsertInt32(0);             
            pck0x50.InsertInt32(0);             
            pck0x50.InsertInt32(0);             
            pck0x50.InsertInt32(0);             
            pck0x50.InsertInt32(0);             
            pck0x50.InsertDwordString("", 0);   
            pck0x50.InsertCString("USA");       
            pck0x50.InsertCString("United States");

            byte[] byteData = new byte[32];

            try
            {
                m_clientSocket.Send(pck0x50.GetData(), SocketFlags.None);   
            }

            catch (Exception exp)
            {
                richTextBox1.AppendText("Exception : " + exp.Message + "\n");
                return;
            }

            int i = m_clientSocket.Receive(byteData);
            richTextBox1.AppendText("Bytes Received :" + i + "\n");

Its my first time i try to use Sockets in C#. Anyways .. i = 0, means i received 0 bytes.
What did i do wrong ?
Title: Re: 0x50 with C# Problem
Post by: brew on August 15, 2007, 01:35 PM
Did you send the 0x01 protocol byte first? And I'm assuming pck0x50.InsertDwordString("D2XP"); 
reverses and concatinates "D2XP" to the buffer, amirite? Not to mention you're missing the verbyte dword (comes right after the client id) Also .... er... "pck0x50.InsertInt32(0);             
            pck0x50.InsertDwordString("", 0);   " Are you sure you have the basic format of the 0x50 packet down?
Title: Re: 0x50 with C# Problem
Post by: PhoeNix_FasT on August 15, 2007, 02:50 PM
0x01 ? Sorry didnt find that
I had a look right here : http://ersan.us/src/bnetdocs/sequence.html

it says:

SEND ->   SID_AUTH_INFO   (0x50)
RECV <-   SID_PING   (0x25)
RECV <-   SID_AUTH_INFO   (0x50)

So would it be enough with

BncsPacket pck0x01 = new BncsPacket((byte)0x01);

and then send this packet ?
Title: Re: 0x50 with C# Problem
Post by: Hdx on August 15, 2007, 03:04 PM
No, its not a packet, its jsut a raw byte, i assume your packetbuffer adds the header info (0xff id length)
Which you wont need for the protocol byte.
As he also said, you're missing the version byte in your 0x50 build, fix that.
~Hdx
Title: Re: 0x50 with C# Problem
Post by: PhoeNix_FasT on August 15, 2007, 03:39 PM
            BncsPacket pck0x50 = new BncsPacket((byte)0x50);
            pck0x50.InsertInt32(0);             
            pck0x50.InsertDwordString("IX86"); 
            pck0x50.InsertDwordString("D2XP");
            pck0x50.InsertByte(0x0A);             
            pck0x50.InsertInt32(0);             
            pck0x50.InsertInt32(0);             
            pck0x50.InsertInt32(0);             
            pck0x50.InsertInt32(0);             
            pck0x50.InsertDwordString("", 0);   
            pck0x50.InsertCString("USA");       
            pck0x50.InsertCString("United States");

            byte[] byteData = new byte[511];

            try
            {
                m_clientSocket.Send(pck0x50.GetData(), SocketFlags.None);
                richTextBox1.AppendText("Sending 0x50\n");
            }

            catch (Exception exp)
            {
                richTextBox1.AppendText("Exception : " + exp.Message + "\n");
                return;
            }

            int i = m_clientSocket.Receive(byteData);
            richTextBox1.AppendText("Bytes Received :" + i + "\n");


i is still 0 ..  >:(
Title: Re: 0x50 with C# Problem
Post by: Hdx on August 15, 2007, 03:43 PM
Where are you sending the protocol byte?
And the verbyte ix 0x0b...
~Hdx
Title: Re: 0x50 with C# Problem
Post by: brew on August 15, 2007, 04:31 PM
Also not to mention that the verbyte is a dword, not a single byte. (see: "Not to mention you're missing the verbyte dword")
are you checking for FD_CLOSE on data arrival? And also checking WSAGetLastError()?
Title: Re: 0x50 with C# Problem
Post by: PhoeNix_FasT on August 15, 2007, 04:59 PM
Ok... i dont give up  :-\
So i updated the 0x50 Packet.
iRes = 58 Bytes (ok seems the packet reached the server)
i still = 0

to brew : FD_CLOSE and WSAGetLastError() is C++ not C#

Here's my Code now

            IPHostEntry ip = Dns.GetHostByName("useast.battle.net");
            IPAddress[] ips = ip.AddressList;
            IPAddress battleNetIP = ips[0];

            m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipEnd = new IPEndPoint(battleNetIP, battleNetPort);
            m_clientSocket.Connect(ipEnd);

            BncsPacket pck0x50 = new BncsPacket((byte)0x50);
            pck0x50.InsertDwordString("0");          // (DWORD)       Protocol ID (0)           
            pck0x50.InsertDwordString("IX86");      // (DWORD)       Platform ID
            pck0x50.InsertDwordString("D2XP");      // (DWORD)       Product ID
            pck0x50.InsertDwordString("0x0b");      // (DWORD)       Version Byte
            pck0x50.InsertDwordString("");          // (DWORD)       Product language
            pck0x50.InsertDwordString("");          // (DWORD)       Local IP for NAT compatibility
            pck0x50.InsertDwordString("");          // (DWORD)       Time zone bias
            pck0x50.InsertDwordString("");          // (DWORD)       Locale ID           
            pck0x50.InsertDwordString("");          // (DWORD)       Language ID
            pck0x50.InsertCString("USA");           // (STRING)     Country abreviation   
            pck0x50.InsertCString("United States"); // (STRING)     Country

            byte[] byteData = new byte[511];

            try
            {
                int iRes = m_clientSocket.Send(pck0x50.GetData(), SocketFlags.None);
                richTextBox1.AppendText("0x50 : " + iRes + " Bytes sent\n");
            }

            catch (Exception exp)
            {
                richTextBox1.AppendText("Exception : " + exp.Message + "\n");
                return;
            }

            int i = m_clientSocket.Receive(byteData);
            richTextBox1.AppendText("Bytes Received : " + i + "\n");

EDIT : Is it better to use Network Stream instead of this what i am doing here ?
Title: Re: 0x50 with C# Problem
Post by: Hdx on August 15, 2007, 05:03 PM
-.-
The verbyte is a dword, not literally "0x0b"
it's actually "\xb\x0\x0\x0"
Also, where is the protocol byte!
I see the protocol version, but not the byte, directly after you create the connection, send a single byte 0x01
Why the HELL are you inserting them all as 'DwordStrings' there is no such thing.
~Hdx
Title: Re: 0x50 with C# Problem
Post by: brew on August 15, 2007, 05:23 PM
Hdx, I think he knows that. Eh... Post us your insert dword string function maybe it's a problem with that? And how do you check for winsock errors in C#, by now if you've made that many errors in your code you probably have gotten ipbanned. But yeah, you still have to send that raw 0x01 byte to battle.net first before sending the 0x50 packet.
Title: Re: 0x50 with C# Problem
Post by: Hdx on August 15, 2007, 05:27 PM
Naw, he doesn't know jack, he just PMed me with a bunch of bull, He needs to learn the basics of data handling in his language before he attempts to make a bot.
He still thinks he should use his packetbuffer for the protocol byte after we have all explained to him several times that its just one raw byte to be sent over to connection before anything else.
int i = clientSocket.send("\x1", SocketFlags.None);
How hard is that to comprehend?
~Hdx
Title: Re: 0x50 with C# Problem
Post by: brew on August 15, 2007, 05:56 PM
Guess so, hdx. I'm glad we have this problem solved.
Title: Re: 0x50 with C# Problem
Post by: MyndFyre on August 21, 2007, 02:12 AM
Quote from: Hdx on August 15, 2007, 05:27 PM
Naw, he doesn't know jack, he just PMed me with a bunch of bull, He needs to learn the basics of data handling in his language before he attempts to make a bot.
He still thinks he should use his packetbuffer for the protocol byte after we have all explained to him several times that its just one raw byte to be sent over to connection before anything else.
int i = clientSocket.send("\x1", SocketFlags.None);
How hard is that to comprehend?
~Hdx
Aside from not being C# or .NET-compliant, not too hard, since there's no overload that accepts (String, SocketFlags) in the parameter list.

if (clientSocket.Send(new byte[] { 1 }) != 1) might be an interesting code concept.

Quote from: Hdx on August 15, 2007, 05:03 PM
Why the HELL are you inserting them all as 'DwordStrings' there is no such thing.
As the documentation (http://www.jinxbot.net/mbncsutil/MBNCSUtil.DataBuffer.InsertDwordString_overload_2.html) states:
Quote
This method inserts a string with the maximum length of 4 into the buffer, reversed. This mimics the C-style declarations of 4-character integer literals:

unsigned long int star_product = 'STAR';


which results in
RATS
being in memory.
Title: Re: 0x50 with C# Problem
Post by: Hdx on August 21, 2007, 02:29 AM
Who says he was using jinx's buffer class?
Anyways, Ya I messed up iy should of been a byte[] but I wrote it off the top of my head so meh.
And I still think they should be treated as the dwords they are not '"Dword String" of value "" '
Which sounds better when reading code, that or 'Dword of value 0'
~Hdx
Title: Re: 0x50 with C# Problem
Post by: MyndFyre on August 21, 2007, 03:38 AM
Quote from: Hdx on August 21, 2007, 02:29 AM
Who says he was using jinx's buffer class?
He did:
Quote from: PhoeNix_FasT on August 15, 2007, 01:10 PM
For this i use MBNCSUtil ( using MBNCSUtil; )
Title: Re: 0x50 with C# Problem
Post by: Hdx on August 21, 2007, 04:24 AM
ah, my bad I haven't really bothered to look at MBNCSutil more hen to fix stupid little problems joe comes  me with.
none the less, Using 'DWordStrings' in this case is just... ya.
I can understand inserting the product/platform ids as such. IF C# dosen't allow for
unsigned long int star_product = 'STAR';
It would make things simpler, but he should still get into the habbit of using things properly. If he were to use them only use on special occasions where the DWord value would be best represented/understood as a string (product/platform ids, as in BNI files)
And treating everything else as its proper numerical value.
/myopinion
~Hdx
Title: Re: 0x50 with C# Problem
Post by: MyndFyre on August 21, 2007, 11:37 AM
The thing is, it's not appropriate to say "InsertProductID" either, because these aren't strictly used for product IDs or platform IDs.  Another example of where they're used is Warcraft III stats.

It's my opinion that code clarity is significantly better when the string literal is present than when you encode the string into a series of bytes or an actual numeric value (which is what I take "treating everything else as its proper numerical value" to mean).  We all find it easier to understand a strings than a series of hex bytes.  Doing it this way also allows your library to black-box any endianness changes that might happen across platforms. 
Title: Re: 0x50 with C# Problem
Post by: Camel on August 21, 2007, 03:13 PM
MindFyre, using constants would avoid that problem. I use the numerical values exclusively, but never directly.
public static final int PRODUCT_CHAT = 0x43484154;
public static final int PRODUCT_STAR = 0x53544152;
public static final int PRODUCT_SEXP = 0x53455850;
public static final int PRODUCT_W2BN = 0x5732424E;
public static final int PRODUCT_D2DV = 0x44324456;
public static final int PRODUCT_D2XP = 0x44325850;
public static final int PRODUCT_JSTR = 0x4A535452;
public static final int PRODUCT_WAR3 = 0x57415233;
public static final int PRODUCT_W3XP = 0x57335850;
public static final int PRODUCT_DRTL = 0x4452544C;
public static final int PRODUCT_DSHR = 0x44534852;
public static final int PRODUCT_SSHR = 0x53534852;
Title: Re: 0x50 with C# Problem
Post by: devcode on August 22, 2007, 11:54 AM
People who lack understanding of the language should stfu aka Hdx, fo reel
Title: Re: 0x50 with C# Problem
Post by: Hdx on August 22, 2007, 12:01 PM
People who lack common decency, and just like to flame people should fuck off. Namely devcode.
I know enough about the language to make my way, and enough about descent (descent not excellent), to voice my opinion on a public forum of which I have been a member for many years.
~Hdx