PDA

View Full Version : Server Query Tool - some aid needed


Azmat
01-06-2007, 10:37 PM
Hi all,
I know this isn't a mod for bf, but i had no clue on where else to put my questions here.
Why post here then ? I would like to reach many people that have some coding experience. Modders have coding experience too, maybe someone knows someone somewhere. Information is already hard to find, getting fellow coders to look would be a plus. Oh, and it is BF related of course :).

What I am trying to make is a remote console application for bf2142 in Java. Sure, there are existing rcons out there, i'm well aware of that. But i'm making this thing as a project for school.
It's all still in baby shoes, but lately i've been so TERRIBLY stuck at something as simple as querying a server for the stats.
If someone, even just one person, would be able to help me out a single bit, i'd be very gratefull, as i can't seem to find a lot of usefull information about this on the web. And i searched and i googled, yes. I have found basic principles, but mainly in PHP, which for one i'm far from good at, and secondly it's a long way from Java :)

[gets technical now]
the principles of a query are
1) Send challenge request (FE FD H9 83 58 34 H0)

2) Receive response from server - remove first 5 bytes of response leaving..

2D 31 35 36 39 38 36 37 37 32 39 00

3) Convert what's left to an integer giving you...

-1442591364

4) Convert that to a hex string then remove everything except the last 8 bytes from the string leaving..

2A372375

6) Convert each pair to a byte and insert into validated query starting at position 7

Eg, declare validated query as..

Dim validated_query() As Byte = {&HFE, &HFD, &H0, &H5D, &HAC, &H81, &H59, &H0, &H0, &H0, &H0, &HFF, &HFF, &HFF, &H1}

Insert 2A at position 7
Insert 37 at position 8
Insert 23 at position 9
Insert 75 at position 10

7) Send back response and you should get the full data

So far i am able to just send a request and proces the challenge. Things go wrong here and i can't figure out what is going wrong.

My challenge request:
byte[] challenge_code = {(byte)0xFE, (byte)0xFD, (byte)0x09, (byte)0x10, (byte)0x20, (byte)0x30, (byte)0x40, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x01};
is correct.
Now when i get my response for the server i get an array of bytes returned. I remove the first 5 bytes from it, then use the following method to get me the integervalue of the remaining bytes:
public static int byteArrayToInt(byte[] b, int offset) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (b[i + offset] & 0x000000FF) << shift;
}
return value;
}

Once i got this integer, i convert this to a hexstring, then take the last 8 bytes:
String hexStringResult = Integer.toHexString(integerResult);
System.out.println("hexStringResult: " + hexStringResult);

String hexStringFinal = hexStringResult.substring(hexStringResult.length() - 8,8);
System.out.println("hexStringFinal: " + hexStringFinal);

Now i take out the 4 pairs from these 8 and reparse them into a byte:
byte pair1 = Byte.parseByte(hexStringFinal.substring(0,2),16); (and similar for other 3 pairs)
I know these pairs are the same from the ones in the hexstring once theyre in the packet (packetsniffing is a must ...).

I then continue with putting these pairs in the query string: the query string needs these pairs to send back information packets (=you solved the challenge etc).
byte[] query_code = {(byte)0xFE, (byte)0xFD, (byte)0x00, (byte)0x10, (byte)0x20, (byte)0x30, (byte)0x00, pair1, pair2, pair3, pair4, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x01};

I proceed with sending this package, but alas ! This is where things just block, i get no response whatsoever back.
Somewhere i must have an error. But where ? I'm clueless and out of ideas.

If anyone has the need to have a serious look at my code so far, he/she can PM me of course.

Thanks in advance anyway !

Kurt
01-08-2007, 12:37 PM
Hey fella

It was me that wrote the longhand way of doing it which I posted over at Kquery (as you've posted above) - I thought it looked familiar :-)

It sounds like you're not constructing your return packet properly. Either that or you're closing the socket after the initial query which you don't need to do. The best aproach might be to find working example of challenge / return / response, hardcode them in your app and play til you construct the valid response then try it for real.

I can give you working examples if you're still struggling.

Cheers
K