/
proto.c
251 lines (206 loc) · 6.35 KB
/
proto.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
/* RSound - A PCM audio client/server
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* RSound is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RSound is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RSound.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "proto.h"
#include "endian.h"
#include "audio.h"
#include <poll.h>
#ifdef _WIN32
#define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#undef close
#define close(x) closesocket(x)
#endif
extern const rsd_backend_callback_t *backend;
typedef struct rsd_proto
{
int proto;
int64_t client_ptr;
int64_t serv_ptr;
char identity[256];
} rsd_proto_t;
static int get_proto(rsd_proto_t *proto, char *rsd_proto_header);
static int send_proto(int ctl_sock, rsd_proto_t *proto);
// Here we handle all requests from the client that are available in the network buffer. We are using non-blocking socket.
// If recv() returns less than we expect, we bail out as there is not more data to be read.
int handle_ctl_request(connection_t *conn, void *data)
{
char rsd_proto_header[RSD_PROTO_MAXSIZE + 1];
rsd_proto_t proto;
struct pollfd fd = {
.fd = conn->ctl_socket,
.events = POLLIN
};
int rc;
for(;;)
{
if ( poll(&fd, 1, 0) < 0 )
{
perror("poll");
return -1;
}
if ( !(fd.revents & POLLIN) )
{
// We're done here.
return 0;
}
memset(rsd_proto_header, 0, sizeof(rsd_proto_header));
rc = recv(conn->ctl_socket, rsd_proto_header, RSD_PROTO_CHUNKSIZE, 0);
if ( rc <= 0 )
{
return 0;
}
char *substr;
// Makes sure we have a valid header before reading any more.
if ( (substr = strstr(rsd_proto_header, "RSD")) == NULL )
{
continue;
}
while ( *substr != ' ' && *substr != '\0' )
substr++;
// Recieve length on the message from client.
long int len = strtol(substr, NULL, 0);
if ( len > RSD_PROTO_MAXSIZE )
{
continue;
}
memset(rsd_proto_header, 0, sizeof(rsd_proto_header));
rc = recv(conn->ctl_socket, rsd_proto_header, len, 0);
if ( rc <= 0 )
return 0;
else if ( rc != len )
{
// We're done here.
return 0;
}
// Let's parse this.
if ( get_proto(&proto, rsd_proto_header) < 0 )
// We recieved invalid proto
continue;
switch ( proto.proto )
{
case RSD_PROTO_NULL:
break;
case RSD_PROTO_STOP:
return -1;
break;
case RSD_PROTO_INFO:
proto.serv_ptr = conn->serv_ptr;
if ( backend->latency != NULL )
{
proto.serv_ptr -= (int)(backend->latency(data) / conn->rate_ratio);
}
if ( send_proto(conn->ctl_socket, &proto) < 0 )
return -1;
break;
case RSD_PROTO_IDENTITY:
strncpy(conn->identity, proto.identity, sizeof(conn->identity));
break;
case RSD_PROTO_CLOSECTL:
send_proto(conn->ctl_socket, &proto);
if ( conn->ctl_socket != 0 )
close(conn->ctl_socket);
conn->ctl_socket = 0;
return 0; // No point in continuing here.
default:
return -1;
}
}
}
static int get_proto(rsd_proto_t *proto, char *rsd_proto_header)
{
const char *substr;
// Jumps forward in the buffer until we hit a valid character as per protocol.
while ( *rsd_proto_header == ' ' )
rsd_proto_header++;
if ( strstr(rsd_proto_header, "NULL") != NULL )
{
proto->proto = RSD_PROTO_NULL;
return 0;
}
else if ( strstr(rsd_proto_header, "STOP") != NULL )
{
proto->proto = RSD_PROTO_STOP;
return 0;
}
else if ( (substr = strstr(rsd_proto_header, "INFO ")) != NULL )
{
proto->proto = RSD_PROTO_INFO;
// Jump forward after INFO
rsd_proto_header += 5;
int64_t client_ptr;
client_ptr = strtoull(rsd_proto_header, NULL, 10);
proto->client_ptr = client_ptr;
return 0;
}
else if ( (substr = strstr(rsd_proto_header, "IDENTITY ")) != NULL )
{
proto->proto = RSD_PROTO_IDENTITY;
rsd_proto_header += strlen("IDENTITY ");
strncpy(proto->identity, rsd_proto_header, sizeof(proto->identity));
proto->identity[sizeof(proto->identity)-1] = '\0';
return 0;
}
else if ( (substr = strstr(rsd_proto_header, "CLOSECTL")) != NULL )
{
proto->proto = RSD_PROTO_CLOSECTL;
return 0;
}
return -1;
}
static int send_proto(int ctl_sock, rsd_proto_t *proto)
{
struct pollfd fd = {
.fd = ctl_sock,
.events = POLLOUT
};
if ( poll(&fd, 1, 0) < 0 )
{
perror("poll");
return -1;
}
if ( fd.revents & POLLOUT )
{
char sendbuf[RSD_PROTO_MAXSIZE] = {0};
char tempbuf[RSD_PROTO_MAXSIZE] = {0};
int rc;
switch ( proto->proto )
{
case RSD_PROTO_INFO:
#ifdef _WIN32
snprintf(tempbuf, RSD_PROTO_MAXSIZE - 1, " INFO %I64d %I64d", (__int64)proto->client_ptr, (__int64)proto->serv_ptr);
#else
snprintf(tempbuf, RSD_PROTO_MAXSIZE - 1, " INFO %lld %lld", (long long int)proto->client_ptr, (long long int)proto->serv_ptr);
#endif
snprintf(sendbuf, RSD_PROTO_MAXSIZE - 1, "RSD%5d%s", (int)strlen(tempbuf), tempbuf);
//log_printf("Sent info: \"%s\"\n", sendbuf);
rc = send(ctl_sock, sendbuf, strlen(sendbuf), 0);
if ( rc < 0 )
return -1;
break;
case RSD_PROTO_CLOSECTL:
strncpy(sendbuf, "RSD 12 CLOSECTL OK", sizeof(sendbuf)-1);
rc = send(ctl_sock, sendbuf, strlen(sendbuf), 0);
if ( rc < 0 )
return -1;
break;
default:
return -1;
}
}
return 0;
}