-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiothconf_data.c
215 lines (189 loc) · 6.51 KB
/
iothconf_data.c
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
/*
* iothconf_data.c: data structure for ioth auto configuration
*
* Copyright 2021 Renzo Davoli - Virtual Square Team
* University of Bologna - Italy
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <iothconf_data.h>
struct ioth_confdata {
struct ioth_confdata *next;
struct ioth *stack;
time_t timestamp;
uint32_t ifindex;
uint16_t datalen;
uint8_t type;
uint8_t flags;
};
static pthread_mutex_t ioth_confdata_mutex = PTHREAD_MUTEX_INITIALIZER;
static struct ioth_confdata *ioth_confdata_root;
void ioth_confdata_add(struct ioth *stack, uint32_t ifindex, uint8_t type, time_t timestamp, uint8_t flags,
void *data, uint16_t datalen) {
struct ioth_confdata **scan, *this;
pthread_mutex_lock(&ioth_confdata_mutex);
for (scan = &ioth_confdata_root; *scan != NULL; scan = &this->next) {
this = *scan;
if (stack == this->stack && type == this->type && datalen == this->datalen &&
memcmp(this + 1, data, datalen) == 0) {
if (timestamp > this->timestamp)
this->timestamp = timestamp;
break;
}
}
if (*scan == NULL) {
this = malloc(sizeof(struct ioth_confdata) + datalen);
if (this != NULL) {
*this = (struct ioth_confdata) {
.next = NULL,
.stack = stack,
.timestamp = timestamp,
.ifindex = ifindex,
.datalen = datalen,
.type = type,
.flags = flags,
};
memcpy(this + 1, data, datalen);
*scan = this;
}
}
pthread_mutex_unlock(&ioth_confdata_mutex);
}
typedef int ioth_confdata_forall_cb(void *data, void *arg);
void ioth_confdata_forall_mask(struct ioth *stack, uint32_t ifindex, uint8_t type, uint8_t mask,
ioth_confdata_forall_cb *callback, void *callback_arg) {
struct ioth_confdata **scan, *this;
int cb_retval = 0;
type &= mask;
pthread_mutex_lock(&ioth_confdata_mutex);
scan = &ioth_confdata_root;
while(*scan != NULL) {
this = *scan;
if ((stack == IOTH_CONFDATA_ANYSTACK || stack == this->stack) &&
(ifindex == 0 || ifindex == this->ifindex) &&
(type == 0 || type == (this->type & mask)) &&
(cb_retval = callback(this + 1, callback_arg)) & IOTH_CONFDATA_FORALL_DELETE) {
*scan = this->next;
free(this);
} else
scan = &this->next;
if (cb_retval & IOTH_CONFDATA_FORALL_BREAK)
break;
}
pthread_mutex_unlock(&ioth_confdata_mutex);
}
static int delete_cb(void *data, void *arg) {
time_t *timestamp = arg;
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
return (*timestamp == 0 || *timestamp > ioth_confdata->timestamp) ?
IOTH_CONFDATA_FORALL_DELETE : 0;
}
void ioth_confdata_free(struct ioth *stack, uint32_t ifindex, uint8_t type, time_t timestamp) {
ioth_confdata_forall(stack, ifindex, type, delete_cb, ×tamp);
}
struct ioth *ioth_confdata_getstack(void *data) {
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
return ioth_confdata->stack;
}
uint8_t ioth_confdata_gettype(void *data) {
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
return ioth_confdata->type;
}
uint32_t ioth_confdata_getifindex(void *data) {
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
return ioth_confdata->ifindex;
}
time_t ioth_confdata_gettimestamp(void *data) {
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
return ioth_confdata->timestamp;
}
uint16_t ioth_confdata_getdatalen(void *data) {
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
return ioth_confdata->datalen;
}
uint8_t ioth_confdata_setflags(void *data, uint8_t flags) {
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
uint8_t oldflags = ioth_confdata->flags;
ioth_confdata->flags |= flags;
return oldflags;
}
uint8_t ioth_confdata_clrflags(void *data, uint8_t flags) {
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
uint8_t oldflags = ioth_confdata->flags;
ioth_confdata->flags &= ~flags;
return oldflags;
}
static int read_timestamp_cb(void *data, void *arg) {
time_t *timestamp = arg;
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
*timestamp = ioth_confdata->timestamp;
return IOTH_CONFDATA_FORALL_BREAK;
}
time_t ioth_confdata_read_timestamp(struct ioth *stack, uint32_t ifindex, uint8_t type) {
time_t timestamp = 0;
type = TIMESTAMP(type);
ioth_confdata_forall(stack, ifindex, type, read_timestamp_cb, ×tamp);
return timestamp;
}
time_t ioth_confdata_new_timestamp(struct ioth *stack, uint32_t ifindex, uint8_t type) {
time_t oldtimestamp = ioth_confdata_read_timestamp(stack, ifindex, type);
type = TIMESTAMP(type);
for (;;) {
struct timeval newtimestamp;
gettimeofday(&newtimestamp, NULL);
if (newtimestamp.tv_sec > oldtimestamp)
return newtimestamp.tv_sec;
usleep(1000000 - newtimestamp.tv_usec);
}
}
void ioth_confdata_write_timestamp(struct ioth *stack, uint32_t ifindex, uint8_t type, time_t timestamp) {
ioth_confdata_add(stack, ifindex, type, timestamp, 0, NULL, 0);
}
static int del_timestamp_cb(void *data, void *arg) {
time_t *timestamp = arg;
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
*timestamp = ioth_confdata->timestamp;
return IOTH_CONFDATA_FORALL_DELETE;
}
void ioth_confdata_del_timestamp(struct ioth *stack, uint32_t ifindex, uint8_t type) {
time_t timestamp = 0;
type = TIMESTAMP(type);
ioth_confdata_forall(stack, ifindex, type, del_timestamp_cb, ×tamp);
}
struct deldata {
void *data;
uint16_t datalen;
int found;
};
static int ioth_confdata_del_cb(void *data, void *arg) {
struct deldata *dd = arg;
struct ioth_confdata *ioth_confdata = ((struct ioth_confdata *) data) - 1;
if (ioth_confdata->datalen == dd->datalen &&
memcmp(data, dd->data, dd->datalen) == 0) {
ioth_confdata->timestamp = 0;
dd->found = 1;
return IOTH_CONFDATA_FORALL_BREAK;
} else
return 0;
}
int ioth_confdata_del(struct ioth *stack, uint32_t ifindex, uint8_t type,
void *data, uint16_t datalen) {
struct deldata dd = {data, datalen, 0};
ioth_confdata_forall(stack, ifindex, type, ioth_confdata_del_cb, &dd);
return dd.found ? 0 : -1;
}