servlib/prophet/HelloTLV.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 "Util.h"
       
    19 #include "HelloTLV.h"
       
    20 #include "BundleCore.h"
       
    21 
       
    22 namespace prophet
       
    23 {
       
    24 
       
    25 HelloTLV::HelloTLV()
       
    26     : BaseTLV(BaseTLV::HELLO_TLV),
       
    27       hf_(HF_UNKNOWN),
       
    28       timer_(0) {}
       
    29 
       
    30 HelloTLV::HelloTLV(hello_hf_t hf,
       
    31                    u_int8_t timer,
       
    32                    const std::string& sender)
       
    33     : BaseTLV(BaseTLV::HELLO_TLV,0,
       
    34               HelloTLVHeaderSize + FOUR_BYTE_ALIGN(sender.size())),
       
    35       hf_(hf),
       
    36       timer_(timer),
       
    37       sender_(sender)
       
    38 {
       
    39 }
       
    40 
       
    41 bool
       
    42 HelloTLV::deserialize(const u_char* bp, size_t len)
       
    43 {
       
    44     HelloTLVHeader* hdr = (HelloTLVHeader*) bp;
       
    45 
       
    46     // weed out the oddball
       
    47     if (bp == NULL) return false;
       
    48 
       
    49     // fail out if wrong byte code
       
    50     if (hdr->type != HELLO_TLV) return false;
       
    51 
       
    52     // fail out if less than min size
       
    53     if (len < HelloTLVHeaderSize) return false;
       
    54 
       
    55     // fail out if length's don't match up
       
    56     size_t hello_len = ntohs(hdr->length);
       
    57     if (len < hello_len) return false;
       
    58     if (hello_len < hdr->name_length) return false;
       
    59 
       
    60     // Parse!
       
    61     hf_     = (hello_hf_t) hdr->HF;
       
    62     length_ = hello_len;
       
    63     timer_  = hdr->timer;
       
    64 
       
    65     std::string name((char*)&hdr->sender_name[0],(int)hdr->name_length);
       
    66     sender_.assign(name);
       
    67 
       
    68     return true;
       
    69 }
       
    70 
       
    71 size_t
       
    72 HelloTLV::serialize(u_char* bp, size_t len) const
       
    73 {
       
    74     // weed out the oddball
       
    75     if (sender_ == "") return 0;
       
    76     if (typecode_ != HELLO_TLV) return 0;
       
    77 
       
    78     // align to 32 bit boundary
       
    79     size_t hello_sz = FOUR_BYTE_ALIGN(sender_.length() + HelloTLVHeaderSize);
       
    80 
       
    81     // check our budget
       
    82     if (hello_sz > len) return 0;
       
    83 
       
    84     // write out to buffer
       
    85     HelloTLVHeader* hdr = (HelloTLVHeader*) bp;
       
    86     length_     = hello_sz;
       
    87     memset(hdr,0,length_);
       
    88     hdr->type   = typecode_;
       
    89     hdr->HF     = hf_;
       
    90     hdr->length = htons(length_);
       
    91     hdr->timer  = timer_;
       
    92     // mask out one-byte value (unnecessary?)
       
    93     hdr->name_length = sender_.length() & 0xff;
       
    94 
       
    95     size_t buflen = hello_sz;
       
    96 
       
    97     memcpy(&hdr->sender_name[0],sender_.c_str(),sender_.length());
       
    98 
       
    99     // walk the end of the buffer to zero out any slack
       
   100     while (buflen-- > HelloTLVHeaderSize + sender_.length())
       
   101         bp[buflen] = '\0';
       
   102 
       
   103     return hello_sz;
       
   104 }
       
   105 
       
   106 }; // namespace prophet