summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/pol/flat.c
blob: 4946d538abfff932fe315612572680870152e100 (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
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Ouroboros - Copyright (C) 2016
 *
 * Policy for flat addresses in a distributed way
 *
 *    Sander Vrijders <sander.vrijders@intec.ugent.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define OUROBOROS_PREFIX "flat-addr-auth"

#include <ouroboros/config.h>
#include <ouroboros/logs.h>
#include <ouroboros/errno.h>
#include <ouroboros/time_utils.h>

#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>

#include "shm_pci.h"
#include "ribmgr.h"
#include "ro.h"

#define POL_RO_ROOT "/flat_addr"

#define TIMEOUT  100 /* ms */
#define STR_SIZE 100

#define FLAT_ADDR_REQ   0
#define FLAT_ADDR_REPLY 1

struct flat_addr_msg {
        uint8_t  code;
        uint64_t addr;
};

struct {
        int      sid;
        uint64_t addr;
        bool     addr_in_use;

        pthread_cond_t  cond;
        pthread_mutex_t lock;
} flat;

static char * my_name(void)
{
        char * name;
        char addr_name[100];

        name = malloc(STR_SIZE);
        if (name == NULL)
                return NULL;

        sprintf(addr_name, "%lu", (unsigned long) flat.addr);
        strcpy(name, POL_RO_ROOT);
        strcat(name, "/");
        strcat(name, addr_name);

        return name;
}

/* FIXME: We should return void */
static int ro_created(const char * name,
                      uint8_t *    data,
                      size_t       len)
{
        struct flat_addr_msg * msg;

        assert(name);
        assert(data);
        assert(len >= sizeof(*msg));

        msg = (struct flat_addr_msg *) data;
        if (msg->code == FLAT_ADDR_REQ &&
            msg->addr == flat.addr) {
                msg->code = FLAT_ADDR_REPLY;
                ro_write(name, data, len);
        }

        return 0;
}

static int ro_updated(const char * name,
                      uint8_t *    data,
                      size_t       len)
{
        struct flat_addr_msg * msg;
        char * ro_name;

        assert(name);
        assert(data);
        assert(len >= sizeof(*msg));

        ro_name = my_name();
        if (ro_name == NULL)
                return -1;

        msg = (struct flat_addr_msg *) data;
        if (msg->code == FLAT_ADDR_REPLY &&
            strcmp(name, ro_name) == 0) {
                pthread_mutex_lock(&flat.lock);
                flat.addr_in_use = true;
                pthread_cond_broadcast(&flat.cond);
                pthread_mutex_unlock(&flat.lock);
        }

        free(data);
        free(ro_name);

        return 0;
}

static struct ro_sub_ops flat_sub_ops = {
        .ro_created = ro_created,
        .ro_updated = ro_updated,
        .ro_deleted = NULL
};

int flat_init(void)
{
        struct ro_props * props;
        pthread_condattr_t cattr;

        srand(time(NULL));
        flat.addr_in_use = false;

        props = malloc(sizeof(*props));
        if (props == NULL)
                return -ENOMEM;

        pthread_mutex_init(&flat.lock, NULL);
        pthread_condattr_init(&cattr);
#ifndef __APPLE__
        pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
#endif
        pthread_cond_init(&flat.cond, &cattr);

        flat.sid = ro_subscribe(POL_RO_ROOT,
                                &flat_sub_ops);
        if (flat.sid < 0) {
                LOG_ERR("Could not subscribe to RIB.");
                pthread_cond_destroy(&flat.cond);
                pthread_mutex_destroy(&flat.lock);
                free(props);
                return -1;
        }

        props->enrol_sync = false;
        props->recv_set = NO_SYNC;
        props->expiry.tv_sec = 0;
        props->expiry.tv_nsec = 0;

        if (ro_create(POL_RO_ROOT, props, NULL, 0)) {
                LOG_ERR("Could not create RO.");
                pthread_cond_destroy(&flat.cond);
                pthread_mutex_destroy(&flat.lock);
                free(props);
                ro_unsubscribe(flat.sid);
                return -1;
        }

        return 0;
}

int flat_fini(void)
{
        pthread_cond_destroy(&flat.cond);
        pthread_mutex_destroy(&flat.lock);
        ro_unsubscribe(flat.sid);
        return 0;
}

uint64_t flat_address(void)
{
        int ret = 0;
        uint64_t max_addr;
        struct dt_const * dtc;
        struct timespec timeout = {(TIMEOUT / 1000),
                                   (TIMEOUT % 1000) * MILLION};
        struct timespec abstime;
        struct ro_props * props;
        struct flat_addr_msg * msg;
        uint8_t * buf;
        char * ro_name;

        dtc = ribmgr_dt_const();
        if (dtc == NULL)
                return INVALID_ADDR;

        while (ret != -ETIMEDOUT) {
                clock_gettime(PTHREAD_COND_CLOCK, &abstime);
                ts_add(&abstime, &timeout, &abstime);

                max_addr = (1 << (8 * dtc->addr_size)) - 1;
                flat.addr = (rand() % (max_addr - 1)) + 1;

                /* FIXME: We must change this to stack memory */
                props = malloc(sizeof(*props));
                if (props == NULL)
                        return INVALID_ADDR;

                props->enrol_sync = false;
                props->recv_set = ALL_MEMBERS;
                props->expiry.tv_sec = TIMEOUT / 1000;
                props->expiry.tv_nsec = (TIMEOUT % 1000) * MILLION;

                buf = malloc(sizeof(*msg));
                if (buf == NULL) {
                        free(props);
                        return INVALID_ADDR;
                }

                msg = (struct flat_addr_msg *) buf;
                msg->code = FLAT_ADDR_REQ;
                msg->addr = flat.addr;

                /* FIXME: We may require functions to construct pathnames */
                ro_name = my_name();
                if (ro_name == NULL) {
                        free(props);
                        return INVALID_ADDR;
                }

                pthread_mutex_lock(&flat.lock);
                if (ro_create(ro_name, props, buf, sizeof(*msg))) {
                        pthread_mutex_unlock(&flat.lock);
                        free(props);
                        free(ro_name);
                        return INVALID_ADDR;
                }
                free(ro_name);

                while (flat.addr_in_use == false) {
                        ret = -pthread_cond_timedwait(&flat.cond,
                                                      &flat.lock,
                                                      &abstime);
                        if (ret == -ETIMEDOUT)
                                break;
                }
                pthread_mutex_unlock(&flat.lock);
        }

        return flat.addr;
}