diff options
Diffstat (limited to 'src/main/java')
10 files changed, 162 insertions, 75 deletions
diff --git a/src/main/java/com/lumaserv/bgp/BGPServer.java b/src/main/java/com/lumaserv/bgp/BGPServer.java index 27019b6..63c0765 100644 --- a/src/main/java/com/lumaserv/bgp/BGPServer.java +++ b/src/main/java/com/lumaserv/bgp/BGPServer.java @@ -45,7 +45,7 @@ public class BGPServer implements Runnable { BGPPacket packet = BGPPacket.read(socket.getInputStream()); if(packet.getType() != BGPPacket.Type.OPEN) continue; - BGPOpen request = new BGPOpen(packet.getMessage()); + BGPOpen request = (BGPOpen)packet.getMessage(); BGPSessionConfiguration config = sessionConfigurations.stream() .filter(c -> c.getRemoteAs() == request.getAsn()) .findFirst() @@ -60,7 +60,7 @@ public class BGPServer implements Runnable { .setVersion(request.getVersion()) .setIdentifier(config.getLocalIdentifier()); try { - socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(response.build()).build()); + socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(response).build()); } catch (IOException e) { e.printStackTrace(); } @@ -87,7 +87,7 @@ public class BGPServer implements Runnable { .setHoldTime(holdTime) .setVersion(version) .setIdentifier(config.getLocalIdentifier()); - socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(request.build()).build()); + socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(request).build()); System.out.println("Sent open"); @@ -100,10 +100,10 @@ System.out.println("Bad open"); return false; } System.out.println("Received open"); - BGPOpen response = new BGPOpen(packet.getMessage()); + BGPOpen response = (BGPOpen)packet.getMessage(); if(config.getRemoteAs() != response.getAsn()) { // TODO shutdown, close -System.out.println("Bad asn"); +System.out.println("Bad asn:" + config.getRemoteAs() + "!=" + response.getAsn()); return false; } diff --git a/src/main/java/com/lumaserv/bgp/BGPSession.java b/src/main/java/com/lumaserv/bgp/BGPSession.java index 3ffadd8..f693f11 100644 --- a/src/main/java/com/lumaserv/bgp/BGPSession.java +++ b/src/main/java/com/lumaserv/bgp/BGPSession.java @@ -27,7 +27,7 @@ public class BGPSession implements Runnable { public void keepAlive() { try { - outputStream.write(new BGPPacket().setType(BGPPacket.Type.KEEPALIVE).setMessage(new byte[0]).build()); + outputStream.write(new BGPPacket().setType(BGPPacket.Type.KEEPALIVE).build()); } catch (IOException e) { e.printStackTrace(); } @@ -43,12 +43,21 @@ public class BGPSession implements Runnable { keepAlive(); break; case UPDATE: { - configuration.getListener().onUpdate(this, new BGPUpdate(packet.getMessage())); + configuration.getListener().onUpdate(this, (BGPUpdate)packet.getMessage()); break; } } } + public void send(BGPPacket packet) { + try { + outputStream.write(packet.build()); + } catch (IOException ex) { + closed = true; + configuration.getListener().onClose(this); + } + } + public void run() { try { while (!closed) { diff --git a/src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java b/src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java index e7cf66b..e0aa3d6 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java +++ b/src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java @@ -1,5 +1,9 @@ package com.lumaserv.bgp.protocol; +import com.lumaserv.bgp.protocol.DataBuilder; +import com.lumaserv.bgp.protocol.message.BGPOpen; +import com.lumaserv.bgp.protocol.message.BGPUpdate; + import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -7,6 +11,8 @@ import lombok.Setter; import java.io.IOException; import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.util.Arrays; @Setter @@ -15,44 +21,58 @@ import java.util.Arrays; public class BGPPacket { Type type; - byte[] message; + DataBuilder message; - public BGPPacket(byte[] packet) { - type = Type.fromValue(packet[18]); - message = new byte[packet.length-19]; - System.arraycopy(packet, 19, message, 0, message.length); + public BGPPacket(ByteBuffer packet) { + type = Type.fromValue(packet.get()); + switch (type) { + case OPEN: + message = new BGPOpen(packet); + break; + case UPDATE: + message = new BGPUpdate(packet); + break; + default: + System.out.println("type: " + type); + break; + } } public byte[] build() { - byte[] packet = new byte[message.length + 19]; - Arrays.fill(packet, 0, 16, (byte) 0xFF); - packet[16] = (byte)(packet.length >> 8); - packet[17] = (byte)(packet.length & 0xFF); - packet[18] = type.getValue(); - System.arraycopy(message, 0, packet, 19, message.length); + ByteBuffer buf = ByteBuffer.allocate(4096).order(ByteOrder.BIG_ENDIAN); // Maximum BGP message size + for (int i=0; i<16; i++) + buf.put((byte) 0xFF); + buf.putShort((short)19); // Length without message payload + buf.put(type.getValue()); + if (message != null) { + message.build(buf); + buf.putShort(16, (short)buf.position()); // Set length + } + byte[] packet = new byte[buf.position()]; + buf.position(0); + buf.get(packet); // TODO is working? return packet; } public static BGPPacket read(InputStream stream) throws IOException { - byte[] packet = new byte[18]; + ByteBuffer buf = ByteBuffer.allocate(4096).order(ByteOrder.BIG_ENDIAN); int value; - for(int i=0; i<packet.length; i++) { + for(int i=0; i<18; i++) { value = stream.read(); if(value == -1) throw new IOException("Unexpected end of stream"); - packet[i] = (byte) value; + buf.put((byte) value); } - int length = ((packet[16] & 0xFF) << 8) | (packet[17] & 0xFF); - byte[] newPacket = new byte[length]; - System.arraycopy(packet, 0, newPacket, 0, packet.length); - packet = newPacket; - for(int i=18; i<packet.length; i++) { + int length = buf.getShort(16); + for(int i=18; i<length; i++) { value = stream.read(); if(value == -1) throw new IOException("Unexpected end of stream"); - packet[i] = (byte) value; + buf.put((byte) value); } - return new BGPPacket(packet); + buf.limit(buf.position()); + buf.position(18); + return new BGPPacket(buf); } @AllArgsConstructor diff --git a/src/main/java/com/lumaserv/bgp/protocol/DataBuilder.java b/src/main/java/com/lumaserv/bgp/protocol/DataBuilder.java new file mode 100644 index 0000000..e21ccec --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/DataBuilder.java @@ -0,0 +1,8 @@ +package com.lumaserv.bgp.protocol; + +import java.nio.ByteBuffer; + +public interface DataBuilder { + + public void build(ByteBuffer b); +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java b/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java index f68677c..fa3ca00 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java +++ b/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java @@ -1,5 +1,6 @@ package com.lumaserv.bgp.protocol; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -7,6 +8,7 @@ import lombok.Setter; import java.net.InetAddress; import java.net.UnknownHostException; +@AllArgsConstructor @NoArgsConstructor @Setter @Getter diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java index b39e473..170acf8 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java @@ -1,5 +1,6 @@ package com.lumaserv.bgp.protocol.attribute; +import lombok.NoArgsConstructor; import lombok.Getter; import lombok.Setter; @@ -7,6 +8,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; +@NoArgsConstructor @Getter @Setter public class ASPathAttribute implements PathAttribute { @@ -30,6 +32,13 @@ public class ASPathAttribute implements PathAttribute { } public void build(ByteBuffer b) { + for (Segment segment: segments) { + b.put(segment.getType()); + b.put((byte)segment.getAsns().size()); + for (Integer asn: segment.getAsns()) { + b.putShort(asn.shortValue()); + } + } } @Getter diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java index c579295..1bf72e4 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java @@ -3,11 +3,13 @@ package com.lumaserv.bgp.protocol.attribute; import java.net.InetAddress; import java.net.UnknownHostException; +import lombok.NoArgsConstructor; import lombok.Getter; import lombok.Setter; import java.nio.ByteBuffer; +@NoArgsConstructor @Getter @Setter public class NextHopAttribute implements PathAttribute { diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java index b8c996d..8cc3474 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java @@ -2,11 +2,14 @@ package com.lumaserv.bgp.protocol.attribute; import java.nio.ByteBuffer; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; import java.util.EnumSet; + +@AllArgsConstructor @Getter @Setter public class OriginAttribute implements PathAttribute { diff --git a/src/main/java/com/lumaserv/bgp/protocol/message/BGPOpen.java b/src/main/java/com/lumaserv/bgp/protocol/message/BGPOpen.java index e83b751..37b791b 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/message/BGPOpen.java +++ b/src/main/java/com/lumaserv/bgp/protocol/message/BGPOpen.java @@ -1,52 +1,54 @@ package com.lumaserv.bgp.protocol.message; +import com.lumaserv.bgp.protocol.DataBuilder; + import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import java.nio.ByteBuffer; + @Getter @Setter @NoArgsConstructor -public class BGPOpen { +public class BGPOpen implements DataBuilder { byte version; int asn; int holdTime; byte[] identifier; - public BGPOpen(byte[] message) { - version = message[0]; - asn = ((message[1] & 0xFF) << 8) | (message[2] & 0xFF); - holdTime = ((message[3] & 0xFF) << 8) | (message[4] & 0xFF); + public BGPOpen(ByteBuffer message) { +System.out.println("BGPOpen pos:" + message.position()); + version = message.get(); + asn = message.getShort() & 0xFFFF; + holdTime = message.getShort() & 0xFFFF; identifier = new byte[4]; +System.out.println("BGPOpen asn:" + asn); + message.get(identifier); } - public byte[] build() { - byte[] message = new byte[5 + identifier.length + 1 + 2 + 2 * 6]; - message[0] = version; - message[1] = (byte) (asn >> 8); - message[2] = (byte) (asn & 0xFF); - message[3] = (byte) (holdTime >> 8); - message[4] = (byte) (holdTime & 0xFF); - System.arraycopy(identifier, 0, message, 5, identifier.length); - message[5 + identifier.length + 0] = 14; // Length (0E) - message[5 + identifier.length + 1] = 2; // Parameter Type: capability (02) - message[5 + identifier.length + 2] = 12; // Length (0C) - - message[5 + identifier.length + 3] = 1; // Type: multiprotocol (01) - message[5 + identifier.length + 4] = 4; // Length (04) - message[5 + identifier.length + 5] = 0; // IPv6 (0002) - message[5 + identifier.length + 6] = 2; // - message[5 + identifier.length + 7] = 0; // Reserved - message[5 + identifier.length + 8] = 1; // Unicast (01) - - message[5 + identifier.length + 9] = 1; // Type: multiprotocol (01) - message[5 + identifier.length +10] = 4; // Length (04) - message[5 + identifier.length +11] = 0; // IPv4 (0001) - message[5 + identifier.length +12] = 1; // - message[5 + identifier.length +13] = 0; // Reserved - message[5 + identifier.length +14] = 1; // Unicast (01) - return message; + public void build(ByteBuffer b) { + b.put(version); + b.put((byte) (asn >> 8)); + b.put((byte) (asn & 0xFF)); + b.put((byte) (holdTime >> 8)); + b.put((byte) (holdTime & 0xFF)); + b.put(identifier); + b.put((byte)14); // Length (0E) + b.put((byte)2); // Parameter Type: capability (02) + b.put((byte)12); // Length (0C) + + b.put((byte)1); // Type: multiprotocol (01) + b.put((byte)4); // Length (04) + b.putShort((short)2); // IPv6 (0002) + b.put((byte)0); // Reserved + b.put((byte)1); // Unicast (01) + + b.put((byte)1); // Type: multiprotocol (01) + b.put((byte)4); // Length (04) + b.putShort((short)1); // IPv4 (0001) + b.put((byte)0); // Reserved + b.put((byte)1); // Unicast (01) } - } diff --git a/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java b/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java index fa23432..ad6c39b 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java +++ b/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java @@ -1,63 +1,95 @@ package com.lumaserv.bgp.protocol.message; +import com.lumaserv.bgp.protocol.DataBuilder; import com.lumaserv.bgp.protocol.IPPrefix; import com.lumaserv.bgp.protocol.attribute.PathAttribute; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; +@NoArgsConstructor @Getter @Setter -public class BGPUpdate { +public class BGPUpdate implements DataBuilder { List<IPPrefix> withdrawnPrefixes = new ArrayList<>(); List<PathAttribute> attributes = new ArrayList<>(); List<IPPrefix> prefixes = new ArrayList<>(); - public BGPUpdate(byte[] message) { - int routesLength = ((message[0] & 0xFF) << 8) | (message[1] & 0xFF); + public BGPUpdate(ByteBuffer message) { + int routesLength = message.getShort() & 0xFFFF; int offset = 2; int offsetOffset = 2; while ((offset - offsetOffset) < routesLength) { IPPrefix prefix = new IPPrefix() - .setLength(message[offset]) + .setLength(message.get()) .setAddress(new byte[4]); offset++; int addressLen = (int) Math.ceil(prefix.getLength() / 8d); - System.arraycopy(message, offset, prefix.getAddress(), 0, addressLen); + message.get(prefix.getAddress(), 0, addressLen); offset += addressLen; withdrawnPrefixes.add(prefix); } - int attributesLength = ((message[offset] & 0xFF) << 8) | (message[offset + 1] & 0xFF); + int attributesLength = message.getShort() & 0xFFFF; offset += 2; offsetOffset = offset; while ((offset - offsetOffset) < attributesLength) { - byte flags = message[offset]; - byte typeCode = message[offset + 1]; - int length = message[offset + 2] & 0xFF; + byte flags = message.get(); + byte typeCode = message.get(); + int length = message.get() & 0xFF; offset += 3; if((flags & 0b0001_0000) > 0) { length <<= 8; - length |= message[offset] & 0xFF; + length |= message.get() & 0xFF; offset++; } byte[] data = new byte[length]; - System.arraycopy(message, offset, data, 0, length); + message.get(data); offset += length; - attributes.add(PathAttribute.from(typeCode, data)); + PathAttribute attr = PathAttribute.from(typeCode, data); + attributes.add(attr); } - while (offset < message.length) { + while (message.hasRemaining()) { IPPrefix prefix = new IPPrefix() - .setLength(message[offset]) + .setLength(message.get()) .setAddress(new byte[4]); offset++; int addressLen = (int) Math.ceil(prefix.getLength() / 8d); - System.arraycopy(message, offset, prefix.getAddress(), 0, addressLen); + message.get(prefix.getAddress(), 0, addressLen); offset += addressLen; prefixes.add(prefix); } } + public void build(ByteBuffer message) { + int first = message.position(); + message.putShort((short)0); // Withdrawn + message.putShort((short)0); // Attributes + // TODO + int pos = message.position(); + for (PathAttribute attr: attributes) { +// FIXME add header + byte flags = 0b0100_0000; // TODO + int start = message.position(); + message.put(flags); + message.put(attr.getTypeCode()); + message.put((byte)0); // Dummy length + attr.build(message); + int length = message.position() - (start + 3); + if (length > 255) + throw new RuntimeException("attribute length > 255"); + message.put(start + 2, (byte)(length & 0xFF)); + } + int attributesLength = message.position() - pos; + message.putShort(first + 2, (short)attributesLength); + for (IPPrefix prefix : prefixes) { + message.put((byte)prefix.getLength()); + int addressLen = (int) Math.ceil(prefix.getLength() / 8d); + message.put(prefix.getAddress(), 0, addressLen); + } + } } |