use of net.i2p.data.Destination in project i2p.i2p by i2p.
the class SOCKSUDPUnwrapper method send.
/**
* May throw RuntimeException from underlying sink
* @throws RuntimeException
*/
public void send(Destination ignored_from, byte[] data) {
SOCKSHeader h;
try {
h = new SOCKSHeader(data);
} catch (IllegalArgumentException iae) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(SOCKSUDPUnwrapper.class);
log.error(iae.toString());
return;
}
Destination dest = h.getDestination();
if (dest == null) {
// no, we aren't going to send non-i2p traffic to a UDP outproxy :)
Log log = I2PAppContext.getGlobalContext().logManager().getLog(SOCKSUDPUnwrapper.class);
log.error("Destination not found: " + h.getHost());
return;
}
cache.put(dest, h);
int headerlen = h.getBytes().length;
byte[] unwrapped = new byte[data.length - headerlen];
System.arraycopy(data, headerlen, unwrapped, 0, unwrapped.length);
this.sink.send(dest, unwrapped);
}
use of net.i2p.data.Destination in project i2p.i2p by i2p.
the class ConnectionManager method shouldRejectConnection.
/**
* @return reason string or null if not rejected
*/
private String shouldRejectConnection(Packet syn) {
// unfortunately we don't have access to the router client manager here,
// so we can't whitelist local access
Destination from = syn.getOptionalFrom();
if (from == null)
return "null";
Hash h = from.calculateHash();
// As of 0.9.9, run the blacklist checks BEFORE the port counters,
// so blacklisted dests will not increment the counters and
// possibly trigger total-counter blocks for others.
// if the sig is absent or bad it will be caught later (in CPH)
String hashes = _context.getProperty(PROP_BLACKLIST, "");
if (!_currentBlacklist.equals(hashes)) {
// rebuild _globalBlacklist when property changes
synchronized (_globalBlacklist) {
if (hashes.length() > 0) {
Set<Hash> newSet = new HashSet<Hash>();
StringTokenizer tok = new StringTokenizer(hashes, ",; ");
while (tok.hasMoreTokens()) {
String hashstr = tok.nextToken();
Hash hh = ConvertToHash.getHash(hashstr);
if (hh != null)
newSet.add(hh);
else
_log.error("Bad blacklist entry: " + hashstr);
}
_globalBlacklist.addAll(newSet);
_globalBlacklist.retainAll(newSet);
_currentBlacklist = hashes;
} else {
_globalBlacklist.clear();
_currentBlacklist = "";
}
}
}
if (hashes.length() > 0 && _globalBlacklist.contains(h))
return "blacklisted globally";
if (_defaultOptions.isAccessListEnabled() && !_defaultOptions.getAccessList().contains(h))
return "not whitelisted";
if (_defaultOptions.isBlacklistEnabled() && _defaultOptions.getBlacklist().contains(h))
return "blacklisted";
if (_dayThrottler != null && _dayThrottler.shouldThrottle(h)) {
_context.statManager().addRateData("stream.con.throttledDay", 1);
if (_defaultOptions.getMaxConnsPerDay() <= 0)
return "throttled by" + " total limit of " + _defaultOptions.getMaxTotalConnsPerDay() + " per day";
else if (_defaultOptions.getMaxTotalConnsPerDay() <= 0)
return "throttled by per-peer limit of " + _defaultOptions.getMaxConnsPerDay() + " per day";
else
return "throttled by per-peer limit of " + _defaultOptions.getMaxConnsPerDay() + " or total limit of " + _defaultOptions.getMaxTotalConnsPerDay() + " per day";
}
if (_hourThrottler != null && _hourThrottler.shouldThrottle(h)) {
_context.statManager().addRateData("stream.con.throttledHour", 1);
if (_defaultOptions.getMaxConnsPerHour() <= 0)
return "throttled by" + " total limit of " + _defaultOptions.getMaxTotalConnsPerHour() + " per hour";
else if (_defaultOptions.getMaxTotalConnsPerHour() <= 0)
return "throttled by per-peer limit of " + _defaultOptions.getMaxConnsPerHour() + " per hour";
else
return "throttled by per-peer limit of " + _defaultOptions.getMaxConnsPerHour() + " or total limit of " + _defaultOptions.getMaxTotalConnsPerHour() + " per hour";
}
if (_minuteThrottler != null && _minuteThrottler.shouldThrottle(h)) {
_context.statManager().addRateData("stream.con.throttledMinute", 1);
if (_defaultOptions.getMaxConnsPerMinute() <= 0)
return "throttled by" + " total limit of " + _defaultOptions.getMaxTotalConnsPerMinute() + " per minute";
else if (_defaultOptions.getMaxTotalConnsPerMinute() <= 0)
return "throttled by per-peer limit of " + _defaultOptions.getMaxConnsPerMinute() + " per minute";
else
return "throttled by per-peer limit of " + _defaultOptions.getMaxConnsPerMinute() + " or total limit of " + _defaultOptions.getMaxTotalConnsPerMinute() + " per minute";
}
return null;
}
use of net.i2p.data.Destination in project i2p.i2p by i2p.
the class ConnectionPacketHandler method verifySignature.
/**
* Verify the signature if necessary.
*
* @throws I2PException if the signature was necessary and it was invalid
*/
private void verifySignature(Packet packet, Connection con) throws I2PException {
// verify the signature if necessary
if (con.getOptions().getRequireFullySigned() || packet.isFlagSet(Packet.FLAG_SYNCHRONIZE | Packet.FLAG_CLOSE)) {
// we need a valid signature
Destination from = con.getRemotePeer();
if (from == null)
from = packet.getOptionalFrom();
boolean sigOk = packet.verifySignature(_context, from, null);
if (!sigOk) {
throw new I2PException("Received unsigned / forged packet: " + packet);
}
}
}
use of net.i2p.data.Destination in project i2p.i2p by i2p.
the class ConnectionPacketHandler method verifyReset.
/**
* Make sure this RST packet is valid, and if it is, act on it.
*
* Prior to 0.9.20, the reset packet must contain a FROM field,
* and we used that for verification.
* As of 0.9.20, we correctly use the connection's remote peer.
*/
private void verifyReset(Packet packet, Connection con) {
if (con.getReceiveStreamId() == packet.getSendStreamId()) {
Destination from = con.getRemotePeer();
if (from == null)
from = packet.getOptionalFrom();
boolean ok = packet.verifySignature(_context, from, null);
if (!ok) {
if (_log.shouldLog(Log.ERROR))
_log.error("Received unsigned / forged RST on " + con);
return;
} else {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Reset received");
// ok, valid RST
con.resetReceived();
con.eventOccurred();
_context.statManager().addRateData("stream.resetReceived", con.getHighestAckedThrough(), con.getLifetime());
// no further processing
return;
}
} else {
if (_log.shouldLog(Log.WARN))
_log.warn("Received a packet for the wrong connection? " + con + " / " + packet);
return;
}
}
use of net.i2p.data.Destination in project i2p.i2p by i2p.
the class ConnectionPacketHandler method verifyPacket.
/**
* Make sure this packet is ok and that we can continue processing its data.
*
* SIDE EFFECT:
* Sets the SendStreamId and RemotePeer for the con,
* using the packet's ReceiveStreamId and OptionalFrom,
* If this is a SYN packet and the con's SendStreamId is not set.
*
* @return true if the packet is ok for this connection, false if we shouldn't
* continue processing.
*/
private boolean verifyPacket(Packet packet, Connection con) throws I2PException {
if (packet.isFlagSet(Packet.FLAG_RESET)) {
verifyReset(packet, con);
return false;
} else {
verifySignature(packet, con);
if (con.getSendStreamId() <= 0) {
if (packet.isFlagSet(Packet.FLAG_SYNCHRONIZE)) {
con.setSendStreamId(packet.getReceiveStreamId());
Destination dest = packet.getOptionalFrom();
if (dest == null) {
if (_log.shouldWarn())
_log.warn("SYN Packet without FROM");
return false;
}
con.setRemotePeer(dest);
return true;
} else {
// neither RST nor SYN and we dont have the stream id yet?
if (packet.getSequenceNum() < MAX_INITIAL_PACKETS) {
return true;
} else {
if (_log.shouldLog(Log.WARN))
_log.warn("Packet without RST or SYN where we dont know stream ID: " + packet);
return false;
}
}
} else {
if (con.getSendStreamId() != packet.getReceiveStreamId()) {
if (_log.shouldLog(Log.WARN))
_log.warn("Packet received with the wrong reply stream id: " + con + " / " + packet);
return false;
} else {
return true;
}
}
}
}
Aggregations