forked from phhusson/mptcp-proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmpproxy.c
139 lines (119 loc) · 4 KB
/
mpproxy.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
//*****************************************************
//*****************************************************
//
// mpctl.c
// Project: mptcp_proxy
//
//*****************************************************
//*****************************************************
//
// GEORG HAMPEL - Bell Labs/NJ/USA: All Rights Reserved
//
//*****************************************************
//*****************************************************
//*****************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define LEN_FIFO_MSG 1000
#include "common.h"
void writehelp() {
printf("Command syntax:\n");
printf(" -L list all sessions and subflows\n");
printf(" -sess [SESS_ID] session id, default = all sessions\n");
printf(" -A add subflow\n");
printf(" -sess [SESS_ID] session id, default = highest SESS_ID\n");
printf(" -ipl [IP_LOCAL] local ip, default = local IP of 1st subflow\n");
printf(" -ipr [IP_REMOTE] remote ip, default = remote IP of 1st subflow\n");
printf(" -ptl [PRT_LOCAL] local port, default = random port\n");
printf(" -ptr [PRT_REMOTE] remote port, default = remote port of 1st subflow\n");
printf(" -if [IF_LOCAL] adds one subflow from IF_LOCAL for SESS_ID - overwrites -ipl specification\n");
printf(" -D delete subflow\n");
printf(" -sess [SESS_ID] session id, default = highest SESS_ID\n");
printf(" -sfl [SFL_ID] subflow id, default = highest candidate subflow\n");
printf(" -S switch subflows\n");
printf(" -sess [SESS_ID] session id, default = highest SESS_ID\n");
printf(" -sfl [SFL_ID] subflow id of new active subflow, default = highest candidate\n");
printf(" -B break subflow\n");
printf(" -sess [SESS_ID] session id, default = highest SESS_ID\n");
printf(" -sfl [SFL_ID] subflow id candidate subflow, default = highest candidate\n");
printf(" start/stop start/stop mptcp_proxy\n");
}
int main(int argc, char **argv)
{
//deal with commandline arguments
char incmd[200];
//analyze commandline args
if( (argc<2) || strcmp(argv[1],"-help")==0 || strcmp(argv[1],"--help")==0 ) {
printf("argc=%d\n", argc);
writehelp();
exit(0);
}
int i;
strcpy(incmd, argv[1]);
if( strcmp(argv[1], "stop")==0) strcpy(incmd, "-Q");
for(i=2; i<argc; i++) {
strcat(incmd," ");
strcat(incmd,argv[i]);
}
if(argc > 1 && strcmp(argv[1], "start")==0) {
int ret = system("mptcp_proxy &");
if(ret == -1)
printf("system return error\n");
}
//make down FIFO
mkfifo(FIFO_NAME_DOWN, (mode_t) 0666);
//open fifo
int fd_down = open(FIFO_NAME_DOWN, O_WRONLY | O_NONBLOCK);
//send data
int ret = write(fd_down, incmd, strlen(incmd));
if(ret < -1){
printf("mpproxy: writing to pipe failed\n");
exit(0);
}
close(fd_down);
//make up FIFO
mkfifo(FIFO_NAME_UP, (mode_t) 0666);
//open fifo
int fd_up = open(FIFO_NAME_UP, O_RDONLY | O_NONBLOCK);
fd_set fds_test;
fd_set fds_input;
FD_ZERO(&fds_input);
FD_SET(fd_up, &fds_input);
fds_test = fds_input;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
char buf[LEN_FIFO_MSG+1];
int fd;
int rtn = select(FD_SETSIZE, &fds_test, (fd_set *) NULL, (fd_set *) NULL, &timeout);
if(rtn == -1) {
printf("mpproxy: select returns=%d, exit program!\n",rtn);
exit(1);
}
for(fd = 0; fd < FD_SETSIZE; fd++){
if(FD_ISSET(fd, &fds_test)){
if (fd == fd_up){
ret = read(fd_up, buf, LEN_FIFO_MSG);
buf[ret] = '\0';
if(ret < -1){
printf("mpproxy: reading from pipe failed\n");
close(fd_up);
exit(0);
}
close(fd_up);
if(buf[0] != '\0') printf("%s\n", buf);
}
}//end if F
}
}