Search code examples
cnetworkingcontiki

Understading tcpdump.c file in Contiki


I am trying to go through this file in Contiki, and finding it difficult to comprehend what is the purpose, and why it's using such an obfuscated code?

My crib is with this part-

  return s(" ping",
     n(IPBUF->destipaddr[3], d(
     n(IPBUF->destipaddr[2], d(
     n(IPBUF->destipaddr[1], d(
     n(IPBUF->destipaddr[0],
         s(" ",
     n(IPBUF->srcipaddr[3], d(
     n(IPBUF->srcipaddr[2], d(
     n(IPBUF->srcipaddr[1], d(
         n(IPBUF->srcipaddr[0],
     buf)))))))))))))))) - buf;

/*---------------------------------------------------------------------------*/
static char *
s(char *str, char *ptr)
{
  strcpy(ptr, str);
  return ptr + strlen(str);
}
/*---------------------------------------------------------------------------*/
int
tcpdump_format(uint8_t *packet, uint16_t packetlen,
           char *buf, uint16_t buflen)
{
  char flags[8];
  if(IPBUF->proto == UIP_PROTO_ICMP) {
    if(ICMPBUF->type == ICMP_ECHO) {
      return s(" ping",
         n(IPBUF->destipaddr[3], d(
         n(IPBUF->destipaddr[2], d(
         n(IPBUF->destipaddr[1], d(
         n(IPBUF->destipaddr[0],
             s(" ",
         n(IPBUF->srcipaddr[3], d(
         n(IPBUF->srcipaddr[2], d(
         n(IPBUF->srcipaddr[1], d(
             n(IPBUF->srcipaddr[0],
         buf)))))))))))))))) - buf;

      /*      return sprintf(buf, "%d.%d.%d.%d %d.%d.%d.%d ping",
             IPBUF->srcipaddr[0], IPBUF->srcipaddr[1],
             IPBUF->srcipaddr[2], IPBUF->srcipaddr[3],
             IPBUF->destipaddr[0], IPBUF->destipaddr[1],
             IPBUF->destipaddr[2], IPBUF->destipaddr[3]);*/
    }

https://github.com/contiki-os/contiki/blob/master/tools/wpcapslip/tcpdump.c

The commented part tells what the code is trying to do, but why in this way?


Solution

  • Contiki is designed for tiny systems, having only a few kilobytes of memory available. (A typical system with full IPv6 networking with sleepy routers and RPL routing needs less than 10 kb RAM and 30 kb ROM.) Probably the standard string library is not used in OS as it takes some memory. Very tiny embedded systems sometimes doesn't use standard libraries and have their own implementation if required.