Esempio n. 1
0
 /** Prepare a routed-to-node message for forwarding. */
 private Message preForward(Message m, short newHTL) {
   m.set(DMT.HTL, newHTL); // update htl
   if (m.getSpec() == DMT.FNPRoutedPing) {
     int x = m.getInt(DMT.COUNTER);
     x++;
     m.set(DMT.COUNTER, x);
   }
   return m;
 }
Esempio n. 2
0
 /**
  * Receive the file.
  *
  * @return True if the whole file was received, false otherwise.
  */
 public boolean receive() {
   while (true) {
     MessageFilter mfSendKilled =
         MessageFilter.create()
             .setSource(peer)
             .setType(DMT.FNPBulkSendAborted)
             .setField(DMT.UID, uid)
             .setTimeout(TIMEOUT);
     MessageFilter mfPacket =
         MessageFilter.create()
             .setSource(peer)
             .setType(DMT.FNPBulkPacketSend)
             .setField(DMT.UID, uid)
             .setTimeout(TIMEOUT);
     if (prb.hasWholeFile()) {
       try {
         peer.sendAsync(DMT.createFNPBulkReceivedAll(uid), null, ctr);
       } catch (NotConnectedException e) {
         // Ignore, we have the data.
       }
       return true;
     }
     Message m;
     try {
       m = prb.usm.waitFor(mfSendKilled.or(mfPacket), ctr);
     } catch (DisconnectedException e) {
       prb.abort(RetrievalException.SENDER_DISCONNECTED, "Sender disconnected");
       return false;
     }
     if (peer.getBootID() != peerBootID) {
       prb.abort(RetrievalException.SENDER_DIED, "Sender restarted");
       return false;
     }
     if (m == null) {
       prb.abort(RetrievalException.TIMED_OUT, "Sender timeout");
       return false;
     }
     if (m.getSpec() == DMT.FNPBulkSendAborted) {
       prb.abort(RetrievalException.SENDER_DIED, "Sender cancelled send");
       return false;
     }
     if (m.getSpec() == DMT.FNPBulkPacketSend) {
       int packetNo = m.getInt(DMT.PACKET_NO);
       byte[] data = ((ShortBuffer) m.getObject(DMT.DATA)).getData();
       prb.received(packetNo, data, 0, data.length);
     }
   }
 }
Esempio n. 3
0
 /**
  * Deal with a routed-to-node message that landed on this node. This is where
  * message-type-specific code executes.
  *
  * @param m
  * @return
  */
 private boolean dispatchRoutedMessage(Message m, PeerNode src, long id) {
   if (m.getSpec() == DMT.FNPRoutedPing) {
     if (logMINOR) Logger.minor(this, "RoutedPing reached other side! (" + id + ")");
     int x = m.getInt(DMT.COUNTER);
     Message reply = DMT.createFNPRoutedPong(id, x);
     if (logMINOR) Logger.minor(this, "Replying - counter = " + x + " for " + id);
     try {
       src.sendAsync(reply, null, nodeStats.routedMessageCtr);
     } catch (NotConnectedException e) {
       if (logMINOR)
         Logger.minor(this, "Lost connection replying to " + m + " in dispatchRoutedMessage");
     }
     return true;
   }
   return false;
 }
Esempio n. 4
0
  private static long getSecretPingResponse(Node source, PeerNode pathway, long uid)
      throws DisconnectedException {
    // wait for a reject or pong
    MessageFilter mfPong =
        MessageFilter.create()
            .setSource(pathway)
            .setField(DMT.UID, uid)
            .setTimeout(SECRETPONG_TIMEOUT)
            .setType(DMT.FNPSecretPong);
    MessageFilter mfRejectLoop =
        MessageFilter.create()
            .setSource(pathway)
            .setField(DMT.UID, uid)
            .setTimeout(SECRETPONG_TIMEOUT)
            .setType(DMT.FNPRejectedLoop);
    Message msg = source.getUSM().waitFor(mfPong.or(mfRejectLoop), null);

    if (msg == null) {
      Logger.error(
          source, "fatal timeout in waiting for secretpong from " + getPortNumber(pathway));
      return -2;
    }

    if (msg.getSpec() == DMT.FNPSecretPong) {
      int suppliedCounter = msg.getInt(DMT.COUNTER);
      long secret = msg.getLong(DMT.SECRET);
      Logger.normal(source, "got secret, counter=" + suppliedCounter);
      return secret;
    }

    if (msg.getSpec() == DMT.FNPRejectedLoop) {
      Logger.error(
          source,
          "top level secret ping should not reject!: "
              + getPortNumber(source)
              + " -> "
              + getPortNumber(pathway));
      return -1;
    }

    return -3;
  }
Esempio n. 5
0
 private void finishDisconnect(final Message m, final PeerNode source) {
   source.disconnected(true, true);
   // If true, remove from active routing table, likely to be down for a while.
   // Otherwise just dump all current connection state and keep trying to connect.
   boolean remove = m.getBoolean(DMT.REMOVE);
   if (remove) node.peers.disconnect(source, false, false, false);
   // If true, purge all references to this node. Otherwise, we can keep the node
   // around in secondary tables etc in order to more easily reconnect later.
   // (Mostly used on opennet)
   boolean purge = m.getBoolean(DMT.PURGE);
   if (purge) {
     OpennetManager om = node.getOpennet();
     if (om != null) om.purgeOldOpennetPeer(source);
   }
   // Process parting message
   int type = m.getInt(DMT.NODE_TO_NODE_MESSAGE_TYPE);
   ShortBuffer messageData = (ShortBuffer) m.getObject(DMT.NODE_TO_NODE_MESSAGE_DATA);
   if (messageData.getLength() == 0) return;
   node.receivedNodeToNodeMessage(source, type, messageData, true);
 }
Esempio n. 6
0
  public boolean handleMessage(Message m) {
    PeerNode source = (PeerNode) m.getSource();
    if (source == null) {
      // Node has been disconnected and garbage collected already! Ouch.
      return true;
    }
    if (logMINOR) Logger.minor(this, "Dispatching " + m + " from " + source);
    if (callback != null) {
      try {
        callback.snoop(m, node);
      } catch (Throwable t) {
        Logger.error(this, "Callback threw " + t, t);
      }
    }
    MessageType spec = m.getSpec();
    if (spec == DMT.FNPPing) {
      // Send an FNPPong
      Message reply = DMT.createFNPPong(m.getInt(DMT.PING_SEQNO));
      try {
        source.sendAsync(reply, null, pingCounter); // nothing we can do if can't contact source
      } catch (NotConnectedException e) {
        if (logMINOR) Logger.minor(this, "Lost connection replying to " + m);
      }
      return true;
    } else if (spec == DMT.FNPStoreSecret) {
      return node.netid.handleStoreSecret(m);
    } else if (spec == DMT.FNPSecretPing) {
      return node.netid.handleSecretPing(m);
    } else if (spec == DMT.FNPDetectedIPAddress) {
      Peer p = (Peer) m.getObject(DMT.EXTERNAL_ADDRESS);
      source.setRemoteDetectedPeer(p);
      node.ipDetector.redetectAddress();
      return true;
    } else if (spec == DMT.FNPTime) {
      return handleTime(m, source);
    } else if (spec == DMT.FNPUptime) {
      return handleUptime(m, source);
    } else if (spec == DMT.FNPSentPackets) {
      source.handleSentPackets(m);
      return true;
    } else if (spec == DMT.FNPVoid) {
      return true;
    } else if (spec == DMT.FNPDisconnect) {
      handleDisconnect(m, source);
      return true;
    } else if (spec == DMT.nodeToNodeMessage) {
      node.receivedNodeToNodeMessage(m, source);
      return true;
    } else if (spec == DMT.UOMAnnounce && source.isRealConnection()) {
      return node.nodeUpdater.uom.handleAnnounce(m, source);
    } else if (spec == DMT.UOMRequestRevocation && source.isRealConnection()) {
      return node.nodeUpdater.uom.handleRequestRevocation(m, source);
    } else if (spec == DMT.UOMSendingRevocation && source.isRealConnection()) {
      return node.nodeUpdater.uom.handleSendingRevocation(m, source);
    } else if (spec == DMT.UOMRequestMain && source.isRealConnection()) {
      return node.nodeUpdater.uom.handleRequestJar(m, source, false);
    } else if (spec == DMT.UOMRequestExtra && source.isRealConnection()) {
      return node.nodeUpdater.uom.handleRequestJar(m, source, true);
    } else if (spec == DMT.UOMSendingMain && source.isRealConnection()) {
      return node.nodeUpdater.uom.handleSendingMain(m, source);
    } else if (spec == DMT.UOMSendingExtra && source.isRealConnection()) {
      return node.nodeUpdater.uom.handleSendingExt(m, source);
    } else if (spec == DMT.FNPOpennetAnnounceRequest) {
      return handleAnnounceRequest(m, source);
    } else if (spec == DMT.FNPRoutingStatus) {
      if (source instanceof DarknetPeerNode) {
        boolean value = m.getBoolean(DMT.ROUTING_ENABLED);
        if (logMINOR)
          Logger.minor(this, "The peer (" + source + ") asked us to set routing=" + value);
        ((DarknetPeerNode) source).setRoutingStatus(value, false);
      }
      // We claim it in any case
      return true;
    } else if (source.isRealConnection() && spec == DMT.FNPLocChangeNotificationNew) {
      double newLoc = m.getDouble(DMT.LOCATION);
      ShortBuffer buffer = ((ShortBuffer) m.getObject(DMT.PEER_LOCATIONS));
      double[] locs = Fields.bytesToDoubles(buffer.getData());

      /**
       * Do *NOT* remove the sanity check below!
       *
       * @see http://archives.freenetproject.org/message/20080718.144240.359e16d3.en.html
       */
      if ((OpennetManager.MAX_PEERS_FOR_SCALING < locs.length) && (source.isOpennet())) {
        if (locs.length > OpennetManager.PANIC_MAX_PEERS) {
          // This can't happen by accident
          Logger.error(
              this,
              "We received "
                  + locs.length
                  + " locations from "
                  + source.toString()
                  + "! That should *NOT* happen! Possible attack!");
          source.forceDisconnect(true);
          return true;
        } else {
          // A few extra can happen by accident. Just use the first 20.
          Logger.normal(
              this,
              "Too many locations from "
                  + source.toString()
                  + " : "
                  + locs.length
                  + " could be an accident, using the first "
                  + OpennetManager.MAX_PEERS_FOR_SCALING);
          double[] firstLocs = new double[OpennetManager.MAX_PEERS_FOR_SCALING];
          System.arraycopy(locs, 0, firstLocs, 0, OpennetManager.MAX_PEERS_FOR_SCALING);
          locs = firstLocs;
        }
      }
      // We are on darknet and we trust our peers OR we are on opennet
      // and the amount of locations sent to us seems reasonable
      source.updateLocation(newLoc, locs);

      return true;
    }

    if (!source.isRoutable()) return false;
    if (logDEBUG) Logger.debug(this, "Not routable");

    if (spec == DMT.FNPNetworkID) {
      source.handleFNPNetworkID(m);
      return true;
    } else if (spec == DMT.FNPSwapRequest) {
      return node.lm.handleSwapRequest(m, source);
    } else if (spec == DMT.FNPSwapReply) {
      return node.lm.handleSwapReply(m, source);
    } else if (spec == DMT.FNPSwapRejected) {
      return node.lm.handleSwapRejected(m, source);
    } else if (spec == DMT.FNPSwapCommit) {
      return node.lm.handleSwapCommit(m, source);
    } else if (spec == DMT.FNPSwapComplete) {
      return node.lm.handleSwapComplete(m, source);
    } else if (spec == DMT.FNPCHKDataRequest) {
      return handleDataRequest(m, source, false);
    } else if (spec == DMT.FNPSSKDataRequest) {
      return handleDataRequest(m, source, true);
    } else if (spec == DMT.FNPInsertRequest) {
      return handleInsertRequest(m, source, false);
    } else if (spec == DMT.FNPSSKInsertRequest) {
      return handleInsertRequest(m, source, true);
    } else if (spec == DMT.FNPSSKInsertRequestNew) {
      return handleInsertRequest(m, source, true);
    } else if (spec == DMT.FNPRHProbeRequest) {
      return handleProbeRequest(m, source);
    } else if (spec == DMT.FNPRoutedPing) {
      return handleRouted(m, source);
    } else if (spec == DMT.FNPRoutedPong) {
      return handleRoutedReply(m);
    } else if (spec == DMT.FNPRoutedRejected) {
      return handleRoutedRejected(m);
      // FIXME implement threaded probe requests of various kinds.
      // Old probe request code was a major pain, never really worked.
      // We should have threaded probe requests (for simple code),
      // and one for each routing strategy.
      //		} else if(spec == DMT.FNPProbeRequest) {
      //			return handleProbeRequest(m, source);
      //		} else if(spec == DMT.FNPProbeReply) {
      //			return handleProbeReply(m, source);
      //		} else if(spec == DMT.FNPProbeRejected) {
      //			return handleProbeRejected(m, source);
      //		} else if(spec == DMT.FNPProbeTrace) {
      //			return handleProbeTrace(m, source);
    } else if (spec == DMT.FNPOfferKey) {
      return handleOfferKey(m, source);
    } else if (spec == DMT.FNPGetOfferedKey) {
      return handleGetOfferedKey(m, source);
    }
    return false;
  }