Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "bgp/tunnel_encap/tunnel_encap.h" 6 : 7 : 8 : #include <algorithm> 9 : #include <sstream> 10 : 11 : 12 : using std::copy; 13 : using std::fill; 14 : using std::string; 15 : 16 61176 : TunnelEncap::TunnelEncap(string encap) { 17 61176 : fill(data_.begin(), data_.end(), 0); 18 61176 : TunnelEncapType::Encap id = TunnelEncapType::TunnelEncapFromString(encap); 19 61175 : if (id == TunnelEncapType::UNSPEC) return; 20 61174 : data_[0] = 0x03; 21 61173 : data_[1] = 0x0C; 22 : // Reserved 23 61173 : put_value(&data_[2], 4, 0); 24 61173 : put_value(&data_[6], 2, id); 25 : } 26 : 27 4843 : TunnelEncap::TunnelEncap(TunnelEncapType::Encap tunnel_encap) { 28 4843 : fill(data_.begin(), data_.end(), 0); 29 4844 : data_[0] = 0x03; 30 4844 : data_[1] = 0x0C; 31 : // Reserved 32 4844 : put_value(&data_[2], 4, 0); 33 4844 : put_value(&data_[6], 2, tunnel_encap); 34 4844 : } 35 : 36 652148 : TunnelEncap::TunnelEncap(const bytes_type &data) { 37 652148 : copy(data.begin(), data.end(), data_.begin()); 38 652190 : } 39 : 40 1160671 : TunnelEncapType::Encap TunnelEncap::tunnel_encap() const { 41 1160671 : if (data_[0] != 0x03 || data_[1] != 0x0c) 42 3 : return TunnelEncapType::UNSPEC; 43 1160652 : uint16_t value = get_value(&data_[6], 2); 44 1160705 : if (TunnelEncapType::TunnelEncapIsValid(value)) { 45 1160472 : return static_cast<TunnelEncapType::Encap>(value); 46 : } else { 47 3 : return TunnelEncapType::UNSPEC; 48 : } 49 : } 50 : 51 4262 : string TunnelEncap::ToString() { 52 4262 : string str("encapsulation:"); 53 4262 : str += TunnelEncapType::TunnelEncapToString(tunnel_encap()); 54 4262 : return str; 55 0 : } 56 : 57 476991 : string TunnelEncap::ToXmppString() { 58 476991 : return TunnelEncapType::TunnelEncapToXmppString(tunnel_encap()); 59 : }