Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: |
| 19 | */ |
| 20 | |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 21 | #include "ccnx-encoding-helper.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 22 | |
| 23 | #include "ns3/name-components.h" |
| 24 | #include "ns3/ccnx-interest-header.h" |
| 25 | #include "ns3/ccnx-content-object-header.h" |
| 26 | |
| 27 | #include <sstream> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 28 | #include <boost/foreach.hpp> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ns3 { |
| 31 | |
| 32 | #define CCN_TT_BITS 3 |
| 33 | #define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1) |
| 34 | #define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1) |
| 35 | #define CCN_TT_HBIT ((unsigned char)(1 << 7)) |
| 36 | |
| 37 | size_t |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 38 | CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator start, size_t val, CcnbParser::ccn_tt tt) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 39 | { |
| 40 | unsigned char buf[1+8*((sizeof(val)+6)/7)]; |
| 41 | unsigned char *p = &(buf[sizeof(buf)-1]); |
| 42 | size_t n = 1; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 43 | p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 44 | ((val & CCN_MAX_TINY) << CCN_TT_BITS) | |
| 45 | (CCN_TT_MASK & tt); |
| 46 | val >>= (7-CCN_TT_BITS); |
| 47 | while (val != 0) { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 48 | (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 49 | n++; |
| 50 | val >>= 7; |
| 51 | } |
| 52 | start.Write (p,n); |
| 53 | return n; |
| 54 | } |
| 55 | |
| 56 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 57 | CcnxEncodingHelper::AppendNumber (Buffer::Iterator start, uint32_t number) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 58 | { |
| 59 | std::ostringstream os; |
| 60 | os << number; |
| 61 | |
| 62 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 63 | written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 64 | written += os.str().size(); |
| 65 | start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size()); |
| 66 | |
| 67 | return written; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 72 | CcnxEncodingHelper::CcnxEncodingHelper::AppendCloser (Buffer::Iterator start) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 73 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 74 | start.WriteU8 (CcnbParser::CCN_CLOSE); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 79 | CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator start, const Name::Components &name) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 80 | { |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 81 | size_t written = 0; |
| 82 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 83 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 84 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component, |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 85 | reinterpret_cast<const uint8_t*>(component.c_str()), component.size()); |
| 86 | } |
| 87 | return written; |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 91 | CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator start, Time time) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 92 | { |
| 93 | // the original function implements Markers... thought not sure what are these markers for... |
| 94 | |
| 95 | // Determine miminal number of bytes required to store the timestamp |
| 96 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 97 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 98 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 99 | required_bytes++; |
| 100 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 101 | size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 102 | |
| 103 | // write part with seconds |
| 104 | ts = time.ToInteger (Time::S) >> 4; |
| 105 | for (int i = 0; i < required_bytes - 2; i++) |
| 106 | start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) ); |
| 107 | |
| 108 | /* arithmetic contortions are to avoid overflowing 31 bits */ |
| 109 | ts = ((time.ToInteger (Time::S) & 15) << 12) + ((time.ToInteger (Time::NS) / 5 * 8 + 195312) / 390625); |
| 110 | for (int i = required_bytes - 2; i < required_bytes; i++) |
| 111 | start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) ); |
| 112 | |
| 113 | return len + required_bytes; |
| 114 | } |
| 115 | |
| 116 | size_t |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 117 | CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator start, CcnbParser::ccn_dtag dtag, |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 118 | const uint8_t *data, size_t size) |
| 119 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 120 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 121 | if (size>0) |
| 122 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 123 | written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 124 | start.Write (data, size); |
| 125 | written += size; |
| 126 | } |
| 127 | written += AppendCloser (start); |
| 128 | |
| 129 | return written; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 134 | CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 135 | { |
| 136 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 137 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Interest, CcnbParser::CCN_DTAG); // <Interest> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 139 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 140 | written += AppendNameComponents (start, interest.GetName()); // <Component>...</Component>... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 141 | written += AppendCloser (start); // </Name> |
| 142 | |
| 143 | if (interest.GetMinSuffixComponents() >= 0) |
| 144 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 145 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MinSuffixComponents, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 146 | written += AppendNumber (start, interest.GetMinSuffixComponents ()); |
| 147 | written += AppendCloser (start); |
| 148 | } |
| 149 | if (interest.GetMaxSuffixComponents() >= 0) |
| 150 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 151 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MaxSuffixComponents, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 152 | written += AppendNumber (start, interest.GetMaxSuffixComponents ()); |
| 153 | written += AppendCloser (start); |
| 154 | } |
| 155 | if (interest.GetExclude().size() > 0) |
| 156 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 157 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Exclude, CcnbParser::CCN_DTAG); // <Exclude> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 158 | written += AppendNameComponents (start, interest.GetExclude()); // <Component>...</Component>... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 159 | written += AppendCloser (start); // </Exclude> |
| 160 | } |
| 161 | if (interest.IsEnabledChildSelector()) |
| 162 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 163 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ChildSelector, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 164 | written += AppendNumber (start, 1); |
| 165 | written += AppendCloser (start); |
| 166 | } |
| 167 | if (interest.IsEnabledAnswerOriginKind()) |
| 168 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 169 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_AnswerOriginKind, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 170 | written += AppendNumber (start, 1); |
| 171 | written += AppendCloser (start); |
| 172 | } |
| 173 | if (interest.GetScope() >= 0) |
| 174 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 175 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Scope, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 176 | written += AppendNumber (start, interest.GetScope ()); |
| 177 | written += AppendCloser (start); |
| 178 | } |
| 179 | if (!interest.GetInterestLifetime().IsZero()) |
| 180 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 181 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_InterestLifetime, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 182 | written += AppendTimestampBlob (start, interest.GetInterestLifetime()); |
| 183 | written += AppendCloser (start); |
| 184 | } |
| 185 | if (interest.GetNonce()>0) |
| 186 | { |
| 187 | uint32_t nonce = interest.GetNonce(); |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 188 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Nonce, |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 189 | reinterpret_cast<const uint8_t*>(&nonce), |
| 190 | sizeof(nonce)); |
| 191 | } |
| 192 | written += AppendCloser (start); // </Interest> |
| 193 | |
| 194 | return written; |
| 195 | } |
| 196 | |
| 197 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 198 | CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 199 | { |
| 200 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 201 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 202 | |
| 203 | // fake signature |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 204 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 205 | // Signature ::= DigestAlgorithm? |
| 206 | // Witness? |
| 207 | // SignatureBits |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 208 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits /> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 209 | written += AppendCloser (start); // </Signature> |
| 210 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 211 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 212 | written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>... |
| 213 | written += AppendCloser (start); // </Name> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 214 | |
| 215 | // fake signature |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 216 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 217 | // SignedInfo ::= PublisherPublicKeyDigest |
| 218 | // Timestamp |
| 219 | // Type? |
| 220 | // FreshnessSeconds? |
| 221 | // FinalBlockID? |
| 222 | // KeyLocator? |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 223 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest /> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 224 | written += AppendCloser (start); // </SignedInfo> |
| 225 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 226 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 227 | |
| 228 | // there is no closing tag !!! |
| 229 | return written; |
| 230 | } |
| 231 | |
| 232 | } // namespace ns3 |