-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonas_pipe.c
280 lines (252 loc) · 6.03 KB
/
personas_pipe.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
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
#include <stdio.h>
#include <stdlib.h>
#include<sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#define MAX_CHILD 1000
void showState(int i, int* childrenState){
printf("[persona] child %d ", i);
if(childrenState[i] == 0)
{
puts("state: start");
}
else if(childrenState[i] == 1)
{
puts("state: DHCP_DISCOVER");
}
else if(childrenState[i] == 2)
{
puts("state: DHCP_OFFER");
}
else if(childrenState[i] == 3)
{
puts("state: sth wrong!");
}
else if(childrenState[i] == 4)
{
puts("state: DHCP REQUEST");
}
else if(childrenState[i] == 5)
{
puts("state success!");
}
else if(childrenState[i] == 6)
{
puts("something wrong!");
}
else if(childrenState[i] == 7)
{
puts("FINISH");
}
else{
puts("execlp not successful");
}
}
int valid(int nm)
{
return (nm == 0) || (nm == 1) || (nm == 2) || (nm == 4) || (nm == 5) || (nm == 7) ;
}
int main(int argc, char **argv)
{
int children_nm;
pid_t pid[MAX_CHILD];
if(argc!=4){
fprintf(stderr,"usage: ./personas <process_nm> <name of a connected interface> <xid>\n");
exit(-1);
}
sscanf(argv[1],"%d", &children_nm);
if(children_nm > MAX_CHILD){
fprintf(stderr, "too many children\n, MAX is %d\n", MAX_CHILD);
exit(-1);
}
int p2c_pfd[MAX_CHILD][2];
int c2p_pfd[MAX_CHILD][2];
int childrenState[MAX_CHILD];
int childrenIP[MAX_CHILD] = {0};
printf("argv[2]:%s argv[3]:%s\n",argv[2], argv[3]);
puts("start running");
for(int i = 0; i < children_nm; i++)
{
if(pipe(p2c_pfd[i]) == -1 || pipe(c2p_pfd[i]) == -1){
perror("pipe");
exit(-1);
}
pid[i] = fork();
if(pid[i] == 0)
{
// need to handle parent's instruction to children
// close(STDOUT_FILENO);
if(dup2(p2c_pfd[i][0], STDIN_FILENO) == -1){
fprintf(stderr, "dup failed");
exit(1);
}
//close(p2c_pfd[i][0]);
close(p2c_pfd[i][1]);
close(c2p_pfd[i][0]);
char buf[1000];
sprintf(buf,"%d",i);
char str1[1000], str2[1000];
memset(str1, '\0', sizeof(str1));
memset(str2, '\0', sizeof(str2));
memcpy(str1, argv[2], strlen(argv[2]));
memcpy(str2, argv[3], strlen(argv[3]));
childrenState[i] = 0;
if(execlp("./virtual_interface","./virtual_interface", str1, buf, (char*) 0)<0){
perror("execlp()");
printf("child:%d\n",i);
exit(-1);
}
}
else
{
close(c2p_pfd[i][1]);
close(p2c_pfd[i][0]);
puts("I am parent");
}
}
int maxfd = 0;
for(int i = 0; i < children_nm; i++){
if(c2p_pfd[i][0] > maxfd) maxfd = c2p_pfd[i][0];
}
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
fd_set readfds;
int target_child;
while(1){
FD_ZERO(&readfds);
// should need to add STDIN to get user input
FD_SET(STDIN_FILENO, &readfds);
for(int i = 0; i < children_nm; i++){
FD_SET(c2p_pfd[i][0], &readfds);
}
//printf("select\n");
if(select(maxfd+1, &readfds, NULL, NULL, &tv)<0){
perror("select()");
exit(-1);
};
if(FD_ISSET(STDIN_FILENO, &readfds)){
//puts("inside");
char buf[128] = {0};
char byte;
int len = 0;
while(read(STDIN_FILENO, &byte, 1)>0){
if(byte=='\n') break;
assert(len < 127);//the last byte of command is always null.
buf[len]=byte;
len+=1;
}
buf[len] = 0;
const char del[2] = " ";
char *token = strtok(buf, del);
if(token != NULL && strcmp(token, "DHCP") == 0)
{
token = strtok(NULL, del);
if(token != NULL && strcmp(token, "init") == 0)
{
token = strtok(NULL, del);
if(token != NULL && strcmp(token, "all") == 0)
{
for(int i = 0; i < children_nm; i++)
{
char sendmsg[100] ={0};
sprintf(sendmsg,"DHCP init\n");
if(childrenState[i] == 0)
{
showState(i, childrenState);
write(p2c_pfd[target_child][1], sendmsg, strlen(sendmsg));
}
}
}
else if(token != NULL)
{
sscanf(token,"%d", &target_child);
char sendmsg[100] ={0};
sprintf(sendmsg,"DHCP init\n");
showState(target_child, childrenState);
if(valid(childrenState[target_child]))
write(p2c_pfd[target_child][1], sendmsg, strlen(sendmsg));
}
}
else if(token != NULL && strcmp(token, "kill") == 0)
{
sscanf(token,"%d", &target_child);
char sendmsg[100] = {0};
sprintf(sendmsg, "DHCP kill\n");
showState(target_child, childrenState);
write(p2c_pfd[target_child][1], sendmsg, strlen(sendmsg));
}
else if(token != NULL && strcmp(token, "release") == 0)
{
sscanf(token,"%d", &target_child);
char sendmsg[100] = {0};
sprintf(sendmsg, "DHCP release\n");
showState(target_child, childrenState);
write(p2c_pfd[target_child][1], sendmsg, strlen(sendmsg));
}
}
else if(token == "STATE")
{
token = strtok(NULL, del);
if(token != NULL)
{
sscanf(token,"%d", &target_child);
showState(target_child, childrenState);
}
}
}
for(int i = 0; i < children_nm; i++){
if(FD_ISSET(c2p_pfd[i][0], &readfds)){
char buf[1000] = {};
int n = read(c2p_pfd[i][0], buf, sizeof(buf)-1);
// processing child input
//printf("parent get from %d %d bytes:[start]%s[end]\n",i,n, buf);
if(strcmp(buf,"[DHCP_DISCOVER]") == 0)
{
childrenState[i] = 1;
}
else if(strcmp(buf, "[DHCP OFFER]") == 0)
{
childrenState[i] = 2;
}
else if(strcmp(buf, "sth wrong!") == 0)
{
childrenState[i] = 3;
}
else if(strcmp(buf, "[DHCP REQUEST]") == 0)
{
childrenState[i] = 4;
}
else if(strcmp(buf, "[DHCP ACK] success!") == 0)
{
childrenState[i] = 5;
}
else if(strcmp(buf, "something wrong!") == 0)
{
childrenState[i] = 6;
}
else if(strcmp(buf, "[FINISH]") == 0)
{
childrenState[i] = 7;
}
else{
childrenState[i] = 8;
}
memset(buf,0,1000);
}
}
// fgets (buf, 128, stdin);
// printf("buf:%s\n",buf);
// //select does not work as expect.
//puts("hois");
}
printf("wait\n");
while(wait(NULL)>0){};
puts("children are all dead");
for(int i = 0; i < children_nm; i++){
close(c2p_pfd[i][0]);
// close(pfd[i][1]);
}
}