<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>DTN Reference Implementation: OfferTLV.cc Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.6 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <h1>OfferTLV.cc</h1><a href="OfferTLV_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * Copyright 2007 Baylor University</span> <a name="l00003"></a>00003 <span class="comment"> *</span> <a name="l00004"></a>00004 <span class="comment"> * Licensed under the Apache License, Version 2.0 (the "License");</span> <a name="l00005"></a>00005 <span class="comment"> * you may not use this file except in compliance with the License.</span> <a name="l00006"></a>00006 <span class="comment"> * You may obtain a copy of the License at</span> <a name="l00007"></a>00007 <span class="comment"> *</span> <a name="l00008"></a>00008 <span class="comment"> * http://www.apache.org/licenses/LICENSE-2.0</span> <a name="l00009"></a>00009 <span class="comment"> *</span> <a name="l00010"></a>00010 <span class="comment"> * Unless required by applicable law or agreed to in writing, software</span> <a name="l00011"></a>00011 <span class="comment"> * distributed under the License is distributed on an "AS IS" BASIS,</span> <a name="l00012"></a>00012 <span class="comment"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span> <a name="l00013"></a>00013 <span class="comment"> * See the License for the specific language governing permissions and</span> <a name="l00014"></a>00014 <span class="comment"> * limitations under the License.</span> <a name="l00015"></a>00015 <span class="comment"> */</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="preprocessor">#include <arpa/inet.h></span> <span class="comment">// for hton[ls] and ntoh[ls]</span> <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="OfferTLV_8h.html">OfferTLV.h</a>"</span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="keyword">namespace </span>prophet <a name="l00021"></a>00021 { <a name="l00022"></a>00022 <a name="l00023"></a><a class="code" href="classprophet_1_1OfferTLV.html#77d6ff73a03e9ab11c531767c49a3870">00023</a> <a class="code" href="classprophet_1_1OfferTLV.html#97065b9dc8825b3c5499a0f8ba7e9902" title="Constructor.">OfferTLV::OfferTLV</a>(<span class="keyword">const</span> <a class="code" href="classprophet_1_1BundleOfferList.html" title="In-memory representation of list of bundle offer entries from a Bundle TLV sent by...">BundleOfferList</a>& bl) <a name="l00024"></a>00024 : <a class="code" href="classprophet_1_1BundleTLV.html">BundleTLV</a>(<a class="code" href="classprophet_1_1BaseTLV.html" title="The Prophet I-D (March 2006) dictates five bytecodes for router state exchange messages...">BaseTLV</a>::OFFER_TLV,0,bl.guess_size(BundleEntrySize)), <a name="l00025"></a>00025 list_(bl) {} <a name="l00026"></a>00026 <a name="l00027"></a><a class="code" href="classprophet_1_1OfferTLV.html#97065b9dc8825b3c5499a0f8ba7e9902">00027</a> OfferTLV::OfferTLV() <a name="l00028"></a>00028 : <a class="code" href="classprophet_1_1BundleTLV.html">BundleTLV</a>(<a class="code" href="classprophet_1_1BaseTLV.html" title="The Prophet I-D (March 2006) dictates five bytecodes for router state exchange messages...">BaseTLV</a>::OFFER_TLV) {} <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="keywordtype">size_t</span> <a name="l00031"></a><a class="code" href="classprophet_1_1OfferTLV.html#d010485b4fe6fec79a419fbf977be183">00031</a> <a class="code" href="classprophet_1_1OfferTLV.html#d010485b4fe6fec79a419fbf977be183" title="Virtual from BaseTLV.">OfferTLV::serialize</a>(u_char* bp, <span class="keywordtype">size_t</span> <a class="code" href="num2sdnv_8c.html#fed088663f8704004425cdae2120b9b3">len</a>)<span class="keyword"> const</span> <a name="l00032"></a>00032 <span class="keyword"></span>{ <a name="l00033"></a>00033 <span class="comment">// weed out the oddball</span> <a name="l00034"></a>00034 <span class="keywordflow">if</span> (bp == NULL) <span class="keywordflow">return</span> 0; <a name="l00035"></a>00035 <span class="keywordflow">if</span> (<a class="code" href="classprophet_1_1BaseTLV.html#88263c57b5c2760ece631ce8943c6249" title="typecode for this TLV">typecode_</a> != <a class="code" href="classprophet_1_1BaseTLV.html#37c3690d6040c870b40fa0436b67b09d4678012b183261a4608ea83babc8c315">BaseTLV::OFFER_TLV</a>) <span class="keywordflow">return</span> 0; <a name="l00036"></a>00036 <a name="l00037"></a>00037 <span class="comment">// check that lengths match up</span> <a name="l00038"></a>00038 <span class="keywordflow">if</span> (<a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a> + <a name="l00039"></a>00039 <a class="code" href="classprophet_1_1OfferTLV.html#22b1901feadb35e7fdc3d333ef97abb5" title="Priority sorted list of Bundles to offer to peer.">list_</a>.<a class="code" href="classprophet_1_1BundleEntryList.html#881e48ea062bab1b88fd041f8f773f4a" title="Estimate serialized buffer length.">guess_size</a>(<a class="code" href="classprophet_1_1BundleTLV.html#a7921e77cc4c5585d79932a24b8a7927">BundleEntrySize</a>) > len) <a name="l00040"></a>00040 <span class="keywordflow">return</span> 0; <a name="l00041"></a>00041 <a name="l00042"></a>00042 <span class="comment">// account for bytes written</span> <a name="l00043"></a>00043 <a class="code" href="classprophet_1_1BaseTLV.html#4c0a7b24b795bafb52c16b24a6188823" title="serialized length of TLV, mutable so it can be assigned by serialize() const">length_</a> = 0; <a name="l00044"></a>00044 <a name="l00045"></a>00045 <span class="comment">// start writing out to buffer</span> <a name="l00046"></a>00046 <a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html" title="Bundle Offer/Response Header p.">BundleTLVHeader</a>* hdr = (<a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html" title="Bundle Offer/Response Header p.">BundleTLVHeader</a>*) bp; <a name="l00047"></a>00047 memset(hdr, 0, <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a>); <a name="l00048"></a>00048 hdr-><a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html#31bfa6f1819c6aecd2065c4f550c0bfe" title="defined as 0xA2">type</a> = <a class="code" href="classprophet_1_1BaseTLV.html#37c3690d6040c870b40fa0436b67b09d4678012b183261a4608ea83babc8c315">BaseTLV::OFFER_TLV</a>; <a name="l00049"></a>00049 hdr-><a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html#f3429726e572924f0520dc0fbcb88006" title="TBD.">flags</a> = 0; <a name="l00050"></a>00050 hdr-><a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html#ec3412375b6310f156fc1121d8101e21" title="Number of bundle offer entries.">offer_count</a> = htons(<a class="code" href="classprophet_1_1OfferTLV.html#22b1901feadb35e7fdc3d333ef97abb5" title="Priority sorted list of Bundles to offer to peer.">list_</a>.<a class="code" href="classprophet_1_1BundleEntryList.html#7f9d7c0e391d203027f2ce3ba3eca232" title="Return number of Bundle offers in list.">size</a>()); <a name="l00051"></a>00051 <a name="l00052"></a>00052 <a class="code" href="classprophet_1_1BaseTLV.html#4c0a7b24b795bafb52c16b24a6188823" title="serialized length of TLV, mutable so it can be assigned by serialize() const">length_</a> += <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a>; <a name="l00053"></a>00053 bp += <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a>; <a name="l00054"></a>00054 len -= <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a>; <a name="l00055"></a>00055 <a name="l00056"></a>00056 <span class="comment">// write out each Bundle offer</span> <a name="l00057"></a>00057 <span class="keywordflow">for</span> (<a class="code" href="classprophet_1_1BundleEntryList.html#3332c2306c20626dd5e0b9767ac2c378">BundleOfferList::const_iterator</a> i = <a class="code" href="classprophet_1_1OfferTLV.html#22b1901feadb35e7fdc3d333ef97abb5" title="Priority sorted list of Bundles to offer to peer.">list_</a>.<a class="code" href="classprophet_1_1BundleEntryList.html#475b43261ea35c8ff5ef1c9104b3fe78" title="These iterators are not thread safe.">begin</a>(); <a name="l00058"></a>00058 i != <a class="code" href="classprophet_1_1OfferTLV.html#22b1901feadb35e7fdc3d333ef97abb5" title="Priority sorted list of Bundles to offer to peer.">list_</a>.<a class="code" href="classprophet_1_1BundleEntryList.html#bc89e63957c0c2242d9d7b1396f273f0" title="These iterators are not thread safe.">end</a>(); <a name="l00059"></a>00059 i++) <a name="l00060"></a>00060 { <a name="l00061"></a>00061 <span class="keyword">const</span> <a class="code" href="classprophet_1_1BundleOfferEntry.html" title="BundleOfferEntry represents one Bundle, a single entry in a Bundle TLV sent by a...">BundleOfferEntry</a>* b = *i; <a name="l00062"></a>00062 <span class="keywordtype">size_t</span> bytes_written = <a name="l00063"></a>00063 <a class="code" href="classprophet_1_1BundleTLV.html#eeadbbb84b780c5d390a9f1e6318c850" title="Serialize BundleOfferTLVEntry out to no more than len bytes of buffer; return bytes...">BundleTLV::write_bundle_entry</a>(b-><a class="code" href="classprophet_1_1BundleTLVEntry.html#1cf4b89ca8699e54a91e04dd4fc7c86e" title="Accessors.">creation_ts</a>(), <a name="l00064"></a>00064 b-><a class="code" href="classprophet_1_1BundleTLVEntry.html#f53626463b117791141a4c6e1367eecc" title="Accessors.">seqno</a>(), b-><a class="code" href="classprophet_1_1BundleTLVEntry.html#c470331376431b7ac04bc76578011298" title="Accessors.">sid</a>(), b-><a class="code" href="classprophet_1_1BundleTLVEntry.html#b0ed670f0d842a2f68a527e72c14d266" title="Accessors.">custody</a>(), <a name="l00065"></a>00065 b-><a class="code" href="classprophet_1_1BundleTLVEntry.html#40379b850503c0a3d9c9bc8bd1143abb" title="Accessors.">accept</a>(), b-><a class="code" href="classprophet_1_1BundleTLVEntry.html#6743ec6543c7e9051d68fc2bf3b166a4" title="Accessors.">ack</a>(), b-><a class="code" href="classprophet_1_1BundleTLVEntry.html#2d97556fa7fb52f5a8b041b463f59e72" title="Accessors.">type</a>(), <a name="l00066"></a>00066 bp, len); <a name="l00067"></a>00067 <a name="l00068"></a>00068 <span class="keywordflow">if</span> (<a class="code" href="classprophet_1_1BundleTLV.html#a7921e77cc4c5585d79932a24b8a7927">BundleEntrySize</a> > bytes_written) <span class="keywordflow">break</span>; <a name="l00069"></a>00069 <a name="l00070"></a>00070 length_ += bytes_written; <a name="l00071"></a>00071 len -= bytes_written; <a name="l00072"></a>00072 bp += bytes_written; <a name="l00073"></a>00073 } <a name="l00074"></a>00074 <a name="l00075"></a>00075 hdr-><a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html#90c4a8059bcc4c022f913e8ea05b3935" title="Length of the TLV in octets, including the TLV header and any nested TLVs.">length</a> = htons(length_); <a name="l00076"></a>00076 <span class="keywordflow">return</span> length_; <a name="l00077"></a>00077 } <a name="l00078"></a>00078 <a name="l00079"></a>00079 <span class="keywordtype">bool</span> <a name="l00080"></a><a class="code" href="classprophet_1_1OfferTLV.html#050f140354ba636ac5b1ed5b0d4dfae6">00080</a> <a class="code" href="classprophet_1_1OfferTLV.html#050f140354ba636ac5b1ed5b0d4dfae6" title="Virtual from BaseTLV.">OfferTLV::deserialize</a>(<span class="keyword">const</span> u_char* bp, <span class="keywordtype">size_t</span> <a class="code" href="num2sdnv_8c.html#fed088663f8704004425cdae2120b9b3">len</a>) <a name="l00081"></a>00081 { <a name="l00082"></a>00082 <span class="comment">// weed out the oddball</span> <a name="l00083"></a>00083 <span class="keywordflow">if</span> (bp == NULL) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00084"></a>00084 <span class="keywordflow">if</span> (<a class="code" href="classprophet_1_1BaseTLV.html#88263c57b5c2760ece631ce8943c6249" title="typecode for this TLV">typecode_</a> != <a class="code" href="classprophet_1_1BaseTLV.html#37c3690d6040c870b40fa0436b67b09d4678012b183261a4608ea83babc8c315">OFFER_TLV</a>) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00085"></a>00085 <span class="keywordflow">if</span> (! <a class="code" href="classprophet_1_1OfferTLV.html#22b1901feadb35e7fdc3d333ef97abb5" title="Priority sorted list of Bundles to offer to peer.">list_</a>.<a class="code" href="classprophet_1_1BundleEntryList.html#2ac8813783a19d6a1ec23915ce958032" title="Return whether list is empty.">empty</a>()) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00086"></a>00086 <a name="l00087"></a>00087 <span class="comment">// check that lengths match up</span> <a name="l00088"></a>00088 <span class="keywordflow">if</span> (<a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a> > len) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00089"></a>00089 <a name="l00090"></a>00090 <span class="comment">// zero out accounting</span> <a name="l00091"></a>00091 <span class="keywordtype">size_t</span> amt_read = 0; <a name="l00092"></a>00092 <a class="code" href="classprophet_1_1BaseTLV.html#4c0a7b24b795bafb52c16b24a6188823" title="serialized length of TLV, mutable so it can be assigned by serialize() const">length_</a> = 0; <a name="l00093"></a>00093 <a name="l00094"></a>00094 <span class="comment">// parse the header</span> <a name="l00095"></a>00095 <a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html" title="Bundle Offer/Response Header p.">BundleTLVHeader</a>* b = (<a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html" title="Bundle Offer/Response Header p.">BundleTLVHeader</a>*) bp; <a name="l00096"></a>00096 <span class="keywordflow">if</span> (b-><a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html#31bfa6f1819c6aecd2065c4f550c0bfe" title="defined as 0xA2">type</a> != <a class="code" href="classprophet_1_1BaseTLV.html#88263c57b5c2760ece631ce8943c6249" title="typecode for this TLV">typecode_</a>) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00097"></a>00097 <a class="code" href="classprophet_1_1BaseTLV.html#4c0a7b24b795bafb52c16b24a6188823" title="serialized length of TLV, mutable so it can be assigned by serialize() const">length_</a> = ntohs(b-><a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html#90c4a8059bcc4c022f913e8ea05b3935" title="Length of the TLV in octets, including the TLV header and any nested TLVs.">length</a>); <a name="l00098"></a>00098 <span class="keywordflow">if</span> (<a class="code" href="classprophet_1_1BaseTLV.html#4c0a7b24b795bafb52c16b24a6188823" title="serialized length of TLV, mutable so it can be assigned by serialize() const">length_</a> > len) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00099"></a>00099 <a class="code" href="classprophet_1_1BaseTLV.html#31f1ebbf538b5b28c42e49e1eef6a34e" title="TLV-specific flags.">flags_</a> = b-><a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html#f3429726e572924f0520dc0fbcb88006" title="TBD.">flags</a>; <a name="l00100"></a>00100 <span class="keywordtype">size_t</span> offer_count = ntohs(b-><a class="code" href="structprophet_1_1BundleTLV_1_1BundleTLVHeader.html#ec3412375b6310f156fc1121d8101e21" title="Number of bundle offer entries.">offer_count</a>); <a name="l00101"></a>00101 <a name="l00102"></a>00102 <span class="comment">// now move past the header</span> <a name="l00103"></a>00103 bp += <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a>; <a name="l00104"></a>00104 len -= <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a>; <a name="l00105"></a>00105 amt_read += <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a>; <a name="l00106"></a>00106 <a name="l00107"></a>00107 <span class="keywordflow">while</span> (len >= <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a> && <a name="l00108"></a>00108 amt_read + <a class="code" href="classprophet_1_1BundleTLV.html#9f8735ed361f09d5bb05fc0975c6b191">BundleTLVHeaderSize</a> <= <a class="code" href="classprophet_1_1BaseTLV.html#4c0a7b24b795bafb52c16b24a6188823" title="serialized length of TLV, mutable so it can be assigned by serialize() const">length_</a> && <a name="l00109"></a>00109 offer_count-- > 0) <a name="l00110"></a>00110 { <a name="l00111"></a>00111 u_int32_t cts = 0; <a name="l00112"></a>00112 u_int32_t seq = 0; <a name="l00113"></a>00113 u_int16_t sid = 0; <a name="l00114"></a>00114 <span class="keywordtype">bool</span> <a class="code" href="dtncat_8c.html#160881851a33309c0106503ac4d14d96">custody</a> = <span class="keyword">false</span>, <a name="l00115"></a>00115 accept = <span class="keyword">false</span>, <a name="l00116"></a>00116 ack = <span class="keyword">false</span>; <a name="l00117"></a>00117 <a class="code" href="classprophet_1_1BundleTLVEntry.html#ba508cc555c3c88394dc4659c7e0d321" title="A BundleTLVEntry can represent either an OFFER or a RESPONSE, depending on what is...">BundleTLVEntry::bundle_entry_t</a> type = <a class="code" href="classprophet_1_1BundleTLVEntry.html#ba508cc555c3c88394dc4659c7e0d3215a1a084212865f78c542f8c458201a19" title="no valid type has been specified">BundleTLVEntry::UNDEFINED</a>; <a name="l00118"></a>00118 <a name="l00119"></a>00119 <span class="keywordtype">size_t</span> bytes_read = <a name="l00120"></a>00120 <a class="code" href="classprophet_1_1BundleTLV.html#4fe36522dccc0c81046b72e54d02d05c" title="Deserialize struct BundleOfferTLVEntry from transport into memory, reading no more...">BundleTLV::read_bundle_entry</a>(&cts,&seq,&sid,&custody, <a name="l00121"></a>00121 &accept,&ack,&type,bp,len); <a name="l00122"></a>00122 <a name="l00123"></a>00123 <span class="keywordflow">if</span> (<a class="code" href="classprophet_1_1BundleTLV.html#a7921e77cc4c5585d79932a24b8a7927">BundleEntrySize</a> > bytes_read) <a name="l00124"></a>00124 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00125"></a>00125 <a name="l00126"></a>00126 <span class="keywordflow">if</span> (type != <a class="code" href="classprophet_1_1BundleTLVEntry.html#ba508cc555c3c88394dc4659c7e0d321fa6f7dc01f904230e508005e6f942085" title="Bundle OFFER.">BundleTLVEntry::OFFER</a>) <a name="l00127"></a>00127 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00128"></a>00128 <a name="l00129"></a>00129 <span class="keywordflow">if</span> (! <a class="code" href="classprophet_1_1OfferTLV.html#22b1901feadb35e7fdc3d333ef97abb5" title="Priority sorted list of Bundles to offer to peer.">list_</a>.<a class="code" href="classprophet_1_1BundleOfferList.html#fd09313f46f237e0246706360b4c7a3d" title="Convenience method to add Bundle to the list; return whether successful.">add_offer</a>(cts,seq,sid,custody,ack)) <a name="l00130"></a>00130 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00131"></a>00131 <a name="l00132"></a>00132 <span class="comment">// account for what's been read</span> <a name="l00133"></a>00133 amt_read += bytes_read; <a name="l00134"></a>00134 len -= bytes_read; <a name="l00135"></a>00135 bp += bytes_read; <a name="l00136"></a>00136 } <a name="l00137"></a>00137 <a name="l00138"></a>00138 <span class="keywordflow">return</span> (amt_read == <a class="code" href="classprophet_1_1BaseTLV.html#4c0a7b24b795bafb52c16b24a6188823" title="serialized length of TLV, mutable so it can be assigned by serialize() const">length_</a>); <a name="l00139"></a>00139 } <a name="l00140"></a>00140 <a name="l00141"></a>00141 }; <span class="comment">// namespace prophet</span> </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Mon Jul 21 14:09:46 2008 for DTN Reference Implementation by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address> </body> </html>