コード例 #1
0
ファイル: suffrage.cpp プロジェクト: seato/suffrage
/*
 * Summary:     Handles (r)esult packet reflex.  Packet information is logged and
 *              result is logged for the specific calculation.
 * Parameters:  (r)esult packet.
 * Return:      None.
 */
void
r_handler(u8 * packet)
{
  R_PKT PKT_R;

  if (packetScanf(packet, "%Zr%z\n", R_ZScanner, &PKT_R) != 3)
    {
      logNormal("r_handler:  Failed at %d\n", packetCursor(packet));
      return; // No harm done so no blinkcoding necessary
    }

  u32 NODE_INDEX; // index holder for if log is valid

  // only log properly formatted packets
  if (INVALID == (NODE_INDEX = log(PKT_R.key.ID, PKT_R.key.TIME)))
    return; // Don't continue if this packet has been received before

  // Handle packet spammers
  else if (PC_NODE_ARR[NODE_INDEX] > (PKT_R.key.TIME / 1000))
    {
      // Decrease the amount of pings recorded; "spammer amnesty" of sorts.
      PC_NODE_ARR[NODE_INDEX] -= 2;
      return; // Don't continue if this IXM is spamming packets right now.
    }

  else if (PKT_R.calc_ver < HOST_CALC_VER)
    return; // Don't continue if this is an old calculation version

  else if (0xffffffff == PKT_R.calc_ver)
    logNormal("Calculation version overflow.\n");

  if (PKT_R.neighbor)
    { // If this a neighboring node
      PKT_R.neighbor = 0; // Reset the flag before forwarding
      NEIGHBORS_ARR[packetSource(packet)] = PKT_R.key.ID; // And remember the ID
    }

  // If all the hoops have been jumped through
  FWD_R_PKT(&PKT_R, packetSource(packet)); // Forward the packet

  if (PKT_R.calc_ver == HOST_CALC_VER) // Same result?
    // Update the results from packets with proper calculation versions
    voteCount(NODE_INDEX, PKT_R.rslt);

  else if (PKT_R.calc_ver > HOST_CALC_VER) // New calculation version?
    { // perform standard procedures
      strikeCheck(); // evaluate the strikes for my neighbors
      flush(); // clear out my records for the new voting session
      voteCount(NODE_INDEX, PKT_R.rslt); // Remember the node's vote
      HOST_CALC = PKT_R.calc; // Remember the new calculation
      HOST_CALC_VER = PKT_R.calc_ver; // Remember the new calculation version
      voteCount(0, calculate(HOST_CALC)); // calculation occurs here
    }

  return;
}
コード例 #2
0
ファイル: suffrage.cpp プロジェクト: seato/suffrage
/*
 * Summary:     Sets a table-printing alarm that will reset itself on interval.
 * Parameters:  (t)able packet.
 * Return:      None.
 */
void
t_handler(u8 * packet)
{
  TERMINAL_FACE = packetSource(packet); // remember where this request came from
  Alarms.set(Alarms.create(printTable), millis()); // schedule the first table

  return;
}
コード例 #3
0
ファイル: PingClient.cpp プロジェクト: njh/EtherSia
boolean PingClient::havePacket()
{
    ICMPv6Packet& packet = (ICMPv6Packet&)_ether.packet();

    if (!_ether.bufferContainsReceived()) {
        return false;
    }

    if (packet.protocol() != IP6_PROTO_ICMP6) {
        // Wrong protocol
        return false;
    }

    if (packet.type != ICMP6_TYPE_ECHO_REPLY) {
        // Wrong ICMPv6 type
        return false;
    }

    if (!_ether.isOurAddress(packetDestination())) {
        // Wrong destination address
        return false;
    }

    if (!_remoteAddress.isZero() && packetSource() != _remoteAddress) {
        // Wrong source address
        return false;
    }

    if (ntohs(packet.echo.identifier) != this->_identifier) {
        // Wrong ICMPv6 Echo identifier
        return false;
    }

    if (ntohs(packet.echo.sequenceNumber) != lastSequenceNumber()) {
        // Sequence numbers didn't match
        return false;
    }

    // Everthing matched up
    this->_gotReply = true;
    this->_timeLastRecieved = micros();
    return true;
}
コード例 #4
0
ファイル: suffrage.cpp プロジェクト: seato/suffrage
/*
 * Summary:     Handles (c)alculation packet reflex.  Packet information is saved
 *              and converted into a R packet to be forwarded to neighboring nodes.
 * Parameters:  (c)alculation packet.
 * Return:      None.
 */
void
c_handler(u8 * packet)
{
  C_PKT PKT_R;

  if (packetScanf(packet, "%Zc%z\n", C_ZScanner, &PKT_R) != 3)
    {
      logNormal("c_handler:  Failed at %d\n", packetCursor(packet));
      return;
    }

  if (PKT_R.calc < 0)
    {
      logNormal("c_handler:  Input %d must be a non-negative integer.\n",
          PKT_R.calc);
      return;
    }

  if (PKT_R.calc > PRIME_THRESHOLD)
    {
      logNormal("c_handler:  Value %d is higher than the threshold %d (%d).\n",
          PKT_R.calc, PRIME_THRESHOLD, PRIME_ARR_THRESHOLD - 1);
      return;
    }

  strikeCheck(); // evaluate the strikes for my neighbors
  flush(); // clear out my records for the new voting session

  R_PKT PKT_T; // synthesize a new packet
  ++HOST_CALC_VER;
  HOST_CALC = PKT_R.calc;
  PKT_T.key.ID = ID_HOST;
  PKT_T.key.TIME = millis();
  PKT_T.calc = HOST_CALC;
  PKT_T.calc_ver = HOST_CALC_VER;
  PKT_T.rslt = VOTE_NODE_ARR[0];

  // If all the hoops have been jumped through
  FWD_R_PKT(&PKT_T, packetSource(packet)); // Forward the packet
  voteCount(0, calculate(HOST_CALC)); // calculation occurs here

  return;
}