Connecting to Erlang’s epmd from Ruby
Lately I’ve been experimenting with creating a pure Ruby library to communicate with Erlang nodes similar to the capabilities offered by the jinterface included with Erlang. Using a combination of the Java source code for jinterface and the documention, I’ve had moderate success getting my code to talk to epmd (Erlang Port Mapping Daemon) and pass the intial Erlang “secret” handshake.
If you’re not familiar with epmd, it’s job is to essentially help nodes find one another. When you start an Erlang node it’ll automatically pass some information about itself to epmd so other nodes can find it. The epmd runs by default on port 4369 and is started automatically when you run an Erlang application (if it’s not running already). There are basically two important requests a node makes to epmd: 1) register itself with epmd and 2) given a name of another node, lookup it’s port. The format of the requests are documented here.
To look up the port of another Erlang node from Ruby (or any other language). Here’s what you need to do:
1. Make a TCP connection to epmd on port 4369
2. Send the port lookup request in the following format
n = name of the node we're interested in | 2 bytes | 1 byte | nodename.size | ---------------------------------------------------- | n + 1 | 122 | n | Where 122 is the tag for port lookup
3. Read the response from epmd. It’ll be in this format
| 1 byte | 1 byte | 2 bytes | -------------------------------------- | 119 | result | port# | Where:119 is the response tag result > 0 means we were successful port# is the port number of the node There's more information after the port, but we'll ignore that for now.
Finally here’s some code to try it out (requires the excellent eventmachine):
To run the example:
1. Start an erlang node in one terminal “erl -sname hello”
2. In another terminal run the ruby code.
If sucessful you should get back the port number of the “hello” node.
Trackbacks
Unfortunately, due to spammers I've had to close both trackbacks and comments.