3

which type of commands should I use for drawing network packet formats like this

        0                   1                   2                   3
      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<+
     |V=2|P|X|  CC   |M|     PT      |       sequence number         | |
     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
     |                           timestamp                           | |
     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
     |           synchronization source (SSRC) identifier            | |
     +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
     |            contributing source (CSRC) identifiers             | |
     |                               ....                            | |
     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
     |                   RTP extension (OPTIONAL)                    | |
   +>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
   | |                          payload  ...                         | |
   | |                               +-------------------------------+ |
   | |                               | RTP padding   | RTP pad count | |
   +>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<+
   | ~                     SRTP MKI (OPTIONAL)                       ~ |
   | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
   | :                 authentication tag (RECOMMENDED)              : |
   | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
   |                                                                   |
   +- Encrypted Portion*                      Authenticated Portion ---+

Should I use matrices, tables or rectangle nodes with relative positioning?

A similar task would be to draw something like this:

enter image description here

I managed to do this with the following code but I am not sure if this is the best solution.

\begin{figure}[h]
    \centering
    \tikzstyle{rect}=[rectangle,minimum width=6cm,minimum height=1cm,draw=black,outer sep=0pt]
    \tikzstyle{base rect}=[rect,minimum width=12cm,outer sep=0pt]
    \tikzstyle{text rect}=[rectangle,minimum width=6cm,align=center,outer sep=0pt]
    \tikzstyle{multipart rect}=[rectangle split,rectangle split horizontal,rectangle split parts = 2,inner xsep = 0.0mm,outer sep=0pt,
    draw=black,align=center,minimum width=6cm,minimum height=1cm]
    \begin{tikzpicture}
        % ip
        \node (IP) [base rect,fill=gray!20] {IP};
    % media plane
    \node (UDP) [rect,fill=blue!20,above left=0.0cm of IP.north] {UDP};
    \node (ICE) [rect,fill=purple!20,above=0.0cm of UDP]  {ICE, STUN, TURN} ;
    \node (DTLS) [rect,fill=orange!20,above=0.0cm of ICE] {DTLS} ;
    \node (SRTP) [multipart rect,rectangle split part fill={green!20,yellow!20},above=0.0cm of DTLS] {\nodepart[text width=3cm]{one} SRTP \nodepart[text width=3cm]{two} SCTP} ;
    \node [text rect,above=0.0cm of SRTP]{Media Plane};

    % signaling plane
    \node (TCP) [rect,fill=blue!20,above right=0.0cm of IP.north]{TCP};
    \node (TLS) [rect,fill=purple!20,above=0.0cm of TCP] {TLS};
    \node (HTTP) [rect,fill=orange!20,above=0.0cm of TLS] {HTTP};
    \node (WS) [rect,fill=green!20,above=0.0cm of HTTP] {WS/Other};
    \node (SDP) [multipart rect,above=0.0cm of WS,rectangle split part fill={gray!20,yellow!20}] {\nodepart[text width=3cm]{one} SDP \nodepart[text width=3cm]{two} SIP/XMPP/\\Other};
    \node [text rect,above=0.0cm of SDP]{Signaling Plane};
\end{tikzpicture}
\caption{WebRTC stack.}
\label{fig:webrtc-stack}

\end{figure}

Bernard
  • 271,350
  • Welcome to TeX.SE :) // Well, you are asking for opinion, which is not really what this place likes (see it's rules). However, my experience is that the choice of representation depends e.g. on A) what you want to show, B) how to present it consistently (i.e. black&white tables, or colored ones), C) how it fits the overall document (it's visual rules so to say), D) the target-audience. // A lazy path can be to cose what's easy for you to do. // Hope this helps a bit. – MS-SPO Aug 28 '21 at 10:36
  • 2
    I suggest to use bytefield package instead of tikz. Some examples: https://tex.stackexchange.com/a/604681/1952, https://tex.stackexchange.com/a/270341/1952 – Ignasi Aug 28 '21 at 10:55
  • @MS-SPO I see, I wanted to ask because diagram like WebRTC stack looks pretty easy but still quite hard to implement in tikz so I though I am missing some obvious options.

    @Ignasi bytefield looks awesome! Thanks!

    – mikeProgrammer Aug 28 '21 at 11:05

1 Answers1

3

An example with bytefield package:

\documentclass[border=2mm]{standalone}
\usepackage{xcolor}
\usepackage{bytefield}
\begin{document}
\begin{bytefield}[bitwidth=.2\linewidth, bitheight=7mm]{4}
\bitbox[]{2}{} & \bitbox[]{2}{Signaling plane}\\
\bitbox[]{2}[]{Media Plane} & \bitbox{1}[bgcolor=gray!30, bitheight=1cm]{SDP} & \bitbox{1}[bgcolor=yellow!30, bitheight=1cm]{SIP/XMPP/\\ Other}\\
\bitbox{1}[bgcolor=green!30]{SRTP} & \bitbox{1}[bgcolor=yellow!30]{SCTP} & \bitbox{2}[bgcolor=green!30]{WS/Other} \\
\bitboxes{2}[bgcolor=orange!30]{{DTLS} {HTTP}} \\
\bitboxes{2}[bgcolor=red!30]{{ICE, STUN, TURN} {TLS}} \\
\bitboxes{2}[bgcolor=blue!30]{{UDP} {TCP}} \\
\bitbox{4}[bgcolor=gray!30]{IP}\\
\end{bytefield}
\end{document}

Edit: Probably \wordbox{1}[bgcolor=gray!30]{IP} is formally better than \bitbox{4}[bgcolor=gray!30]{IP}.

enter image description here

Ignasi
  • 136,588
  • This doesn't compile for me with TeXlive 2019. Manually updating the bytefield package (to version 2021/08/17 v2.7) helped. – Frederick Nord May 26 '22 at 21:19