Wire Protocol Serialization

早过忘川 提交于 2020-01-06 07:27:40

问题


I'm looking for what I'll call a 'binary serializer/deserializer code generator' for lack of a better term that specifically allows you to specify the on-the-wire format with arbitrary bit lengths and then generates the necessary C/C++ code to pack/unpack packets in that format. I started down the path of using a struct with bit fields but after reading this post I'm wondering if there's already something out there that handles all the messy problems. An example data structure I would need to deal with:

struct header {
    unsigned int val1 : 8;
    unsigned int val2 : 24;
    unsigned int val3 : 16
    unsigned int val4 : 2;
    unsigned int val5 : 3;
    unsigned int val6 : 1;
    unsigned int val7 : 10;
}

The motivation for keep the fields of the data structure like that is that it makes the programmers job easier to set/get the fields based on a what they match in the protocol, ex. val5 might be a meaningful 3 bit flag. Yes I could just have two 32 bit values for the whole struct and have to use bit masks and stuff to keep track of everything but why?

I'm aware of things like Google Proto Buf and the like, but AFAIK these all focus on the programmer side data structure and don't allow you to specify specific bit patterns - imagine trying to create the client code for low level protocols where the binary wire format is how it's specified. The closest thing I've found is protlr which sounds great except it doesn't appear to be FOSS. Other posts on SO point to:

  • RedBlocks which appears to be part of a full blown embedded framework.
  • PADS which seems extremely stale and overly complicated for my needs.
  • binpac which sounds interesting but I can't find an example of using it to parse arbitrary bit lengths (e.g. 1 bit, 2 bit, 17 bit fields) or if it also has a serialization method since it seems to be focused on one way deserialization for intrusion detection.

Is there a FOSS alternative that meets my criteria besides rolling yet another serialization format, or can someone provide an example using one of these references for the structure above?


回答1:


You might consider ASN.1 for this and use PER (aligned or unaligned). You can use either BIT STRING types constrained to your needed lengths, or INTEGER types with constraints to limit values to the number of bits you would like. Since ASN.1 and its encoding rules are independent of machine architecture and programming language, you don't have to worry about whether your machine is big-endian or little-endian, or whether one end of the communications prefers Java rather than C or C++. A good ASN.1 Tool handles all of that for you. You can find out more about ASN.1 at the ASN.1 Project page which has a link Introduction to ASN.1 as well as a list of ASN.1 Tools (some free some commercial). The reason I mention UNALIGNED PER is that you can literally send exactly the number of bits across that line as you desire with no added padding bits between.

For BIT STRINGS, you can even assign names to individual bits that have some meaning to you for your application.



来源:https://stackoverflow.com/questions/50818005/wire-protocol-serialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!