update rc util import

This commit is contained in:
Jeff Becker 2018-08-24 13:26:17 -04:00
parent bee5eee0b1
commit a11bd44a7c
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
1 changed files with 86 additions and 50 deletions

View File

@ -110,7 +110,7 @@ main(int argc, char *argv[])
"please specify: \n"
"--generate with a path to a router contact file\n"
"--update with a path to a router contact file\n"
"--list \n"
"--list path to nodedb skiplist\n"
"--import with a path to a router contact file\n"
"--export a hex formatted public key\n"
"--locate a hex formatted public key"
@ -133,18 +133,17 @@ main(int argc, char *argv[])
char *conffname;
char defaultConfName[] = "daemon.ini";
conffname = defaultConfName;
char *rcfname;
char defaultRcName[] = "other.signed";
rcfname = defaultRcName;
bool haveRequiredOptions = false;
char *rcfname = nullptr;
char *nodesdir = nullptr;
while(1)
{
static struct option long_options[] = {
{"file", required_argument, 0, 'f'},
{"config", required_argument, 0, 'c'},
{"logLevel", required_argument, 0, 'o'},
{"generate", required_argument, 0, 'g'},
{"update", required_argument, 0, 'u'},
{"list", no_argument, 0, 'l'},
{"list", required_argument, 0, 'l'},
{"import", required_argument, 0, 'i'},
{"export", required_argument, 0, 'e'},
{"locate", required_argument, 0, 'q'},
@ -153,7 +152,7 @@ main(int argc, char *argv[])
{"verify", required_argument, 0, 'V'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "c:o:g:lu:i:e:q:nr:V:", long_options,
c = getopt_long(argc, argv, "f:c:o:g:lu:i:e:q:nr:V:", long_options,
&option_index);
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
if(c == -1)
@ -192,52 +191,47 @@ main(int argc, char *argv[])
}
break;
case 'V':
rcfname = optarg;
haveRequiredOptions = true;
verifyMode = true;
rcfname = optarg;
verifyMode = true;
break;
case 'f':
rcfname = optarg;
break;
case 'l':
haveRequiredOptions = true;
listMode = true;
nodesdir = optarg;
listMode = true;
break;
case 'i':
// printf ("option -g with value `%s'\n", optarg);
rcfname = optarg;
haveRequiredOptions = true;
importMode = true;
nodesdir = optarg;
importMode = true;
break;
case 'e':
// printf ("option -g with value `%s'\n", optarg);
rcfname = optarg;
haveRequiredOptions = true;
exportMode = true;
rcfname = optarg;
exportMode = true;
break;
case 'q':
// printf ("option -g with value `%s'\n", optarg);
rcfname = optarg;
haveRequiredOptions = true;
locateMode = true;
rcfname = optarg;
locateMode = true;
break;
case 'g':
// printf ("option -g with value `%s'\n", optarg);
rcfname = optarg;
haveRequiredOptions = true;
genMode = true;
rcfname = optarg;
genMode = true;
break;
case 'u':
// printf ("option -u with value `%s'\n", optarg);
rcfname = optarg;
haveRequiredOptions = true;
updMode = true;
rcfname = optarg;
updMode = true;
break;
case 'n':
haveRequiredOptions = true;
localMode = true;
localMode = true;
break;
case 'r':
rcfname = optarg;
haveRequiredOptions = true;
readMode = true;
rcfname = optarg;
readMode = true;
break;
default:
printf("Bad option: %c\n", c);
@ -245,11 +239,6 @@ main(int argc, char *argv[])
}
}
#undef MIN
if(!haveRequiredOptions)
{
llarp::LogError("Parameters dont all have their required parameters.\n");
return 0;
}
if(verifyMode)
{
llarp_crypto crypto;
@ -313,6 +302,65 @@ main(int argc, char *argv[])
std::cout << std::endl;
return 0;
}
if(listMode)
{
llarp_crypto crypto;
llarp_crypto_libsodium_init(&crypto);
auto nodedb = llarp_nodedb_new(&crypto);
llarp_nodedb_iter itr;
itr.visit = [](llarp_nodedb_iter *i) -> bool {
std::cout << llarp::PubKey(i->rc->pubkey) << std::endl;
return true;
};
if(llarp_nodedb_load_dir(nodedb, nodesdir) > 0)
llarp_nodedb_iterate_all(nodedb, itr);
llarp_nodedb_free(&nodedb);
return 0;
}
if(importMode)
{
if(rcfname == nullptr)
{
std::cout << "no file to import" << std::endl;
return 1;
}
llarp_crypto crypto;
llarp_crypto_libsodium_init(&crypto);
auto nodedb = llarp_nodedb_new(&crypto);
if(!llarp_nodedb_ensure_dir(nodesdir))
{
std::cout << "failed to ensure " << nodesdir << strerror(errno)
<< std::endl;
return 1;
}
llarp_nodedb_set_dir(nodedb, nodesdir);
llarp_rc rc;
if(!llarp_rc_read(rcfname, &rc))
{
std::cout << "failed to read " << rcfname << " " << strerror(errno)
<< std::endl;
return 1;
}
if(!llarp_rc_verify_sig(&crypto, &rc))
{
std::cout << rcfname << " has invalid signature" << std::endl;
return 1;
}
if(!llarp_nodedb_put_rc(nodedb, &rc))
{
std::cout << "failed to store " << strerror(errno) << std::endl;
return 1;
}
std::cout << "imported " << llarp::PubKey(rc.pubkey) << std::endl;
return 0;
}
if(!genMode && !updMode && !listMode && !importMode && !exportMode
&& !locateMode && !localMode && !readMode)
{
@ -407,19 +455,7 @@ main(int argc, char *argv[])
iter.visit = printNode;
llarp_main_iterateDatabase(ctx, iter);
}
if(importMode)
{
llarp_main_loadDatabase(ctx);
llarp::LogInfo("Loading ", rcfname);
llarp_rc rc;
llarp_rc_clear(&rc);
if(!llarp_rc_read(rcfname, &rc))
{
llarp::LogError("Can't load RC");
return 0;
}
llarp_main_putDatabase(ctx, &rc);
}
if(exportMode)
{
llarp_main_loadDatabase(ctx);