1

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.

Isuru
  • 7,893
  • 8
  • 30
  • 38
  • possible duplicate of [sending and receiving UDP packets using Java?](http://stackoverflow.com/questions/10556829/sending-and-receiving-udp-packets-using-java) – tomvodi Apr 02 '15 at 04:56
  • My question is not about java example to send and receive UDP packets but to differentiate them. – Isuru Apr 02 '15 at 05:03
  • 1
    unless each protocol is clearly identifiable from the other, say, they all have a header, then this is going to be problematic. you can write code to guess, but it might not work all of the time. i would be asking the question why you want to support multiple protocols on the same port, because this isn't really something you would do if you can avoid it. you are "breaking the rules of UDP networking" by doing this. – slipperyseal Apr 02 '15 at 05:03
  • Well in my case STUN and RTP has to receive to same port in order to deal with the NAT lies between endpoints. So I need to be able to identify them separately to respond to these packets correspondingly. – Isuru Apr 02 '15 at 05:23
  • Not sure what you want to achieve exactly but if it's a NAT problem it is better dealt with at the OS level, you know – fge Apr 02 '15 at 05:24
  • In my case I have a RTP entity which will receive RTP stream from over network party. But to maintain the NAT mapping there is a STUN request send to the same port. So I have no option but to handle them properly. So I need a way to differentiate them. – Isuru Apr 02 '15 at 05:26

2 Answers2

2

Here is a c++ version which I am sure can be rewritten in java, but it gives you an idea what to do. Btw buffer variable holds a most recent udp datagram received.

 if((buffer[0]==0) || (buffer[0]==1))
      return stun; // STUN packet

 if((buffer[0]>=128) && (buffer[0]<=191))
      return rtp; // RTP packet

 if((buffer[0]>=20)  && (buffer[0]<=64))
      return dtls; // DTLS packet                                                               
Aki
  • 3,709
  • 2
  • 29
  • 37
  • This is actually the correct answer, because it is specified in RFC5764: https://tools.ietf.org/html/rfc5764#page-14 – LubosD Dec 11 '19 at 16:45
1

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.

Atsby
  • 2,277
  • 12
  • 14