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