javarpcunidata

RPC and UniData simulation using Java


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:

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.


Solution

  • 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?