Exemplo n.º 1
0
  /** Called by servlet to save a new torrent file generated from local data */
  public synchronized byte[] getTorrentData() {
    Map<String, Object> m = new HashMap<String, Object>();
    if (announce != null) m.put("announce", announce);
    if (announce_list != null) m.put("announce-list", announce_list);
    // misc. optional  top-level stuff
    if (comment != null) m.put("comment", comment);
    if (created_by != null) m.put("created by", created_by);
    if (creation_date != 0) m.put("creation date", creation_date / 1000);

    Map<String, BEValue> info = createInfoMap();
    m.put("info", info);
    // don't save this locally, we should only do this once
    return BEncoder.bencode(m);
  }
Exemplo n.º 2
0
 /**
  * Send the DHT port numbers
  *
  * @since DHT
  */
 public static void sendDHT(Peer peer, int qport, int rport) {
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("port", Integer.valueOf(qport));
   map.put("rport", Integer.valueOf(rport));
   byte[] payload = BEncoder.bencode(map);
   try {
     int hisMsgCode = peer.getHandshakeMap().get("m").getMap().get(TYPE_DHT).getInt();
     peer.sendExtension(hisMsgCode, payload);
   } catch (Exception e) {
     // NPE, no DHT caps
     // if (log.shouldLog(Log.INFO))
     //    log.info("DHT msg exception to " + peer, e);
   }
 }
Exemplo n.º 3
0
 /** REQUEST and REJECT are the same except for message type */
 private static void sendMessage(Peer peer, int type, int piece) {
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("msg_type", Integer.valueOf(type));
   map.put("piece", Integer.valueOf(piece));
   byte[] payload = BEncoder.bencode(map);
   try {
     int hisMsgCode = peer.getHandshakeMap().get("m").getMap().get(TYPE_METADATA).getInt();
     peer.sendExtension(hisMsgCode, payload);
   } catch (Exception e) {
     // NPE, no metadata capability
     // if (log.shouldLog(Log.INFO))
     //    log.info("Metadata send req msg exception to " + peer, e);
   }
 }
Exemplo n.º 4
0
 /**
  * @param metasize -1 if unknown
  * @param pexAndMetadata advertise these capabilities
  * @param dht advertise DHT capability
  * @return bencoded outgoing handshake message
  */
 public static byte[] getHandshake(int metasize, boolean pexAndMetadata, boolean dht) {
   Map<String, Object> handshake = new HashMap<String, Object>();
   Map<String, Integer> m = new HashMap<String, Integer>();
   if (pexAndMetadata) {
     m.put(TYPE_METADATA, Integer.valueOf(ID_METADATA));
     m.put(TYPE_PEX, Integer.valueOf(ID_PEX));
     if (metasize >= 0) handshake.put("metadata_size", Integer.valueOf(metasize));
   }
   if (dht) {
     m.put(TYPE_DHT, Integer.valueOf(ID_DHT));
   }
   // include the map even if empty so the far-end doesn't NPE
   handshake.put("m", m);
   handshake.put("p", Integer.valueOf(TrackerClient.PORT));
   handshake.put("v", "I2PSnark");
   handshake.put("reqq", Integer.valueOf(5));
   return BEncoder.bencode(handshake);
 }
Exemplo n.º 5
0
 private static void sendPiece(Peer peer, int piece, byte[] data) {
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("msg_type", Integer.valueOf(TYPE_DATA));
   map.put("piece", Integer.valueOf(piece));
   map.put("total_size", Integer.valueOf(data.length));
   byte[] dict = BEncoder.bencode(map);
   byte[] payload = new byte[dict.length + data.length];
   System.arraycopy(dict, 0, payload, 0, dict.length);
   System.arraycopy(data, 0, payload, dict.length, data.length);
   try {
     int hisMsgCode = peer.getHandshakeMap().get("m").getMap().get(TYPE_METADATA).getInt();
     peer.sendExtension(hisMsgCode, payload);
   } catch (Exception e) {
     // NPE, no metadata caps
     // if (log.shouldLog(Log.INFO))
     //    log.info("Metadata send piece msg exception to " + peer, e);
   }
 }
Exemplo n.º 6
0
 private byte[] calculateInfoHash() {
   Map<String, BEValue> info = createInfoMap();
   if (_log.shouldLog(Log.DEBUG)) {
     StringBuilder buf = new StringBuilder(128);
     buf.append("info: ");
     for (Map.Entry<String, BEValue> entry : info.entrySet()) {
       String key = entry.getKey();
       Object val = entry.getValue();
       buf.append(key).append('=');
       buf.append(val.toString());
     }
     _log.debug(buf.toString());
   }
   byte[] infoBytes = BEncoder.bencode(info);
   // _log.debug("info bencoded: [" + Base64.encode(infoBytes, true) + "]");
   MessageDigest digest = SHA1.getInstance();
   byte hash[] = digest.digest(infoBytes);
   if (_log.shouldLog(Log.DEBUG)) _log.debug("info hash: " + I2PSnarkUtil.toHex(hash));
   return hash;
 }
Exemplo n.º 7
0
 /**
  * added.f and dropped unsupported
  *
  * @param pList non-null
  * @since 0.8.4
  */
 public static void sendPEX(Peer peer, List<Peer> pList) {
   if (pList.isEmpty()) return;
   Map<String, Object> map = new HashMap<String, Object>();
   byte[] peers = new byte[HASH_LENGTH * pList.size()];
   int off = 0;
   for (Peer p : pList) {
     System.arraycopy(p.getPeerID().getDestHash(), 0, peers, off, HASH_LENGTH);
     off += HASH_LENGTH;
   }
   map.put("added", peers);
   byte[] payload = BEncoder.bencode(map);
   try {
     int hisMsgCode = peer.getHandshakeMap().get("m").getMap().get(TYPE_PEX).getInt();
     peer.sendExtension(hisMsgCode, payload);
   } catch (Exception e) {
     // NPE, no PEX caps
     // if (log.shouldLog(Log.INFO))
     //    log.info("PEX msg exception to " + peer, e);
   }
 }
Exemplo n.º 8
0
 /** @since 0.8.4 */
 public synchronized byte[] getInfoBytes() {
   if (infoMap == null) createInfoMap();
   return BEncoder.bencode(infoMap);
 }