void remoteServer::OnInterest (Ccnx::Name name, Ccnx::Selectors selectors){ Ccnx::Name dataName; clog<<"on interest data name "<<name<<endl; std::string consumer = name.getCompAsString(2); //consumer std::string producer = name.getCompAsString(1); //producer std::string endPoint = name.getCompAsString(3); //endpoint std::string action = name.getCompAsString(4); //action std::string prefix = name.getCompAsString(0); std::string app; std::string session; vector <string> app_session; boost::split(app_session, prefix,boost::is_any_of("_")); app = app_session[0]; session = app_session[1]; OrganizerSession *oSession; ParticipantSession *pSession; Context::instance()->retrieveSession(app, session, &oSession, &pSession); std::string msg(""); //resolving name if (endPoint.compare("shared-key") == 0){ dataName = parseSharedKey(name,msg,oSession,pSession); } if (endPoint.compare("membership") == 0){ dataName = parseMembership(name,oSession,pSession); } if (endPoint.compare("public-key") == 0){ dataName = parsePublicKey(name, msg,oSession,pSession); } std::clog<<"dataname back to consumer "<<dataName<<std::endl; std::clog<<"sent data msg" <<msg<<std::endl; handler.publishData (dataName, msg, 5); }
Ccnx::Name remoteServer::parseMembership(Ccnx::Name name, OrganizerSession *oSession, ParticipantSession *pSession){ std::string action = name.getCompAsString(4); //action std::string consumer = name.getCompAsString(2); //consumer Ccnx::Name dataName = name; if (action.compare("join") == 0 || action.compare("accept") == 0 || action.compare("reject") == 0){ dataName.appendComp("code=0"); } std::clog<<"action: "<<action<<endl; if (action.compare("join") == 0){ // std::string consumer = name.getCompAsString(1); //consumer if (oSession){ std::clog<<"consumer "<<consumer<<endl; oSession->recvJoinRemote(consumer); } } if (action.compare("accept") == 0){ if (pSession){ pSession->recvAcceptJoinRemote(); } } if (action.compare("reject") == 0){ if (pSession){ pSession->recvRejectJoinRemote(); } } return dataName; };
Ccnx::Name remote::getBaseName(Ccnx::Name name) { Ccnx::Name base = Ccnx::Name(); for (int i = 0; i <=4 ;i++) { base.appendComp(name.getCompAsString(i)); } return base; }
int remoteServer::init(std::string app, std::string session, std::string consumer){ std::string prefix(app); prefix.append("_"); prefix.append(session); Ccnx::Name interestBaseName = Ccnx::Name(); interestBaseName.appendComp(prefix); interestBaseName.appendComp(consumer); handler.setInterestFilter (interestBaseName, boost::bind (&remoteServer::OnInterest, this, _1, _2)); return 0; }
Ccnx::BytesPtr ObjectDb::fetchSegment(const Ccnx::Name& deviceName, sqlite3_int64 segment) { sqlite3_stmt* stmt; sqlite3_prepare_v2(m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1, &stmt, 0); CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_TRANSIENT); sqlite3_bind_int64(stmt, 2, segment); BytesPtr ret; int res = sqlite3_step(stmt); if (res == SQLITE_ROW) { const unsigned char* buf = reinterpret_cast<const unsigned char*>(sqlite3_column_blob(stmt, 0)); int bufBytes = sqlite3_column_bytes(stmt, 0); ret = make_shared<Bytes>(buf, buf + bufBytes); } sqlite3_finalize(stmt); // update last used time m_lastUsed = time(NULL); return ret; }
void Dispatcher::Did_FetchManager_FileSegmentFetch_Execute(Ccnx::Name deviceName, Ccnx::Name fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco) { // fileSegmentBaseName: /<device_name>/<appname>/file/<hash> const Bytes& hashBytes = fileSegmentBaseName.getCompFromBack(0); Hash hash(head(hashBytes), hashBytes.size()); _LOG_DEBUG("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment); // _LOG_DEBUG ("Looking up objectdb for " << hash); map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find(hash); if (db != m_objectDbMap.end()) { db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf()); } else { _LOG_ERROR("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf().size()); } // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash)); // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ()); }
void Dispatcher::Did_LocalPrefix_Updated(const Ccnx::Name& forwardingHint) { Name effectiveForwardingHint; if (m_localUserName.size() >= forwardingHint.size() && m_localUserName.getPartialName(0, forwardingHint.size()) == forwardingHint) { effectiveForwardingHint = Name("/"); // "directly" accesible } else { effectiveForwardingHint = forwardingHint; } Name oldLocalPrefix = m_syncLog->LookupLocalLocator(); if (oldLocalPrefix == effectiveForwardingHint) { _LOG_DEBUG( "Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint << ", but effective prefix didn't change"); return; } if (effectiveForwardingHint == Name("/") || effectiveForwardingHint == Name(BROADCAST_DOMAIN)) { _LOG_DEBUG("Basic effective prefix [" << effectiveForwardingHint << "]. Updating local prefix, but don't reregister"); m_syncLog->UpdateLocalLocator(effectiveForwardingHint); return; } _LOG_DEBUG("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint); m_server->registerPrefix(effectiveForwardingHint); m_syncLog->UpdateLocalLocator(effectiveForwardingHint); if (oldLocalPrefix == Name("/") || oldLocalPrefix == Name(BROADCAST_DOMAIN)) { _LOG_DEBUG("Don't deregister basic prefix: " << oldLocalPrefix); return; } m_server->deregisterPrefix(oldLocalPrefix); }
bool ObjectDb::DoesExist(const boost::filesystem::path& folder, const Ccnx::Name& deviceName, const std::string& hash) { fs::path actualFolder = folder / "objects" / hash.substr(0, 2); bool retval = false; sqlite3* db; int res = sqlite3_open((actualFolder / hash.substr(2, hash.size() - 2)).c_str(), &db); if (res == SQLITE_OK) { sqlite3_stmt* stmt; sqlite3_prepare_v2(db, "SELECT count(*), count(nullif(content_object,0)) FROM File WHERE device_name=?", -1, &stmt, 0); CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_TRANSIENT); int res = sqlite3_step(stmt); if (res == SQLITE_ROW) { int countAll = sqlite3_column_int(stmt, 0); int countNonNull = sqlite3_column_int(stmt, 1); _LOG_TRACE("Total segments: " << countAll << ", non-empty segments: " << countNonNull); if (countAll > 0 && countAll == countNonNull) { retval = true; } } sqlite3_finalize(stmt); } sqlite3_close(db); return retval; }
void ObjectDb::saveContentObject(const Ccnx::Name& deviceName, sqlite3_int64 segment, const Ccnx::Bytes& data) { sqlite3_stmt* stmt; sqlite3_prepare_v2(m_db, "INSERT INTO File " "(device_name, segment, content_object) " "VALUES (?, ?, ?)", -1, &stmt, 0); //_LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]"); CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_STATIC); sqlite3_bind_int64(stmt, 2, segment); sqlite3_bind_blob(stmt, 3, &data[0], data.size(), SQLITE_STATIC); sqlite3_step(stmt); //_LOG_DEBUG ("After saving object: " << sqlite3_errmsg (m_db)); sqlite3_finalize(stmt); // update last used time m_lastUsed = time(NULL); }
int remote::init(std::string prefix,std::string producer, std::string consumer,std::string endpoint,std::string action){ Ccnx::Name interestBaseName = Ccnx::Name(); interestBaseName.appendComp(prefix); interestBaseName.appendComp(producer); interestBaseName.appendComp(consumer); interestBaseName.appendComp(endpoint); interestBaseName.appendComp(action); //action interestBaseName.appendComp("xxx"); //rand+auth_token/ // InterestBaseName = Ccnx::Name (interestName); // Ccnx::Wrapper handler; std::clog<<interestBaseName<<std::endl; handler.sendInterest (interestBaseName, Ccnx::Closure (boost::bind (&remote::runDataCallback, this, _1, _2), boost::bind (&remote::runTimeoutCallback, this, _1, _2, _3)), Ccnx::Selectors ().scope (Ccnx::Selectors::SCOPE_LOCAL_HOST)); // sleep (3); return 0; }
void remote::runDataCallback(Name name, Ccnx::PcoPtr pco) { std::clog<<"data callback name: "<<name<<std::endl; Ccnx::BytesPtr content = pco->contentPtr (); std::clog<<"data callback content: "<<content<<std::endl; std::string action = name.getCompAsString(4); //action std::string consumer = name.getCompAsString(2); //consumer std::string producer = name.getCompAsString(1); //producer std::string endPoint = name.getCompAsString(3); //endpoint std::string prefix = name.getCompAsString(0); std::string app; std::string session; vector <string> app_session; split(prefix, app_session, "_"); app = app_session[0]; session = app_session[1]; int size = name.size(); std::string lastComponent =name.getCompAsString(size-1); vector <string> splitPara; split(lastComponent, splitPara, ","); if (splitPara.size() == 1 && splitPara[0].compare("code=0") == 0) return; int code; int version =0; int flag; //1: first packet 0:later packets int seqnum = 0; int chunkSize = 0; if (splitPara.size() == 3){ flag = 1; vector <string> tmp1,tmp2,tmp3; split(splitPara[0], tmp1, "="); code = atoi(tmp1[1].c_str()); split(splitPara[1], tmp2, "="); version = atoi(tmp2[1].c_str()); split(splitPara[2], tmp3, "="); chunkSize = atoi(tmp3[1].c_str()); seqnum = 1; } else{ flag = 0; std::string tmp = name.getCompFromBackAsString(1); vector <string> chunkTmp; split(tmp, chunkTmp, "="); seqnum = atoi(chunkTmp[1].c_str()); // cout<<"seqnum chunk"<<seqnum<<" "<<chunk<<endl; } OrganizerSession *oSession; ParticipantSession *pSession; Context::instance()->retrieveSession(app, session, &oSession, &pSession); if (action.compare("fetch") == 0) { if (endPoint.compare("public-key") == 0) { if (pSession) { pSession->recvPublicKeyRemote(producer, version, seqnum, chunkSize, string ((char*)Ccnx::head (*content), content->size ())); } if (oSession) { oSession->recvPublicKeyRemote(producer, version, seqnum, chunkSize, string ((char*)Ccnx::head (*content), content->size ())); } } if (endPoint.compare("shared-key") == 0) { //decrypt std::string privateKey; privateKey = KeyDB::instance().getPriKey(); EVP_PKEY *pri_key = KeyDB::instance().str2priKey(privateKey); RSA *decrypt_key; /*for test*/ FILE *rsa_pkey_file; rsa_pkey_file = fopen("../db/pri1.pem", "r"); if (!PEM_read_RSAPrivateKey(rsa_pkey_file, &decrypt_key, NULL, NULL)) { fprintf(stderr, "Error loading RSA Private Key File.\n"); ERR_print_errors_fp(stderr); } /*test end*/ std::string encrypt_string = string ((char*)Ccnx::head (*content), content->size ()); cout<<encrypt_string<<endl; char *encrypt = (char *)(encrypt_string.c_str()); char *decrypt = NULL; int encrypt_len = content->size(); cout<<"encrypt_len"<<encrypt_len<<endl; // decrypt_key = EVP_PKEY_get1_RSA(pri_key); //important line decrypt = (char *)malloc(encrypt_len); memset(decrypt,0,(encrypt_len)); char *err = (char *)malloc(130); if(RSA_private_decrypt(encrypt_len, (unsigned char*)encrypt, (unsigned char*)decrypt, decrypt_key, RSA_PKCS1_OAEP_PADDING) == -1) { ERR_load_crypto_strings(); ERR_error_string(ERR_get_error(), err); fprintf(stderr, "Error encrypting message: %s\n", err); } cout<<decrypt<<endl; std::string decrypt_string = string(decrypt); cout<<decrypt_string<<endl; if (pSession) { pSession->recvSharedKeyRemote(version, seqnum, chunkSize, decrypt_string); } } } if (flag == 1) { for (seqnum = 1; seqnum < chunkSize; seqnum++) { Ccnx::Name interestName = Ccnx::Name(); interestName = getBaseName(name); interestName.appendComp("v="+boost::lexical_cast <string>(version)); interestName.appendComp("chunk="+(boost::lexical_cast <string>(seqnum+1))); interestName.appendComp("xxx"); handler.sendInterest (interestName, Ccnx::Closure (boost::bind (&remote::runDataCallback, this, _1, _2), boost::bind (&remote::runTimeoutCallback, this, _1, _2, _3)), Ccnx::Selectors ().scope (Ccnx::Selectors::SCOPE_LOCAL_HOST)); } } }
void Dispatcher::Did_FetchManager_FileFetchComplete_Execute(Ccnx::Name deviceName, Ccnx::Name fileBaseName) { // fileBaseName: /<device_name>/<appname>/file/<hash> _LOG_DEBUG("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName); const Bytes& hashBytes = fileBaseName.getCompFromBack(0); Hash hash(head(hashBytes), hashBytes.size()); _LOG_DEBUG("Extracted hash: " << hash.shortHash()); if (m_objectDbMap.find(hash) != m_objectDbMap.end()) { // remove the db handle m_objectDbMap.erase(hash); // to commit write } else { _LOG_ERROR("no db available for this file: " << hash); } FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash(hash); for (FileItems::iterator file = filesToAssemble->begin(); file != filesToAssemble->end(); file++) { boost::filesystem::path filePath = m_rootDir / file->filename(); try { if (filesystem::exists(filePath) && filesystem::last_write_time(filePath) == file->mtime() && #if BOOST_VERSION >= 104900 filesystem::status(filePath).permissions() == static_cast<filesystem::perms>(file->mode()) && #endif *Hash::FromFileContent(filePath) == hash) { _LOG_DEBUG("Asking to assemble a file, but file already exists on a filesystem"); continue; } } catch (filesystem::filesystem_error& error) { _LOG_ERROR("File operations failed on [" << filePath << "] (ignoring)"); } if (ObjectDb::DoesExist(m_rootDir / ".chronoshare", deviceName, boost::lexical_cast<string>(hash))) { bool ok = m_objectManager.objectsToLocalFile(deviceName, hash, filePath); if (ok) { last_write_time(filePath, file->mtime()); #if BOOST_VERSION >= 104900 permissions(filePath, static_cast<filesystem::perms>(file->mode())); #endif m_fileState->SetFileComplete(file->filename()); } else { _LOG_ERROR("Notified about complete fetch, but file cannot be restored from the database: [" << filePath << "]"); } } else { _LOG_ERROR(filePath << " supposed to have all segments, but not"); // should abort for debugging } } }
Ccnx::Name remoteServer::parseSharedKey(Ccnx::Name name, std::string &ret, OrganizerSession *oSession, ParticipantSession *pSession){ int flag; std::string consumer = name.getCompAsString(2); //consumer std::string producer = name.getCompAsString(1); //producer std::string endPoint = name.getCompAsString(3); //endpoint std::string action = name.getCompAsString(4); //action std::string prefix = name.getCompAsString(0); //prefix int size = name.size(); int version = 0; int chunkNum = 0; int chunkSize = 0; Ccnx::Name dataName = name; if (action.compare("fetch") == 0){ std::string lastComponent = name.getCompAsString(size-2); if (boost::starts_with(lastComponent,"chunk=")) { flag = 1; //later packet } else{ flag = 0; //para } // if (flag == 0){ chunkSize = 0; chunkNum = 0; if (oSession) { oSession->recvFetchSharedKeyRemote(consumer, version, chunkNum,chunkSize, ret); } //encrypt vector<std::string> tmp2; boost::split(tmp2,prefix,boost::is_any_of("_")); std::string app = tmp2[0]; std::string publicKey[3]; KeyDB::instance().getKeyFromUser(app,(KeyDB::global), publicKey,consumer); // cout<<"after fetch"<<endl; cout<<"key content"<<endl; cout<<publicKey[1]<<endl; EVP_PKEY *pub_key = KeyDB::instance().str2Key(publicKey[1]); // std::string plain_string = ret; // cout<<"plain "<<plain_string<<endl; // char *plain2 = (char *)(plain_string.c_str()); // cout<<pub_key->type<<endl; // cout<<pub_key->pkey.ptr<<endl; char plain[128]; sprintf(plain,"%s",ret.c_str()); // cout<<"aaaaa"<<endl; // cout<<plain<<endl; // plain[3]='\0'; // plain[plain_string.size()] = '\0'; // cout<<"sadfsf"<<endl; /*for test*/ /* RSA *encrypt_key; FILE *rsa_pkey_file; rsa_pkey_file = fopen("../db/pub1.pub", "r"); if (rsa_pkey_file == NULL) { cout<<"no pointer"<<endl; } cout<<"before bbbbb"<<endl; if (!PEM_read_RSA_PUBKEY(rsa_pkey_file, &encrypt_key, NULL, NULL)) { fprintf(stderr, "Error loading RSA Public Key File.\n"); ERR_print_errors_fp(stderr); } fclose(rsa_pkey_file); */ /*test end*/ RSA *encrypt_key; // EVP_PKEY_set1_RSA(pub_key,encrypt_key); encrypt_key = EVP_PKEY_get1_RSA(pub_key); int encrypt_len; cout<<RSA_size(encrypt_key)<<endl; char *encrypt = (char *)malloc(RSA_size(encrypt_key)); // memset(encrypt,0,RSA_size(encrypt_key)); // cout<<"before encrpyt"<<endl; if((encrypt_len = RSA_public_encrypt(strlen(plain), (unsigned char*)plain, (unsigned char*)encrypt, encrypt_key, RSA_PKCS1_OAEP_PADDING)) == -1) { ERR_load_crypto_strings(); } /* char *encrypt_test = (char *)malloc(RSA_size(encrypt_key)); // memset(encrypt,0,RSA_size(encrypt_key)); cout<<"before encrpyt"<<endl; if((encrypt_len = RSA_public_encrypt(strlen(plain), (unsigned char*)plain, (unsigned char*)encrypt_test, encrypt_key_test, RSA_PKCS1_OAEP_PADDING)) == -1) { ERR_load_crypto_strings(); } cout<<"test "<<encrypt_test<<endl; */ // cout<<"encrypt text:"<<endl; // cout<<encrypt<<endl; ret = string(encrypt,encrypt_len); std::string tmp = "code=0,version="; std::string v = boost::lexical_cast <string>(version); tmp.append(v); tmp.append(",chunk="); std::string ch = boost::lexical_cast <string>(chunkSize); tmp.append(ch); dataName.appendComp(tmp); } else{ dataName= name; vector<std::string> str1; boost::split(str1,lastComponent,boost::is_any_of("=")); std::string versionComponent = name.getCompAsString(size-3); vector<std::string> str2; boost::split(str2,versionComponent,boost::is_any_of("=")); version = atoi(str2[1].c_str()); chunkNum = atoi(str1[1].c_str()); if (oSession) { oSession->recvFetchSharedKeyRemote(consumer, version, chunkNum, chunkSize, ret); } char *from = (char *)ret.c_str(); char *to = NULL ; // get public key // do_encrypt(&to, from,key,len); } } else{ if (boost::starts_with(action,"update")){ dataName.appendComp("code=0"); vector<std::string> tmp; boost::split(tmp,action,boost::is_any_of("_")); version = atoi(tmp[1].c_str()); if (pSession) { pSession->recvRenewSharedKeyRemote(version); } // // orgsession->recvRenewSharedKeyRemote(version); } } return dataName; };
Ccnx::Name remoteServer::parsePublicKey(Ccnx::Name name, std::string &ret, OrganizerSession *oSession, ParticipantSession *pSession){ int flag; // std::string consumer = name.getCompAsString(1); //consumer // std::string producer = name.getCompAsString(2); //producer // std::string endPoint = name.getCompAsString(3); //endpoint // std::string action = name.getCompAsString(4); //action std::string consumer = name.getCompAsString(2); //consumer std::string producer = name.getCompAsString(1); //producer std::string endPoint = name.getCompAsString(3); //endpoint std::string action = name.getCompAsString(4); //action int size = name.size(); int version = 0; int chunkNum = 0; int chunkSize = 0; Ccnx::Name dataName = name; if (action.compare("fetch") == 0){ std::string lastComponent = name.getCompAsString(size-2); if (boost::starts_with(lastComponent,"chunk=")) { flag = 1; } else{ flag = 0; //para } // if (flag == 0){ chunkSize = 0; chunkNum = 0; if (oSession) { oSession->recvFetchPublicKeyRemote(consumer, version, chunkNum,chunkSize, ret); } if (pSession) { pSession->recvFetchPublicKeyRemote(consumer, version, chunkNum,chunkSize, ret); } std::string tmp = "code=0,version="; std::string v = boost::lexical_cast <string>(version); tmp.append(v); tmp.append(",chunk="); std::string ch = boost::lexical_cast <string>(chunkNum); tmp.append(ch); dataName.appendComp(tmp); } else{ dataName= name; vector<std::string> str1; boost::split(str1,lastComponent,boost::is_any_of("=")); std::string versionComponent = name.getCompAsString(size-3); vector<std::string> str2; boost::split(str2,versionComponent,boost::is_any_of("=")); version = atoi(str2[1].c_str()); chunkNum = atoi(str1[1].c_str()); if (oSession) { oSession->recvFetchPublicKeyRemote(consumer, version, chunkNum,chunkSize, ret); } if (pSession) { pSession->recvFetchPublicKeyRemote(consumer, version, chunkNum,chunkSize, ret); } } } return dataName; };