Connecting to Erlang’s epmd from Ruby

Dave Bryson - daveb - April 24, 2009

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):

ruby_2_epmd.rb

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.



Categories: Blogs  Dave Bryson  

Comments

anonymous avatar

You might want to also look at twotp, a project that does the same for python. I figure you might find the source code helpful, since Ruby and Python have some similarities. Also this project is structured around an event based framework (Twisted) which i think provides some of the features eventmachine provides in your context.

https://launchpad.net/twotp

Cheers,
Stephan

Posted by snies on 26 Apr 2009 at 13:42



 
anonymous avatar

nice info men…it’s good thank’s very much

Posted by ryanita on 28 Apr 2009 at 10:32



 


Add comment

Name:

Email:

URL:

Smileys

Remember my personal information

Notify me of follow-up comments?