summaryrefslogtreecommitdiff
path: root/ecp/server/vlink.c
blob: 17c84ad4e5c16ccad78639a4c2fd911af5180716 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <ecp/core.h>
#include <ecp/ht.h>
#include <ecp/tm.h>
#include <ecp/vconn/vconn.h>
#include <ecp/dir/dir.h>

#include "dir.h"
#include "vlink.h"
#include "ht.h"

#include "server.h"

static ecp_ht_table_t *vlink_conn = NULL;
static pthread_rwlock_t vlink_conn_rwlock;
static pthread_t vlink_keyx_thd;

static SRVConfig *srv_config;

#define MAX(X, Y)   (((X) > (Y)) ? (X) : (Y))

int vlink_open_conn(ECPSocket *sock, ECPNode *node, ECPConnection **_conn) {
    ECPConnection *conn;
    int rv;

    *_conn = NULL;
    conn = malloc(sizeof(ECPConnection));
    if (conn == NULL) return ECP_ERR_ALLOC;

    ecp_vlink_init(conn, sock);
    rv = ecp_conn_open(conn, node);
    if (rv) return rv;

    *_conn = conn;
    return ECP_OK;
}

void vlink_new_node(ECPSocket *sock, ECPDirItem *dir_item) {
    ECPConnection *conn;
    int rv;

    if (!(dir_item->roles & ECP_ROLE_VCONN) || (memcmp(&dir_item->node.key_perma.public, &srv_config->key_perma.public, sizeof(srv_config->key_perma.public)) == 0)) return;

    pthread_rwlock_rdlock(&vlink_conn_rwlock);
    conn = ecp_ht_search(vlink_conn, &dir_item->node.key_perma.public);
    pthread_rwlock_unlock(&vlink_conn_rwlock);

    if (conn) return;

    rv = vlink_open_conn(sock, &dir_item->node, &conn);
    if (rv) {
        LOG(LOG_ERR, "vlink_new_node: open conn err:%d\n", rv);
        return;
    }

    pthread_rwlock_wrlock(&vlink_conn_rwlock);
    rv = ht_insert_conn(vlink_conn, conn);
    pthread_rwlock_unlock(&vlink_conn_rwlock);

    if (rv) {
        LOG(LOG_ERR, "vlink_new_node: ins conn err:%d\n", rv);
        ecp_conn_close(conn);
        return;
    }

    LOG(LOG_DEBUG, "vlink_new_node: new connection\n");
}

void vlink_del_node(ECPDirItem *dir_item) {
    ECPConnection *conn;

    if (!(dir_item->roles & ECP_ROLE_VCONN) || (memcmp(&dir_item->node.key_perma.public, &srv_config->key_perma.public, sizeof(srv_config->key_perma.public)) == 0)) return;

    pthread_rwlock_rdlock(&vlink_conn_rwlock);
    conn = ecp_ht_search(vlink_conn, &dir_item->node.key_perma.public);
    if (conn) ecp_conn_set_uflags(conn, VLINK_UFLAG_DISCONNECT);
    pthread_rwlock_unlock(&vlink_conn_rwlock);
}

void vlink_keyx(ECPSocket *sock, int keyx_period) {
    struct hashtable_itr itr;
    ECPConnection *conn;
    ecp_ecdh_public_t *conn_next;
    ECPConnection *keyx_conn[MAX_NODE_ANNOUNCE];
    unsigned int ht_count;
    int keyx_cnt;
    int i;
    int rv;

    conn_next = NULL;
    do {
        uint32_t delay;
        uint32_t rnd;

        keyx_cnt = 0;
        pthread_rwlock_rdlock(&vlink_conn_rwlock);

        ht_count = ecp_ht_count(vlink_conn);
        if (ht_count > 0) {
            ecp_ht_itr_create(&itr, vlink_conn);
            if (conn_next) {
                rv = ecp_ht_itr_search(&itr, conn_next);
                if (rv) {
                    LOG(LOG_ERR, "vlink_keyx: itr search err:%d\n", rv);
                    break;
                }
                conn_next = NULL;
            }
            do {
                conn = ecp_ht_itr_value(&itr);

                if (keyx_cnt < MAX_NODE_ANNOUNCE) {
                    keyx_conn[keyx_cnt] = conn;
                    keyx_cnt++;
                } else {
                    conn_next = ecp_ht_itr_key(&itr);
                    break;
                }

                rv = ecp_ht_itr_advance(&itr);
            } while (rv == ECP_OK);
        }

        /* no need to copy conn_next key, since this is the only thread that can remove connection */
        pthread_rwlock_unlock(&vlink_conn_rwlock);

        /* we are counting delay for total hashtable entries; delay for absolute max ht_count (256 msgs with max load, keyx period of 600s) is ~70ms */
        delay = keyx_period * 1000000 / MAX(ht_count, 100);
        for (i=0; i<keyx_cnt; i++) {
            int is_d, is_reg, is_open, is_z;
            ecp_sts_t now;

            conn = keyx_conn[i];

            is_d = is_reg = is_open = is_z = 0;
            now = ecp_tm_get_s();

            ecp_conn_lock(conn);
            is_d = _ecp_conn_test_uflags(conn, VLINK_UFLAG_DISCONNECT);
            is_reg = _ecp_conn_is_reg(conn);
            if (is_reg) is_open = _ecp_conn_is_open(conn);
            if (is_open) is_z = _ecp_conn_is_zombie(conn, now, KEYX_PERIOD * 3);
            ecp_conn_unlock(conn);

            if (is_d) {
                ECPConnection *_conn = NULL;

                LOG(LOG_DEBUG, "vlink_keyx: disconnect\n");
                /* close all inbound connections */
                while ((_conn = ecp_sock_gct_search(conn->sock, &conn->remote.key_perma.public))) {
                    ecp_conn_close(_conn);
                    ecp_conn_refcount_dec(_conn);
                }

                pthread_rwlock_wrlock(&vlink_conn_rwlock);
                ht_remove_conn(vlink_conn, conn);
                pthread_rwlock_unlock(&vlink_conn_rwlock);

                ecp_conn_close(conn);
            } else if (!is_reg || is_z) {
                ECPConnection *_conn = NULL;

                LOG(LOG_DEBUG, "vlink_keyx: reconnect\n");
                rv = vlink_open_conn(conn->sock, &conn->remote, &_conn);
                if (rv) {
                    LOG(LOG_ERR, "vlink_keyx: conn open err:%d\n", rv);
                    continue;
                }

                pthread_rwlock_wrlock(&vlink_conn_rwlock);
                ht_remove_conn(vlink_conn, conn);
                rv = ht_insert_conn(vlink_conn, _conn);
                pthread_rwlock_unlock(&vlink_conn_rwlock);

                ecp_conn_close(conn);
                if (rv) {
                    LOG(LOG_ERR, "vlink_keyx: conn insert err:%d\n", rv);
                    ecp_conn_close(_conn);
                }
            } else if (is_open) {
                ssize_t _rv;

                _rv = ecp_send_keyx_req(conn, 1);
                if (_rv < 0) LOG(LOG_ERR, "vlink_keyx: send keyx req err:%ld\n", _rv);
            }

            if (delay) {
                /* randomize over delay / 2 and delay + delay / 2 */
                rnd = arc4random_uniform(delay + 1);
                usleep(delay / 2 + rnd);
            }
        }

    } while (conn_next);

    LOG(LOG_DEBUG, "vlink_keyx: connection count:%u\n", ht_count);
}

static void *_vlink_keyx(void *arg) {
    ECPSocket *sock = arg;
    uint32_t rnd;
    time_t tv_sec;
    int delay;

    while (1) {
        tv_sec = time(NULL);

        vlink_keyx(sock, KEYX_PERIOD);

        /* resolution is 1s */
        delay = KEYX_PERIOD - (time(NULL) - tv_sec);
        if (delay > 0) {
            rnd = arc4random_uniform(delay + 1);
            sleep(delay / 2 + rnd);
        }
    }

    return NULL;
}


int vlink_start_keyx(ECPSocket *sock) {
    int rv;

    rv = pthread_create(&vlink_keyx_thd, NULL, _vlink_keyx, sock);
    if (rv) return ECP_ERR;
    return ECP_OK;
}

int vlink_init(ECPSocket *sock) {
    ECPContext *ctx = sock->ctx;
    int rv;

    srv_config = srv_get_config();

    rv = pthread_rwlock_init(&vlink_conn_rwlock, NULL);
    if (rv) return ECP_ERR;

    vlink_conn = ecp_ht_create_keys();
    if (vlink_conn == NULL) {
        pthread_rwlock_destroy(&vlink_conn_rwlock);
        return ECP_ERR_ALLOC;
    }

    return ECP_OK;
}