-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
835 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
.DS_Store | ||
/build | ||
*.iml | ||
*.apk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...src/main/java/ru/esmukov/kpfu/lightningrodandroidvpnpoc/packetfilter/ByteBufferUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ru.esmukov.kpfu.lightningrodandroidvpnpoc.packetfilter; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Created by kostya on 16/12/2016. | ||
*/ | ||
|
||
public class ByteBufferUtils { | ||
|
||
public static void moveLeft(ByteBuffer buffer, int shiftBytes) { | ||
int length = buffer.limit(); | ||
|
||
for (int i = shiftBytes; i < length; i++) { | ||
buffer.put(i - shiftBytes, buffer.get(i)); | ||
} | ||
buffer.position(0); | ||
buffer.limit(length - shiftBytes); | ||
} | ||
|
||
public static void moveRight(ByteBuffer buffer, int shiftBytes) { | ||
int length = buffer.limit(); | ||
|
||
buffer.limit(length + shiftBytes); | ||
for (int i = length - 1; i >= 0; i--) { | ||
buffer.put(i + shiftBytes, buffer.get(i)); | ||
} | ||
buffer.position(0); | ||
} | ||
|
||
public static void copy(ByteBuffer from, ByteBuffer to) { | ||
to.limit(from.limit() + 1); // java.nio.BufferOverflowException | ||
to.put(from.array(), 0, from.limit()); | ||
to.limit(from.limit()); | ||
to.position(0); | ||
} | ||
|
||
public static void put6bytes(ByteBuffer byteBuffer, long l) { | ||
// first byte | ||
byteBuffer.put((byte) ((l >>> (8 * 5)) & 0xFF)); | ||
// second byte | ||
byteBuffer.put((byte) ((l >>> (8 * 4)) & 0xFF)); | ||
// rest 4 bytes | ||
byteBuffer.putInt((int) (l & 0xFFFFFFFFL)); | ||
} | ||
|
||
public static long get6bytes(ByteBuffer byteBuffer) { | ||
long l = 0; | ||
l |= (long) (byteBuffer.get() & 0xFF) << (8 * 5); | ||
l |= (long) (byteBuffer.get() & 0xFF) << (8 * 4); | ||
l |= byteBuffer.getInt() & 0xFFFFFFFFL; | ||
return l; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/ru/esmukov/kpfu/lightningrodandroidvpnpoc/packetfilter/PacketFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ru.esmukov.kpfu.lightningrodandroidvpnpoc.packetfilter; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Created by kostya on 16/12/2016. | ||
*/ | ||
|
||
/** | ||
* Filter (and change, if needed) a packet between remote side and a local VpnService TUN. | ||
*/ | ||
public interface PacketFilter { | ||
/** | ||
* Outgoing L3 IP4 packet | ||
* | ||
* @param ip4Packet Local IP4 packet. This method should convert this buffer | ||
* to a format expected by the remote side | ||
* @return Should the packet be sent? Drop it otherwise. | ||
*/ | ||
boolean fromLocalToRemote(ByteBuffer ip4Packet); | ||
|
||
/** | ||
* Incoming packet | ||
* | ||
* @param remotePacket This method should convert this raw packet received | ||
* from the remote side to a local IP4 packet. | ||
* @return Should the packet be accepted? Drop it otherwise. | ||
*/ | ||
boolean fromRemoteToLocal(ByteBuffer remotePacket); | ||
|
||
/** | ||
* Initiate custom outgoing packets | ||
* | ||
* @param remotePacket | ||
* @return Is a packet to be sent put to the `remotePacket` buffer | ||
*/ | ||
boolean nextCustomPacketToRemote(ByteBuffer remotePacket); | ||
} |
76 changes: 76 additions & 0 deletions
76
app/src/main/java/ru/esmukov/kpfu/lightningrodandroidvpnpoc/packetfilter/PacketInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package ru.esmukov.kpfu.lightningrodandroidvpnpoc.packetfilter; | ||
|
||
import android.util.Log; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Created by kostya on 06/01/2017. | ||
*/ | ||
|
||
public class PacketInfo { | ||
private static final String TAG = "PacketInfo"; | ||
|
||
public static void prepend(ByteBuffer packet, int etherType) { | ||
// todo looks like we don't need to ever set the TUN_PKT_STRIP flag, right? | ||
// see: | ||
// static ssize_t tun_put_user(struct tun_struct *tun, | ||
// struct tun_file *tfile, | ||
// struct sk_buff *skb, | ||
// struct iov_iter *iter) | ||
|
||
// http://lxr.free-electrons.com/source/include/uapi/linux/if_ether.h | ||
short flags = 0; | ||
|
||
ByteBufferUtils.moveRight(packet, 4); | ||
|
||
packet.putShort(0, flags); | ||
|
||
// Ethertype as per http://lxr.free-electrons.com/source/include/uapi/linux/if_ether.h | ||
packet.putShort(2, (short) etherType); | ||
} | ||
|
||
public static PacketInfoHeader strip(ByteBuffer remotePacket) throws Exception { | ||
int length = remotePacket.limit(); | ||
|
||
if (length < 4) { | ||
throw new Exception("packet with invalid length (<4)"); | ||
} | ||
|
||
/* | ||
struct tun_pi { | ||
__u16 flags; | ||
__be16 proto; | ||
}; | ||
*/ | ||
|
||
int flags = ((remotePacket.get(0) & 0xFF) << 8) | (remotePacket.get(1) & 0xFF); | ||
int proto = ((remotePacket.get(2) & 0xFF) << 8) | (remotePacket.get(3) & 0xFF); | ||
|
||
if (flags != 0) { | ||
Log.w(TAG, "received non-zero flags: " + flags); | ||
} | ||
|
||
// todo ?? check PI protocol as per | ||
// static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, | ||
// void *msg_control, struct iov_iter *from, | ||
// int noblock) | ||
|
||
ByteBufferUtils.moveLeft(remotePacket, 4); | ||
|
||
return new PacketInfoHeader(proto); | ||
} | ||
|
||
public static class PacketInfoHeader { | ||
private int mProto; | ||
|
||
private PacketInfoHeader(int mProto) { | ||
this.mProto = mProto; | ||
} | ||
|
||
public int getProto() { | ||
return mProto; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.