RawSocket SPI java

Linux raw socket via Java DatagramChannel

License

License

Categories

Categories

Java Languages AWS Container PaaS Providers
GroupId

GroupId

com.github.alei121
ArtifactId

ArtifactId

rawsocketspi-java
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

RawSocket SPI java
Linux raw socket via Java DatagramChannel
Project URL

Project URL

http://github.com/alei121
Source Code Management

Source Code Management

https://github.com/alei121/rawsocketspi.git

Download rawsocketspi-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.alei121/rawsocketspi-java/ -->
<dependency>
    <groupId>com.github.alei121</groupId>
    <artifactId>rawsocketspi-java</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.alei121/rawsocketspi-java/
implementation 'com.github.alei121:rawsocketspi-java:1.0.0'
// https://jarcasting.com/artifacts/com.github.alei121/rawsocketspi-java/
implementation ("com.github.alei121:rawsocketspi-java:1.0.0")
'com.github.alei121:rawsocketspi-java:jar:1.0.0'
<dependency org="com.github.alei121" name="rawsocketspi-java" rev="1.0.0">
  <artifact name="rawsocketspi-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.alei121', module='rawsocketspi-java', version='1.0.0')
)
libraryDependencies += "com.github.alei121" % "rawsocketspi-java" % "1.0.0"
[com.github.alei121/rawsocketspi-java "1.0.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

rawsocketspi

Java SPI for raw socket

Support for Linux raw socket only, the library uses Java SPI to provide easy access to network packet data via Java DatagramChannel.

Usage

  import java.nio.channels.DatagramChannel;
  import com.github.alei121.rawsocketspi.RSSelectorProvider;
  import com.github.alei121.rawsocketspi.RSSocketAddress;

  // Create DatagramChannel and bind it
	RSSelectorProvider provider = new RSSelectorProvider();
	DatagramChannel channel = provider.openDatagramChannel();
	RSSocketAddress address = new RSSocketAddress("eth1");
	channel.bind(address);

Maven

<dependency>
	<groupId>com.github.alei121</groupId>
	<artifactId>rawsocketspi</artifactId>
	<classifier>amd64-Linux</classifier>
	<version>1.0.0</version>
</dependency>

Blocking read/write only, no selector

rawsocketspi only implements blocking read/write.

With better CPUs, OSs, VMs..., computer systems are now more capable of handling large number of threads. For raw socket, even using one thread per port, the number of threads is still small because the number of network ports are limited by hardware and physical space. The intention of socket selector was to reduce the number of threads, in which it seems unnecessary in this case.

It is the author believes that application implementation should take the simplest form. If implementation warrents for a large number of threads, it should be handled by the lower layer (e.g. OS, VM, CPU...etc) instead of adjusting design to reduce threads.

Test setup

Testing was done in VMWare Fusion:

  • 2 custom networks vmnet2 and vmnet3 (no dhcp, no nat, no host connect)
  • 1 VM running Ubuntu 1G RAM 20G disk for build and run (add vmnet2 and vmnet3)
  • 2 VMs running Ubuntu 512G RAM for testing (one on vmnet2, one on vmnet3)

For reference my environment is:

  • VMWare Fusion 7.1.0
  • Ubuntu 14.04 and 12.04
  • OpenJDK 7

To run examples on Ubuntu with openjdk:

mvn clean install

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/

/usr/lib/jvm/java-7-openjdk-amd64/bin/java -cp rawsocketspi-dist/target/rawsocketspi-1.0-SNAPSHOT-amd64-Linux.jar:rawsocketspi-examples/target/rawsocketspi-examples-1.0-SNAPSHOT.jar com.github.alei121.rawsocketspi.examples.SimpleCapture eth1

/usr/lib/jvm/java-7-openjdk-amd64/bin/java -cp rawsocketspi-dist/target/rawsocketspi-1.0-SNAPSHOT-amd64-Linux.jar:rawsocketspi-examples/target/rawsocketspi-examples-1.0-SNAPSHOT.jar com.github.alei121.rawsocketspi.examples.SimpleBridge eth1 eth2

More details to come...

Versions

Version
1.0.0