-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathutilwrapper.cpp
More file actions
952 lines (819 loc) · 24 KB
/
utilwrapper.cpp
File metadata and controls
952 lines (819 loc) · 24 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
// Copyright (c) 2014-2016 The Bitcoin Core developers
// Original code was distributed under the MIT software license.
// Copyright (c) 2014-2017 Coin Sciences Ltd
// MultiChain code distributed under the GPLv3 license, see COPYING file.
#include "multichain/multichain.h"
#include "crypto/sha256.h"
#include "structs/base58.h"
#ifndef WIN32
#include <sys/ioctl.h>
#else
#ifdef _MSC_VER
#pragma warning(disable:4786)
#pragma warning(disable:4804)
#pragma warning(disable:4805)
#pragma warning(disable:4717)
#endif
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0501
#ifdef _WIN32_IE
#undef _WIN32_IE
#endif
#define _WIN32_IE 0x0501
#define WIN32_LEAN_AND_MEAN 1
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <io.h> /* for _commit */
#include <shlobj.h>
#endif
#include "chainparams/chainparams.h"
#include "utils/util.h"
#include "utils/utilstrencodings.h"
#include "structs/hash.h"
#include "core/main.h"
#include "net/net.h"
#define MC_DCT_SEED_NODE_MAX_SIZE 32
//#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
//#include <boost/algorithm/string/join.hpp>
//#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
//#include <boost/foreach.hpp>
#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>
//#include <boost/thread.hpp>
//#include <openssl/crypto.h>
//#include <openssl/rand.h>
/*
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/program_options/detail/config_file.hpp>
*/
using namespace std;
const boost::filesystem::path mc_GetDataDir(const char *network_name,int create);
void mc_Params::Parse(int argc, const char* const argv[])
{
int i,length;
const char* exe_name;
ParseParameters(argc,argv);
mc_ExpandDataDirParam();
m_NumArguments=0;
length=MC_DCT_SEED_NODE_MAX_SIZE+1;
for (i = 1; i < argc; i++)
{
if(argv[i][0] != '-')
{
m_NumArguments++;
length+=strlen(argv[i])+1;
}
}
if(m_NumArguments)
{
m_Arguments=(char**)mc_New((m_NumArguments+1)*sizeof(char*));
m_Arguments[0]=(char*)mc_New(length);
}
m_NumArguments=0;
length=0;
for (i = 1; i < argc; i++)
{
if(argv[i][0] != '-')
{
m_Arguments[m_NumArguments]=m_Arguments[0]+length;
strcpy(m_Arguments[m_NumArguments],argv[i]);
m_NumArguments++;
length+=strlen(argv[i])+1;
}
}
if(m_NumArguments)
{
exe_name=argv[0];
for(i=0;i<(int)strlen(argv[0]);i++)
{
if((argv[0][i]=='/') || (argv[0][i]=='\\'))
{
exe_name=argv[0]+i+1;
}
}
if((strcmp(exe_name,"multichain-util") == 0) || (strcmp(exe_name,"multichain-util.exe") == 0))
{
m_FirstArgumentType=MC_FAT_COMMAND;
}
else
{
if((strcmp(exe_name,"multichain-cli") == 0) ||
(strcmp(exe_name,"multichain-cli.exe") == 0) ||
(strcmp(exe_name,"multichaind") == 0) ||
(strcmp(exe_name,"multichaind.exe") == 0))
{
m_FirstArgumentType=MC_FAT_NETWORK;
for(i=0;i<(int)strlen(m_Arguments[0]);i++)
{
if(m_FirstArgumentType == MC_FAT_NETWORK)
{
if(m_Arguments[0][i] == '@')
{
m_FirstArgumentType=MC_FAT_NETWORKSEED;
m_Arguments[0][i]=0x00;
}
}
}
if((m_FirstArgumentType == MC_FAT_NETWORK) && ((strcmp(exe_name,"multichaind") == 0) || (strcmp(exe_name,"multichaind.exe") == 0)))
{
m_Arguments[m_NumArguments]=m_Arguments[0]+length;
m_Arguments[m_NumArguments][0]=0x00;
length++;
mc_MapStringString *mapConfig;
int err;
const char *seed_node;
mapConfig=new mc_MapStringString;
err=mc_ReadGeneralConfigFile(mapConfig,mc_gState->m_Params->NetworkName(),"seed",".dat");
if(err == MC_ERR_NOERROR)
{
seed_node=mapConfig->Get("seed");
if(seed_node)
{
if(strlen(seed_node) <= MC_DCT_SEED_NODE_MAX_SIZE)
{
strcpy(m_Arguments[m_NumArguments],seed_node);
length+=strlen(seed_node);
}
}
}
delete mapConfig;
m_NumArguments++;
}
}
}
}
}
const char *mc_Params::NetworkName()
{
if((m_FirstArgumentType == MC_FAT_NETWORK) || (m_FirstArgumentType == MC_FAT_NETWORKSEED))
{
return m_Arguments[0];
}
return NULL;
}
const char *mc_Params::SeedNode()
{
const char *seed_node;
if(m_FirstArgumentType == MC_FAT_NETWORKSEED)
{
return m_Arguments[0]+strlen(m_Arguments[0])+1;
}
if(m_FirstArgumentType == MC_FAT_NETWORK)
{
seed_node=m_Arguments[m_NumArguments-1];
if(*seed_node)
{
return seed_node;
}
}
return NULL;
}
const char* mc_State::GetSeedNode()
{
const char *seed_node;
seed_node=mc_gState->m_Params->SeedNode();
if(seed_node == NULL)
{
/*
int seed_node_size;
seed_node=(char*)mc_gState->m_NetworkParams->GetParam("seednode",&seed_node_size);
if(seed_node_size <= 1)
{
seed_node=NULL;
}
*/
}
return seed_node;
}
const char *mc_Params::Command()
{
if(m_FirstArgumentType == MC_FAT_COMMAND)
{
return m_Arguments[0];
}
return NULL;
}
const char *mc_Params::DataDir()
{
return DataDir(1,1);
}
const char *mc_Params::DataDir(int network_specific,int create)
{
const char *name=NULL;
if(network_specific)
{
name=NetworkName();
}
boost::filesystem::path path=mc_GetDataDir(name,create);
if(network_specific)
{
strcpy(m_DataDirNetSpecific,path.string().c_str());
return m_DataDirNetSpecific;
}
strcpy(m_DataDir,path.string().c_str());
return m_DataDir;
}
const char* mc_Params::GetOption(const char* strArg, const char* strDefault)
{
return GetArg(string(strArg),string(strDefault)).c_str();
}
int64_t mc_Params::GetOption(const char* strArg, int64_t nDefault)
{
return GetArg(string(strArg),nDefault);
}
int64_t mc_Params::HasOption(const char* strArg)
{
return mapArgs.count(string(strArg));
}
boost::filesystem::path mc_GetDefaultDataDir()
{
// Windows < Vista: C:\Documents and Settings\Username\Application Data\MultiChain
// Windows >= Vista: C:\Users\Username\AppData\Roaming\MultiChain
// Mac and Unix: ~/.multichain
#ifdef WIN32
// Windows
return GetSpecialFolderPath(CSIDL_APPDATA) / "MultiChain";
#else
// Mac and Unix
boost::filesystem::path pathRet;
char* pszHome = getenv("HOME");
if (pszHome == NULL || strlen(pszHome) == 0)
pathRet = boost::filesystem::path("/");
else
pathRet = boost::filesystem::path(pszHome);
return pathRet / ".multichain";
#endif
}
int mc_GetDataDirArg(char *buf)
{
if (mapArgs.count("-datadir"))
{
strcpy(buf,mapArgs["-datadir"].c_str());
return 1;
}
return 0;
}
void mc_UnsetDataDirArg()
{
if (mapArgs.count("-datadir"))
{
mapArgs.erase("-datadir");
}
}
void mc_SetDataDirArg(char *buf)
{
mapArgs["-datadir"] = string(buf);
}
void mc_ExpandDataDirParam()
{
if (mapArgs.count("-datadir"))
{
string original=mapArgs["-datadir"];
if(original.size() > 1)
{
if( (*(original.c_str()) == '~') && (*(original.c_str() + 1) == '/') )
{
const char *homedir=__US_UserHomeDir();
if(homedir)
{
mapArgs["-datadir"]=strprintf("%s%s",homedir,original.c_str()+1);
}
}
}
}
}
void mc_CheckDataDirInConfFile()
{
if (mapArgs.count("-datadir"))
{
return;
}
mc_MapStringString *mapConfig;
mapConfig=new mc_MapStringString;
if(mc_ReadGeneralConfigFile(mapConfig,NULL,"multichain",".conf") == 0)
{
if(mapConfig->Get("datadir") != NULL)
{
mapArgs["-datadir"]=strprintf("%s",mapConfig->Get("datadir"));
mc_ExpandDataDirParam();
}
}
}
const boost::filesystem::path mc_GetDataDir(const char *network_name,int create)
{
boost::filesystem::path path;
if (mapArgs.count("-datadir")) {
path = boost::filesystem::system_complete(mapArgs["-datadir"]);
if (!boost::filesystem::is_directory(path))
{
return path;
}
}
else
{
path = mc_GetDefaultDataDir();
}
if(network_name)
{
path /= std::string(network_name);
}
if(create)
{
boost::filesystem::create_directories(path);
}
return path;
}
void mc_RemoveDataDir(const char *network_name)
{
boost::filesystem::path path;
if (mapArgs.count("-datadir")) {
path = boost::filesystem::system_complete(mapArgs["-datadir"]);
}
else
{
path = mc_GetDefaultDataDir();
}
if(network_name)
{
path /= std::string(network_name);
}
boost::filesystem::remove_all(path);
}
void mc_RemoveDir(const char *network_name,const char *dir_name)
{
boost::filesystem::path path;
if (mapArgs.count("-datadir")) {
path = boost::filesystem::system_complete(mapArgs["-datadir"]);
}
else
{
path = mc_GetDefaultDataDir();
}
if(network_name)
{
path /= std::string(network_name);
}
if(dir_name)
{
path /= std::string(dir_name);
}
boost::filesystem::remove_all(path);
}
string mc_GetFullFileName(const char *network_name,const char *filename, const char *extension,int options)
{
int create;
std::string fullName = filename;
std::string backupName;
fullName += extension;
boost::filesystem::path pathFile;
create=0;
if(options & MC_FOM_CREATE_DIR)
{
create=1;
}
switch(options & MC_FOM_RELATIVE_MASK)
{
case MC_FOM_NONE:
pathFile=fullName;
break;
case MC_FOM_RELATIVE_TO_DATADIR:
pathFile = mc_GetDataDir(network_name,create) / fullName;
break;
}
return pathFile.string();
}
int mc_GetFullFileName(const char *network_name,const char *filename, const char *extension,int options,char *buf)
{
strcpy(buf,mc_GetFullFileName(network_name,filename,extension,options).c_str());
return MC_ERR_NOERROR;
}
int mc_BackupFile(const char *network_name,const char *filename, const char *extension,int options)
{
std::string fullName = mc_GetFullFileName(network_name,filename,extension,options);
std::string backupName;
backupName=fullName + ".bak";
if(rename(fullName.c_str(),backupName.c_str()))
{
return MC_ERR_FILE_WRITE_ERROR;
}
return MC_ERR_NOERROR;
}
int mc_RecoverFile(const char *network_name,const char *filename, const char *extension,int options)
{
std::string fullName = mc_GetFullFileName(network_name,filename,extension,options);
std::string backupName;
backupName=fullName + ".bak";
if(rename(backupName.c_str(),fullName.c_str()))
{
return MC_ERR_FILE_WRITE_ERROR;
}
return MC_ERR_NOERROR;
}
FILE *mc_OpenFile(const char *network_name,const char *filename, const char *extension,const char *mode, int options)
{
return fopen(mc_GetFullFileName(network_name,filename,extension,options).c_str(), mode);
}
int mc_RemoveFile(const char *network_name,const char *filename, const char *extension,int options)
{
return unlink(mc_GetFullFileName(network_name,filename,extension,options).c_str());
}
void mc_CloseFile(FILE *fHan)
{
if(fHan)
{
fclose(fHan);
}
}
size_t mc_ReadFileToBuffer(FILE *fHan,char **lpptr)
{
size_t size;
*lpptr=NULL;
if(fHan==NULL)
{
return -MC_ERR_INTERNAL_ERROR;
}
fseek(fHan, 0L, SEEK_END);
size = ftell(fHan);
fseek(fHan, 0L, SEEK_SET);
if(size<=0)
{
return size;
}
*lpptr=(char*)mc_New(size);
if(*lpptr)
{
if(fread(*lpptr,size,1,fHan) != size)
{
mc_Delete(*lpptr);
*lpptr=NULL;
return -MC_ERR_FILE_READ_ERROR;
}
}
else
{
return -MC_ERR_ALLOCATION;
}
return size;
}
boost::filesystem::path mc_GetConfigFile(const char *network_name,const char *file_name,const char *extension)
{
string fileName="multichain";
if(file_name)
{
fileName = file_name;
}
if(extension)
{
fileName += extension;
}
boost::filesystem::path pathConfigFile(GetArg("-conf", fileName));
if (!pathConfigFile.is_complete())
pathConfigFile = mc_GetDataDir(network_name,0) / pathConfigFile;
return pathConfigFile;
}
static void mc_InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
{
// interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
if (name.find("-no") == 0)
{
std::string positive("-");
positive.append(name.begin()+3, name.end());
if (mapSettingsRet.count(positive) == 0)
{
bool value = !GetBoolArg(name, false);
mapSettingsRet[positive] = (value ? "1" : "0");
}
}
}
int mc_ReadParamArgs(mc_MapStringString *mapConfig,
int argc, char* argv[],
const char *prefix)
{
int p;
char *ptr;
map<string, string>* mapSettingsRet=(std::map<string, string>*)mapConfig->mapObject;
for(int argi=1;argi<argc;argi++)
{
// Don't overwrite existing settings so command line settings override bitcoin.conf
if(argv[argi][0]== '-')
{
p=0;
ptr=argv[argi]+1;
while((p == 0) && *ptr)
{
if(*ptr == '=')
{
p=ptr-argv[argi];
}
ptr++;
}
if(p > 1)
{
string strKey = argv[argi]+1;
strKey=strKey.substr(0,p-1);
strKey.erase(std::remove(strKey.begin(), strKey.end(), '-'), strKey.end());
strKey.erase(std::remove(strKey.begin(), strKey.end(), '_'), strKey.end());
strKey = string(prefix) + strKey;
if ((*mapSettingsRet).count(strKey) == 0)
{
(*mapSettingsRet)[strKey] = argv[argi]+p+1;
// interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
mc_InterpretNegativeSetting(strKey, *mapSettingsRet);
}
}
}
}
return MC_ERR_NOERROR;
}
int mc_ReadConfigFile(
boost::filesystem::path fileConfig,
map<string, string>* mapSettingsRet,
map<string, vector<string> >* mapMultiSettingsRet,
const char *prefix)
{
try
{
boost::filesystem::ifstream streamConfig(fileConfig);
if (!streamConfig.good())
return MC_ERR_NOERROR; // No bitcoin.conf file is OK
set<string> setOptions;
setOptions.insert("*");
// boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end;
// while(it != end)
for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
{
// Don't overwrite existing settings so command line settings override bitcoin.conf
string strKey = it->string_key;
strKey.erase(std::remove(strKey.begin(), strKey.end(), '-'), strKey.end());
strKey.erase(std::remove(strKey.begin(), strKey.end(), '_'), strKey.end());
strKey = string(prefix) + strKey;
if ((*mapSettingsRet).count(strKey) == 0)
{
(*mapSettingsRet)[strKey] = it->value[0];
// interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
mc_InterpretNegativeSetting(strKey, *mapSettingsRet);
}
if(mapMultiSettingsRet)
{
(*mapMultiSettingsRet)[strKey].push_back(it->value[0]);
}
/*
try
{
++it;
}
catch(std::exception &e1) {
++it;
}
*/
}
} catch(std::exception &e) {
fprintf(stderr,"ERROR: reading configuration file: %s\n", e.what());
return MC_ERR_FILE_READ_ERROR;
}
return MC_ERR_NOERROR;
}
int mc_Params::ReadConfig(const char *network_name)
{
mc_ReadConfigFile(mc_GetConfigFile(network_name,"multichain",".conf"),&mapArgs, &mapMultiArgs,"-");
return mc_ReadConfigFile(mc_GetConfigFile(NULL,NULL,".conf"),&mapArgs, &mapMultiArgs,"-");
}
int mc_ReadGeneralConfigFile(mc_MapStringString *mapConfig,const char *network_name,const char *file_name,const char *extension)
{
return mc_ReadConfigFile(mc_GetConfigFile(network_name,file_name,extension),(std::map<string, string>*)mapConfig->mapObject, NULL,"");
}
int mc_MultichainParams::SetGlobals()
{
m_IsProtocolMultiChain=1;
void *ptr=GetParam("chainprotocol",NULL);
if(ptr)
{
if(strcmp((char*)ptr,"multichain"))
{
m_IsProtocolMultiChain=0;
}
}
m_ProtocolVersion=ProtocolVersion();
MIN_RELAY_TX_FEE=(unsigned int)GetInt64Param("minimumrelayfee");
MAX_OP_RETURN_RELAY=(unsigned int)GetInt64Param("maxstdopreturnsize");
MAX_OP_RETURN_RELAY=GetArg("-datacarriersize", MAX_OP_RETURN_RELAY);
MAX_BLOCK_SIZE=(unsigned int)GetInt64Param("maximumblocksize");
DEFAULT_BLOCK_MAX_SIZE=MAX_BLOCK_SIZE;
while(MAX_BLOCK_SIZE>MAX_BLOCKFILE_SIZE)
{
MAX_BLOCKFILE_SIZE *= 2;
}
while(MAX_BLOCK_SIZE>MAX_SIZE)
{
MAX_SIZE *= 2;
}
MAX_STANDARD_TX_SIZE=(unsigned int)GetInt64Param("maxstdtxsize");
MAX_SCRIPT_ELEMENT_SIZE=(unsigned int)GetInt64Param("maxstdelementsize");
COINBASE_MATURITY=(int)GetInt64Param("rewardspendabledelay");
COIN=GetInt64Param("nativecurrencymultiple");
CENT=COIN/100;
MAX_MONEY=GetInt64Param("maximumperoutput");
if((mc_gState->m_NetworkParams->GetInt64Param("initialblockreward") == 0) && (mc_gState->m_NetworkParams->GetInt64Param("firstblockreward") <= 0))
{
COIN=0;
CENT=0;
MAX_MONEY=0;
}
if(mc_gState->m_Features->ShortTxIDAsAssetRef() == 0)
{
m_AssetRefSize=MC_AST_ASSET_REF_SIZE;
}
return MC_ERR_NOERROR;
}
void mc_SHA256::Init()
{
m_HashObject=new CSHA256;
((CSHA256*)m_HashObject)->Reset();
}
void mc_SHA256::Destroy()
{
if(m_HashObject)
{
delete (CSHA256*)m_HashObject;
}
}
void mc_SHA256::Reset()
{
if(m_HashObject)
{
((CSHA256*)m_HashObject)->Reset();
}
}
void mc_SHA256::Write(const void *lpData,int size)
{
if(m_HashObject)
{
((CSHA256*)m_HashObject)->Write((const unsigned char*)lpData,size);
}
}
void mc_SHA256::GetHash(unsigned char *hash)
{
if(m_HashObject)
{
((CSHA256*)m_HashObject)->Finalize(hash);
}
}
int mc_MultichainParams::Import(const char *name,const char *source_address)
{
return MC_ERR_NOERROR;
}
std::string MultichainServerAddress()
{
string result=string(mc_gState->m_NetworkParams->Name());
unsigned char *ptr;
result+="@";
if(mc_gState->m_IPv4Address)
{
ptr=(unsigned char *)(&(mc_gState->m_IPv4Address));
result+=strprintf("%u.%u.%u.%u",ptr[3],ptr[2],ptr[1],ptr[0]);
}
else
{
result+="<server-ip-address>";
}
return result;
}
int mc_SetIPv4ServerAddress(const char* host)
{
char host_copy[16];
char *ptr;
uint32_t result;
int count,v,i;
result=0;
mc_gState->m_IPv4Address=0;
if((strlen(host)<7) || (strlen(host)>15))
{
return 0;
}
memcpy(host_copy,host,strlen(host)+1);
count=0;
for(i=0;i<(int)strlen(host);i++)
{
if(host_copy[i] == '.')
{
host_copy[i]=0x00;
count++;
}
}
if(count != 3)
{
return 0;
}
ptr=host_copy;
for(i=0;i<4;i++)
{
if((strlen(ptr)<1) || (strlen(ptr)>3))
{
return 0;
}
v=atoi(ptr);
if((v<0) || (v>255))
{
return 0;
}
result=(result << 8) + v;
ptr+=strlen(ptr)+1;
}
mc_gState->m_IPv4Address=result;
return result;
}
int mc_FindIPv4ServerAddress(uint32_t *all_ips,int max_ips)
{
int i, l, c;
uint32_t ip;
unsigned char *ptr;
int result;
mc_gState->m_IPv4Address=0;
l=0;
result=0;
c=0;
#ifdef MAC_OSX
struct ifaddrs *ifaddr = NULL;
if (getifaddrs(&ifaddr) == -1) {
return c;
}
if (!ifaddr) {
return c;
}
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock > 0) {
for (struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == 0) {
continue;
}
int family = ifa->ifa_addr->sa_family;
if (family != AF_INET) {
continue;
}
uint32_t a = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
ptr=(unsigned char*)&a;
#elif !defined WIN32
int sock;
struct ifreq ifreqs[20];
struct ifconf ic;
ic.ifc_len = sizeof ifreqs;
ic.ifc_req = ifreqs;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if( (sock >= 0) && (ioctl(sock, SIOCGIFCONF, &ic) >= 0) )
{
for (i = 0; i < (int)(ic.ifc_len/sizeof(struct ifreq)); i++)
{
uint32_t a;
a=((struct sockaddr_in*)&ifreqs[i].ifr_addr)->sin_addr.s_addr;
ptr=(unsigned char*)&a;
#else
struct hostent *phe = gethostbyname("");
if (phe)
{
for (int i = 0; phe->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
ptr=(unsigned char*)&addr;
#endif
if ((ptr[0] != 127) && (ptr[0] != 0))
{
ip=((uint32_t)ptr[0]<<24)+((uint32_t)ptr[1]<<16)+((uint32_t)ptr[2]<<8)+(uint32_t)ptr[3];
if( (ptr[0] == 10) || ((ptr[0] == 192) && (ptr[1] == 168)) || ((ptr[0] == 172) && ((ptr[1] >= 16) && (ptr[1] <= 31))) )
{
if( (l == 0) && (result == 0) )
{
mc_gState->m_IPv4Address=ip;
l=1;
result=1;
}
}
else
{
mc_gState->m_IPv4Address=ip;
l=0;
result=2;
}
if(c<max_ips-1)
{
all_ips[c]=ip;
c++;
}
}
}
}
#ifdef MAC_OSX
if (sock > 0) {
close(sock);
}
freeifaddrs(ifaddr);
#endif
return c;
}