Exemplo n.º 1
0
void find_user(
        n::connection_manager& con, 
        sc::session_library& sec, 
        const n::endpoint& ep, 
        const ms::greet_find_request& r, 
        user_info_map& users)
{
    //find from user
    auto fup = users.find(r.from_id());
    if(fup == users.end()) return;

    //find search user
    auto up = users.find(r.search_id());
    if(up == users.end()) return;

    auto& f = fup->second;
    if(con.is_disconnected(n::make_address_str(ep))) return;
    f.ep = ep;

    auto& i = up->second;

    LOG << "found match " << f.id << " " << f.ext.ip << ":" << f.ext.port << " <==> " <<  i.id << " " << i.ext.ip << ":" << i.ext.port << std::endl;

    //send response to both clients
    ms::greet_find_response fr{true, i.id, i.local,  i.ext};
    send_response(con, sec, fr, f);

    ms::greet_find_response ir{true, f.id, f.local,  f.ext};
    send_response(con, sec, ir, i);
}
Exemplo n.º 2
0
bool is_disconnected_and_cleanup(const std::string& addr,
        n::connection_manager& con, 
        sc::encrypted_channels& sec) 
{
    if(con.is_disconnected(addr))
    {
        sec.remove_channel(addr);
        return true;
    }
    return false;
}
Exemplo n.º 3
0
void send_response(
        n::connection_manager& con, 
        sc::encrypted_channels& sec, 
        const ms::greet_find_response& r, 
        const user_info& u)
{
    REQUIRE_FALSE(u.tcp_ep.protocol.empty());
    m::message m = r;

    auto address = n::make_address_str(u.tcp_ep); 
    m.meta.to = {address, u.response_service_address};

    //encrypt using public key
    auto data = u::encode(m);
    data = u::compress(data);
    data = sec.encrypt(address, data);
    con.send(address, data);
}
Exemplo n.º 4
0
void send_response(
        n::connection_manager& con, 
        sc::session_library& sec, 
        const ms::greet_find_response& r, 
        const user_info& u)
{
    m::message m = r;

    auto address = n::make_address_str(u.ep); 
    m.meta.to = {address, u.response_service_address};

    LOG << "sending reply to " << address << std::endl;

    //encrypt using public key
    auto data = u::encode(m);
    data = sec.encrypt(address, data);
    con.send(address, data);
}
Exemplo n.º 5
0
void send_pub_key(
        n::connection_manager& con, 
        sc::encrypted_channels& sec, 
        const n::endpoint& ep,  
        const ms::greet_key_request& req, 
        user_info_map& users, 
        const sc::private_key& pkey)
{
    ms::greet_key_response rep{pkey.public_key()};
    m::message m = rep;

    auto address = n::make_address_str(ep); 
    m.meta.to = {address, req.response_service_address()};

    //send plaintext
    auto data = u::encode(m);
    data = u::compress(data);
    data = sec.encrypt(address, data);
    con.send(address, data);
}
Exemplo n.º 6
0
void register_user(
        n::connection_manager& con, 
        sc::session_library& sec, 
        const n::endpoint& ep, 
        const ms::greet_register& r, 
        user_info_map& m)
{
    auto address = n::make_address_str(ep);
    if(r.id().empty()) return;
    if(con.is_disconnected(address)) return;

    //use user specified ip, otherwise use socket ip
    ms::greet_endpoint local = r.local();
    ms::greet_endpoint ext = {ep.address, ep.port};

    user_info i = {r.id(), local, ext, r.response_service_address(), ep};
    m[i.id] = i;

    sec.create_session(address, r.pub_key());

    LOG << "registered " << i.id << " " << i.ext.ip << ":" << i.ext.port << std::endl;
}