Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Ethereal-dev: [Ethereal-dev] Performance - efficient coding of dissectors - examples

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Yaniv Kaul <ykaul@xxxxxxxxxxxx>
Date: Mon, 13 Sep 2004 15:52:48 +0300
Examples where static const array would have been more efficient:
1. packet-ip.c:

switch (icmp_type) {
 case ICMP_ECHOREPLY:
   strcpy(type_str, "Echo (ping) reply");
   break;
 case ICMP_UNREACH:
   strcpy(type_str, "Destination unreachable");
   if (icmp_code < N_UNREACH) {
     sprintf(code_str, "(%s)", unreach_str[icmp_code]);
   } else {
     strcpy(code_str, "(Unknown - error?)");
   }
   break;


2. packet-quakeworld.c:                    strcpy(command, "Ping");
3 .packet-fc.c:
static gchar *
fctl_to_str (const guint8 *fctl, gchar *str, gboolean is_ack)
{
   int stroff = 0;
   guint8 tmp = 0;

   if (str == NULL)
       return (str);

   if (fctl[2] & 0x80) {
       strcpy (str, "Exchange Responder, ");
       stroff += 20;
   }
   else {
       strcpy (str, "Exchange Originator, ");
       stroff += 21;
   }

   if (fctl[2] & 0x40) {
       strcpy (&str[stroff], "Seq Recipient, ");
       stroff += 15;
   }

4. packet-dns.c:
dns_class_name() function and the whole use of it.



Examples where proto_tree_add_xxxx should have been used instead of proto_tree_add_text():
packet-atm.c:
case TAG_MAC_ADDRESS:
 proto_tree_add_text(dest_tree, tvb, offset, 6, "MAC address: %s"
                     ether_to_str(tvb_get_ptr(tvb, offset, 6)));
 break;


or, same file:
proto_tree_add_text(tree, tvb, offset, 1, "Number of TLVs: %u", num_tlvs);

packet-dhcpv6.c:
ti = proto_tree_add_text(bp_tree, tvb, off, 4 + optlen,
       "%s", val_to_str(opttype, opttype_vals, "DHCP option %u"));

packet-dns.c

packet-eap.c:
proto_tree_add_text(eap_tree, tvb, offset, 1, "MS-CHAPv2-ID: %d",
                   tvb_get_guint8(tvb, offset));

packet-http.c:
proto_tree_add_text(chunk_subtree, tvb, offset,
   chunk_offset - offset, "Chunk size: %u octets",
   chunk_size);

packet-icmpv6.c:

proto_tree_add_text(icmp6opt_tree, tvb,
   offset + offsetof(struct nd_opt_prefix_info, nd_opt_pi_prefix_len),
   1, "Prefix length: %u", pi->nd_opt_pi_prefix_len);


Examples of defining variables inside loops (I agree, any normal compiler would put it outside the loop somehow, but still):
packet-tcp.c:
for(tmptnp=*tnp;tmptnp;tmptnp=tmptnp->next){
       if(tmptnp->nxtpdu<=seq){
               struct tcp_next_pdu *oldtnp;
...


Those are all just random examples, I might have got some wrong, and I skipped zillions.