servlib/prophet/BundleTLV.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2007 Baylor University
       
     3  *
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  *
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  *
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifndef _PROPHET_BUNDLE_TLV_H_
       
    18 #define _PROPHET_BUNDLE_TLV_H_
       
    19 
       
    20 #include <sys/types.h>
       
    21 #include "BaseTLV.h"
       
    22 #include "BundleTLVEntry.h"
       
    23 #include "BundleTLVEntryList.h"
       
    24 
       
    25 namespace prophet
       
    26 {
       
    27 
       
    28 class BundleTLV : public BaseTLV
       
    29 {
       
    30 public:
       
    31     /**
       
    32      * Bundle Offer/Response Header<br>
       
    33      * p. 30, 4.4.5<br>
       
    34      * <br>
       
    35      * After the routing information has been passed, the node will ask the
       
    36      * other node to review available bundles and determine which bundles it
       
    37      * will accept for relay.  The source relay will determine which bundles
       
    38      * to offer based on relative delivery predictabilities as explained in
       
    39      * Section 3.6.  The Bundle Offer TLV also lists the bundles that a
       
    40      * PRoPHET acknowledgement has been issued for.  Those bundles have the
       
    41      * PRoPHET ACK flag set in their entry in the list.  When a node
       
    42      * receives a PRoPHET ACK for a bundle, it MUST remove any copies of
       
    43      * that bundle from its buffers, but SHOULD keep an entry of the
       
    44      * acknowledged bundle to be able to further propagate the PRoPHET ACK.
       
    45      * <br><br>
       
    46      * The Response message is identical to the request message with the
       
    47      * exception that the flag indicate acceptance of the bundle.
       
    48      */
       
    49     struct BundleTLVHeader {
       
    50         u_int8_t type; ///< defined as 0xA2
       
    51         u_int8_t flags; ///< TBD
       
    52         /**
       
    53          * Length of the TLV in octets, including the TLV header and any
       
    54          * nested TLVs.
       
    55          */
       
    56         u_int16_t length;
       
    57         /**
       
    58          * Number of bundle offer entries.
       
    59          */
       
    60         u_int16_t offer_count;
       
    61         u_int16_t unused__;
       
    62     } __attribute__((packed));
       
    63 
       
    64     /**
       
    65      * Bundle Offer/Response Entry
       
    66      * p. 30, 4.4.5
       
    67      */
       
    68     struct BundleEntry {
       
    69         /**
       
    70          * ID string of the destination of the bundle as predefined in the
       
    71          * dictionary TLV.
       
    72          */
       
    73         u_int16_t dest_string_id;
       
    74         /**
       
    75          * The encoding of the B_Flags in the request are: <br>
       
    76          * <br>
       
    77          *      Flag 0: Custody Offered     0b1 <br>
       
    78          *      Flag 1: Reserved            0b1 <br>
       
    79          *      Flag 2: Reserved            0b1 <br>
       
    80          *      Flag 3: Reserved            0b1 <br>
       
    81          *      Flag 4: Reserved            0b1 <br>
       
    82          *      Flag 5: Reserved            0b1 <br>
       
    83          *      Flag 6: Reserved            0b1 <br>
       
    84          *      Flag 7: PRoPHET ACK         0b1 <br>
       
    85          * <br>
       
    86          * The encoding of the B_flag values in the response are: <br>
       
    87          * <br>
       
    88          *      Flag 0: Custody Accepted    0b1 <br>
       
    89          *      Flag 1: Bundle Accepted     0b1 <br>
       
    90          *      Flag 2: Reserved            0b1 <br>
       
    91          *      Flag 3: Reserved            0b1 <br>
       
    92          *      Flag 4: Reserved            0b1 <br>
       
    93          *      Flag 5: Reserved            0b1 <br>
       
    94          *      Flag 6: Reserved            0b1 <br>
       
    95          *      Flag 7: Reserved            0b1 <br>
       
    96          * <br> 
       
    97          */
       
    98         u_int8_t b_flags;
       
    99         u_int8_t unused__;
       
   100         u_int32_t creation_timestamp; ///< This bundle's creation timestamp
       
   101         u_int32_t seqno; ///< NOT IN SPEC
       
   102     } __attribute__((packed));
       
   103 
       
   104     /**
       
   105      * Flag values for Bundle Offer/Response Entry
       
   106      * p. 31, 4.4.5
       
   107      */
       
   108     typedef enum {
       
   109         CUSTODY  = 1 << 0, ///< custody offered or accepted on this bundle
       
   110         ACCEPTED = 1 << 1, ///< this bundle is accepted for relay
       
   111         ACK      = 1 << 7  ///< Prophet ACK for this bundle
       
   112     } bundletlv_flags_t;
       
   113 
       
   114     static const size_t BundleTLVHeaderSize = sizeof(struct BundleTLVHeader);
       
   115 
       
   116     static const size_t BundleEntrySize = sizeof(struct BundleEntry);
       
   117 
       
   118     /**
       
   119      * Destructor.
       
   120      */
       
   121     virtual ~BundleTLV() {}
       
   122 
       
   123 protected:
       
   124     /**
       
   125      * Constructor. Protected to force access through derived classes.
       
   126      */
       
   127     BundleTLV(BaseTLV::prophet_tlv_t type = BaseTLV::UNKNOWN_TLV,
       
   128               u_int8_t flags = 0, u_int16_t length = 0)
       
   129         : BaseTLV(type,flags,length) {}
       
   130 
       
   131     /**
       
   132      * Serialize BundleOfferTLVEntry out to no more than len bytes of
       
   133      * buffer; return bytes written
       
   134      */
       
   135     size_t write_bundle_entry(u_int32_t cts, u_int32_t seq, u_int16_t sid,
       
   136                              bool custody, bool accept, bool ack,
       
   137                              BundleTLVEntry::bundle_entry_t type,
       
   138                              u_char* bp, size_t len) const;
       
   139 
       
   140     /**
       
   141      * Deserialize struct BundleOfferTLVEntry from transport into memory,
       
   142      * reading no more than len bytes from buffer; return bytes read
       
   143      */
       
   144     size_t read_bundle_entry(u_int32_t *cts, u_int32_t *seq, u_int16_t *sid,
       
   145                              bool *custody, bool *accept, bool *ack,
       
   146                              BundleTLVEntry::bundle_entry_t *type,
       
   147                              const u_char* bp, size_t len );
       
   148 }; // class BundleTLV
       
   149 
       
   150 }; // namespace prophet
       
   151 
       
   152 #endif // _PROPHET_BUNDLE_TLV_H_