akmhoque | 099495b | 2014-03-11 16:01:19 -0500 | [diff] [blame^] | 1 | #include "nlsr_cert_store.hpp" |
| 2 | |
| 3 | namespace nlsr |
| 4 | { |
| 5 | static bool |
| 6 | nlsrCertificateStoreEntryCompare(NlsrCertificateStoreEntry& ncse1, |
| 7 | NlsrCertificateStoreEntry& ncse2) |
| 8 | |
| 9 | { |
| 10 | return ncse1.getCert()->getName().toUri() == |
| 11 | ncse2.getCert()->getName().toUri() ; |
| 12 | } |
| 13 | |
| 14 | static bool |
| 15 | nlsrCertificateStoreEntryCompareByName(NlsrCertificateStoreEntry& ncse1, |
| 16 | std::string compCertName) |
| 17 | |
| 18 | { |
| 19 | return ncse1.getCert()->getName().toUri() == compCertName ; |
| 20 | } |
| 21 | |
| 22 | bool |
| 23 | NlsrCertificateStore::addCertificate(NlsrCertificateStoreEntry & ncse) |
| 24 | { |
| 25 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 26 | std::find_if( certTable.begin(), certTable.end(), |
| 27 | bind(&nlsrCertificateStoreEntryCompare, _1, ncse)); |
| 28 | if(it == certTable.end()) |
| 29 | { |
| 30 | certTable.push_back(ncse); |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | if( it != certTable.end() ) |
| 35 | { |
| 36 | if ( (*it).getCertSeqNum() < ncse.getCertSeqNum() ) |
| 37 | { |
| 38 | certTable.erase(it); |
| 39 | certTable.push_back(ncse); |
| 40 | return true; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | bool |
| 48 | NlsrCertificateStore::addCertificate( |
| 49 | ndn::shared_ptr<ndn::IdentityCertificate> pcert, uint32_t csn, bool isv) |
| 50 | { |
| 51 | NlsrCertificateStoreEntry ncse(pcert, csn, isv); |
| 52 | return addCertificate(ncse); |
| 53 | } |
| 54 | |
| 55 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 56 | NlsrCertificateStore::getCertificateFromStore(const std::string certName) |
| 57 | { |
| 58 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 59 | std::find_if( certTable.begin(), certTable.end(), |
| 60 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 61 | if(it == certTable.end()) |
| 62 | { |
| 63 | ndn::shared_ptr<ndn::IdentityCertificate> cert= |
| 64 | ndn::make_shared<ndn::IdentityCertificate>(); |
| 65 | |
| 66 | return std::make_pair(cert,false); |
| 67 | } |
| 68 | |
| 69 | return std::make_pair((*it).getCert(),true); |
| 70 | } |
| 71 | |
| 72 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 73 | NlsrCertificateStore::getCertificateFromStore( |
| 74 | const std::string certName, int checkSeqNum) |
| 75 | { |
| 76 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 77 | std::find_if( certTable.begin(), certTable.end(), |
| 78 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 79 | if(it == certTable.end()) |
| 80 | { |
| 81 | ndn::shared_ptr<ndn::IdentityCertificate> cert= |
| 82 | ndn::make_shared<ndn::IdentityCertificate>(); |
| 83 | |
| 84 | return std::make_pair(cert,false); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | if( (*it).getCertSeqNum() == checkSeqNum ) |
| 89 | { |
| 90 | return std::make_pair((*it).getCert(),true); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return std::make_pair((*it).getCert(),false); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | bool |
| 99 | NlsrCertificateStore::isCertificateNewInStore(const std::string certName, |
| 100 | int checkSeqNo) |
| 101 | { |
| 102 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 103 | std::find_if( certTable.begin(), certTable.end(), |
| 104 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 105 | if(it != certTable.end()) |
| 106 | { |
| 107 | return (*it).getCertSeqNum() < checkSeqNo ; |
| 108 | } |
| 109 | |
| 110 | return true; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | bool |
| 115 | NlsrCertificateStore::removeCertificateFromStroe(const std::string certName) |
| 116 | { |
| 117 | std::list<NlsrCertificateStoreEntry>::iterator it = |
| 118 | std::find_if( certTable.begin(), certTable.end(), |
| 119 | bind(&nlsrCertificateStoreEntryCompareByName, _1, certName)); |
| 120 | if(it != certTable.end()) |
| 121 | { |
| 122 | certTable.erase(it); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | NlsrCertificateStore::printCertStore() |
| 131 | { |
| 132 | std::list<NlsrCertificateStoreEntry>::iterator it; |
| 133 | for(it=certTable.begin(); it!=certTable.end(); ++it) |
| 134 | { |
| 135 | std::cout<<(*it)<<std::endl; |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | } |