fix up win32 specific parts

pull/7/head
Jeff Becker 6 years ago
parent 1b20ba6a6c
commit 0d2bce2089

@ -278,23 +278,18 @@ main(int argc, char *argv[])
// "encryption.key") // "encryption.key")
fs::path encryption_keyfile = "encryption.key"; fs::path encryption_keyfile = "encryption.key";
llarp::SecretKey encryption; llarp::SecretKey encryption;
#ifdef _WIN32
llarp_findOrCreateEncryption(&crypt, encryption_keyfile.string().c_str(), llarp_findOrCreateEncryption(&crypt, encryption_keyfile.string().c_str(),
&encryption); &encryption);
#else
llarp_findOrCreateEncryption(&crypt, encryption_keyfile.c_str(),
&encryption);
#endif
llarp_rc_set_pubenckey(&tmp, llarp::seckey_topublic(encryption)); llarp_rc_set_pubenckey(&tmp, llarp::seckey_topublic(encryption));
// get identity public sig key // get identity public sig key
fs::path ident_keyfile = "identity.key"; fs::path ident_keyfile = "identity.key";
byte_t identity[SECKEYSIZE]; byte_t identity[SECKEYSIZE];
#ifdef _WIN32 llarp_findOrCreateIdentity(&crypt, ident_keyfile.string().c_str(),
llarp_findOrCreateIdentity(&crypt, ident_keyfile.string().c_str(), identity); identity);
#else
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
#endif
llarp_rc_set_pubsigkey(&tmp, llarp::seckey_topublic(identity)); llarp_rc_set_pubsigkey(&tmp, llarp::seckey_topublic(identity));
// this causes a segfault // this causes a segfault
@ -302,11 +297,8 @@ main(int argc, char *argv[])
// set filename // set filename
fs::path our_rc_file = rcfname; fs::path our_rc_file = rcfname;
// write file // write file
#ifdef _WIN32
llarp_rc_write(&tmp, our_rc_file.string().c_str()); llarp_rc_write(&tmp, our_rc_file.string().c_str());
#else
llarp_rc_write(&tmp, our_rc_file.c_str());
#endif
// release memory for tmp lists // release memory for tmp lists
llarp_rc_free(&tmp); llarp_rc_free(&tmp);
} }
@ -324,11 +316,8 @@ main(int argc, char *argv[])
llarp_crypto_libsodium_init(&crypt); llarp_crypto_libsodium_init(&crypt);
fs::path ident_keyfile = "identity.key"; fs::path ident_keyfile = "identity.key";
byte_t identity[SECKEYSIZE]; byte_t identity[SECKEYSIZE];
#ifdef _WIN32 llarp_findOrCreateIdentity(&crypt, ident_keyfile.string().c_str(),
llarp_findOrCreateIdentity(&crypt, ident_keyfile.string().c_str(), identity); identity);
#else
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
#endif
// get identity public key // get identity public key
uint8_t *pubkey = llarp::seckey_topublic(identity); uint8_t *pubkey = llarp::seckey_topublic(identity);
llarp_rc_set_pubsigkey(&rc, pubkey); llarp_rc_set_pubsigkey(&rc, pubkey);
@ -337,11 +326,7 @@ main(int argc, char *argv[])
// set filename // set filename
fs::path our_rc_file_out = "update_debug.rc"; fs::path our_rc_file_out = "update_debug.rc";
// write file // write file
#ifdef _WIN32
llarp_rc_write(&tmp, our_rc_file_out.string().c_str()); llarp_rc_write(&tmp, our_rc_file_out.string().c_str());
#else
llarp_rc_write(&tmp, our_rc_file_out.c_str());
#endif
} }
if(listMode) if(listMode)
{ {

@ -12,6 +12,7 @@
#include "mem.hpp" #include "mem.hpp"
static const char skiplist_subdirs[] = "0123456789abcdef"; static const char skiplist_subdirs[] = "0123456789abcdef";
static const fs::path RC_FILE_EXT = ".signed";
struct llarp_nodedb struct llarp_nodedb
{ {
@ -21,7 +22,7 @@ struct llarp_nodedb
llarp_crypto *crypto; llarp_crypto *crypto;
// std::map< llarp::pubkey, llarp_rc > entries; // std::map< llarp::pubkey, llarp_rc > entries;
std::unordered_map< llarp::PubKey, llarp_rc, llarp::PubKeyHash > entries; std::unordered_map< llarp::PubKey, llarp_rc, llarp::PubKey::Hash > entries;
fs::path nodePath; fs::path nodePath;
void void
@ -38,11 +39,11 @@ struct llarp_nodedb
llarp_rc * llarp_rc *
getRC(const llarp::PubKey &pk) getRC(const llarp::PubKey &pk)
{ {
return &entries[pk]; return &entries.at(pk);
} }
bool bool
Has(const llarp::PubKey &pk) Has(const llarp::PubKey &pk) const
{ {
return entries.find(pk) != entries.end(); return entries.find(pk) != entries.end();
} }
@ -66,7 +67,7 @@ struct llarp_nodedb
*/ */
bool bool
pubKeyExists(llarp_rc *rc) pubKeyExists(llarp_rc *rc) const
{ {
// extract pk from rc // extract pk from rc
llarp::PubKey pk = rc->pubkey; llarp::PubKey pk = rc->pubkey;
@ -105,22 +106,16 @@ struct llarp_nodedb
} }
std::string std::string
getRCFilePath(const byte_t *pubkey) getRCFilePath(const byte_t *pubkey) const
{ {
char ftmp[68] = {0}; char ftmp[68] = {0};
const char *hexname = const char *hexname =
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(pubkey, ftmp); llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(pubkey, ftmp);
std::string hexString(hexname); std::string hexString(hexname);
#ifdef _WIN32 hexString += RC_FILE_EXT;
std::string filepath = nodePath.string(); std::string skiplistDir;
#else skiplistDir += hexString[hexString.length() - 1];
std::string filepath = nodePath; fs::path filepath = nodePath / skiplistDir / hexString;
#endif
filepath.append(PATH_SEP);
filepath.append(&hexString[hexString.length() - 1]);
filepath.append(PATH_SEP);
filepath.append(hexname);
filepath.append(".signed");
return filepath; return filepath;
} }
@ -172,6 +167,8 @@ struct llarp_nodedb
for(const char &ch : skiplist_subdirs) for(const char &ch : skiplist_subdirs)
{ {
if(!ch)
continue;
std::string p; std::string p;
p += ch; p += ch;
fs::path sub = path / p; fs::path sub = path / p;
@ -192,7 +189,7 @@ struct llarp_nodedb
auto itr = fs::begin(i); auto itr = fs::begin(i);
while(itr != fs::end(i)) while(itr != fs::end(i))
#else #else
auto itr = i.begin(); auto itr = i.begin();
while(itr != itr.end()) while(itr != itr.end())
#endif #endif
{ {
@ -207,20 +204,12 @@ struct llarp_nodedb
bool bool
loadfile(const fs::path &fpath) loadfile(const fs::path &fpath)
{ {
#if __APPLE__ && __MACH__ if(fpath.extension() != RC_FILE_EXT)
// skip .DS_Store files
if(strstr(fpath.c_str(), ".DS_Store") != 0)
{
return false; return false;
}
#endif
llarp_rc rc; llarp_rc rc;
llarp_rc_clear(&rc); llarp_rc_clear(&rc);
#ifdef _WIN32
if(!llarp_rc_read(fpath.string().c_str(), &rc)) if(!llarp_rc_read(fpath.string().c_str(), &rc))
#else
if(!llarp_rc_read(fpath.c_str(), &rc))
#endif
{ {
llarp::LogError("Signature read failed", fpath); llarp::LogError("Signature read failed", fpath);
return false; return false;

@ -166,11 +166,7 @@ llarp_router::try_connect(fs::path rcfile)
{ {
llarp_rc remote; llarp_rc remote;
llarp_rc_new(&remote); llarp_rc_new(&remote);
#ifdef _WIN32
if(!llarp_rc_read(rcfile.string().c_str(), &remote)) if(!llarp_rc_read(rcfile.string().c_str(), &remote))
#else
if(!llarp_rc_read(rcfile.c_str(), &remote))
#endif
{ {
llarp::LogError("failure to decode or verify of remote RC"); llarp::LogError("failure to decode or verify of remote RC");
return; return;
@ -200,24 +196,15 @@ llarp_router::EnsureIdentity()
{ {
if(!EnsureEncryptionKey()) if(!EnsureEncryptionKey())
return false; return false;
#ifdef _WIN32
return llarp_findOrCreateIdentity(&crypto, ident_keyfile.string().c_str(), return llarp_findOrCreateIdentity(&crypto, ident_keyfile.string().c_str(),
identity); identity);
#else
return llarp_findOrCreateIdentity(&crypto, ident_keyfile.c_str(), identity);
#endif
} }
bool bool
llarp_router::EnsureEncryptionKey() llarp_router::EnsureEncryptionKey()
{ {
#ifdef _WIN32
return llarp_findOrCreateEncryption( return llarp_findOrCreateEncryption(
&crypto, encryption_keyfile.string().c_str(), &this->encryption); &crypto, encryption_keyfile.string().c_str(), &this->encryption);
#else
return llarp_findOrCreateEncryption(&crypto, encryption_keyfile.c_str(),
&this->encryption);
#endif
} }
void void
@ -247,11 +234,8 @@ llarp_router::SaveRC()
if(llarp_rc_bencode(&rc, &buf)) if(llarp_rc_bencode(&rc, &buf))
{ {
#ifdef _WIN32
std::ofstream f(our_rc_file.string()); std::ofstream f(our_rc_file.string());
#else
std::ofstream f(our_rc_file);
#endif
if(f.is_open()) if(f.is_open())
{ {
f.write((char *)buf.base, buf.cur - buf.base); f.write((char *)buf.base, buf.cur - buf.base);
@ -759,27 +743,10 @@ llarp_router::InitOutboundLink()
{ {
if(outboundLink) if(outboundLink)
return true; return true;
#ifdef __MINGW32__
llarp_iwp_args args = {
.crypto = &crypto,
.logic = logic,
.cryptoworker = tp,
.router = this,
.keyfile = transport_keyfile.string().c_str(),
};
#elif !defined(_MSC_VER)
llarp_iwp_args args = {
.crypto = &crypto,
.logic = logic,
.cryptoworker = tp,
.router = this,
.keyfile = transport_keyfile.c_str(),
};
#else
llarp_iwp_args args = { llarp_iwp_args args = {
&crypto, logic, tp, this, transport_keyfile.string().c_str(), &crypto, logic, tp, this, transport_keyfile.string().c_str(),
}; };
#endif
auto link = new(std::nothrow) llarp_link(args); auto link = new(std::nothrow) llarp_link(args);
@ -931,11 +898,9 @@ llarp_rc_read(const char *fpath, llarp_rc *result)
printf("File[%s] not found\n", fpath); printf("File[%s] not found\n", fpath);
return false; return false;
} }
#ifdef _WIN32
std::ifstream f(our_rc_file.string(), std::ios::binary); std::ifstream f(our_rc_file.string(), std::ios::binary);
#else
std::ifstream f(our_rc_file, std::ios::binary);
#endif
if(!f.is_open()) if(!f.is_open())
{ {
printf("Can't open file [%s]\n", fpath); printf("Can't open file [%s]\n", fpath);
@ -989,11 +954,7 @@ llarp_rc_write(struct llarp_rc *rc, const char *fpath)
if(llarp_rc_bencode(rc, &buf)) if(llarp_rc_bencode(rc, &buf))
{ {
#ifdef _WIN32
std::ofstream f(our_rc_file.string(), std::ios::binary); std::ofstream f(our_rc_file.string(), std::ios::binary);
#else
std::ofstream f(our_rc_file, std::ios::binary);
#endif
if(f.is_open()) if(f.is_open())
{ {
f.write((char *)buf.base, buf.cur - buf.base); f.write((char *)buf.base, buf.cur - buf.base);
@ -1065,21 +1026,13 @@ llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath,
{ {
llarp::LogInfo("generating new identity key"); llarp::LogInfo("generating new identity key");
crypto->identity_keygen(secretkey); crypto->identity_keygen(secretkey);
#ifdef _WIN32
std::ofstream f(path.string(), std::ios::binary); std::ofstream f(path.string(), std::ios::binary);
#else
std::ofstream f(path, std::ios::binary);
#endif
if(f.is_open()) if(f.is_open())
{ {
f.write((char *)secretkey, SECKEYSIZE); f.write((char *)secretkey, SECKEYSIZE);
} }
} }
#ifdef _WIN32
std::ifstream f(path.string(), std::ios::binary); std::ifstream f(path.string(), std::ios::binary);
#else
std::ifstream f(path, std::ios::binary);
#endif
if(f.is_open()) if(f.is_open())
{ {
f.read((char *)secretkey, SECKEYSIZE); f.read((char *)secretkey, SECKEYSIZE);
@ -1101,21 +1054,14 @@ llarp_findOrCreateEncryption(llarp_crypto *crypto, const char *fpath,
{ {
llarp::LogInfo("generating new encryption key"); llarp::LogInfo("generating new encryption key");
crypto->encryption_keygen(*encryption); crypto->encryption_keygen(*encryption);
#ifdef _WIN32
std::ofstream f(path.string(), std::ios::binary); std::ofstream f(path.string(), std::ios::binary);
#else
std::ofstream f(path, std::ios::binary);
#endif
if(f.is_open()) if(f.is_open())
{ {
f.write((char *)encryption, SECKEYSIZE); f.write((char *)encryption, SECKEYSIZE);
} }
} }
#ifdef _WIN32
std::ifstream f(path.string(), std::ios::binary); std::ifstream f(path.string(), std::ios::binary);
#else
std::ifstream f(path, std::ios::binary);
#endif
if(f.is_open()) if(f.is_open())
{ {
f.read((char *)encryption, SECKEYSIZE); f.read((char *)encryption, SECKEYSIZE);
@ -1172,23 +1118,7 @@ namespace llarp
if(!StrEq(key, "*")) if(!StrEq(key, "*"))
{ {
llarp::LogInfo("interface specific binding activated"); llarp::LogInfo("interface specific binding activated");
#ifdef __MINGW32__
llarp_iwp_args args = {
.crypto = &self->crypto,
.logic = self->logic,
.cryptoworker = self->tp,
.router = self,
.keyfile = self->transport_keyfile.string().c_str(),
};
#elif !defined(_MSC_VER)
llarp_iwp_args args = {
.crypto = &self->crypto,
.logic = self->logic,
.cryptoworker = self->tp,
.router = self,
.keyfile = self->transport_keyfile.c_str(),
};
#else
llarp_iwp_args args = { llarp_iwp_args args = {
&self->crypto, &self->crypto,
self->logic, self->logic,
@ -1196,7 +1126,6 @@ namespace llarp
self, self,
self->transport_keyfile.string().c_str(), self->transport_keyfile.string().c_str(),
}; };
#endif
link = new(std::nothrow) llarp_link(args); link = new(std::nothrow) llarp_link(args);

Loading…
Cancel
Save