C
C#9mo ago
Asher

❔ Constructing big endian byte message from multiple integers

Hey all, I'm following the torrent protocol trying to create messages for the protocol to give trackers; I'm not getting a response back and I'm not 100% sure what I got wrong, here is some sample code and the protocol itself:
4 Replies
Asher
Asher9mo ago
example for this particular request:
Offset Size Name Value
0 64-bit integer protocol_id 0x41727101980 // magic constant
8 32-bit integer action 0 // connect
12 32-bit integer transaction_id // this is a random number
16
Offset Size Name Value
0 64-bit integer protocol_id 0x41727101980 // magic constant
8 32-bit integer action 0 // connect
12 32-bit integer transaction_id // this is a random number
16
in total byte[16] my struct for this:
public struct ConnectRequest
{
public Int64 protocol_id = 0x41727101980;

// 0 = connect
public Int32 action = 0;

public Int32 transaction_id;

public const int BYTE_COUNT = 16;

public ConnectRequest()
{
// random 32-bit integer
Random r = new Random();
transaction_id = r.Next(Int32.MinValue, Int32.MaxValue);
}

public byte[] Serialize()
{
byte[] bytes = new byte[BYTE_COUNT];
byte[] protocolId, action, transactionId;

protocolId = BitConverter.GetBytes(protocol_id);

// 4 bytes = 32 bits
action = new byte[] { 0, 0, 0, 0 };
transactionId = BitConverter.GetBytes(transaction_id);

bytes = protocolId.Concat(action.Concat(transactionId)).ToArray();
if (bytes.Length > 16) throw new Exception("Wrong length");

return bytes;
}

/// <summary>
/// checks if the transaction ids match
/// </summary>
/// <param name="cr"></param>
/// <returns></returns>
public bool CheckResponse(ConnectResponse cr) => cr.transaction_id == transaction_id;
}
public struct ConnectRequest
{
public Int64 protocol_id = 0x41727101980;

// 0 = connect
public Int32 action = 0;

public Int32 transaction_id;

public const int BYTE_COUNT = 16;

public ConnectRequest()
{
// random 32-bit integer
Random r = new Random();
transaction_id = r.Next(Int32.MinValue, Int32.MaxValue);
}

public byte[] Serialize()
{
byte[] bytes = new byte[BYTE_COUNT];
byte[] protocolId, action, transactionId;

protocolId = BitConverter.GetBytes(protocol_id);

// 4 bytes = 32 bits
action = new byte[] { 0, 0, 0, 0 };
transactionId = BitConverter.GetBytes(transaction_id);

bytes = protocolId.Concat(action.Concat(transactionId)).ToArray();
if (bytes.Length > 16) throw new Exception("Wrong length");

return bytes;
}

/// <summary>
/// checks if the transaction ids match
/// </summary>
/// <param name="cr"></param>
/// <returns></returns>
public bool CheckResponse(ConnectResponse cr) => cr.transaction_id == transaction_id;
}
Asher
Asher9mo ago
this is the big endian part, which I assume I haven't done yet
No description
Asher
Asher9mo ago
I checked my hardware and its little endian, does that mean I need to reverse each number's bytes? or just reverse the entire array? for example: for array : { 0x36, 0xf9, 0x1b, 0x8d } (together: 36F91B8D) first option: 639FB1D8 or the entire array: D8B119F36
Accord
Accord9mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.