summaryrefslogtreecommitdiff
path: root/src/main/java/com/lumaserv/bgp/BGPServer.java
blob: 63c07654df4b54520790aa09ebc33173c01415b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.lumaserv.bgp;

import com.lumaserv.bgp.protocol.BGPPacket;
import com.lumaserv.bgp.protocol.message.BGPOpen;
import lombok.Getter;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class BGPServer implements Runnable {

    final ServerSocket serverSocket;
    @Getter
    final List<BGPSessionConfiguration> sessionConfigurations = new ArrayList<>();

    public BGPServer() throws IOException {
        this(179);
    }

    public BGPServer(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }

    private static boolean checkEqual(byte[] a, byte[] b) {
        if(a == b)
            return true;
        if(a == null || b == null)
            return false;
        if(a.length != b.length)
            return false;
        for(int i=0; i<a.length; i++) {
            if(a[i] != b[i])
                return false;
        }
        return true;
    }

    public void run() {
        while (true) {
            try {
                Socket socket = serverSocket.accept();
                BGPPacket packet = BGPPacket.read(socket.getInputStream());
                if(packet.getType() != BGPPacket.Type.OPEN)
                    continue;
                BGPOpen request = (BGPOpen)packet.getMessage();
                BGPSessionConfiguration config = sessionConfigurations.stream()
                        .filter(c -> c.getRemoteAs() == request.getAsn())
                        .findFirst()
                        .orElse(null);
                if(config == null)
                    continue;
                BGPSession session = new BGPSession(socket, config);
                config.getListener().onOpen(session);
                BGPOpen response = new BGPOpen()
                        .setAsn(config.getLocalAs())
                        .setHoldTime(request.getHoldTime())
                        .setVersion(request.getVersion())
                        .setIdentifier(config.getLocalIdentifier());
                try {
                    socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(response).build());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                session.keepAlive();
                new Thread(session).start();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public boolean connect(BGPSessionConfiguration config, String host) throws IOException {
        return connect(config, host, 179);
    }

    public boolean connect(BGPSessionConfiguration config, String host, int port) throws IOException {
        try {
            byte version = 4;
            int holdTime = 90;

            Socket socket = new Socket(host, port);
            BGPOpen request = new BGPOpen()
                    .setAsn(config.getLocalAs())
                    .setHoldTime(holdTime)
                    .setVersion(version)
                    .setIdentifier(config.getLocalIdentifier());
            socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(request).build());

System.out.println("Sent open");


            BGPPacket packet = BGPPacket.read(socket.getInputStream());
System.out.println("Received packet");
            if(packet.getType() != BGPPacket.Type.OPEN) {
                // TODO close
System.out.println("Bad open");
                return false;
            }
System.out.println("Received open");
            BGPOpen response = (BGPOpen)packet.getMessage();
            if(config.getRemoteAs() != response.getAsn()) {
                // TODO shutdown, close
System.out.println("Bad asn:" + config.getRemoteAs() + "!=" + response.getAsn());
                return false;
            }

            BGPSession session = new BGPSession(socket, config);
            config.getListener().onOpen(session);

            session.keepAlive();
System.out.println("Sent keepalive");

            new Thread(session).start();
            return true;
        } catch (IOException ex) {
            ex.printStackTrace();
            throw(ex);
        }
    }
}