-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
X32TCP.c
445 lines (426 loc) · 11.5 KB
/
X32TCP.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//
// X32TCP.c
//
// Created on: Oct 3, 2015
// Author: Patrick-Gilles Maillot
//
// Driving an X32 from TCP:
//
// The program acts as a daemon/server, reading its config from command line params
// it opens a UDP connection with X32
// it sets a server, binding to a TCP socket and waiting for client connections
// upon client connection, the client data is parsed and sent to the X32 UDP connection
// data back from the X32 is expected/available is sent as a reply towards the client
//
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#ifdef __WIN32__
#include <windows.h>
#else
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h> //for threading , link with lpthread
#define closesocket(s) close(s)
#define MySleep(a) usleep(a*1000)
#define WSACleanup()
#define SOCKET_ERROR -1
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#endif
#define PORT 10041
#define BACKLOG 10
#define TIMEOUT 10
#define BSIZE 512 // send/receive buffer sizes
#define XREMOTE_TIMEOUT 9 // timeout set to 9 seconds
//
int X32debug, X32verbose;
//
// Macros:
//
#ifdef __WIN32__
#define millisleep(x) Sleep((x))
#define skt_close(s) closesocket((s))
#else
#define millisleep(x) usleep((x)*1000)
#define skt_close(s) close((s))
#endif
// External calls used
extern void Xfdump(char *header, char *buf, int len, int debug);
extern void Xsdump(char *out, char *buf, int len);
extern int Xcparse(char *buf, char *line);
//
//
void *serve_client_handler(int client_sock);
void XRcvClean(int X_fd);
int X32_init();
//
// X32 data in global dataset
struct sockaddr_in Xip; // X32 IP
struct sockaddr* Xip_addr = (struct sockaddr *) &Xip;
char Xip_str[20], Xport_str[8];
//
//
#ifdef __WIN32__
int Xip_len = sizeof(Xip); // length of addresses
struct timeval timeout;
#else
socklen_t Xip_len = sizeof(Xip); // length of addresses
#endif
//
//
//
int main(int argc, char **argv) {
struct sockaddr_in Sip; // Server IP
struct sockaddr* Sip_addr = (struct sockaddr *) &Sip;
char Sport_str[8];
struct sockaddr_in Cip; // Client IP
struct sockaddr* Cip_addr = (struct sockaddr *) &Cip;
int S_fd, C_fd; // Server, Client sockets
#ifdef __WIN32__
WSADATA wsa;
int Sip_len = sizeof(Sip); // length of addresses
int Cip_len = sizeof(Cip); // length of addresses
HANDLE thread;
#else
socklen_t Sip_len = sizeof(Sip); // length of addresses
socklen_t Cip_len = sizeof(Cip); // length of addresses
pthread_t thread;
#endif
//
int backlog, reuse_addr = 1;
char input_ch;
//
// Set (temporary) default value for X32 IP
strcpy(Xip_str, "127.0.0.1");
// Set (temporary) default value for server port
strcpy(Sport_str, "10041");
X32debug = 0;
X32verbose = 0;
backlog = BACKLOG;
//
// manage command line arguments
while ((input_ch = getopt(argc, argv, "b:i:d:p:v:h")) != (char) -1) {
switch (input_ch) {
case 'b':
sscanf(optarg, "%d", &backlog);
break;
case 'i':
strcpy(Xip_str, optarg);
break;
case 'd':
sscanf(optarg, "%d", &X32debug);
break;
case 'p':
strcpy(Sport_str, optarg);
break;
case 'v':
sscanf(optarg, "%d", &X32verbose);
break;
default:
case 'h':
printf("usage: X32TCP [-b [10] server max connections backlog]\n");
printf(" [-i X32 console ipv4 address]\n");
printf(" [-d 0/1, [0], debug option]\n");
printf(" [-v 0/1 [1], verbose option]\n");
printf(" [-p [10041] server port]\n\n");
printf(" After starting, the server waits for clients to connect and send X32_command\n");
printf(" format like commands, sent as character strings to the TCP server.\n");
printf(" All commands from connected clients are parsed and formatted to X32 OSC standard\n");
printf(" before being sent to X32. If X32 answers, the X32 OSC data is formatted to readable\n");
printf(" format before being returned to the connected client as a string (null characters\n");
printf(" are replaced with '~' to ease printing or parsing).\n");
printf(" If no answer after a timeout of 10ms from X32, the string 'no data' is returned.\n\n");
printf(" The command 'exit' from a connected client input closes the respective client stream.\n");
return 0;
break;
}
}
//
#ifdef __WIN32__
//Initialize winsock
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("Failed. Error Code : %d", WSAGetLastError());
exit(1);
}
#endif
//
printf(" X32TCP - v0.90 - (c)2015 Patrick-Gilles Maillot\n\n");
fflush(stdout);
//
if ((S_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Socket failure!!\n");
exit(1);
}
if (setsockopt(S_fd, SOL_SOCKET, SO_REUSEADDR, (char*) &reuse_addr,
sizeof(reuse_addr)) < 0) {
printf("Call to setsockopt() error");
exit(1);
}
memset(&Sip, 0, sizeof(Sip));
memset(&Cip, 0, sizeof(Cip));
Sip.sin_family = AF_INET;
Sip.sin_port = htons(PORT);
Sip.sin_addr.s_addr = INADDR_ANY;
if ((bind(S_fd, Sip_addr, Sip_len)) < 0) { //sizeof(struct sockaddr)
printf("Binding Failure\n");
exit(1);
}
if ((listen(S_fd, backlog)) < 0) {
printf("Listening Failure\n");
exit(1);
}
if (X32verbose) {
printf("Server now accepting Client connections\n");
fflush(stdout);
}
while (1) {
if ((C_fd = accept(S_fd, Cip_addr, &Cip_len)) < 0) {
perror("accept");
exit(1);
}
if (X32debug) {
printf("Server got connection from client %s\n",
inet_ntoa(Cip.sin_addr));
fflush(stdout);
}
#ifdef __WIN32__
if ((thread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) serve_client_handler, (LPVOID) C_fd, 0, NULL)) < 0) {
#else
if ((pthread_create(&thread, NULL, serve_client_handler, C_fd)) < 0) {
#endif
printf("Could not create thread\n");
}
if (X32debug) {
printf("Thread 0x%x created\n", (unsigned int) thread);
fflush(stdout);
}
// serve_client_handler(C_fd);
} // While serving clients
//
skt_close(S_fd);
WSACleanup();
return 0;
} //End of main
void *serve_client_handler(int ssock) {
char c_buf[BSIZE], r_buf[BSIZE], s_buf[BSIZE], o_buf[BSIZE];
int c_len, r_len, s_len;
int p_status;
int X_fd; // X32 socket returned by X32_init()
fd_set ufds1;
int i, keep_on = 1;
//
// Initialize X32 connection for the client to obtain an X32 socket descriptor
if ((X_fd = X32_init()) <= 0)
exit(1);
//
// Free X32 buffer from any remaining data
XRcvClean(X_fd);
timeout.tv_sec = 0;
timeout.tv_usec = TIMEOUT * 1000; //Set timeout in ms
if (X32verbose) {
printf("New client connected!\n");
fflush(stdout);
}
if (X32debug) {
printf("Thread using socket: %d\n", ssock);
fflush(stdout);
}
while (keep_on) {
//
// Read from TCP client
if ((c_len = recv(ssock, c_buf, BSIZE, 0)) < 0) {
printf("Error reading Client data\n");
keep_on = 0;
break;
} else if (c_len > 0) {
//
// Send to X32, after data interpretation
c_buf[--c_len] = '\0';
if (c_len) {
// printf("nb: %d,", c_len);
// for (i = 0; i< c_len; i++) printf("%2x ", c_buf[i]); printf("\n"); fflush(stdout);
if (strncmp(c_buf, "exit", 4) == 0)
break; // exit while() and close socket.
if (X32debug) {
printf("server received from TCP client %d bytes:\n", c_len);
for (i = 0; i < c_len; i++)
printf("%02x ", c_buf[i]);
printf("\n");
fflush(stdout);
}
s_len = Xcparse(s_buf, c_buf);
if (X32debug) {
printf("server sends to UDP server: %s %d\n", s_buf, s_len);
fflush(stdout);
}
if (sendto(X_fd, s_buf, s_len, 0, Xip_addr, Xip_len) < 0) {
perror("Error while sending data");
keep_on = 0;
break;
}
}
FD_ZERO(&ufds1);
FD_SET(X_fd, &ufds1);
if ((p_status = select(X_fd + 1, &ufds1, NULL, NULL, &timeout)) != -1) {
if (FD_ISSET(X_fd, &ufds1) > 0) {
if (X32debug) {
printf("Server reading from UDP: p_status = %d\n", p_status);
fflush(stdout);
}
r_len = recvfrom(X_fd, r_buf, BSIZE, 0, 0, 0);
if (X32debug) {
Xfdump("X->", r_buf, r_len, 0);
fflush(stdout);
}
r_buf[r_len] = '\0';
if (r_len > 0) {
//
// Make UDP message readable for TCP client
Xsdump(o_buf, r_buf, r_len);
if (X32debug) {
printf(
"Server received: %s, %d from UDP; sending to TCP client\n",
o_buf, strlen(o_buf));
fflush(stdout);
}
if ((send(ssock, o_buf, strlen(o_buf), 0)) < 0) {
printf("Failure Sending Message back to Client\n");
keep_on = 0;
break;
}
if (X32debug) {
printf("Server sent: %s; Number of bytes sent: %d\n",
r_buf, r_len);
fflush(stdout);
}
} else {
if (X32debug) {
printf("Server received no data from UDP\n");
fflush(stdout);
}
}
} else {
if (X32debug) {
printf("No data after timeout\n");
fflush(stdout);
}
if ((send(ssock, "no data", 7, 0)) < 0) {
printf("Failure Sending Message back to Client\n");
keep_on = 0;
break;
}
}
} else {
printf("Reading for X32 data failed\n");
keep_on = 0;
}
}
} // While keep_on...
//
//Close Connection Socket
skt_close(ssock);
if (X32verbose) {
printf("Client socket closed\n");
fflush(stdout);
}
return (0);
}
int X32_init() {
int x_len, p_status;
char x_buf[BSIZE];
int X_fd; // X32 socket
fd_set ufds0;
//
// Load the X32 address we connect to; we're a client to X32, keep it simple.
// Initialize communication with X32 server at IP ip and PORT port
// Set default values to match your X32 desk
strcpy(Xport_str, "10023");
// Create UDP socket
if ((X_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("failed to create X32 socket");
return (0);
}
// Construct server sockaddr_in structure
memset(&Xip, 0, sizeof(Xip)); // Clear struct
Xip.sin_family = AF_INET; // Internet/IP
Xip.sin_addr.s_addr = inet_addr(Xip_str); // IP address
Xip.sin_port = htons(atoi(Xport_str)); // server port/
//
//register for receiving X32 console updates
// Non blocking mode
timeout.tv_sec = 0;
timeout.tv_usec = TIMEOUT * 1000; //Set timeout in ms
//
// All done. Let's send and receive messages
// Establish logical connection with X32 server
if (X32verbose) {
printf("Connecting to X32.");
fflush(stdout);
}
while (1) {
if (sendto(X_fd, "/info\0\0\0", 8, 0, Xip_addr, Xip_len) < 0) {
perror("Error while sending data");
return (0);
}
FD_ZERO(&ufds0);
FD_SET(X_fd, &ufds0);
if((p_status = select(X_fd + 1, &ufds0, NULL, NULL, &timeout)) != -1) {
if (FD_ISSET(X_fd, &ufds0) > 0) {
x_len = recvfrom(X_fd, x_buf, BSIZE, 0, 0, 0);
if (X32debug) {
Xfdump("X->", x_buf, x_len, 0);
fflush(stdout);
}
if (strcmp(x_buf, "/info") == 0)
break; // Connected!
} // ... else timeout
if (X32verbose) {
printf(".");
fflush(stdout);
}
millisleep(500); // wait a little before asking again
} else {
perror("Error while reading data");
return (0);
}
}
if (X32verbose) {
printf(" Done!\n");
fflush(stdout);
}
return (X_fd);
}
void XRcvClean(int X_fd) {
//
// Empty reception buffer while data is available
//
int x_len, p_status;
char x_buf[BSIZE];
fd_set ufds2;
do {
FD_ZERO(&ufds2);
FD_SET(X_fd, &ufds2);
if ((p_status = select(X_fd + 1, &ufds2, NULL, NULL, &timeout)) != -1) {
x_len = 0;
if (FD_ISSET(X_fd, &ufds2) > 0) {
x_len = recvfrom(X_fd, x_buf, BSIZE, 0, 0, 0);
}
if (X32debug) {
printf("XrcvClean: p_status = %d, x_len = %d\n", p_status, x_len);
fflush(stdout);
}
}
} while (p_status > 0);
return;
}