.net - Protocol error resistant socket server -


i'm developing server on c# (using *async methods). works fine, until 1 side violates protocol (e.g. attack on server).

the client , server interchange messages of following structure:

  1. first 4 bytes define length (n) of message body in bytes
  2. the following n bytes define message body

if transmits wrong length - comunication between client , server becomes unpredictable.

so idea create self-synchronizing protocol easy way.

i'm using tcp protocol, idea break message packets , no 2 messages should share same packet - way i'll able ignore protocol violations , restore communication, if goes wrong.

i want use tcp that, packet same tcp segment. there few catches:

  • the mtu (which defines mss) differ , there no predefined value buffer size use (correct me if i'm wrong)
  • i couldn't find way manupulate tcp segments directly (without "stream" abstraction) yet

i'm new socket server programming, need help. maybe can share common solution problem (the fault-resistant protocol) or describe common pitfalls, or maybe provide useful links.

i'm developing under .net , don't want use p/invokes if can avoided.

the tcp abstraction of stream, no in-built message boundaries, , shouldn't trying violate abstraction.

the main strategy dealing misbehaving clients rigorously sanity-check input provided clients (for example, usual set upper bound on allowed size of protocol-level messages). when sanity checks indicate protocol has been violated, stop processing of erroneous message. may want log error, and/or report client.

if protocol violation such cannot resynchronise, have no choice disconnect client. fine; misbehaving client has no right expect level of service.

you can design protocol allows resynchronisation - simplest example use delimiters @ boundary between subsequent protocol messages (the delimiter not allowed occur within message). many of old "line-based" internet protocols, ftp, smtp , irc work way (the delimiter in case newline character).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -