servlib/prophet/BundleTLV.cc
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 #include <arpa/inet.h> // for hton[ls] and ntoh[ls]
       
    18 #include "BundleTLV.h"
       
    19 
       
    20 namespace prophet
       
    21 {
       
    22 
       
    23 size_t BundleTLV::write_bundle_entry(u_int32_t cts, u_int32_t seq,
       
    24                                      u_int16_t sid, bool custody,
       
    25                                      bool accept, bool ack,
       
    26                                      BundleTLVEntry::bundle_entry_t type, 
       
    27                                      u_char* bp, size_t len) const
       
    28 {
       
    29     // weed out the oddball
       
    30     if (bp == NULL) return 0;
       
    31     if (type == BundleTLVEntry::UNDEFINED) return 0;
       
    32 
       
    33     // check that lengths match up
       
    34     if (BundleEntrySize > len) return 0;
       
    35 
       
    36     // start writing out to buffer
       
    37     BundleEntry* b = (BundleEntry*) bp;
       
    38     memset(b, 0, BundleEntrySize);
       
    39     if (custody) b->b_flags |= CUSTODY;
       
    40     if (accept)  b->b_flags |= ACCEPTED;
       
    41     if (ack)     b->b_flags |= ACK;
       
    42     b->dest_string_id     = htons(sid);
       
    43     b->creation_timestamp = htonl(cts);
       
    44     b->seqno              = htonl(seq);
       
    45 
       
    46     return BundleEntrySize;
       
    47 }
       
    48 
       
    49 size_t BundleTLV::read_bundle_entry(u_int32_t *cts, u_int32_t *seq,
       
    50                                     u_int16_t *sid, bool *custody,
       
    51                                     bool *accept, bool *ack,
       
    52                                     BundleTLVEntry::bundle_entry_t *type,
       
    53                                     const u_char* bp, size_t len)
       
    54 {
       
    55     // weed out the oddball
       
    56     if (bp      == NULL ||
       
    57         cts     == NULL ||
       
    58         seq     == NULL ||
       
    59         sid     == NULL ||
       
    60         custody == NULL ||
       
    61         accept  == NULL ||
       
    62         ack     == NULL ||
       
    63         type    == NULL)   return 0;
       
    64 
       
    65     // check that lengths match up
       
    66     if (BundleEntrySize > len) return 0;
       
    67 
       
    68     // start reading in the entry
       
    69     BundleEntry* b = (BundleEntry*) bp;
       
    70     u_int8_t flags = b->b_flags & 0xff; // mask out one byte
       
    71     *custody = ((flags & CUSTODY)  == CUSTODY);
       
    72     *accept  = ((flags & ACCEPTED) == ACCEPTED);
       
    73     *ack     = ((flags & ACK)      == ACK);
       
    74     // infer whether OFFER or RESPONSE
       
    75     *type = BundleTLVEntry::decode_flags(*custody,*accept,*ack);
       
    76     *sid = ntohs(b->dest_string_id);
       
    77     *cts = ntohl(b->creation_timestamp);
       
    78     *seq = ntohl(b->seqno);
       
    79 
       
    80     return BundleEntrySize;
       
    81 }
       
    82 
       
    83 };