forked from jl777/iguana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iguana_json.c
executable file
·424 lines (407 loc) · 15.1 KB
/
iguana_json.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
/******************************************************************************
* Copyright © 2014-2015 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#include "iguana777.h"
int32_t iguana_launchcoin(char *symbol,cJSON *json);
struct iguana_jsonitem { struct queueitem DL; uint32_t expired,allocsize; char **retjsonstrp; char jsonstr[]; };
queue_t finishedQ,helperQ;
static struct iguana_info Coins[64];
const char *Hardcoded_coins[][3] = { { "BTC", "bitcoin", "0" }, { "BTCD", "BitcoinDark", "129" } };
struct iguana_info *iguana_coin(const char *symbol)
{
struct iguana_info *coin; int32_t i = 0;
if ( symbol == 0 )
{
for (i=sizeof(Hardcoded_coins)/sizeof(*Hardcoded_coins); i<sizeof(Coins)/sizeof(*Coins); i++)
{
if ( Coins[i].symbol[0] == 0 )
{
memset(&Coins[i],0,sizeof(Coins[i]));
printf("iguana_coin.(new) -> %p\n",&Coins[i]);
return(&Coins[i]);
} return(0);
printf("i.%d (%s) vs name.(%s)\n",i,Coins[i].name,symbol);
}
}
else
{
for (i=0; i<sizeof(Hardcoded_coins)/sizeof(*Hardcoded_coins); i++)
{
coin = &Coins[i];
if ( strcmp(symbol,Hardcoded_coins[i][0]) == 0 )
{
if ( coin->chain == 0 )
{
strcpy(coin->name,Hardcoded_coins[i][1]);
coin->myservices = atoi(Hardcoded_coins[i][2]);
strcpy(coin->symbol,symbol);
coin->chain = iguana_chainfind(coin->symbol);
iguana_initcoin(coin);
}
return(coin);
}
}
}
return(0);
}
cJSON *iguana_peerjson(struct iguana_info *coin,struct iguana_peer *addr)
{
cJSON *array,*json = cJSON_CreateObject();
jaddstr(json,"ipaddr",addr->ipaddr);
jaddnum(json,"protover",addr->protover);
jaddnum(json,"relay",addr->relayflag);
jaddnum(json,"height",addr->height);
jaddnum(json,"rank",addr->rank);
jaddnum(json,"usock",addr->usock);
if ( addr->dead != 0 )
jaddnum(json,"dead",addr->dead);
jaddnum(json,"ready",addr->ready);
array = cJSON_CreateObject();
jaddnum(array,"version",addr->msgcounts.version);
jaddnum(array,"verack",addr->msgcounts.verack);
jaddnum(array,"getaddr",addr->msgcounts.getaddr);
jaddnum(array,"addr",addr->msgcounts.addr);
jaddnum(array,"inv",addr->msgcounts.inv);
jaddnum(array,"getdata",addr->msgcounts.getdata);
jaddnum(array,"notfound",addr->msgcounts.notfound);
jaddnum(array,"getblocks",addr->msgcounts.getblocks);
jaddnum(array,"getheaders",addr->msgcounts.getheaders);
jaddnum(array,"headers",addr->msgcounts.headers);
jaddnum(array,"tx",addr->msgcounts.tx);
jaddnum(array,"block",addr->msgcounts.block);
jaddnum(array,"mempool",addr->msgcounts.mempool);
jaddnum(array,"ping",addr->msgcounts.ping);
jaddnum(array,"pong",addr->msgcounts.pong);
jaddnum(array,"reject",addr->msgcounts.reject);
jaddnum(array,"filterload",addr->msgcounts.filterload);
jaddnum(array,"filteradd",addr->msgcounts.filteradd);
jaddnum(array,"filterclear",addr->msgcounts.filterclear);
jaddnum(array,"merkleblock",addr->msgcounts.merkleblock);
jaddnum(array,"alert",addr->msgcounts.alert);
jadd(json,"msgcounts",array);
return(json);
}
cJSON *iguana_peersjson(struct iguana_info *coin)
{
cJSON *retjson,*array; int32_t i; struct iguana_peer *addr;
retjson = cJSON_CreateObject();
array = cJSON_CreateArray();
for (i=0; i<coin->MAXPEERS; i++)
{
addr = &coin->peers.active[i];
if ( addr->usock >= 0 && addr->ipbits != 0 && addr->ipaddr[0] != 0 )
jaddi(array,iguana_peerjson(coin,addr));
}
jadd(retjson,"peers",array);
jaddnum(retjson,"maxpeers",coin->MAXPEERS);
jaddstr(retjson,"coin",coin->symbol);
return(retjson);
}
char *iguana_genericjson(char *method,cJSON *json)
{
cJSON *retjson,*array; int32_t i;
if ( strcmp(method,"list") == 0 )
{
retjson = cJSON_CreateObject();
array = cJSON_CreateArray();
for (i=0; i<sizeof(Coins)/sizeof(*Coins); i++)
{
if ( Coins[i].symbol[0] != 0 )
jaddistr(array,Coins[i].symbol);
}
jadd(retjson,"coins",array);
return(jprint(retjson,1));
}
if ( strcmp(method,"peers") == 0 )
{
retjson = cJSON_CreateObject();
array = cJSON_CreateArray();
for (i=0; i<sizeof(Coins)/sizeof(*Coins); i++)
{
if ( Coins[i].symbol[0] != 0 )
jaddi(array,iguana_peersjson(&Coins[i]));
}
jadd(retjson,"allpeers",array);
return(jprint(retjson,1));
}
return(clonestr("{\"result\":\"stub processed generic json\"}"));
}
char *iguana_json(struct iguana_info *coin,char *method,cJSON *json)
{
int32_t i,max,retval; struct iguana_peer *addr; char *ipaddr; cJSON *retjson = 0;
if ( strcmp(method,"peers") == 0 )
return(jprint(iguana_peersjson(coin),1));
else if ( strcmp(method,"addnode") == 0 )
{
if ( (ipaddr= jstr(json,"ipaddr")) != 0 )
{
iguana_possible_peer(coin,ipaddr);
return(clonestr("{\"result\":\"addnode submitted\"}"));
} else return(clonestr("{\"error\":\"addnode needs ipaddr\"}"));
}
else if ( strcmp(method,"nodestatus") == 0 )
{
if ( (ipaddr= jstr(json,"ipaddr")) != 0 )
{
for (i=0; i<coin->MAXPEERS; i++)
{
addr = &coin->peers.active[i];
if ( strcmp(addr->ipaddr,ipaddr) == 0 )
return(jprint(iguana_peerjson(coin,addr),1));
}
return(clonestr("{\"result\":\"nodestatus couldnt find ipaddr\"}"));
} else return(clonestr("{\"error\":\"nodestatus needs ipaddr\"}"));
}
else if ( strcmp(method,"maxpeers") == 0 )
{
retjson = cJSON_CreateObject();
if ( (max= juint(json,"max")) <= 0 )
max = 1;
else if ( max > IGUANA_MAXPEERS )
max = IGUANA_MAXPEERS;
if ( max > coin->MAXPEERS )
{
for (i=max; i<coin->MAXPEERS; i++)
if ( (addr= coin->peers.ranked[i]) != 0 )
addr->dead = 1;
}
coin->MAXPEERS = max;
jaddnum(retjson,"maxpeers",coin->MAXPEERS);
jaddstr(retjson,"coin",coin->symbol);
return(jprint(retjson,1));
}
else if ( strcmp(method,"startcoin") == 0 )
{
coin->active = 1;
return(clonestr("{\"result\":\"coin started\"}"));
}
else if ( strcmp(method,"pausecoin") == 0 )
{
coin->active = 1;
return(clonestr("{\"result\":\"coin paused\"}"));
}
else if ( strcmp(method,"addcoin") == 0 )
{
if ( (retval= iguana_launchcoin(coin->symbol,json)) > 0 )
return(clonestr("{\"result\":\"coin added\"}"));
else if ( retval == 0 )
return(clonestr("{\"result\":\"coin already there\"}"));
else return(clonestr("{\"error\":\"error adding coin\"}"));
}
return(clonestr("{\"result\":\"stub processed iguana json\"}"));
}
char *iguana_jsonstr(struct iguana_info *coin,char *jsonstr)
{
cJSON *json; char *retjsonstr,*methodstr;
if ( (json= cJSON_Parse(jsonstr)) != 0 )
{
if ( (methodstr= jstr(json,"method")) != 0 )
retjsonstr = iguana_json(coin,methodstr,json);
else retjsonstr = clonestr("{\"error\":\"no method in JSON\"}");
free_json(json);
} else retjsonstr = clonestr("{\"error\":\"cant parse JSON\"}");
printf("iguana_jsonstr.(%s)\n",retjsonstr);
return(retjsonstr);
}
char *iguana_genericjsonstr(char *jsonstr)
{
cJSON *json; char *retjsonstr,*methodstr;
if ( (json= cJSON_Parse(jsonstr)) != 0 )
{
if ( (methodstr= jstr(json,"method")) != 0 )
retjsonstr = iguana_genericjson(methodstr,json);
else retjsonstr = clonestr("{\"error\":\"no method in generic JSON\"}");
free_json(json);
} else retjsonstr = clonestr("{\"error\":\"cant parse generic JSON\"}");
return(retjsonstr);
}
int32_t iguana_processjsonQ(struct iguana_info *coin) // reentrant, can be called during any idletime
{
struct iguana_jsonitem *ptr;
if ( (ptr= queue_dequeue(&finishedQ,0)) != 0 )
{
if ( ptr->expired != 0 )
{
printf("garbage collection: expired.(%s)\n",ptr->jsonstr);
myfree(ptr,ptr->allocsize);
} else queue_enqueue("finishedQ",&finishedQ,&ptr->DL,0);
}
if ( (ptr= queue_dequeue(&coin->jsonQ,0)) != 0 )
{
if ( (*ptr->retjsonstrp= iguana_jsonstr(coin,ptr->jsonstr)) == 0 )
*ptr->retjsonstrp = clonestr("{\"error\":\"null return from iguana_jsonstr\"}");
queue_enqueue("finishedQ",&finishedQ,&ptr->DL,0);
return(1);
}
return(0);
}
char *iguana_blockingjsonstr(struct iguana_info *coin,char *jsonstr,uint64_t tag,int32_t maxmillis)
{
struct iguana_jsonitem *ptr; char *retjsonstr; int32_t len,allocsize; double expiration = milliseconds() + maxmillis;
if ( coin == 0 )
return(iguana_genericjsonstr(jsonstr));
else
{
len = (int32_t)strlen(jsonstr);
allocsize = sizeof(*ptr) + len + 1;
ptr = mycalloc('J',1,allocsize);
ptr->allocsize = allocsize;
ptr->retjsonstrp = &retjsonstr;
memcpy(ptr->jsonstr,jsonstr,len+1);
queue_enqueue("jsonQ",&coin->jsonQ,&ptr->DL,0);
while ( milliseconds() < expiration )
{
usleep(100);
if ( (retjsonstr= *ptr->retjsonstrp) != 0 )
{
printf("blocking retjsonstr.(%s)\n",retjsonstr);
queue_delete(&finishedQ,&ptr->DL,allocsize,1);
return(retjsonstr);
}
usleep(1000);
}
printf("(%s) expired\n",jsonstr);
ptr->expired = (uint32_t)time(NULL);
return(clonestr("{\"error\":\"iguana jsonstr expired\"}"));
}
}
char *iguana_JSON(char *jsonstr)
{
cJSON *json,*retjson; uint64_t tag; uint32_t timeout; int32_t retval;
struct iguana_info *coin; char *method,*retjsonstr,*symbol,*retstr = 0;
printf("iguana_JSON.(%s)\n",jsonstr);
if ( (json= cJSON_Parse(jsonstr)) != 0 )
{
if ( (method= jstr(json,"method")) != 0 && strcmp(method,"addcoin") == 0 )
{
if ( (retval= iguana_launchcoin(jstr(json,"coin"),json)) > 0 )
return(clonestr("{\"result\":\"launched coin\"}"));
else if ( retval == 0 ) return(clonestr("{\"result\":\"coin already launched\"}"));
else return(clonestr("{\"error\":\"error launching coin\"}"));
}
if ( (tag= j64bits(json,"tag")) == 0 )
randombytes((uint8_t *)&tag,sizeof(tag));
if ( (symbol= jstr(json,"coin")) != 0 )
{
if ( (coin= iguana_coin(symbol)) != 0 && coin->launched == 0 )
iguana_launchcoin(symbol,json);
}
else coin = 0;
if ( (timeout= juint(json,"timeout")) == 0 )
timeout = IGUANA_JSONTIMEOUT;
if ( (retjsonstr= iguana_blockingjsonstr(coin,jsonstr,tag,timeout)) != 0 )
{
if ( (retjson= cJSON_Parse(retjsonstr)) == 0 )
{
printf("retjsonstr.(%s)\n",retjsonstr);
retjson = cJSON_Parse("{\"error\":\"cant parse retjsonstr\"}");
}
jdelete(retjson,"tag");
jadd64bits(retjson,"tag",tag);
retstr = jprint(retjson,1);
free(retjsonstr);//,strlen(retjsonstr)+1);
}
free_json(json);
} else retstr = clonestr("{\"error\":\"cant parse JSON\"}");
if ( retstr == 0 )
retstr = clonestr("{\"error\":\"null return\"}");
return(retstr);
}
void iguana_issuejsonstrM(void *arg)
{
cJSON *json; int32_t fd; char *retjsonstr,*jsonstr = arg;
retjsonstr = iguana_JSON(jsonstr);
if ( (json= cJSON_Parse(jsonstr)) != 0 )
{
if ( (fd= juint(json,"retdest")) > 0 )
{
send(fd,jsonstr,(int32_t)strlen(jsonstr)+1,MSG_NOSIGNAL);
}
free_json(json);
return;
}
printf("%s\n",retjsonstr);
free(retjsonstr);//,strlen(retjsonstr)+1);
free(jsonstr);//,strlen(jsonstr)+1);
}
void iguana_helper(void *arg)
{
int32_t flag; struct iguana_bundlereq *req; queue_t *Q = arg;
printf("start helper\n");
while ( 1 )
{
flag = 0;
if ( (req= queue_dequeue(Q,0)) != 0 )
{
printf("START emittxdata\n");
iguana_emittxdata(req->coin,req);
flag++;
printf("FINISH emittxdata\n");
}
if ( flag == 0 )
sleep(1);
}
}
void iguana_main(void *arg)
{
int32_t i,len,flag; cJSON *json; uint8_t secretbuf[512]; char *coinargs,*secret,*jsonstr = arg;
// portable_OS_init()?
mycalloc(0,0,0);
iguana_initQ(&helperQ,"helperQ");
ensure_directory("DB");
ensure_directory("tmp");
if ( jsonstr != 0 && (json= cJSON_Parse(jsonstr)) != 0 )
{
IGUANA_NUMHELPERS = juint(json,"numhelpers");
if ( (secret= jstr(json,"secret")) != 0 )
{
len = (int32_t)strlen(secret);
if ( is_hexstr(secret) != 0 && len <= (sizeof(secretbuf)<<1) )
{
len >>= 1;
decode_hex(secretbuf,len,secret);
} else vcalc_sha256(0,secretbuf,(void *)secret,len), len = sizeof(bits256);
}
if ( jobj(json,"coins") != 0 )
coinargs = jsonstr;
}
if ( IGUANA_NUMHELPERS == 0 )
{
#ifdef __linux__
IGUANA_NUMHELPERS = 8;
#else
IGUANA_NUMHELPERS = 1;
#endif
}
for (i=0; i<IGUANA_NUMHELPERS; i++)
iguana_launch("helpers",iguana_helper,&helperQ,IGUANA_HELPERTHREAD);
if ( coinargs != 0 )
iguana_launch("iguana_coins",iguana_coins,coinargs,IGUANA_PERMTHREAD);
else
{
#ifdef __APPLE__
sleep(1);
iguana_JSON("{\"agent\":\"iguana\",\"method\":\"addcoin\",\"coin\":\"BTCD\",\"active\":1}");
#endif
}
while ( 1 )
{
flag = 0;
for (i=0; i<sizeof(Coins)/sizeof(*Coins); i++)
if ( Coins[i].symbol[0] != 0 )
flag += iguana_processjsonQ(&Coins[i]);
if ( flag == 0 )
usleep(100000);
}
}