int generate_new_message(char* msgbuf, int result_out, int role, int local_peer, signed
                         int proto, pmesg* pm, int payload_version, int payload_offset,
                         char  payload_data_last_field, ushort payload_data_size_field,
                         const void *payload_data, int data_xo) {

  int result = 10;
  int control_word;
  int tmp;
  *(_DWORD *)result_out = 0;
  memset( msgbuf, 0, 0x2000u);

  /*  build the control word */
  control_word = -(data_xo != 0) & 0x80 | -(payload_data != 0) & 0x40 | -(a7 != 0) &
  0x20 | -(payload_version != 0) & 0x10 | -(pm != 0) & 8 | -(proto != 6) & 4 |
  -(local_peer != 0) & 2 | role != 0;

  *(_WORD *)(msgbuf + 8) = control_word;

  /* Is external ip and port field to be set, then copy over IP and port from a5  */
  if ( control_word & 8 ) {
      *(_DWORD *)(msgbuf + 10) = *(_DWORD *)(pm + 4);
      *(_WORD *)(msgbuf + 14) = htons_0(*(_WORD *)(pm + 2));
      result = 16;
  }

  /* copy over the payload version field */
  if ( control_word & 0x10 ) {
    *(_DWORD *)(msgbuf + result) = payload_version;
    result += 4;
  }
 
  /* copy over the payload offset field */
  if ( control_word & 0x20 ) {
    *(_DWORD *)(msgbuf + result) = payload_offset;
    result += 4;
  }

  /* copy over payload data field */
  if ( control_word & 0x40 ) {
    /* Last field */
    *(_BYTE *)(msgbuf + result) = payload_data_last_field;
    tmp = result + 1;
    /* size field */
    *(_WORD *)(msgbuf + tmp) = payload_data_size_field;
    result = tmp + 2;
    /* max data size is 8188 bytes */
    if ( result + (unsigned int) > 0x1FFC ) {
      *(_DWORD *)result_out = 0;
      return result;
    }
    memcpy((char *) msgbuf + result, payload_data, payload_data_size_field);
    result += payload_data_size_field;
  }

  /* populate the data_x0 field */
  if ( (_BYTE)control_word < 0 ) {
    if ( (unsigned int)(result + 26) > 0x1FFC ) {
      *(_DWORD *)result_out = 0;
      return result;
    }
    enter_CS((void *)(msgbuf + result));
    result = result+26;
  }

  result = pack_message(msgbuf, result);
  *(_DWORD *)result_out = result;
  return result;
}

SOURCE LISTING 6: P2P message composition



 


 







Acknowledements

  This material is based upon work supported through the U.S. Army Research Office under the Cyber-TA Research Grant No. W911NF-06-1- 0316 and by the National Science Foundation, Grant No. CNS-07-16 612. The views expressed in this document are those of the authors and do not necessarily represent the official position of the sponsors.