Skip to content

Commit

Permalink
use new ivshmem version, update socks proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSchuetz97 committed Jan 24, 2022
1 parent a37e59a commit a3fbede
Show file tree
Hide file tree
Showing 33 changed files with 1,726 additions and 221 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# tcp2ivshmem
tcp2ivshmem is a java program to tunnel tcp connections through qemu ivshmem shared memory without using a network interface to do so.
tcp2ivshmem is a java command line application to tunnel tcp connections through qemu ivshmem shared memory without using a network interface to do so.
## License
tcp2ivshmem is released under the GNU General Public License Version 3. <br>A copy of the GNU General Public License Version 3 can be found in the COPYING file.<br>

The file "mvnw" is part of the maven-wrapper project, released under the Apache License Version 2.<br>
See https://github.com/takari/maven-wrapper for more information regarding maven-wrapper.
## Building
If you do not want to build tcp2ivshmem yourself then you may also use the standalone executable jar file inside the examples folder.
If you do not want to build tcp2ivshmem yourself then you may also use the standalone executable jar file inside the
examples folder or one of the releases provided on github.

Requirements:
* JDK 7 or newer (Oracle or OpenJDK both work fine.)

Install Ivshmem4j 1.0 to your local maven repository (see https://github.com/AlexanderSchuetz97/Ivshmem4j)<br>
Once Ivshmem4j is installed to your local maven repository run:
````
./mvnw clean package
````
Expand Down
2 changes: 1 addition & 1 deletion examples/guestToHostSSHGuest.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
java -jar tcp2ivshmem-0.1.jar -s -d 134217728
java -jar tcp2ivshmem-0.2.jar -s -d 134217728
pause
2 changes: 1 addition & 1 deletion examples/guestToHostSSHHost.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
java -jar tcp2ivshmem-0.1.jar -m -d /dev/shm/tcp2ivshmem -b 134217728 -R 8000:127.0.0.1:22
java -jar tcp2ivshmem-0.2.jar -m -d /dev/shm/tcp2ivshmem -b 134217728 -R 8000:127.0.0.1:22
Binary file removed examples/tcp2ivshmem-0.1.jar
Binary file not shown.
Binary file added examples/tcp2ivshmem-0.2.jar
Binary file not shown.
33 changes: 10 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright Alexander Schütz, 2020
This file is part of tcp2ivshmem.
tcp2ivshmem is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
tcp2ivshmem is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
A copy of the GNU General Public License should be provided
in the COPYING file in top level directory of Ivshmem4j.
If not, see <https://www.gnu.org/licenses/>.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.aschuetz</groupId>
<groupId>io.github.alexanderschuetz97</groupId>
<artifactId>tcp2ivshmem</artifactId>
<version>0.2-SNAPSHOT</version>
<build>
Expand Down Expand Up @@ -71,7 +52,7 @@ If not, see <https://www.gnu.org/licenses/>.
<configuration>
<archive>
<manifest>
<mainClass>de.aschuetz.tcp2ivshmem.Main</mainClass>
<mainClass>io.github.alexanderschuetz97.tcp2ivshmem.Main</mainClass>
</manifest>
<manifestEntries>
<Built-By>tcp2ivshmem</Built-By>
Expand All @@ -87,9 +68,15 @@ If not, see <https://www.gnu.org/licenses/>.

<dependencies>
<dependency>
<groupId>de.aschuetz</groupId>
<groupId>io.github.alexanderschuetz97</groupId>
<artifactId>ivshmem4j</artifactId>
<version>1.1</version>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Alexander Schütz, 2020
* Copyright Alexander Schütz, 2020-2022
*
* This file is part of tcp2ivshmem.
*
Expand All @@ -17,24 +17,22 @@
* in the COPYING file in top level directory of tcp2ivshmem.
* If not, see <https://www.gnu.org/licenses/>.
*/
package de.aschuetz.tcp2ivshmem;

import de.aschuetz.ivshmem4j.api.SharedMemory;
import de.aschuetz.ivshmem4j.api.SharedMemoryException;
import de.aschuetz.ivshmem4j.common.NativeLibraryLoaderHelper;
import de.aschuetz.ivshmem4j.linux.doorbell.IvshmemLinuxClient;
import de.aschuetz.ivshmem4j.linux.plain.LinuxMappedFileSharedMemory;
import de.aschuetz.ivshmem4j.util.RingBuffer;
import de.aschuetz.ivshmem4j.windows.IvshmemWindowsDevice;
import de.aschuetz.tcp2ivshmem.config.Configuration;
import de.aschuetz.tcp2ivshmem.config.Forwarding;
import de.aschuetz.tcp2ivshmem.config.OS;
import de.aschuetz.tcp2ivshmem.ivshmem.Constants;
import de.aschuetz.tcp2ivshmem.ivshmem.IvhsmemMasterBridge;
import de.aschuetz.tcp2ivshmem.ivshmem.IvshmemBridge;
import de.aschuetz.tcp2ivshmem.ivshmem.IvshmemSlaveBridge;
import de.aschuetz.tcp2ivshmem.packets.PacketUtil;
import de.aschuetz.tcp2ivshmem.servers.Socks5Server;
package io.github.alexanderschuetz97.tcp2ivshmem;


import io.github.alexanderschuetz97.tcp2ivshmem.config.Configuration;
import io.github.alexanderschuetz97.tcp2ivshmem.config.Forwarding;
import io.github.alexanderschuetz97.tcp2ivshmem.config.OS;
import io.github.alexanderschuetz97.tcp2ivshmem.ivshmem.Constants;
import io.github.alexanderschuetz97.tcp2ivshmem.ivshmem.IvhsmemMasterBridge;
import io.github.alexanderschuetz97.tcp2ivshmem.ivshmem.IvshmemBridge;
import io.github.alexanderschuetz97.tcp2ivshmem.ivshmem.IvshmemSlaveBridge;
import io.github.alexanderschuetz97.tcp2ivshmem.ivshmem.RingBuffer;
import io.github.alexanderschuetz97.ivshmem4j.api.Ivshmem;
import io.github.alexanderschuetz97.ivshmem4j.api.IvshmemMemory;
import io.github.alexanderschuetz97.ivshmem4j.api.WindowsIvshmemPCIDevice;
import io.github.alexanderschuetz97.nativeutils.api.NativeMemory;
import io.github.alexanderschuetz97.nativeutils.api.NativeUtils;

import java.io.IOException;
import java.net.ServerSocket;
Expand All @@ -46,7 +44,9 @@ public class Main {

public static Configuration config;

public static SharedMemory memory;
public static IvshmemMemory shmemory;

public static NativeMemory memory;

public static boolean useInterrupts = false;

Expand Down Expand Up @@ -92,75 +92,65 @@ public static void printUsageAndExit() {
System.exit(-1);
}

public static void setupJNI() {
try {
NativeLibraryLoaderHelper.loadNativeLibraries();
} catch (LinkageError err) {
System.out.println("Unable to load native libraries for Ivshmem4j.");
err.printStackTrace();
System.exit(-1);
}
}

public static void listWindowsDevices() {
if (OS.detect() != OS.WINDOWS) {
System.out.println("-ls is only supported on windows.");
System.exit(-1);
}

Collection<IvshmemWindowsDevice> devices = null;
Collection<WindowsIvshmemPCIDevice> devices = null;
try {
devices = IvshmemWindowsDevice.getSharedMemoryDevices();
} catch (SharedMemoryException e) {
devices = Ivshmem.windowsListPCI();
} catch (Exception e) {
System.out.println("Couldn't enumerate ivhshmem pci devices");
e.printStackTrace();
System.exit(-1);
}

System.out.println("Available devices: " + devices.size());
for (IvshmemWindowsDevice device : devices) {
System.out.println("Size: " + device.getSharedMemorySize() + " bytes Device: " + device.getNameAsString());
for (WindowsIvshmemPCIDevice device : devices) {
System.out.println("Size: " + device.getSize() + " bytes Device: " + device.getName());
}

System.out.println();
System.out.println("You may use either the size of the device or a part of the device path (uses \"contains\") to identify the device");

}

public static void createMemory() throws SharedMemoryException {
public static void createMemory() {
if (config.getOperatingSystem() == OS.LINUX) {
if (Boolean.TRUE.equals(config.getLinuxIsPlain())) {
if (config.getSize() == null) {
memory = LinuxMappedFileSharedMemory.open(config.getDevice());
shmemory = Ivshmem.plain(config.getDevice());
return;
}

memory = LinuxMappedFileSharedMemory.createOrOpen(config.getDevice(), config.getSize());
shmemory = Ivshmem.plain(config.getDevice(), config.getSize());
return;
}

memory = IvshmemLinuxClient.connect(config.getDevice());
shmemory = Ivshmem.doorbell(config.getDevice(), 5000, Main.ex);
return;
}

Collection<IvshmemWindowsDevice> devices = IvshmemWindowsDevice.getSharedMemoryDevices();
Collection<WindowsIvshmemPCIDevice> devices = Ivshmem.windowsListPCI();

for (IvshmemWindowsDevice device : devices) {
for (WindowsIvshmemPCIDevice device : devices) {
if (config.getDevice() == null) {
memory = device.open();
shmemory = Ivshmem.windowsPCI(device, Main.ex);
return;
}

if (config.getDevice().equals(String.valueOf(device.getSharedMemorySize()))) {
System.out.println("Will open device " + device.getNameAsString());
memory = device.open();
if (config.getDevice().equals(String.valueOf(device.getSize()))) {
System.out.println("Will open device " + device.getName());
shmemory = Ivshmem.windowsPCI(device, Main.ex);

return;
}

if(device.isNameValid() && device.getNameAsString().contains(config.getDevice())) {
System.out.println("Will open device " + device.getNameAsString());
memory = device.open();
if(device.getName() != null && device.getName().contains(config.getDevice())) {
System.out.println("Will open device " + device.getName());
shmemory = Ivshmem.windowsPCI(device, Main.ex);
return;
}

Expand All @@ -187,7 +177,11 @@ public static void main(String[] args) {
return;
}

setupJNI();
if (!NativeUtils.isLinux() && !NativeUtils.isWindows()) {
System.out.println("Unsupported OS or CPU architecture.");
System.exit(-1);
return;
}

if (args.length == 1 && "-ls".equalsIgnoreCase(args[0])) {
listWindowsDevices();
Expand All @@ -214,7 +208,8 @@ public static void main(String[] args) {

try {
createMemory();
} catch (SharedMemoryException e) {
memory = shmemory.getMemory();
} catch (Exception e) {
System.out.println("Error creating shared memory.");
e.printStackTrace();
System.exit(-1);
Expand All @@ -227,12 +222,12 @@ public static void main(String[] args) {

System.out.println("Using shared memory: " + memory);

if (memory.getSharedMemorySize() < Constants.MIN_REQUIRED_MEMORY_SIZE) {
if (memory.size() < Constants.MIN_REQUIRED_MEMORY_SIZE) {
System.out.println("Shared memory is too small.");
System.exit(-1);
}

long tempRes = memory.getSharedMemorySize() - Constants.MEMORY_OVERHEAD - (2* RingBuffer.OVERHEAD);
long tempRes = memory.size() - Constants.MEMORY_OVERHEAD - (2* RingBuffer.OVERHEAD);
tempRes -= tempRes % 32;
ringBufferSize = tempRes / 2;
if (ringBufferSize <= 0) {
Expand All @@ -241,23 +236,22 @@ public static void main(String[] args) {
}

clientToServerOffset = serverToClientOffset + RingBuffer.OVERHEAD + ringBufferSize;
memory.startNecessaryThreads(Main.ex);

if (!memory.supportsInterrupts() && Boolean.TRUE.equals(config.useInterrupts())) {
if (!shmemory.supportsInterrupts() && Boolean.TRUE.equals(config.useInterrupts())) {
System.out.println("Interrupts not supported by shared memory.");
System.exit(-1);
return;
}

if (memory.supportsInterrupts() && !Boolean.FALSE.equals(config.useInterrupts())) {
if (shmemory.supportsInterrupts() && !Boolean.FALSE.equals(config.useInterrupts())) {
useInterrupts = true;
}

System.out.println("Using ring buffer size: " + ringBufferSize + ".");


if (useInterrupts) {
System.out.println("Own Peer ID " + memory.getOwnPeerID());
System.out.println("Own Peer ID " + shmemory.getOwnPeerID());
System.out.println("Will use interrupts if other side supports them too.");
} else {
System.out.println("We do not support interrupts.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Alexander Schütz, 2020
* Copyright Alexander Schütz, 2020-2022
*
* This file is part of tcp2ivshmem.
*
Expand All @@ -17,11 +17,10 @@
* in the COPYING file in top level directory of tcp2ivshmem.
* If not, see <https://www.gnu.org/licenses/>.
*/
package de.aschuetz.tcp2ivshmem.config;
package io.github.alexanderschuetz97.tcp2ivshmem.config;

import de.aschuetz.ivshmem4j.api.SharedMemoryException;
import de.aschuetz.ivshmem4j.windows.IvshmemWindowsDevice;
import de.aschuetz.tcp2ivshmem.ivshmem.Constants;
import io.github.alexanderschuetz97.tcp2ivshmem.ivshmem.Constants;
import io.github.alexanderschuetz97.ivshmem4j.api.Ivshmem;

import java.util.*;

Expand Down Expand Up @@ -312,11 +311,11 @@ private void validateArgs() throws IllegalArgumentException {
errors.add("Device is missing. Use -d.");
} else {
try {
if (IvshmemWindowsDevice.getSharedMemoryDevices().size() > 1) {
if (Ivshmem.windowsListPCI().size() > 1) {
errors.add("Device is missing and there is more than 1 PCI Device attached to this vm.");
}
} catch (SharedMemoryException e) {
errors.add("Couldn't enumerate ivhshmem pci devices err:" + e.getMessage());
} catch (Exception e) {
errors.add("Couldn't enumerate ivhshmem pci devices err:" + e.getClass().getName() + " " +e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Alexander Schütz, 2020
* Copyright Alexander Schütz, 2020-2022
*
* This file is part of tcp2ivshmem.
*
Expand All @@ -17,7 +17,7 @@
* in the COPYING file in top level directory of tcp2ivshmem.
* If not, see <https://www.gnu.org/licenses/>.
*/
package de.aschuetz.tcp2ivshmem.config;
package io.github.alexanderschuetz97.tcp2ivshmem.config;

public class Forwarding {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Alexander Schütz, 2020
* Copyright Alexander Schütz, 2020-2022
*
* This file is part of tcp2ivshmem.
*
Expand All @@ -17,7 +17,7 @@
* in the COPYING file in top level directory of tcp2ivshmem.
* If not, see <https://www.gnu.org/licenses/>.
*/
package de.aschuetz.tcp2ivshmem.config;
package io.github.alexanderschuetz97.tcp2ivshmem.config;

public enum OS {
WINDOWS, LINUX, OTHER;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Alexander Schütz, 2020
* Copyright Alexander Schütz, 2020-2022
*
* This file is part of tcp2ivshmem.
*
Expand All @@ -17,12 +17,12 @@
* in the COPYING file in top level directory of tcp2ivshmem.
* If not, see <https://www.gnu.org/licenses/>.
*/
package de.aschuetz.tcp2ivshmem.ivshmem;
package io.github.alexanderschuetz97.tcp2ivshmem.ivshmem;

import de.aschuetz.tcp2ivshmem.Main;
import io.github.alexanderschuetz97.tcp2ivshmem.Main;

import static de.aschuetz.tcp2ivshmem.ivshmem.Constants.ADDRESS_STATE;
import static de.aschuetz.tcp2ivshmem.ivshmem.Constants.ADDRESS_WATCHDOG;
import static io.github.alexanderschuetz97.tcp2ivshmem.ivshmem.Constants.ADDRESS_STATE;
import static io.github.alexanderschuetz97.tcp2ivshmem.ivshmem.Constants.ADDRESS_WATCHDOG;

public class ClearWatchdogShutdownHook extends Thread {

Expand Down
Loading

0 comments on commit a3fbede

Please sign in to comment.