0
votes

I have an application that receives packets over a network. I am able to interpret all the field value except the payload (data) section.

consider a packet structure: src addr | src port | dest addr | dest port | data | checksum

the packet itself is stored as a char* and i can get all the values src/des addr, port. But the payload section confuses me. I am able to print it in hex format but printing it in char gives non-ascii value. When i copy the payload section to a new string and print the new string i get nothing i.e. it dost print anything and its length is 0. I don get any error or warning. Could some one pls tell where i am going wrong? I think im missing something here.

Here is the section of code:

  typedef struct pcap_802_15_4_s{
   guint16 fcf;
   unsigned char seqno;
   guint16 dpan;
   guint16 daddr;
   guint16 saddr;
   char *payload_data;  
  } pcap_802_15_4_t;


char *packet = read_serial_packet(src, &len);


      if (!packet)
    exit(0);
      else if (packet[0] != TOS_SERIAL_802_15_4_ID) {
    printf("bad packet (serial type is %02x, not %02x)\n", packet[0], TOS_SERIAL_802_15_4_ID);
      }

      plen = packet[1];
      printf("Received packet of length %i: \n", plen);
      if (plen != len) {
    printf("Packet format error: read packet length (%hhx) is different than expected from frame (%hhx).\n", plen, len);
      }

      struct timeval tv;    
      gettimeofday(&tv, NULL);


      i = 2;
      // Read in FCF and i+=2
      fcf = packet[i+1] << 8 | packet[i];
      packet_802_15_4.fcf = fcf;
      i += 2;


      {
    if ((fcf & 0x7) == 0x01) {
      printf("  Frame type: data\n");
    }
    else if ((fcf & 0x7) == 0x02) {
      printf("  Frame type: acknowledgement\n");
    }
    else {
      printf("  Frame type: other\n");
    }

    printf("  Security: %s\n", (fcf & (1 << 3)) ? "enabled":"disabled");
    printf("  Frame pending: %s\n", (fcf & (1 << 4)) ? "yes":"no");
    printf("  Ack request: %s\n", (fcf & (1 << 5)) ? "yes":"no");
    printf("  Intra-PAN: %s\n", (fcf & (1 << 6)) ? "yes":"no");
    intraPan = (fcf & (1 << 6));
      }


      {
    unsigned char seqno = packet[i++];
    packet_802_15_4.seqno = seqno;
    printf("  Sequence number: 0x%hhx\n", seqno);
    printf("  Sequence number(dump): 0x%hhx\n", packet_802_15_4.seqno);

      }

      {
    char addrLen = (fcf >> 10) & 0x3;
    short saddr = 0;
    long long laddr = 0;

    // 16- and 64-bit destinations have a PAN ID
    if (addrLen == 2 || addrLen == 3) { 
      short destPan = packet[i++] << 8 | packet[i++];
      packet_802_15_4.dpan = destPan;
      printf("  Destination PAN: 0x%02hx\n", destPan);
      printf("  Destination PAN (dump): 0x%02hx\n", packet_802_15_4.dpan);
    }

    switch (addrLen) {
    case 0:
      printf("  Destination address: none\n");
      break;
    case 1:
      printf("  Destination address: invalid? (0x01)\n");
      break;
    case 2:
      saddr =  (packet[i] << 8 | packet[i+1]);
      i += 2;
      packet_802_15_4.daddr = saddr;
      printf("  Destination address: 0x%04hx\n", saddr);
      printf("  case 2: Destination address(dump): 0x%04hx\n", packet_802_15_4.daddr);
      break;
    case 3: {
      int j;
      for (j = 0; j < 8; j++) {
        laddr = laddr << 8;
        laddr |= packet[i++];
      }
      packet_802_15_4.daddr = saddr;
      printf("  Destination address: 0x%016llx\n", laddr);
      printf("  case 3: Destination address(dump): 0x%016llx\n", (long long unsigned int)packet_802_15_4.daddr);
      break;
    }
    default:
      printf("  Destination address: parse serror\n");
    }
      }


      {
    char addrLen = (fcf >> 14) & 0x3;
    short saddr = 0;
    long long laddr = 0;

    if (!intraPan) { // Intra-PAN packet
      short srcPan = packet[i] << 8 | packet[i+1];
      //packet_802_15_4.span = srcPan;
      i += 2;
      printf("  Source PAN: 0x%02hx\n", srcPan);
    }

    switch (addrLen) {
    case 0:
      printf("  Source address: none\n");
      break;
    case 1:
      printf("  Source address: invalid? (0x01)\n");
      break;
    case 2:
      saddr =  (packet[i] << 8 | packet[i + 1]);
      packet_802_15_4.saddr = saddr;
      i += 2;
      printf("  case(2): Source address: 0x%04hx\n", saddr);
      break;
    case 3: {
      int j;
      for (j = 0; j < 8; j++) {
        laddr = laddr << 8;
        laddr |= packet[i++];
      }
      printf("  case(3): Source address: 0x%016llx\n", laddr);
      packet_802_15_4.saddr = saddr;
      break;
    }
    default:
      printf("  Source address: parse serror\n");
    }
      }

      if (iframes) {
    printf("  I-Frame: %s\n", (packet[i++] == 0x3f)? "yes":"no");
      }

      printf("  AM type: 0x%02hhx\n", packet[i++]);

      if (i >= plen) {
    printf("Packet format error: read packet is shorter than expected.\n");
      }
      else {
        int j=0;
    packet_802_15_4.payload_data = (char *)malloc(sizeof(char) * (plen - i));
    for (; i < plen; i++) {
      printf("Payload byte %d: 0x%02hhx \n",i,packet[i]);
      printf("Payload byte %d: %c \n",i,packet[i]);
      packet_802_15_4.payload_data[j] = (char) packet[i];
      j++;  
    }

The Hex output of payload: 0x00 0x01 0x10 0xdd

1
Sounds like you are trying to treat the payload as a string, when it might be binary data. What does your hex version of the payload look like?John3136
payload in hex : 0x00 0x01 0x10 0xddbroun
What are you expecting the payload to be?John3136
it is a counter with value incremented for every packetbroun
Sounds like trying to print the value with %c makes no sense.John3136

1 Answers

0
votes

1) If you have a Wireshark, capture several packets and check the payload representation there. If Wireshark displays your payload right, then receiver part of the code must be problematic.

2) Are you sure you don't have any IP header or TCP header? Are you transferring raw ethernet?

3) Are you sure the payload is nothing but the characters?