aboutsummaryrefslogtreecommitdiff
path: root/src/BootstrapConnection.cpp
blob: 7c75cde5581c2ada3d748035f716ab0d34382db0 (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
#include "../include/sibs/BootstrapConnection.hpp"
#include "../include/Log.hpp"
#include <sibs/SafeDeserializer.hpp>

namespace sibs
{
    BootstrapConnection::BootstrapConnection(const Ipv4 &bootstrapAddress)
    {
        serverPeer = connections.connectServer(bootstrapAddress, std::bind(&BootstrapConnection::receiveDataFromServer, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    }
    
    // TODO: This is vulnerable against MitM attack, replace with asymmetric cryptography, get data signed with server private key and verify against known server public key
    void BootstrapConnection::receiveDataFromServer(std::shared_ptr<DirectConnectionPeer> peer, const void *data, const usize size)
    {
        sibs::SafeDeserializer deserializer((const u8*)data, size);
        PubsubKey pubsubKey;
        deserializer.extract(pubsubKey.data.data(), PUBSUB_KEY_LENGTH);
        
        auto listenerFuncIt = listenCallbackFuncs.find(pubsubKey);
        if(listenerFuncIt == listenCallbackFuncs.end())
        {
            Log::debug("BoostrapConnection: No listener found for key XXX, ignoring...");
            return;
        }
        
        while(!deserializer.empty())
        {
            sa_family_t addressFamily = deserializer.extract<u32>();
            if(addressFamily == AF_INET)
            {
                in_addr_t ipv4Address = deserializer.extract<u32>();
                u16 port = deserializer.extract<u32>();
                Ipv4 newPeer;
                newPeer.address.sin_family = addressFamily;
                newPeer.address.sin_addr.s_addr = ipv4Address;
                newPeer.address.sin_port = port;
                memset(newPeer.address.sin_zero, 0, sizeof(newPeer.address.sin_zero));
                // TODO: Move connection to thread and add callback function, just like @receiveData and @send
                connections.connect(newPeer, std::bind(&BootstrapConnection::receiveDataFromPeer, this, listenerFuncIt->second, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
            }
            else
                Log::error("BootstrapConnection: Unknown address family: %d", addressFamily);
        }
    }
    
    void BootstrapConnection::receiveDataFromPeer(BoostrapConnectionListenCallbackFunc listenCallbackFunc, std::shared_ptr<DirectConnectionPeer> peer, const void *data, const usize size)
    {
        if(listenCallbackFunc)
            listenCallbackFunc(data, size);
    }
    
    void BootstrapConnection::listen(const PubsubKey &pubsubKey, BoostrapConnectionListenCallbackFunc callbackFunc)
    {
        if(listenCallbackFuncs.find(pubsubKey) != listenCallbackFuncs.end())
            throw PubsubKeyAlreadyListeningException("");
        listenCallbackFuncs[pubsubKey] = callbackFunc;
        connections.send(serverPeer, std::make_shared<std::vector<u8>>(pubsubKey.data.data(), pubsubKey.data.data()));
    }
}