forked from saru2017/packet_repeater
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrontend_15.4.c
300 lines (226 loc) · 5.47 KB
/
frontend_15.4.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "watalib.h"
#define BUFSIZE 1024
#define MAX_CLIENTS 10
int g_clients[MAX_CLIENTS];
void watalab_list_serials()
{
struct stat st;
char filename[256];
int ret;
for(int i = 0; i < 20; i++){
sprintf(filename, "/dev/ttyS%d", i);
ret = stat(filename, &st);
if(ret == 0){
printf("%s\n",filename);
}
}
}
int watalab_open_serial(char *device)
{
int i, fd;
struct termios tty;
char *c, buf[256];
if((fd = open(device, O_RDWR|O_NOCTTY)) == -1){
watalab_list_serials();
exit(EXIT_FAILURE);
}
memset(&tty, 0, sizeof(tty));
tty.c_cflag = CS8|CLOCAL|CREAD;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
cfsetospeed(&tty, B9600);
cfsetispeed(&tty, B9600);
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tty);
return fd;
}
/***************************************************************/
void watalab_init_clients()
{
int i;
for(i = 0; i < MAX_CLIENTS; i++){
g_clients[i] = 0;
}
}
/***************************************************************/
/***************************************************************/
void watalab_set_fds(fd_set* pfds, int sock)
{
int i;
FD_ZERO(pfds);
FD_SET(sock, pfds);
for(i = 0; i < MAX_CLIENTS; i++){
if(g_clients[i] == 1){
FD_SET(i, pfds);
}
}
}
/***************************************************************/
/***************************************************************/
void watalab_add(int sock)
{
if(sock < MAX_CLIENTS){
g_clients[sock] = 1;
}else{
printf("connection overflow\n");
exit(-1);
}
}
/***************************************************************/
/***************************************************************/
void watalab_remove(int id)
{
g_clients[id] = 0;
}
/***************************************************************/
/***************************************************************/
int watalab_get_max_sock()
{
int i;
int max_sock = 0;
for(i = 0; i < MAX_CLIENTS; i++){
if(g_clients[i] == 1){
max_sock = i;
}
}
return max_sock;
}
/***************************************************************/
/***************************************************************/
void watalab_broadcast(char* buf, int size, int from)
{
int i;
for(i = 0; i < MAX_CLIENTS; i++){
if(i == from){
continue;
}
if(g_clients[i] == 1){
write(i, buf, size);
}
}
}
/***************************************************************/
/************************************************************/
int watalab_tcp_listen(int port)
{
int sock;
struct sockaddr_in addr;
int yes = 1;
int ret;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) {
perror("socket");
exit(-1);
}
bzero((char *) &addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
if(ret < 0) {
perror("bind");
exit(-1);
}
ret = listen(sock, 5);
if(ret < 0) {
perror("listen");
close(sock);
exit(-1);
}
return sock;
}
/************************************************************/
/************************************************************/
int watalab_accept(int sock_listen){
struct sockaddr addr;
int len;
int ret;
len = sizeof(int);
ret = accept(sock_listen, &addr, (socklen_t*) &len);
return ret;
}
/************************************************************/
/************************************************************/
int watalab_tcp_connect(const char *hostname, int port)
{
int sock;
int ret;
struct sockaddr_in addr;
struct hostent *host;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
host = gethostbyname(hostname);
addr.sin_addr = *(struct in_addr *)(host->h_addr_list[0]);
addr.sin_port = htons(port);
ret = connect(sock, (struct sockaddr *)&addr, sizeof addr);
if(ret < 0){
return -1;
}else{
return sock;
}
}
/************************************************************/
void watalab_connect(char *server_name, int port)
{
}
void watalab_do_client(int fd, int sock)
{
fd_set fds;
int max_sock;
int i;
FD_ZERO(&fds);
watalab_set_fds(&fds, fd);
watalab_set_fds(&fds, sock);
if(sock > fd){
max_sock = sock;
}else{
max_sock = fd;
}
printf("Do client\n");
printf("fd: %d\n", fd);
printf("sock: %d\n", sock);
select(max_sock + 1, &fds, NULL, NULL, NULL);
printf("After Select\n");
if(FD_ISSET(fd, &fds) != 0){
char buf[1024];
int size;
size = read(fd, buf, 1024);
buf[size] = '\x00';
write(sock, buf, size);
printf("USB buf size: %d\n", size);
}
if(FD_ISSET(sock, &fds) != 0){
char buf[1024];
int size;
size = read(sock, buf, 1024);
buf[size] = '\x00';
write(fd, buf, size);
printf("Sock buf size: %d\n", size);
}
}
int main(int argc, char** argv)
{
int sock_listen;
char serial_name[1024];
char server_name[1024];
int port;
int fd, sock;
if(argc != 4){
printf("usage: %s [serial] [servername] [port]\n", argv[0]);
exit(-1);
}
printf("Frontend Started\n");
strcpy(serial_name, argv[1]);
strcpy(server_name, argv[2]);
port = atoi(argv[3]);
fd = watalab_open_serial(serial_name);
sock = watalab_tcp_connect(server_name, port);
printf("fd: %d\n", fd);
printf("sock: %d\n", sock);
while(true){
watalab_do_client(fd, sock);
}
close(fd);
close(sock);
}