0

I am attempting to write a simulator for an RPC server, so that I can use the UniData Java library. If I decompile their api, I see this snippet of code.

private byte[] headerBuffer;

private int readHeaderLength() {
    return ((this.headerBuffer[4] & 0xFF) << 24) + 
    ((this.headerBuffer[5] & 0xFF) << 16) + 
    ((this.headerBuffer[6] & 0xFF) << 8) + 
    ((this.headerBuffer[7] & 0xFF) << 0);
  }

The headBuffer attribute is being filled with something as follows:

final Socket socket = new Socket("127.0.0.1", 31438);
final DataInputStream dataIn = new DataInputStream(socket.getInputStream());
dataIn.readFully(this.headerBuffer);

From digging around, it seems to be that this portion of the code is responsible for initializing the RPC connection to some server.

Inorder to simulate this, I have created a server that listens on this port, and opens the output stream, as follows:

@Override
public void run() {
    try {
        final DataInputStream dataIn = new DataInputStream(socket.getInputStream());
        final DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());
        final String connectionResponse = "SIMULATED_CONNECTION";
        dataOut.write(connectionResponse.getBytes());
        System.out.println("#### message sent");
        while (true) {
            final byte[] data = new byte[1024];
            dataIn.readFully(data);
            final String input = new String(data);
            if (input == null || input.equals(".")) {
                System.out.println("### the input line is " + input);
                break;
            }
            System.out.println("## received message " + input);
        }
    } catch (final IOException e) {
        System.out.println("Error handling request : " + e.getMessage());
    } finally {
        try {
            socket.close();
        } catch (final IOException e) {
            System.out.println("## cant close socket " + e.getMessage());
        }
    }
}

However, I cannot for the life of me figure out what readHeaderLength() actually seeks to retrieve from the connection. While I can see the decompiled code, here is my best stab at understanding what it is attempting to parse out. I am looking at this website and this answer:

  • 0xFF = 00000000 00000000 00000000 11111111 (signifies a 32 bit int value)

  • value & 0xFF = replace all 0s that overlap with 1s to zeros, aside from the first 8 bits

  • if this.headerBuffer[5] evaluates to some value, that could yield ... 01100100 00000101 & ...00000000 11111111 = 00000000 00000101

  • when (this.headerBuffer[5] & 0xFF) evaluates, << 16 will add 16 more zeros to the right.

So what could this.headerBuffer[] actually contain?

If I write something simple like:

final String data = "sample";
final byte[] bytes = data.getBytes();
for (final byte bit : bytes) {    
    System.out.println(bit);
}

I get output of: 115 97 109 112 108 101

Hence, the method in question is most likely looking for some well formatted header to synchronize on when the RPC connection is made. I have tried to look at this website to understand what sort of header I should be passing as part of my server response so that readHeaderLength() method is in favor of communication, but have yet to find a clear answer.

  • Is my understanding of how the readHeaderLength() method works correct?
  • Am I correct in inferring that it is most likely looking for an RPC header?
Community
  • 1
  • 1
angryip
  • 2,140
  • 5
  • 33
  • 67

1 Answers1

1

the headerBuffer byte array contains a 4 byte int value in array index positions 4, 5, 6, 7. readHeaderLength method is just turning those 4 byte-array byte values into an actual int type value using bit shift operations.

Edit: Most likely this is the length of the header of the input stream from the socket, after the header begins the stream's body. I suggest research more how that code knows a "header length" value is stored in index positions 4, 5, 6, 7. Is that defined in RPC header?

geneSummons
  • 907
  • 5
  • 15
  • I cannot say why it is those particular elements that it cares about. Trying to find the RPC header is just as difficult as answering that question. God knows where this documentation has disappeared. – angryip Dec 06 '16 at 18:50