3

I am designing a simple protocol on top of UDP and now I've realized that someone else can send a packet to the port I'm listening on. Such packet will obviusly be incorrect for my application (I'm not concerned about security now)

Is there the way of filtering these invalid messages? I was thinking about adding some fixed magic number at the beginning of each packet, but how large should it be? Is 16 bits enough?

cube
  • 3,867
  • 7
  • 32
  • 52

2 Answers2

1

I believe the typical solution is to require a handshake (that could include a "long enough" magic number) in the beginning of the session. Of course the "session" is something your protocol needs to keep track of, UDP does not have the concept. Keeping a list ip, port and last packet receive time for all current sessions should do it: then you can drop all packets from a peers that have not done the handshake beforehand. This not only prevents random unknown app traffic from breaking your application but will also prevent multiple legitimate peers from screwing up each others traffic.

Additionally you could add either a session id or an increasing packet sequence number (with allowances for missing packets) to packets if you need to ensure that the peer agrees with you on which session this is.

Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54
  • This is all completely inappropriate for UDP. – user207421 Apr 12 '14 at 10:52
  • Mind explaining why? Simple applications might require no "session" knowledge at all (in which case a simple magic number in every packet may be good enough) but UDP is used for a lot of things... As an example http://www.gamers.org/dEngine/quake/QDP/qnp.html – Jussi Kukkonen Apr 12 '14 at 11:00
  • @EJP please see comment above (forgot to name you in the first one) – Jussi Kukkonen Apr 12 '14 at 11:16
  • Actually that's almost exactly what I want to do. The only question now is the length. – cube Apr 12 '14 at 14:58
0

Probably. Java uses 32 for .class files (0xcafebabe). But you only have 534 bytes in practical UDP so you might need to save a couple.

user207421
  • 305,947
  • 44
  • 307
  • 483