コード例 #1
0
ファイル: packetformats.c プロジェクト: klkblake/serval-dna
int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id,
		int recvttl,
		struct sockaddr *recvaddr, size_t recvaddrlen, int parseP)
{
  /* Make sure that the packet is meant for us, and is not mal-formed */
  int version;
  int cipher;
  int length;
  int payloadRotation;

  version=(packet[2]<<8)|packet[3];
  length=(packet[4]<<8)|packet[5];
  cipher=(packet[6]<<8)|packet[7];
  if (version!=1) return WHY("Unknown packet format version");
  if (cipher!=0) return WHY("Unknown packet cipher");
  if (length!=len) return WHY("Packet length incorrect");

  if (cipher) 
	  if (packetDecipher(packet,len,cipher)) 
		  return WHY("Could not decipher packet");

  /* Make sure the transaction ID matches */
  if (transaction_id)
    {
      int i;
	  for(i=0;i<TRANSID_SIZE;i++)
		if (packet[OFS_TRANSIDFIELD+i]!=transaction_id[i])
		  return WHY("transaction ID mismatch");
    }
  
  /* Unrotate the payload */
  payloadRotation=packet[OFS_ROTATIONFIELD];
  {
    unsigned char temp[256];
    bcopy(&packet[len-payloadRotation],&temp[0],payloadRotation);
    bcopy(&packet[HEADERFIELDS_LEN],&packet[HEADERFIELDS_LEN+payloadRotation],
	  len-(HEADERFIELDS_LEN)-payloadRotation);
    bcopy(&temp[0],&packet[HEADERFIELDS_LEN],payloadRotation);
  }

  if (debug&DEBUG_PACKETFORMATS) {
    DEBUG("Packet passes sanity checks and is ready for decoding");
    dump("unrotated packet",packet,len);
  }

  if (parseP) return process_packet(packet,len,recvttl,recvaddr,recvaddrlen); else return 0;
}
コード例 #2
0
int packetOk(unsigned char *packet,int len,unsigned char *transaction_id)
{
  /* Make sure that the packet is meant for us, and is not mal-formed */
  int version;
  int cipher;
  int length;
  int payloadRotation;

  if (len<HEADERFIELDS_LEN) return setReason("Packet is too short");
  if (packet[0]!=0x41||packet[1]!=0x10) return setReason("Packet has incorrect magic value");

  version=(packet[2]<<8)|packet[3];
  length=(packet[4]<<8)|packet[5];
  cipher=(packet[6]<<8)|packet[7];
  if (version!=1) return setReason("Unknown packet format version");
  if (cipher!=0) return setReason("Unknown packet cipher");
  if (length!=len) return setReason("Packet length incorrect");

  if (cipher) 
	  if (packetDecipher(packet,len,cipher)) 
		  return setReason("Could not decipher packet");

  /* Make sure the transaction ID matches */
  if (transaction_id)
    {
      int i;
	  for(i=0;i<TRANSID_SIZE;i++)
		if (packet[OFS_TRANSIDFIELD+i]!=transaction_id[i])
		  return setReason("transaction ID mismatch");
    }
  
  /* Unrotate the payload */
  payloadRotation=packet[OFS_ROTATIONFIELD];
  {
    unsigned char temp[256];
    bcopy(&packet[len-payloadRotation],&temp[0],payloadRotation);
    bcopy(&packet[HEADERFIELDS_LEN],&packet[HEADERFIELDS_LEN+payloadRotation],
	  len-(HEADERFIELDS_LEN)-payloadRotation);
    bcopy(&temp[0],&packet[HEADERFIELDS_LEN],payloadRotation);
  }

  if (debug>1) fprintf(stderr,"Packet passes sanity checks and is ready for decoding.\n");
  if (debug>2) dump("unrotated packet",packet,len);

  return 0;
}