title: Refactor existing C structure to accommodate more elements
body: <p>I have a C language sniffer program.
  There is a list to record how many packages (packets) caught per source ip and destination ip pair.
  Like I got such packages:</p>
  
  <pre><code>[192.168.1.1] -&gt; [192.168.1.2] Total package amount:[100]
  [192.168.1.3] -&gt; [192.168.1.4] Total package amount:[100]
  [192.168.1.4] -&gt; [192.168.1.3] Total package amount:[100]
  </code></pre>
  
  <p>My structure is:</p>
  
  <pre><code> struct node {
    u_char s_ip[16];        //source IP
    u_char d_ip[16];        //destination IP
    u_int32_t package_amount;   //Would be sum
    struct node *next;
  };
  struct node *head;      
  struct node *search;      //(1)position (2)for new malloc
  struct node *tail;
  </code></pre>
  
  <p>But as you know, TCP and UDP have ports, so I want to add port fields to my structure, then it looks like:</p>
  
  <pre><code>struct tu_node {
    u_char s_ip[16];        //source IP
    u_char d_ip[16];        //destination IP
    u_int32_t s_port;      //source port
    u_int32_t d_port;      //destination port
    u_int32_t package_amount;   
    struct node *next;
  };
  struct tu_node *tu_head;         // tu means tcp and udp
  struct tu_node *tu_search;    
  struct tu_node *tu_tail;  
  </code></pre>
  
  <p>Although My sniffer is limited in that it can only parse TCP and UDP, I want to print out unknown protocols package amount per s_ip and d_ip pairs. As you see, now the port fields (s_port and d_port) are NOT applicable to unknown protocols. The simple way to me is to define a new struct, like:</p>
  
  <pre><code>struct unknown_node {
    u_char s_ip[16];        //source IP
    u_char d_ip[16];        //destination IP
    u_int32_t package_amount;   
    struct node *next;
  };
  struct unknown_node *unknown_head;      
  struct unknown_node *unknown_search;      
  struct unknown_node *unknown_tail;  
  </code></pre>
  
  <p>Now I've got a problem.
  As I have two different structures, so I need two different functions to handle the list operations (head, search and tail) basically.</p>
  
  <p>So could anyone help me to think about how to redesign/refactor above structures or give me a point to refactor my list functions, to be a good design? </p>
  
  <hr>
  
  <p>So far, I got two probable but unclear methods:</p>
  
  <ol>
  <li>One is use something like <code>#if</code> in <code>struct node</code> (the first code snippet) to accommodate d_port and s_port. But I don't think it is feasible because sniffer only know TCP and UDP at runtime, but <code>#if</code> directives need be fixed at compile time.</li>
  <li>The second one is think about how to implement "overload" function to compatible "struct tu_node" and "struct unknown_node" both. But seems C language can't.</li>
  </ol>