/
main.c
211 lines (190 loc) · 5.3 KB
/
main.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
/*
* Copyright (C) 2008, 2009, 2010 Kaspar Schleiser <kaspar@schleiser.de>
* Copyright (C) 2013 INRIA
* Copyright (C) 2013 Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief Default application that shows a lot of functionality of RIOT
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Oliver Hahm <oliver.hahm@inria.fr>
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include "shell.h"
#include "coap.h"
#include <time.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <stdint.h>
#include "native_internal.h"
#include <net/if.h>
#include <linux/if_tun.h>
#include <linux/if_ether.h>
int coap_cmd(int argc, char **argv);
void end (void) __attribute__((destructor));
static const shell_command_t shell_commands[] = {
{ "coap", "Starts X amount of coap clients", coap_cmd },
{ NULL, NULL, NULL }
};
int i,tap_num,status;
char comm[50];
int main(void)
{
puts("RIOT CoAP Client Manager");
/* start shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
/* should be never reached */
return 0;
}
int coap_cmd(int argc, char **argv){
(void)argc;
(void)argv;
//READ THE SIZE OF TAP_AMOUNT FILE
char ffile_size[2];
int file_size;
int fp = open("./set_tap_up/tap_char_size.txt", O_RDONLY);
if(0 > fp)
{
printf("\n open() Error!!!\n");
return 1;
}
read(fp,ffile_size,2);
//CLOSE FILE
if(0 > close(fp))
{
printf("\n close() Error!!!\n");
return 1;
}
file_size = (int)(ffile_size[0] - '0');
//READ THE AMOUNT OF COAP CLIENTS TO BE CREATED
char file_num[file_size];
fp = open("./set_tap_up/tap_amount.txt", O_RDONLY);
if(0 > fp)
{
printf("\n open() Error!!!\n");
return 1;
}
read(fp,file_num,file_size);
//CLOSE FILE
if(0 > close(fp))
{
printf("\n close() Error!!!\n");
return 1;
}
//CONVERT IT TO INT
tap_num=0;
if(file_size == 4){
tap_num += (int)(file_num[0] - '0')*1000;
tap_num += (int)(file_num[1] - '0')*100;
tap_num += (int)(file_num[2] - '0')*10;
tap_num += (int)(file_num[3] - '0');
}
else if(file_size == 3){
tap_num += (int)(file_num[0] - '0')*100;
tap_num += (int)(file_num[1] - '0')*10;
tap_num += (int)(file_num[2] - '0');
}
else if(file_size == 2){
tap_num += (int)(file_num[0] - '0')*10;
tap_num += (int)(file_num[1] - '0');
}
else
tap_num += (int)(file_num[0] - '0');
printf("Amount of CoAP clients will be created: %d\n",tap_num);
strcpy(comm,"sysctl net.ipv6.conf.all.forwarding=1");
status=system(comm);
//Initalize our file lock
struct flock fl;
pid_t tid = syscall(SYS_gettid);
fl.l_type = F_WRLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
fl.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
fl.l_start = 0; /* Offset from l_whence */
fl.l_len = 0; /* length, 0 = to EOF */
fl.l_pid = tid; /* our PID */
int num_size=0;
char num_size2char[2];
//Start Coap Clients
for (i=0;i<tap_num;i++){
fl.l_type = F_WRLCK; // tell it to lock the region
fp = open("./coap/tap_control.txt", O_WRONLY | O_TRUNC);
fcntl(fp, F_SETLKW, &fl); //Locks the file for writing
if(i/1000 >= 1.0)
num_size=4;
else if(i/100 >= 1.0)
num_size=3;
else if(i/10 >= 1.0)
num_size=2;
else
num_size=1;
char num_w[num_size];
sprintf(num_w,"%d",i);
write(fp,num_w,num_size);
if(0 > close(fp))
{
printf("\n close() Error!!!\n");
return 1;
}
fl.l_type = F_UNLCK; // tell it to unlock the region
fcntl(fp, F_SETLK, &fl); // set the region to unlocked
//SET FILE SIZE
fp = open("./coap/tap_control_size.txt", O_WRONLY | O_TRUNC);
fl.l_type = F_WRLCK; // tell it to lock the region
fcntl(fp, F_SETLKW, &fl); //Locks the file for writing
strcpy(num_size2char,"");
sprintf(num_size2char,"%d",num_size);
write(fp,num_size2char,2);
if(0 > close(fp))
{
printf("\n close() Error!!!\n");
return 1;
}
fl.l_type = F_UNLCK; // tell it to unlock the region
fcntl(fp, F_SETLK, &fl); // set the region to unlocked
strcpy(comm,"xterm -hold ./coap/bin/native/coap.elf &");
status=system(comm);
struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = 550000000;
nanosleep(&tim , &tim2);
}
(void)status;
return 0;
}
//Destructor
__attribute__((destructor)) void end (void)
{
char num[4];
for (i=0;i<tap_num;i++){
sprintf(num,"%d",i);
//Delete previous Taps
strcpy(comm,"ip link delete tap");
strcat(comm,num);
status=system(comm);
}
//Kill CoAP clients
strcpy(comm,"pkill -9 coap.elf");
status=system(comm);
}