Introducing ChatKeeper — a new tool from Martian Software that keeps your ChatGPT conversation history safe, searchable, and accessible forever. Try it for free!

Nailgun Protocol

Source Background Quick Start Protocol Changelog License JavaDocs

Overview

Nailgun includes both server and client implementations of the protocol, so this document serves mainly to satisfy the curious and to provide a guide to those who like to peer into code.

The goal of the Nailgun protocol is to allow for remotely-hosted individual command line programs. To this end, it makes the following available to the server:

The server can read from the client's stdin, and can direct output to the client's stdout and stderr. The server can also specify an exit code which the client program will use to exit immediately.

The initial implementation uses a TCP socket. There may be a performance benefit to using domain sockets on *nix systems sometime in the future.

Communications proceed as follows (chunk descriptions are below):

  1. Client connects to server.
  2. Client transmits zero or more "Argument" chunks.
  3. Client transmits zero or more "Environment" chunks.
  4. Client transmits exactly one "Working Directory" chunk.
  5. Client transmits exactly one "Command" chunk.
  6. If server requires input from stdin, server transmits exactly one "Start-reading-input" chunk.

after step 5 (or 6), the following may happen, interleaved and in any order:

  1. Client transmits zero or more "stdin" chunks (Only if the client has received a "Start-reading-input" chunk, and only until the client transmits a "stdin-eof" chunk).
  2. Server transmits zero or more "stdout" chunks.
  3. Server transmits zero or more "stderr" chunks.

These steps repeat indefinitely until the server transmits an "exit" chunk.

A "chunk" is a variable-length block of data beginning with a 5-byte chunk header and followed by an optional payload. The chunk header consists of:

Argument Chunks

Argument chunks are the arguments available to the ng client via its argv[] array, with the following modifications:

Exactly one argument is sent in its entirety in each argument chunk.

Environment Chunks

Environment chunks are the environment variables available to the ng client via its env[] array, with no modifications.

Exactly one environment variable is sent in its entirety in each environment chunk.

Working Directory Chunk

Exactly one working directory chunk must be sent by the client, containing the working directory from which the client was launched.

Command Chunk

Exactly one command chunk must be sent by the client, containing in its entirety the command to run.

Start-reading-input Chunk

The first time the server tries to read from the client's stdin, it sends one of these to the client to tell it to start forwarding its input to the server.

Stdin, Stdout, Stderr Chunks

Each stream may consist of multiple chunks as necessary. For example, the client will probably buffer stdin before transmitting it to the server, so the payloads of Stdin chunks will always have a size less than or equal to the buffer size. The server may need to interleave stdout and stderr, resulting in some small chunks as well.


Martian Software, Inc. footer logo