forked from tandyuk/jhPrimeminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonrpc.cpp
477 lines (464 loc) · 15.8 KB
/
jsonrpc.cpp
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include"global.h"
#include <signal.h>
#ifndef _WIN32
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#endif
SOCKET jsonRpc_openConnection(char *ip, int Port)
{
SOCKET s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKADDR_IN addr;
memset(&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family=AF_INET;
addr.sin_port=htons(Port);
addr.sin_addr.s_addr=inet_addr(ip);
int result = connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));
if( result )
{
return 0;
}
return s;
}
/*
* A full request was received from a client a can now be processed
*/
void jsonRpc_processRequest(jsonRpcServer_t* jrs, jsonRpcClient_t* client)
{
char* requestData = (char*)(client->recvBuffer+client->recvDataHeaderEnd);
sint32 requestLength = (sint32)(client->recvDataSizeFull - client->recvDataHeaderEnd);
// parse data
//jsonParser_parse(requestData, requestLength);
jsonObject_t* jsonObject = jsonParser_parse((uint8*)requestData, requestLength);
// get method
jsonObject_t* jsonMethodName = jsonObject_getParameter(jsonObject, "method");
// jsonObject_t* jsonParameter = jsonObject_getParameter(jsonObject, "params"); unused?
uint32 methodNameLength = 0;
uint8* methodNameString = jsonObject_getStringData(jsonMethodName, &methodNameLength);
if( methodNameString )
{
if( methodNameLength == 7 && memcmp(methodNameString, "getwork", 7) == 0 )
{
// ...
}
else
{
printf("JSON-RPC: Unknown method to call - ");
for(uint32 i=0; i<methodNameLength; i++)
{
printf("%c", methodNameString[i]);
}
printf("\n");
}
}
else
{
// invalid json data
// kick the client
#ifdef _WIN32
closesocket(client->clientSocket);
#else
close(client->clientSocket);
#endif
client->clientSocket = 0;
client->disconnected = true;
}
jsonObject_freeObject(jsonObject);
//// close connection (no keep alive)
//closesocket(client->clientSocket);
//client->clientSocket = 0;
//client->disconnected = true;
// reset pointers and indices in case the client sends another request (keep alive)
client->recvIndex = 0;
client->recvDataHeaderEnd = 0;
client->recvDataSizeFull = 0;
}
/*
* Creates a new instance of jsonRPC
* port is the port identifier that is used for incoming connections
* Returns NULL if the socket could not be created (most likely due to port being blocked)
*/
jsonRpcServer_t* jsonRpc_createServer(uint16 port)
{
jsonRpcServer_t* jr = (jsonRpcServer_t*)malloc(sizeof(jsonRpcServer_t));
memset(jr, 0, sizeof(jsonRpcServer_t));
// open port for incoming connections
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN addr;
memset(&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=INADDR_ANY;
#ifdef _WIN32
if( bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN)) == SOCKET_ERROR )
#else
if( bind(s, (SOCKADDR*)&addr,sizeof(SOCKADDR_IN)) == -1 )
#endif
{
#ifdef _WIN32
closesocket(s);
#else
close(s);
#endif
free(jr);
return NULL;
}
listen(s, 64);
jr->acceptSocket = s;
// init misc
jr->list_connections = simpleList_create(32);
// return created server instance
return jr;
}
/*
* Called when the server detects a new incoming connection
*/
void jsonRpcServer_newClient(jsonRpcServer_t* jrs, SOCKET s)
{
// alloc client struct
jsonRpcClient_t* client = (jsonRpcClient_t*)malloc(sizeof(jsonRpcClient_t));
memset(client, 0, sizeof(jsonRpcClient_t));
// init client
client->jsonRpcServer = jrs;
client->clientSocket = s;
// set socket as non-blocking
#ifdef _WIN32
unsigned int nonblocking=1;
unsigned int cbRet;
WSAIoctl(s, FIONBIO, &nonblocking, sizeof(nonblocking), NULL, 0, (LPDWORD)&cbRet, NULL, NULL);
#else
fcntl(s, F_SETFL, O_NONBLOCK);
//TODO: not sure what to do about the recv buffer passed to WSAIoctl here..
#endif
// init recv buffer
client->recvIndex = 0;
client->recvSize = JSON_INITIAL_RECV_BUFFER_SIZE;
client->recvBuffer = (uint8*)malloc(JSON_INITIAL_RECV_BUFFER_SIZE);
// add to client list
simpleList_add(jrs->list_connections, client);
}
/*
* Called when the server knows there is data to be received for a specific client
* This method is also called when the client closes the connection
*/
bool jsonRpcServer_receiveData(jsonRpcServer_t* jrs, jsonRpcClient_t* client)
{
uint32 remainingRecvSize = client->recvSize - client->recvIndex;
if( remainingRecvSize == 0 )
{
// not enough data storage left
// try to enlarge buffer if allowed
if( client->recvSize < JSON_MAX_RECV_BUFFER_SIZE )
{
client->recvSize = std::min<unsigned long>(client->recvSize*2, JSON_MAX_RECV_BUFFER_SIZE); // double buffer size
client->recvBuffer = (uint8*)realloc(client->recvBuffer, client->recvSize);
// recalculate remaining buffer storage
remainingRecvSize = client->recvSize - client->recvIndex;
}
else
{
// evil client sends too much data
// disconnect
printf("JSON-RPC warning: Client tried to send more than 4MB of data\n");
#ifdef _WIN32
closesocket(client->clientSocket);
#else
close(client->clientSocket);
#endif
client->clientSocket = 0;
return false;
}
}
// recv
sint32 r = recv(client->clientSocket, (char*)(client->recvBuffer+client->recvIndex), remainingRecvSize, 0);
if( r <= 0 )
{
#ifdef _WIN32
closesocket(client->clientSocket);
#else
close(client->clientSocket);
#endif
client->clientSocket = 0;
return false;
}
else
{
client->recvIndex += r;
// process data (if no header end found yet)
if( client->recvDataHeaderEnd == 0 )
{
// did we receive the end of the header already?
sint32 scanStart = (sint32)client->recvIndex - (sint32)r;
scanStart = std::max(scanStart-8, 0);
sint32 scanEnd = (sint32)(client->recvIndex);
scanEnd = std::max(scanEnd-4, 0);
for(sint32 s=scanStart; s<=scanEnd; s++)
{
// is header end?
if( client->recvBuffer[s+0] == '\r' &&
client->recvBuffer[s+1] == '\n' &&
client->recvBuffer[s+2] == '\r' &&
client->recvBuffer[s+3] == '\n' )
{
if( s == 0 )
{
printf("JSON-RPC warning: Client sent headerless HTTP request\n");
#ifdef _WIN32
closesocket(client->clientSocket);
#else
close(client->clientSocket);
#endif
return false;
}
// reset some header related values
client->useBasicHTTPAuth = false;
// iterate header for content length field
// when parsing the header we can assume two things
// *) there will be always 4 bytes after endOfHeaderPtr
// *) the 4 bytes at the end will always be \r\n\r\n
char* endOfHeaderPtr = (char*)(client->recvBuffer+s);
char* parsePtr = (char*)client->recvBuffer;
sint32 contentLength = -1;
while( parsePtr < endOfHeaderPtr )
{
// is content-length parameter?
if( (endOfHeaderPtr-parsePtr) >= 15 && memcmp(parsePtr, "Content-Length:", 15) == 0 )
{
// parameter found
// read value and end header parsing (for now)
if( parsePtr[15] == ' ' ) // is there a space between the number and the colon?
contentLength = atoi(parsePtr+16);
else
contentLength = atoi(parsePtr+15);
break;
}
// is http authorization parameter?
//
if( (endOfHeaderPtr-parsePtr) >= 21 && memcmp(parsePtr, "Authorization: Basic ", 21) == 0 )
{
// auth parameter found
char* base64EncodedLogin = (char*)(parsePtr+21);
uint8 httpLoginRaw[256];
sint32 decodedLen = 0;
base64_decode((const unsigned char*)base64EncodedLogin, strstr(base64EncodedLogin, "\r\n")-base64EncodedLogin, httpLoginRaw, &decodedLen);
// make sure the httpLoginRaw string is null-terminated...
httpLoginRaw[255] = '\0';
// split into username and password
char* offsetSplit = strstr((char*)httpLoginRaw, ":");
if( offsetSplit )
{
char* username = (char*)httpLoginRaw;
char* password = offsetSplit+1;
*offsetSplit = '\0';
fStrCpy(client->httpAuthUsername, username, 63);
fStrCpy(client->httpAuthPassword, password, 63);
client->useBasicHTTPAuth = true;
}
else
{
printf("JSON-RPC: Unable to parse HTTP authorization parameter\n");
}
}
// continue to next line
while( parsePtr < endOfHeaderPtr )
{
parsePtr++;
if( parsePtr[0] == '\r' && parsePtr[1] == '\n' )
{
// next line found
parsePtr += 2;
break;
}
}
}
if( contentLength <= 0 )
{
printf("JSON-RPC warning: Content-Length header field not present\n");
#ifdef _WIN32
closesocket(client->clientSocket);
#else
close(client->clientSocket);
#endif
return false;
}
// mark header and packet size, then finish processing header
client->recvDataHeaderEnd = s+4;
client->recvDataSizeFull = client->recvDataHeaderEnd + contentLength;
}
}
}
// process data (if header found and full packet received)
if( client->recvDataHeaderEnd != 0 && client->recvIndex >= client->recvDataSizeFull )
{
// process request
jsonRpc_processRequest(jrs, client);
// wipe processed packet and shift remaining data back
client->recvDataHeaderEnd = 0;
// uint32 pRecvIndex = client->recvIndex; unused?
client->recvIndex -= client->recvDataSizeFull;
client->recvDataSizeFull = 0;
if( client->recvIndex > 0 )
#ifdef _WIN32
__debugbreak(); // todo -> The client tried to send us more than one single request?
#else
raise(SIGTRAP);
#endif
}
}
return true;
}
/*
* Frees all memory used by a client
* Does not close connection
*/
void jsonRpc_deleteClient(jsonRpcClient_t* client)
{
free(client->recvBuffer);
free(client);
}
/*
* Begins the jsonRPC server endless loop
* Never quits unless jsonRpc_stop() is called (todo)
* Can be run in a separate thread
*/
int jsonRpc_run(jsonRpcServer_t* jrs)
{
fd_set fd;
timeval sTimeout;
sTimeout.tv_sec = 1;
sTimeout.tv_usec = 0;
while( true )
{
FD_ZERO(&fd);
// add server accept socket
FD_SET(jrs->acceptSocket, &fd);
// add all connected sockets
for(uint32 i=0; i<jrs->list_connections->objectCount; i++)
{
jsonRpcClient_t* client = (jsonRpcClient_t*)simpleList_get(jrs->list_connections, i);
FD_SET(client->clientSocket, &fd);
}
// check for socket events
sint32 r = select(0, &fd, 0, 0, &sTimeout); // wait one second
if( r )
{
// check for new connections
if( FD_ISSET(jrs->acceptSocket, &fd) )
{
jsonRpcServer_newClient(jrs, accept(jrs->acceptSocket, 0, 0));
}
// check for client data received
for(uint32 i=0; i<jrs->list_connections->objectCount; i++)
{
jsonRpcClient_t* client = (jsonRpcClient_t*)simpleList_get(jrs->list_connections, i);
if( FD_ISSET(client->clientSocket, &fd) )
{
if( jsonRpcServer_receiveData(jrs, client) == false )
{
// something went wrong inside of _receiveData()
// make sure the client gets disconnected
client->disconnected = true;
}
}
if( client->disconnected )
{
// delete client
jsonRpc_deleteClient(client);
// remove from list
jrs->list_connections->objects[i] = jrs->list_connections->objects[jrs->list_connections->objectCount-1];
jrs->list_connections->objectCount--;
}
}
}
else
Sleep(1);
}
return 0;
}
void jsonRpc_sendResponse(jsonRpcServer_t* jrs, jsonRpcClient_t* client, jsonObject_t* jsonObjectReturnValue)
{
// build json request data
// example: {"method": "getwork", "params": [], "id":0}
fStr_t* fStr_jsonRequestData = fStr_alloc(1024*64); // 64KB (this is also used as the recv buffer!)
fStr_appendFormatted(fStr_jsonRequestData, "{ \"result\": ");
// { "result": "Hallo JSON-RPC", "error": null, "id": 1}
jsonBuilder_buildObjectString(fStr_jsonRequestData, jsonObjectReturnValue);
fStr_append(fStr_jsonRequestData, ", \"error\": null, \"id\": 1");
// prepare header
fStr_buffer1kb_t fStrBuffer_header;
fStr_t* fStr_headerData = fStr_alloc(&fStrBuffer_header, FSTR_FORMAT_UTF8);
// header fields
fStr_appendFormatted(fStr_headerData, "HTTP/1.1 200 OK\r\n");
fStr_appendFormatted(fStr_headerData, "Server: ypoolbackend 0.1\r\n");
fStr_appendFormatted(fStr_headerData, "Connection: keep-alive\r\n"); // no keep-alive
fStr_appendFormatted(fStr_headerData, "Content-Type: application/json\r\n");
fStr_appendFormatted(fStr_headerData, "Content-Length: %d\r\n", fStr_len(fStr_jsonRequestData));
fStr_appendFormatted(fStr_headerData, "\r\n"); // empty line concludes the header
// send header and data
send(client->clientSocket, fStr_get(fStr_headerData), fStr_len(fStr_headerData), 0);
send(client->clientSocket, fStr_get(fStr_jsonRequestData), fStr_len(fStr_jsonRequestData), 0);
fStr_free(fStr_jsonRequestData);
}
/*
* Allows to directly send raw json data instead of generating it on the fly
*/
void jsonRpc_sendResponseRaw(jsonRpcServer_t* jrs, jsonRpcClient_t* client, fStr_t* fStr_responseRawData, char* additionalHeaderData)
{
// build json request data
// example: {"method": "getwork", "params": [], "id":0}
fStr_t* fStr_jsonRequestData = fStr_alloc(1024*64); // 64KB (this is also used as the recv buffer!)
fStr_appendFormatted(fStr_jsonRequestData, "{ \"result\": ");
// { "result": "Hallo JSON-RPC", "error": null, "id": 1}
fStr_append(fStr_jsonRequestData, fStr_responseRawData);
fStr_append(fStr_jsonRequestData, ", \"error\": null, \"id\": 1}");
// prepare header
fStr_buffer1kb_t fStrBuffer_header;
fStr_t* fStr_headerData = fStr_alloc(&fStrBuffer_header, FSTR_FORMAT_UTF8);
// header fields
fStr_appendFormatted(fStr_headerData, "HTTP/1.1 200 OK\r\n");
fStr_appendFormatted(fStr_headerData, "Server: ypoolbackend 0.1\r\n");
fStr_appendFormatted(fStr_headerData, "Connection: keep-alive\r\n"); // keep-alive
fStr_appendFormatted(fStr_headerData, "Content-Type: application/json\r\n");
fStr_appendFormatted(fStr_headerData, "Content-Length: %d\r\n", fStr_len(fStr_jsonRequestData));
if( additionalHeaderData )
fStr_appendFormatted(fStr_headerData, "%s", additionalHeaderData);
fStr_appendFormatted(fStr_headerData, "\r\n"); // empty line concludes the header
// send header and data
send(client->clientSocket, fStr_get(fStr_headerData), fStr_len(fStr_headerData), 0);
send(client->clientSocket, fStr_get(fStr_jsonRequestData), fStr_len(fStr_jsonRequestData), 0);
fStr_free(fStr_jsonRequestData);
}
/*
* Allows to directly send raw json data instead of generating it on the fly
*/
void jsonRpc_sendFailedToAuthorize(jsonRpcServer_t* jrs, jsonRpcClient_t* client)
{
// build json request data
// example: {"method": "getwork", "params": [], "id":0}
fStr_t* fStr_jsonRequestData = fStr_alloc(1024*64); // 64KB (this is also used as the recv buffer!)
fStr_append(fStr_jsonRequestData, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""
"\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">"
"<HTML>"
"<HEAD>"
"<TITLE>Error</TITLE>"
"<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>"
"</HEAD>"
"<BODY><H1>401 Unauthorized.</H1></BODY>"
"</HTML>"
);
// prepare header
fStr_buffer1kb_t fStrBuffer_header;
fStr_t* fStr_headerData = fStr_alloc(&fStrBuffer_header, FSTR_FORMAT_UTF8);
// header fields
fStr_appendFormatted(fStr_headerData, "HTTP/1.0 401 Authorization Required\r\n");
fStr_appendFormatted(fStr_headerData, "Server: ypoolbackend 0.1\r\n");
fStr_appendFormatted(fStr_headerData, "WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n");
fStr_appendFormatted(fStr_headerData, "Connection: keep-alive\r\n"); // keep-alive
fStr_appendFormatted(fStr_headerData, "Content-Type: text/html\r\n");
fStr_appendFormatted(fStr_headerData, "Content-Length: %d\r\n", fStr_len(fStr_jsonRequestData));
fStr_appendFormatted(fStr_headerData, "\r\n"); // empty line concludes the header
// send header and data
send(client->clientSocket, fStr_get(fStr_headerData), fStr_len(fStr_headerData), 0);
send(client->clientSocket, fStr_get(fStr_jsonRequestData), fStr_len(fStr_jsonRequestData), 0);
fStr_free(fStr_jsonRequestData);
}