forked from I2P_Developers/i2p.i2p
PORT msg support; port and extension listener stubs
This commit is contained in:
@ -53,6 +53,7 @@ class Message
|
|||||||
|
|
||||||
// Used for HAVE, REQUEST, PIECE and CANCEL messages.
|
// Used for HAVE, REQUEST, PIECE and CANCEL messages.
|
||||||
// low byte used for EXTENSION message
|
// low byte used for EXTENSION message
|
||||||
|
// low two bytes used for PORT message
|
||||||
int piece;
|
int piece;
|
||||||
|
|
||||||
// Used for REQUEST, PIECE and CANCEL messages.
|
// Used for REQUEST, PIECE and CANCEL messages.
|
||||||
@ -107,6 +108,9 @@ class Message
|
|||||||
if (type == EXTENSION)
|
if (type == EXTENSION)
|
||||||
datalen += 1;
|
datalen += 1;
|
||||||
|
|
||||||
|
if (type == PORT)
|
||||||
|
datalen += 2;
|
||||||
|
|
||||||
// add length of data for piece or bitfield array.
|
// add length of data for piece or bitfield array.
|
||||||
if (type == BITFIELD || type == PIECE || type == EXTENSION)
|
if (type == BITFIELD || type == PIECE || type == EXTENSION)
|
||||||
datalen += len;
|
datalen += len;
|
||||||
@ -130,6 +134,9 @@ class Message
|
|||||||
if (type == EXTENSION)
|
if (type == EXTENSION)
|
||||||
dos.writeByte((byte) piece & 0xff);
|
dos.writeByte((byte) piece & 0xff);
|
||||||
|
|
||||||
|
if (type == PORT)
|
||||||
|
dos.writeShort(piece & 0xffff);
|
||||||
|
|
||||||
// Send actual data
|
// Send actual data
|
||||||
if (type == BITFIELD || type == PIECE || type == EXTENSION)
|
if (type == BITFIELD || type == PIECE || type == EXTENSION)
|
||||||
dos.write(data, off, len);
|
dos.write(data, off, len);
|
||||||
|
@ -72,7 +72,6 @@ public class Peer implements Comparable
|
|||||||
* relevant MetaInfo.
|
* relevant MetaInfo.
|
||||||
*/
|
*/
|
||||||
public Peer(PeerID peerID, byte[] my_id, MetaInfo metainfo)
|
public Peer(PeerID peerID, byte[] my_id, MetaInfo metainfo)
|
||||||
throws IOException
|
|
||||||
{
|
{
|
||||||
this.peerID = peerID;
|
this.peerID = peerID;
|
||||||
this.my_id = my_id;
|
this.my_id = my_id;
|
||||||
@ -253,6 +252,13 @@ public class Peer implements Comparable
|
|||||||
out.sendExtension(0, ExtensionHandshake.getPayload());
|
out.sendExtension(0, ExtensionHandshake.getPayload());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((options & OPTION_DHT) != 0 && util.getDHT() != null) {
|
||||||
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("Peer supports DHT, sending PORT message");
|
||||||
|
int port = util.getDHT().getPort();
|
||||||
|
out.sendPort(port);
|
||||||
|
}
|
||||||
|
|
||||||
// Send our bitmap
|
// Send our bitmap
|
||||||
if (bitfield != null)
|
if (bitfield != null)
|
||||||
s.out.sendBitfield(bitfield);
|
s.out.sendBitfield(bitfield);
|
||||||
@ -303,7 +309,8 @@ public class Peer implements Comparable
|
|||||||
dout.write(19);
|
dout.write(19);
|
||||||
dout.write("BitTorrent protocol".getBytes("UTF-8"));
|
dout.write("BitTorrent protocol".getBytes("UTF-8"));
|
||||||
// Handshake write - options
|
// Handshake write - options
|
||||||
dout.writeLong(OPTION_EXTENSION);
|
// FIXME not if DHT disabled
|
||||||
|
dout.writeLong(OPTION_EXTENSION | OPTION_DHT);
|
||||||
// Handshake write - metainfo hash
|
// Handshake write - metainfo hash
|
||||||
byte[] shared_hash = metainfo.getInfoHash();
|
byte[] shared_hash = metainfo.getInfoHash();
|
||||||
dout.write(shared_hash);
|
dout.write(shared_hash);
|
||||||
@ -343,7 +350,7 @@ public class Peer implements Comparable
|
|||||||
_log.debug("Read the remote side's hash and peerID fully from " + toString());
|
_log.debug("Read the remote side's hash and peerID fully from " + toString());
|
||||||
|
|
||||||
if (options != 0) {
|
if (options != 0) {
|
||||||
// send them something
|
// send them something in runConnection() above
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug("Peer supports options 0x" + Long.toString(options, 16) + ": " + toString());
|
_log.debug("Peer supports options 0x" + Long.toString(options, 16) + ": " + toString());
|
||||||
}
|
}
|
||||||
@ -351,6 +358,11 @@ public class Peer implements Comparable
|
|||||||
return bs;
|
return bs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 0.8.4 */
|
||||||
|
public long getOptions() {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isConnected()
|
public boolean isConnected()
|
||||||
{
|
{
|
||||||
return state != null;
|
return state != null;
|
||||||
|
@ -171,6 +171,11 @@ class PeerConnectionIn implements Runnable
|
|||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug("Received cancel(" + piece + "," + begin + ") from " + peer + " on " + peer.metainfo.getName());
|
_log.debug("Received cancel(" + piece + "," + begin + ") from " + peer + " on " + peer.metainfo.getName());
|
||||||
break;
|
break;
|
||||||
|
case 9: // PORT message
|
||||||
|
int port = din.readUnsignedShort();
|
||||||
|
ps.portMessage(port);
|
||||||
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("Received port message from " + peer + " on " + peer.metainfo.getName());
|
||||||
case 20: // Extension message
|
case 20: // Extension message
|
||||||
int id = din.readUnsignedByte();
|
int id = din.readUnsignedByte();
|
||||||
byte[] payload = new byte[i-2];
|
byte[] payload = new byte[i-2];
|
||||||
|
@ -547,4 +547,12 @@ class PeerConnectionOut implements Runnable
|
|||||||
m.len = bytes.length;
|
m.len = bytes.length;
|
||||||
addMessage(m);
|
addMessage(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 0.8.4 */
|
||||||
|
void sendPort(int port) {
|
||||||
|
Message m = new Message();
|
||||||
|
m.type = Message.PORT;
|
||||||
|
m.piece = port;
|
||||||
|
addMessage(m);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1086,6 +1086,12 @@ public class PeerCoordinator implements PeerListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 0.8.4 */
|
||||||
|
public void gotExtension(Peer peer, int id, byte[] bs) {}
|
||||||
|
|
||||||
|
/** @since 0.8.4 */
|
||||||
|
public void gotPort(Peer peer, int port) {}
|
||||||
|
|
||||||
/** Return number of allowed uploaders for this torrent.
|
/** Return number of allowed uploaders for this torrent.
|
||||||
** Check with Snark to see if we are over the total upload limit.
|
** Check with Snark to see if we are over the total upload limit.
|
||||||
*/
|
*/
|
||||||
|
@ -179,4 +179,23 @@ interface PeerListener
|
|||||||
* @since 0.8.2
|
* @since 0.8.2
|
||||||
*/
|
*/
|
||||||
PartialPiece getPartialPiece(Peer peer, BitField havePieces);
|
PartialPiece getPartialPiece(Peer peer, BitField havePieces);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when an extension message is received.
|
||||||
|
*
|
||||||
|
* @param peer the Peer that got the message.
|
||||||
|
* @param id the message ID
|
||||||
|
* @param bs the message payload
|
||||||
|
* @since 0.8.4
|
||||||
|
*/
|
||||||
|
void gotExtension(Peer peer, int id, byte[] bs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when an extension message is received.
|
||||||
|
*
|
||||||
|
* @param peer the Peer that got the message.
|
||||||
|
* @param port the port
|
||||||
|
* @since 0.8.4
|
||||||
|
*/
|
||||||
|
void gotPort(Peer peer, int port);
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,6 @@ import java.util.Set;
|
|||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
|
||||||
import org.klomp.snark.bencode.BDecoder;
|
|
||||||
import org.klomp.snark.bencode.BEValue;
|
|
||||||
|
|
||||||
class PeerState implements DataLoader
|
class PeerState implements DataLoader
|
||||||
{
|
{
|
||||||
private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(PeerState.class);
|
private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(PeerState.class);
|
||||||
@ -485,22 +482,13 @@ class PeerState implements DataLoader
|
|||||||
/** @since 0.8.2 */
|
/** @since 0.8.2 */
|
||||||
void extensionMessage(int id, byte[] bs)
|
void extensionMessage(int id, byte[] bs)
|
||||||
{
|
{
|
||||||
if (id == 0) {
|
listener.gotExtension(peer, id, bs);
|
||||||
InputStream is = new ByteArrayInputStream(bs);
|
}
|
||||||
try {
|
|
||||||
BDecoder dec = new BDecoder(is);
|
/** @since 0.8.4 */
|
||||||
BEValue bev = dec.bdecodeMap();
|
void portMessage(int port)
|
||||||
Map map = bev.getMap();
|
{
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
listener.gotPort(peer, port);
|
||||||
_log.debug("Got extension handshake message " + bev.toString());
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
|
||||||
_log.debug("Failed extension decode", e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
|
||||||
_log.debug("Got extended message type: " + id + " length: " + bs.length);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void unknownMessage(int type, byte[] bs)
|
void unknownMessage(int type, byte[] bs)
|
||||||
|
Reference in New Issue
Block a user