Newer
Older
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#include "plugin_agent.h"
#include "util/alg_ds/alg/alg.h"
#include "util/alg_ds/ds/lock_guard/lock_guard.h"
#include "util/compare.h"
#include "util/conf_file.h"
#include <assert.h>
#include <arpa/inet.h>
#include <dlfcn.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
/*
static
void* rx_plugin_agent(void* p_v)
{
plugin_ag_t* p = (plugin_ag_t*)p_v;
const int port = 8080;
char buf[128] = {0};
p->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
assert(p->sockfd != -1 && "Error creating socket");
struct sockaddr_in serv_addr = {.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr.s_addr = INADDR_ANY};
int rc = bind(p->sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
assert(rc != -1 && "Error while binding. Address already in use?");
while(true){
struct sockaddr_in cli_addr;
socklen_t len = sizeof(cli_addr);
// Receive file name
rc = recvfrom(p->sockfd, buf, 128, 0, (struct sockaddr *)&cli_addr,&len);
if(p->flag_shutdown)
break;
assert(rc > -1 && rc < 128 && "Buffer overflow");
printf("Name of the file = %s\n",buf);
// Receive file size
int size = 0;
rc = recvfrom(p->sockfd, &size, sizeof(int), 0, (struct sockaddr *)&cli_addr, &len);
if(p->flag_shutdown)
break;
assert(rc == sizeof(int));
printf("Size of the file = %d\n",size);
char* data = calloc(1, size);
assert(data != NULL && "Memory exhausted!");
// Receive file itself
rc = recvfrom(p->sockfd,data,size,0,(struct sockaddr *)&cli_addr, &len);
if(p->flag_shutdown)
break;
assert(rc == size);
// Save file
FILE* fptr = fopen(buf, "wb");
rc = fwrite(data,size,1,fptr);
assert(rc == 1);
free(data);
fclose(fptr);
// Change the file permissions as it is a shared object
int const mode = strtol("0755", 0, 8);
rc = chmod(buf, mode);
assert(rc > -1);
if(p->flag_shutdown)
break;
char full_path[PATH_MAX] = {0};
char* ptr = getcwd(full_path, PATH_MAX);
ptr[strlen(ptr)] = '/';
memcpy(full_path + strlen(full_path), buf, strlen(buf));
// Load the plugin in the agent
load_plugin_ag(p, full_path);
printf("File received and loaded\n");
}
printf("Closing the socket\n");
close(p->sockfd);
return NULL;
}
*/
static inline
void free_sm_agent(void* key, void* value)
{
assert(key != NULL);
assert(value != NULL);
sm_agent_t* sm = (sm_agent_t*)value;
void* handle = sm->handle;
sm->free_sm(sm);
if(handle != NULL)
dlclose(handle);
}
static inline
void check_dl_error(void)
{
const char* error = dlerror();
if (error != NULL) {
printf("Error from dlerror = %s \n", error);
fflush(stdout);
assert(0 != 0 && "error loading the init of the shared object");
}
}
static
int is_regular_file(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
static
void load_all_pugin_ag(plugin_ag_t* p, const char* dir_path)
{
/* Scanning the in directory */
DIR* fd = opendir(dir_path);
assert(fd != NULL && "Error opening the input directory");
struct dirent* in_file = readdir(fd);
while (in_file != NULL) {
// We don't want current and parent directories
if (!strcmp (in_file->d_name, ".")){
in_file = readdir(fd);
continue;
}
if (!strcmp (in_file->d_name, "..")){
in_file = readdir(fd);
continue;
}
char file_path[1024] = {0};
size_t const sz_dir_path = strlen(dir_path);
assert(sz_dir_path < 1024);
strncat(file_path, dir_path, sz_dir_path < 1024 ? sz_dir_path : 1024 );
size_t const rem = 1024 - sz_dir_path;
size_t const sz_in = strlen(in_file->d_name);
assert(sz_in < rem && "Lacking space...");
strncat(file_path + sz_dir_path, in_file->d_name, sz_in < rem ? sz_in : rem );
const char* needle = ".conf";
const char* ans = strstr(file_path, needle);
if(ans == NULL && is_regular_file(file_path)) // Not a Configuration file
load_plugin_ag(p, file_path);
in_file = readdir(fd);
}
closedir(fd);
}
void unload_all_pugin_ag(plugin_ag_t* p, const char* path)
{
assert(p != NULL);
assert(path != NULL);
assert(0!=0 && "Not implemented!");
}
static
sm_io_ag_ran_t cp_io_ran(sm_io_ag_ran_t const* src)
{
assert(src != NULL);
sm_io_ag_ran_t dst = {0};
// Read
memcpy(dst.read_ind_tbl, src->read_ind_tbl, sizeof(read_ind_fp)*SM_AGENT_IF_READ_V0_END);
memcpy(dst.read_setup_tbl, src->read_setup_tbl, sizeof(read_e2_setup_fp)*SM_AGENT_IF_E2_SETUP_ANS_V0_END);
//read_rsu_fp read_rsu_tbl[0];
//read_rsu_fp read_rsu_tbl[0];
// Write
memcpy(dst.write_ctrl_tbl, src->write_ctrl_tbl, sizeof(write_ctrl_fp)*SM_AGENT_IF_WRITE_CTRL_V0_END);
memcpy(dst.write_subs_tbl, src->write_subs_tbl, sizeof(write_subs_fp)*SM_AGENT_IF_WRITE_SUBS_V0_END);
return dst;
}
void init_plugin_ag(plugin_ag_t* p, const char* dir_path, sm_io_ag_ran_t io)
{
assert(p != NULL);
assert(dir_path != NULL);
p->dir_path = (char*)dir_path;
p->io = cp_io_ran(&io);
p->flag_shutdown = false;
pthread_mutexattr_t attr= {0};
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
int rc = pthread_mutex_init(&p->sm_ds_mtx, &attr);
assert(rc == 0);
const size_t ran_func_size = sizeof(uint16_t);
assoc_init(&p->sm_ds, ran_func_size, cmp_ran_func_id, free_sm_agent );
load_all_pugin_ag(p, dir_path);
// pthread_create(&p->thread_rx, NULL, rx_plugin_agent, p);
}
void free_plugin_ag(plugin_ag_t* p)
{
assert(p != NULL);
assoc_free(&p->sm_ds);
pthread_mutex_destroy(&p->sm_ds_mtx);
// Thread management
p->flag_shutdown = true;
int rc = shutdown(p->sockfd, SHUT_RDWR);
if(rc != 0){
printf("Closing the agent socket: %s \n", strerror(errno));
}
//assert(rc == 0);
// rc = pthread_join(p->thread_rx, NULL);
// assert(rc == 0);
}
static inline
bool absolute_path(const char* path)
{
return path != NULL ? true : false;
}
void load_plugin_ag(plugin_ag_t* p, const char* path)
{
//ToDo: Looks code from a sophomore. DO IT PROPERLY
assert(p != NULL);
assert(path != NULL);
printf("[E2 AGENT]: Opening plugin from path = %s \n", path );
void* handle = dlopen(path, RTLD_NOW);
if(handle == NULL){
printf("Error while opening the shared object = %s \n", dlerror());
}
assert(handle != NULL && "Could not open the file path");
dlerror();
char* so_name = strrchr(path, '/');
char* ptr_so_name = NULL;
if(absolute_path(so_name) == true){
ptr_so_name = so_name + strlen("/lib");
assert(so_name != NULL);
} else {
ptr_so_name = (char*)path + strlen("lib");
}
char symbol_so[256] = {'m','a','k','e','_'};
char* needle = "_sm.so";
char* match = strstr( ptr_so_name, needle); // search_naive(strlen(needle), needle, strlen(ptr_so_name ), ptr_so_name);
assert(match != NULL && "Could not find the string _sm.so in the so\n");
char* ptr = &symbol_so[5];
assert(match > ptr_so_name);
strncat(ptr, ptr_so_name , match - ptr_so_name);
ptr += match - ptr_so_name;
const char* suffix = "_sm_agent";
strncat(ptr,suffix, strlen(suffix));
sm_agent_t* (*fp)(sm_io_ag_ran_t);
fp = dlsym(handle, symbol_so);
check_dl_error();
sm_agent_t* sm = fp(p->io);
sm->handle = handle;
assert(sm != NULL);
const uint16_t ran_func_id = sm->ran_func_id;
{
lock_guard(&p->sm_ds_mtx);
assoc_insert(&p->sm_ds, &ran_func_id, sizeof(ran_func_id), sm);
}
// printf("AGENT: Accepting SM ID = %d with def = %s \n", sm->ran_func_id, sm->ran_func_name);
}
void unload_plugin_ag(plugin_ag_t* p, uint16_t key)
{
assert(p != NULL);
assert(key != 0);
assert(0!=0 && "Not implemented!");
}
sm_agent_t* sm_plugin_ag(plugin_ag_t* p, uint16_t key)
{
assert(p != NULL);
assert(key > 0 && "Reserved value");
lock_guard(&p->sm_ds_mtx);
void* start_it = assoc_front(&p->sm_ds);
void* end_it = assoc_end(&p->sm_ds);
void* it = find_if(&p->sm_ds, start_it, end_it, &key, eq_ran_func_id);
assert(it != end_it && "RAN function ID not found in the RAN");
sm_agent_t* sm = assoc_value(&p->sm_ds, it);
assert(sm->ran_func_id == key);
return sm;
}
size_t size_plugin_ag(plugin_ag_t* p)
{
assert(p != NULL);
lock_guard(&p->sm_ds_mtx);
size_t s = assoc_size(&p->sm_ds);
return s;
}