Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHIM #131

Merged
merged 37 commits into from
Oct 21, 2024
Merged

SHIM #131

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: 11
java-version: 17
distribution: 'temurin'
- name: Enable 127.0.0.x addresses
run: |
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]

### TODO for 0.4.0
- Fix showpaths to show "127.0.0.81:31038" i.o. "/192.168.53.20"
- Fix @Disabled tests
- Support topofile port range
- Upgrade all JUnit topo files to post 0.11 with new format (including port range)
Expand All @@ -21,6 +22,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Inherit DatagramChannel
- Consider using https://github.com/ascopes/protobuf-maven-plugin (more up to date)

### Added
- Add a SHIM, required for #130 (topo file port range support).
[#130](https://github.com/scionproto-contrib/jpan/pull/130)

## [0.3.1] - 2024-10-11

### Added
Expand Down
2 changes: 1 addition & 1 deletion config/checkstyle/checkstyle-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- Checks for imports -->
<!-- See http://checkstyle.sf.net/config_import.html -->
<module name="AvoidStarImport">
<property name="excludes" value="java.io,java.util,java.net,io.grpc,org.junit.jupiter.api.Assertions,org.scion.jpan.internal,org.scion.jpan,org.scion.jpan.demo.inspector.ByteUtil,org.xbill.DNS"/>
<property name="excludes" value="java.io,java.util,java.net,io.grpc,org.junit.jupiter.api,org.junit.jupiter.api.Assertions,org.scion.jpan.internal,org.scion.jpan,org.scion.jpan.demo.inspector.ByteUtil,org.xbill.DNS"/>
<!-- <property name="severity" value="warning" />-->
</module>
<module name="NeedBraces"/>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/scion/jpan/AbstractDatagramChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ abstract class AbstractDatagramChannel<C extends AbstractDatagramChannel<?>> imp
private boolean cfgRemoteDispatcher = false;
private InetSocketAddress overrideExternalAddress = null;

protected AbstractDatagramChannel(ScionService service) throws IOException {
this(service, DatagramChannel.open());
}

protected AbstractDatagramChannel(
ScionService service, java.nio.channels.DatagramChannel channel) {
this.channel = channel;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/scion/jpan/ScionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ protected ScionService(String addressOrHost, Mode mode) {
segmentStub = SegmentLookupServiceGrpc.newBlockingStub(channel);
}
shutdownHook = addShutdownHook();
Shim.install(this);
try {
getLocalIsdAs(); // Init
} catch (RuntimeException e) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/scion/jpan/Scmp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

public class Scmp {

Expand Down Expand Up @@ -426,7 +427,7 @@
*/
@Deprecated
public static ScmpChannel createChannel(int listeningPort) throws IOException {
return new ScmpChannel(Scion.defaultService(), listeningPort);
return new ScmpChannel(Scion.defaultService(), listeningPort, DatagramChannel.open());
}

/**
Expand All @@ -440,7 +441,7 @@
@Deprecated
public static ScmpChannel createChannel(ScionService service, int listeningPort)
throws IOException {
return new ScmpChannel(service, listeningPort);
return new ScmpChannel(service, listeningPort, DatagramChannel.open());

Check warning on line 444 in src/main/java/org/scion/jpan/Scmp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/scion/jpan/Scmp.java#L444

Added line #L444 was not covered by tests
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/scion/jpan/ScmpChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public class ScmpChannel implements AutoCloseable {
private final InternalChannel channel;

ScmpChannel() throws IOException {
this(Scion.defaultService(), 12345);
this(Scion.defaultService(), 12345, DatagramChannel.open());
}

ScmpChannel(ScionService service, int port) throws IOException {
this.channel = new InternalChannel(service, port);
ScmpChannel(ScionService service, int port, DatagramChannel channel) throws IOException {
this.channel = new InternalChannel(service, port, channel);
}

/**
Expand Down Expand Up @@ -166,8 +166,9 @@ private class InternalChannel extends AbstractDatagramChannel<InternalChannel> {
private final Selector selector;
private Predicate<Scmp.EchoMessage> echoListener;

protected InternalChannel(ScionService service, int port) throws IOException {
super(service);
protected InternalChannel(ScionService service, int port, DatagramChannel channel)
throws IOException {
super(service, channel);

// selector
this.selector = Selector.open();
Expand Down
46 changes: 38 additions & 8 deletions src/main/java/org/scion/jpan/ScmpResponder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@
import org.scion.jpan.internal.InternalConstants;
import org.scion.jpan.internal.ScionHeaderParser;
import org.scion.jpan.internal.ScmpParser;
import org.scion.jpan.internal.Shim;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ScmpResponder implements AutoCloseable {
private static final Logger log = LoggerFactory.getLogger(ScmpResponder.class);
private final InternalChannel channel;

private ScmpResponder(ScionService service, int port) throws IOException {
this.channel = new InternalChannel(service, port);
private ScmpResponder(
ScionService service, int port, DatagramChannel channel, Selector selector, Shim shim) {
this.channel = new InternalChannel(service, port, channel, selector, shim);
}

public static Builder newBuilder() {
Expand Down Expand Up @@ -76,23 +78,31 @@
* <p>SCMP requests can be monitored and intercepted through a listener, see {@link
* #setScmpEchoListener(Predicate)}.
*
* <p>This method blocks until the responder's thread is interrupted.
* <p>This method blocks until {@link #close()} is called.
*
* @throws IOException If an IO exception occurs.
*/
public void start() throws IOException {
this.channel.start();
this.channel.sendEchoResponses();
}

private static class InternalChannel extends AbstractDatagramChannel<InternalChannel> {
private final Selector selector;
private Predicate<Scmp.EchoMessage> echoListener;
private final int port;
private final Shim shim;

protected InternalChannel(ScionService service, int port) throws IOException {
super(service);
protected InternalChannel(
ScionService service, int port, DatagramChannel channel, Selector selector, Shim shim) {
super(service, channel);
this.shim = shim;
this.port = port;
this.selector = selector;
}

void start() throws IOException {
// selector
this.selector = Selector.open();
super.channel().configureBlocking(false);
super.channel().register(selector, SelectionKey.OP_READ);

Expand Down Expand Up @@ -121,6 +131,9 @@
hdrType = receiveExtensionHeader(buffer, hdrType);

if (hdrType != InternalConstants.HdrTypes.SCMP) {
if (shim != null) {
shim.forward(buffer, super.channel());
}
continue; // drop
}
return ScionHeaderParser.extractResponsePath(buffer, srcAddress);
Expand All @@ -135,6 +148,9 @@
readLock().lock();
writeLock().lock();
try {
if (shim != null) {
shim.signalReadiness();
}
while (true) {
ByteBuffer buffer = getBufferReceive(DEFAULT_BUFFER_SIZE);
ResponsePath path = receiveLoop(buffer);
Expand Down Expand Up @@ -163,7 +179,11 @@
sendRaw(buffer, path);
log.info("Responded to SCMP {} from {}", type, path.getRemoteAddress());
} else {
log.info("Dropped SCMP message with type {} from {}", type, path.getRemoteAddress());
if (shim != null) {
shim.forward(buffer, super.channel());

Check warning on line 183 in src/main/java/org/scion/jpan/ScmpResponder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/scion/jpan/ScmpResponder.java#L183

Added line #L183 was not covered by tests
} else {
log.info("Dropped SCMP message with type {} from {}", type, path.getRemoteAddress());

Check warning on line 185 in src/main/java/org/scion/jpan/ScmpResponder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/scion/jpan/ScmpResponder.java#L185

Added line #L185 was not covered by tests
}
}
}
} finally {
Expand Down Expand Up @@ -203,6 +223,9 @@
public static class Builder {
private ScionService service;
private int port = Constants.SCMP_PORT;
private DatagramChannel channel;
private Selector selector;
private Shim shim;

public Builder setLocalPort(int localPort) {
this.port = localPort;
Expand All @@ -217,10 +240,17 @@
public ScmpResponder build() {
ScionService service2 = service == null ? ScionService.defaultService() : service;
try {
return new ScmpResponder(service2, port);
channel = channel == null ? DatagramChannel.open() : channel;
selector = selector == null ? Selector.open() : selector;
} catch (IOException e) {
throw new ScionRuntimeException(e);
}
return new ScmpResponder(service2, port, channel, selector, shim);
}

public Builder setShim(Shim shim) {
this.shim = shim;
return this;
}
}
}
Loading
Loading