Search code examples
javaudprtpstunmultiplexing

Differentiate STUN, RTP, DTLS packets - java program


I'm writing a small UDP server, where it might receive different UDP protocol messages like STUN, RTP, RTCP, DTLS packets in to the same port.

I should first be able to differentiate each kind of protocol messages in order to execute different logic.

Where can I find a sample for java, better a library to help me with this.


Solution

  • I should first be able to differentiate each kind of protocol messages in order to execute different logic.

    Why? I would simply run the logics in parallel; i.e., pass every packet on this port to every logic.

    The logics should be designed to ignore ill-formed requests (after all, if they are going to run on the open Internet, they ought to be robust enough to deal with even maliciously crafted packets).

    If some incoming requests are -polyglots-; i.e., they are valid in multiple protocols, then the client is going to receive multiple responses. In this design, it is left to the client code to deal with the response(s) that aren't legal. Maybe the client is robust enough to ignore them. Or maybe the client will retry the protocol from the start, possibly picking a new sequence number or something that isn't a polyglot anymore (by pure luck).

    I don't think there's a "really good" solution to this problem, as UDP packets are too small to waste space with unique identifiers for each protocol. The UDP protocol was designed to use separate ports for different services. The only "correct" solution is to place services that run on the same port on different IP addresses.