Ports are really important in networking. Basically ports provide multiple connections to the same machine, 1 IP address can have multiple open ports. TcpMux was invented to solve the problem of not knowing which application runs on which port.
How it works
TcpMux runs on tcp port 1. You need to send the name of the service to it, you want to connect to. TcpMux connects you to that service without you needing to know the port of that service.
Xinetd
To use TcpMux you first have to install xinetd
. This is a super-server, that launches servers when you connect to them, rather than running the server all the time. First we have to create a PoC server application. I will use python for this, It's only 2 (or 1) lines of code.
server.py:
#! /usr/bin/python2.7
print ("Application ended")
We will use print because Xinetd connects the TcpMux socket with the stdin and stdout of the real server.
Next we have to setup and configure Xinetd, but first we need to install it:
apt-get install xinetd
, if you get prompted if you want to install it, just type y
and press enter
Next we will have to add configurations to xinetd:
Adding TcpMux
By default xinetd loads service configuration files from the /etc/xinetd.d
directory. You will need to create a new file here. The name of the file can be anything, it's not related to the service. I will name it tcpmux.
Place the following line to tcpmux
:
service tcpmux
{
type = INTERNAL
id = tcpmux
socket_type = stream
protocol = tcp
user = root
wait = no
}
where:
type = INTERNAL
means that this service is provided by xinetd itself.id = tcpmux
is a unique ID for this servicesocket_type = stream
this is a stream socketprotocol = tcp
, use TCP as a protocoluser = root
, launch this service as rootwait = no
, means our service is multi-threaded and xinetd will handle more than one request until the application closes
Adding our test service
Navigate to the same folder /etc/xinetd.d
and add a new file. I will name it myservice.
Place the following lines into myservice
:
service ftp
{
disable = no
type = TCPMUX UNLISTED
socket_type = stream
protocol = tcp
wait = no
user = root
server = /root/public/server.py
instances = 10
log_type = SYSLOG daemon debug
}
where:
disable = no
, means that this service is enabledtype = TCPMUX UNLISTED
, means that tcpmux will handle requests for this service, unlisted means, that this service isn't listed under/etc/services
or the name and the port number listed there doesn't match.server = /root/public/server.py
specifies the path of our server to execute.instances = 10
, specifies the maximum number of server applications to start.log_type = SYSLOG daemon debug
, specifies to log to thesyslog
file with thedaemon
facility, withdebug
set for the log level
Now go ahead and restart xinetd by executing:
service xinetd restart
Testing out TcpMux
To test it out we need to send a message to port 1 to the server, with the name of the service we want to start and a CRLF
or \r\n
. If we succeed, we will get the message sent by python. If we have a configuration error we will get something like: -Service name not found
. I wrote a quick c# code to test this out:
using System.Net.Sockets;
namespace Application
{
public class App
{
private void TcpMux()
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] data = Encoding.ASCII.GetBytes("ftp\r\n");
s.Connect("192.168.10.62", 1);
s.Send(data);
byte[] response = new byte[2048];
s.Receive(response, 0, response.Length, SocketFlags.None);
Console.WriteLine(Encoding.ASCII.GetString(response));
}
public static void Main(String[] args)
{
TcpMux();
}
}
}
Summary
TcpMux is not really a supported protocol according to Wikipedia it has been deprecated in 2016. I just figured it will be intresting to look at this rare protocol. We will for sure look at xinetd more briefly in another post. I don't recommend to use TcpMux. I only tested xinetd with TcpMux, but it was really slow, with this simple application too. Now go and test out TcpMux in your linux box!