Jeff Thompson | 990599b | 2013-08-27 15:14:25 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <sstream> |
| 8 | #include <iostream> |
| 9 | #include "../ndn-cpp/interest.hpp" |
| 10 | #include "../ndn-cpp/data.hpp" |
| 11 | #include "../ndn-cpp/forwarding-entry.hpp" |
| 12 | #include "../ndn-cpp/security/key-chain.hpp" |
| 13 | |
| 14 | using namespace std; |
| 15 | using namespace ndn; |
| 16 | |
| 17 | unsigned char Interest1[] = { |
| 18 | 0x01, 0xd2, 0xf2, 0xfa, 0xa5, 0x6e, 0x64, 0x6e, 0x78, 0x00, 0xfa, 0x02, 0x85, 0xe0, 0xa0, 0x1e, 0x09, 0x39, 0x68, |
| 19 | 0xf9, 0x74, 0x0c, 0xe7, 0xf4, 0x36, 0x1b, 0xab, 0xf5, 0xbb, 0x05, 0xa4, 0xe5, 0x5a, 0xac, 0xa5, 0xe5, 0x8f, 0x73, |
| 20 | 0xed, 0xde, 0xb8, 0xe0, 0x13, 0xaa, 0x8f, 0x00, 0xfa, 0xcd, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x72, 0x65, 0x67, |
| 21 | 0x00, 0xfa, 0x29, 0xad, 0x04, 0x82, 0x02, 0xaa, 0x03, 0xb2, 0x08, 0x85, 0x4d, 0xdb, 0xf6, 0x97, 0x79, 0xcd, 0xf4, |
| 22 | 0xef, 0x74, 0xbe, 0x99, 0x47, 0x44, 0x78, 0xc8, 0xbc, 0x3c, 0xa0, 0x87, 0x3e, 0x0f, 0xfa, 0x1f, 0xa6, 0x01, 0x20, |
| 23 | 0xaa, 0x27, 0x6d, 0xb1, 0x22, 0xb8, 0x34, 0x04, 0xe5, 0x95, 0xa8, 0xa3, 0xca, 0xea, 0xf0, 0x96, 0x30, 0x27, 0x66, |
| 24 | 0x58, 0xba, 0x4e, 0x7b, 0xea, 0xad, 0xb4, 0xb9, 0x1a, 0x8c, 0xc5, 0x8e, 0x19, 0xac, 0x4a, 0x42, 0x28, 0x95, 0x07, |
| 25 | 0xed, 0x8d, 0x60, 0x9a, 0xa9, 0xbe, 0xf6, 0x1a, 0x5a, 0x50, 0x7f, 0x34, 0x9c, 0x83, 0xd2, 0x94, 0x4b, 0x8c, 0x16, |
| 26 | 0xfe, 0xcf, 0xd9, 0x0d, 0x4a, 0x40, 0xdd, 0xb8, 0x68, 0x75, 0x92, 0xc0, 0xa5, 0x75, 0x17, 0x56, 0x42, 0x35, 0xb2, |
| 27 | 0xe3, 0x59, 0xdb, 0x54, 0xf5, 0x1a, 0x37, 0xe1, 0xac, 0x39, 0xe5, 0x18, 0xa2, 0x19, 0x6e, 0x3f, 0xfd, 0xa7, 0xeb, |
| 28 | 0x2f, 0xb3, 0x01, 0xf3, 0xc4, 0x04, 0xdd, 0x00, 0x00, 0xf2, 0x00, 0x01, 0xa2, 0x03, 0xe2, 0x02, 0x85, 0xef, 0x7c, |
| 29 | 0x4f, 0x5d, 0x47, 0x43, 0xa8, 0xb8, 0x58, 0x6e, 0xa2, 0xe7, 0x41, 0xb7, 0xfc, 0x39, 0xd1, 0xdc, 0x0d, 0xbe, 0x1b, |
| 30 | 0x19, 0x30, 0xe7, 0x87, 0xcf, 0xd1, 0xd8, 0x33, 0xea, 0x7a, 0x61, 0x00, 0x02, 0xba, 0xb5, 0x04, 0xfc, 0xe9, 0xe2, |
| 31 | 0x53, 0xd7, 0x00, 0x01, 0xe2, 0x01, 0x82, 0x19, 0xfd, 0x30, 0x82, 0x01, 0x9b, 0x30, 0x82, 0x01, 0x04, 0x02, 0x09, |
| 32 | 0x00, 0xb7, 0xd8, 0x5c, 0x90, 0x6b, 0xad, 0x52, 0xee, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
| 33 | 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x12, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x07, 0x61, |
| 34 | 0x78, 0x65, 0x6c, 0x63, 0x64, 0x76, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x32, 0x30, 0x34, 0x32, 0x38, 0x32, 0x33, 0x34, |
| 35 | 0x34, 0x33, 0x37, 0x5a, 0x17, 0x0d, 0x31, 0x32, 0x30, 0x35, 0x32, 0x38, 0x32, 0x33, 0x34, 0x34, 0x33, 0x37, 0x5a, |
| 36 | 0x30, 0x12, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x07, 0x61, 0x78, 0x65, 0x6c, 0x63, 0x64, |
| 37 | 0x76, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, |
| 38 | 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xe1, 0x7d, 0x30, 0xa7, 0xd8, 0x28, 0xab, 0x1b, |
| 39 | 0x84, 0x0b, 0x17, 0x54, 0x2d, 0xca, 0xf6, 0x20, 0x7a, 0xfd, 0x22, 0x1e, 0x08, 0x6b, 0x2a, 0x60, 0xd1, 0x6c, 0xb7, |
| 40 | 0xf5, 0x44, 0x48, 0xba, 0x9f, 0x3f, 0x08, 0xbc, 0xd0, 0x99, 0xdb, 0x21, 0xdd, 0x16, 0x2a, 0x77, 0x9e, 0x61, 0xaa, |
| 41 | 0x89, 0xee, 0xe5, 0x54, 0xd3, 0xa4, 0x7d, 0xe2, 0x30, 0xbc, 0x7a, 0xc5, 0x90, 0xd5, 0x24, 0x06, 0x7c, 0x38, 0x98, |
| 42 | 0xbb, 0xa6, 0xf5, 0xdc, 0x43, 0x60, 0xb8, 0x45, 0xed, 0xa4, 0x8c, 0xbd, 0x9c, 0xf1, 0x26, 0xa7, 0x23, 0x44, 0x5f, |
| 43 | 0x0e, 0x19, 0x52, 0xd7, 0x32, 0x5a, 0x75, 0xfa, 0xf5, 0x56, 0x14, 0x4f, 0x9a, 0x98, 0xaf, 0x71, 0x86, 0xb0, 0x27, |
| 44 | 0x86, 0x85, 0xb8, 0xe2, 0xc0, 0x8b, 0xea, 0x87, 0x17, 0x1b, 0x4d, 0xee, 0x58, 0x5c, 0x18, 0x28, 0x29, 0x5b, 0x53, |
| 45 | 0x95, 0xeb, 0x4a, 0x17, 0x77, 0x9f, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 46 | 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0xcb, 0x3a, 0xb0, 0x35, 0x7d, 0x7c, 0xd2, 0xae, |
| 47 | 0x97, 0xed, 0x50, 0x1e, 0x51, 0xa3, 0xa0, 0xe2, 0x81, 0x7d, 0x41, 0x8e, 0x47, 0xfb, 0x17, 0x90, 0x64, 0x77, 0xaf, |
| 48 | 0x61, 0x49, 0x5a, 0x7e, 0x8d, 0x87, 0x89, 0x14, 0x10, 0x65, 0xb0, 0x82, 0xd0, 0x01, 0xf4, 0xb1, 0x51, 0x93, 0xd0, |
| 49 | 0xb4, 0x3f, 0xb6, 0x61, 0xcd, 0xe2, 0x0a, 0x64, 0x98, 0x37, 0x2c, 0x6a, 0xbb, 0xd3, 0xdc, 0xb9, 0xf0, 0xd1, 0x26, |
| 50 | 0x59, 0xef, 0x07, 0xb3, 0xc6, 0xdb, 0xdf, 0x8b, 0xdf, 0x2f, 0x65, 0x47, 0x7e, 0xed, 0x7a, 0xdc, 0xd4, 0x57, 0xd7, |
| 51 | 0x93, 0xb1, 0xc2, 0x7b, 0xad, 0xda, 0x7c, 0x5a, 0xde, 0x80, 0xce, 0x95, 0xb7, 0xd8, 0x82, 0x7f, 0xe7, 0x8c, 0x8a, |
| 52 | 0x35, 0xf3, 0xfb, 0x4b, 0xa6, 0x48, 0xa0, 0x81, 0xbe, 0x2c, 0xfe, 0x84, 0x23, 0x1a, 0xba, 0xb3, 0xc2, 0xb5, 0x31, |
| 53 | 0x74, 0x6d, 0xf2, 0xe0, 0x49, 0x2b, 0x00, 0x00, 0x00, 0x01, 0x9a, 0x02, 0xd5, 0x05, 0x8a, 0x04, 0xca, 0xbe, 0x73, |
| 54 | 0x65, 0x6c, 0x66, 0x72, 0x65, 0x67, 0x00, 0xf2, 0xfa, 0xa5, 0x6d, 0x65, 0x6b, 0x69, 0x00, 0x00, 0x04, 0xfa, 0x8e, |
| 55 | 0x33, 0x00, 0x03, 0xd2, 0xd6, 0x32, 0x31, 0x34, 0x37, 0x34, 0x38, 0x33, 0x36, 0x34, 0x37, 0x00, 0x00, 0x00, 0x00, |
| 56 | 0x00, 0x00, 0x02, 0xd2, 0x8e, 0x31, 0x00, 0x00, |
| 57 | 1 |
| 58 | }; |
| 59 | |
| 60 | static inline string toString(const vector<unsigned char> &v) |
| 61 | { |
| 62 | return string(&v[0], &v[0] + v.size()); |
| 63 | } |
| 64 | |
| 65 | static void dumpForwardingEntry(const ForwardingEntry &forwardingEntry) |
| 66 | { |
| 67 | cout << "action: " << forwardingEntry.getAction() << endl; |
| 68 | cout << "prefix: " << forwardingEntry.getPrefix().to_uri() << endl; |
| 69 | cout << "publisherPublicKeyDigest: " |
| 70 | << (forwardingEntry.getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() > 0 ? toHex(forwardingEntry.getPublisherPublicKeyDigest().getPublisherPublicKeyDigest()) : "<none>") << endl; |
| 71 | cout << "faceId: "; |
| 72 | if (forwardingEntry.getFaceId() >= 0) |
| 73 | cout << forwardingEntry.getFaceId() << endl; |
| 74 | else |
| 75 | cout << "<none>" << endl; |
| 76 | cout << "forwardingFlags: "; |
| 77 | if (forwardingEntry.getForwardingFlags() >= 0) |
| 78 | cout << forwardingEntry.getForwardingFlags() << endl; |
| 79 | else |
| 80 | cout << "<none>" << endl; |
| 81 | cout << "freshnessSeconds: "; |
| 82 | if (forwardingEntry.getFreshnessSeconds() >= 0) |
| 83 | cout << forwardingEntry.getFreshnessSeconds() << endl; |
| 84 | else |
| 85 | cout << "<none>" << endl; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Show the interest name and scope, and expect the name to have 4 components where the last component is a data packet |
| 90 | * whose content is a forwarding entry. |
| 91 | */ |
| 92 | static void dumpInterestWithForwardingEntry(const Interest &interest) |
| 93 | { |
| 94 | if (interest.getName().getComponentCount() != 4) { |
| 95 | cout << "Error: expected the interest name to have 4 components. Got: " << interest.getName().to_uri() << endl; |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | cout << "scope: "; |
| 100 | if (interest.getScope() >= 0) |
| 101 | cout << interest.getScope() << endl; |
| 102 | else |
| 103 | cout << "<none>" << endl; |
| 104 | cout << "name[0]: " << toString(interest.getName().getComponent(0).getValue()) << endl; |
| 105 | cout << "name[1]: " << toHex(interest.getName().getComponent(1).getValue()) << endl; |
| 106 | cout << "name[2]: " << toString(interest.getName().getComponent(2).getValue()) << endl; |
| 107 | cout << "name[3] decoded as Data, showing content as ForwardingEntry: " << endl; |
| 108 | |
| 109 | Data data; |
| 110 | data.wireDecode(interest.getName().getComponent(3).getValue()); |
| 111 | |
| 112 | ForwardingEntry forwardingEntry; |
| 113 | forwardingEntry.wireDecode(data.getContent()); |
| 114 | dumpForwardingEntry(forwardingEntry); |
| 115 | } |
| 116 | |
| 117 | int main(int argc, char** argv) |
| 118 | { |
| 119 | try { |
| 120 | Interest interest; |
| 121 | interest.wireDecode(Interest1, sizeof(Interest1)); |
| 122 | cout << "Interest:" << endl; |
| 123 | dumpInterestWithForwardingEntry(interest); |
| 124 | |
| 125 | ptr_lib::shared_ptr<vector<unsigned char> > encoding = interest.wireEncode(); |
| 126 | cout << endl << "Re-encoded interest " << toHex(*encoding) << endl; |
| 127 | |
| 128 | Interest reDecodedInterest; |
| 129 | reDecodedInterest.wireDecode(*encoding); |
| 130 | cout << "Re-decoded Interest:" << endl; |
| 131 | dumpInterestWithForwardingEntry(reDecodedInterest); |
| 132 | } catch (exception &e) { |
| 133 | cout << "exception: " << e.what() << endl; |
| 134 | } |
| 135 | return 0; |
| 136 | } |