netlink/netlink-attribute.cc
changeset 66 2fe1f3e576c9
parent 63 e89dca438df6
equal deleted inserted replaced
65:227f6347e4e1 66:2fe1f3e576c9
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2008 Liu Jian
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License version 2 as
       
     7  * published by the Free Software Foundation;
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    17  *
       
    18  * Author: Liu Jian <liujatp@gmail.com>
       
    19  *         Hajime Tazaki <tazaki@sfc.wide.ad.jp>
       
    20  */
       
    21 
       
    22 #include "netlink-attribute.h"
       
    23 #include "netlink-message.h"
       
    24 #include "netlink-message-route.h"
       
    25 #include "ns3/address-utils.h"
       
    26 
       
    27 namespace ns3 {
       
    28 
       
    29 
       
    30 /***********************************************************************************
       
    31 * \ NetlinkAttributeValue
       
    32 ***********************************************************************************/
       
    33 NetlinkAttributeValue::NetlinkAttributeValue ()
       
    34 {
       
    35   m_type = UNSPEC;
       
    36   m_u32 = 0;
       
    37 }
       
    38 
       
    39 NetlinkAttributeValue:: NetlinkAttributeValue (NetlinkAttributeValueType type, uint8_t v)
       
    40 {
       
    41   NS_ASSERT (type == U8);
       
    42   m_type = U8;
       
    43   m_u8 = v;
       
    44 }
       
    45 NetlinkAttributeValue:: NetlinkAttributeValue (NetlinkAttributeValueType type, uint16_t v)
       
    46 {
       
    47   NS_ASSERT (type == U16);
       
    48   m_type = U16;
       
    49   m_u16 = v;
       
    50 }
       
    51 NetlinkAttributeValue:: NetlinkAttributeValue (NetlinkAttributeValueType type, uint32_t v)
       
    52 {
       
    53   NS_ASSERT (type == U32);
       
    54   m_type = U32;
       
    55   m_u32 = v;
       
    56 }
       
    57 NetlinkAttributeValue:: NetlinkAttributeValue (NetlinkAttributeValueType type, uint64_t v)
       
    58 {
       
    59   NS_ASSERT (type == U64);
       
    60   m_type = U64;
       
    61   m_u64 = v;
       
    62 }
       
    63 NetlinkAttributeValue:: NetlinkAttributeValue (NetlinkAttributeValueType type, std::string v)
       
    64 {
       
    65   NS_ASSERT (type == STRING);
       
    66   m_type = STRING;
       
    67   m_string = v;
       
    68 }
       
    69 NetlinkAttributeValue:: NetlinkAttributeValue (NetlinkAttributeValueType type, Address v)
       
    70 {
       
    71   NS_ASSERT (type == ADDRESS);
       
    72   m_type = ADDRESS;
       
    73   m_address = v;
       
    74 }
       
    75 
       
    76 void
       
    77 NetlinkAttributeValue::SetType (NetlinkAttributeValueType type)
       
    78 {
       
    79   m_type = type;
       
    80 }
       
    81 NetlinkAttributeValueType
       
    82 NetlinkAttributeValue::GetType (void) const
       
    83 {
       
    84   return m_type;
       
    85 }
       
    86 void
       
    87 NetlinkAttributeValue::SetAddress (Address value)
       
    88 {
       
    89   m_address = value;
       
    90 }
       
    91 void
       
    92 NetlinkAttributeValue::SetString (std::string value)
       
    93 {
       
    94   m_string = value;
       
    95 }
       
    96 void
       
    97 NetlinkAttributeValue::SetU64 (uint64_t value)
       
    98 {
       
    99   m_u64 = value;
       
   100 }
       
   101 void
       
   102 NetlinkAttributeValue::SetU32 (uint32_t value)
       
   103 {
       
   104   m_u32 = value;
       
   105 }
       
   106 void
       
   107 NetlinkAttributeValue::SetU16 (uint16_t value)
       
   108 {
       
   109   m_u16 = value;
       
   110 }
       
   111 void
       
   112 NetlinkAttributeValue::SetU8 (uint8_t value)
       
   113 {
       
   114   m_u8 = value;
       
   115 }
       
   116 Address
       
   117 NetlinkAttributeValue::GetAddress (void) const
       
   118 {
       
   119   return m_address;
       
   120 }
       
   121 std::string
       
   122 NetlinkAttributeValue::GetString (void) const
       
   123 {
       
   124   return m_string;
       
   125 }
       
   126 uint64_t
       
   127 NetlinkAttributeValue::GetU64 (void) const
       
   128 {
       
   129   return m_u64;
       
   130 }
       
   131 uint32_t
       
   132 NetlinkAttributeValue::GetU32 (void) const
       
   133 {
       
   134   return m_u32;
       
   135 }
       
   136 uint16_t
       
   137 NetlinkAttributeValue::GetU16 (void) const
       
   138 {
       
   139   return m_u16;
       
   140 }
       
   141 uint8_t
       
   142 NetlinkAttributeValue::GetU8 (void) const
       
   143 {
       
   144   return m_u8;
       
   145 }
       
   146 
       
   147 void
       
   148 NetlinkAttributeValue::Serialize (Buffer::Iterator& start) const
       
   149 {
       
   150   uint32_t len;
       
   151 
       
   152   if (m_type == U8)
       
   153     {
       
   154       start.WriteU8 (m_u8);
       
   155       len = 1;
       
   156     }
       
   157   else if(m_type == U16)
       
   158     {
       
   159       start.WriteU16 (m_u16);
       
   160       len = 2;
       
   161     }
       
   162   else if(m_type == U32)
       
   163     {
       
   164       start.WriteU32 (m_u32);
       
   165       len = 4;
       
   166     }
       
   167   else if(m_type == STRING)
       
   168     {
       
   169       start.Write ((const uint8_t *)m_string.c_str (), (uint32_t)m_string.size ()+1);
       
   170       len = (uint32_t)m_string.size () + 1;
       
   171     }
       
   172   else if(m_type == ADDRESS)
       
   173     {
       
   174       WriteTo (start, m_address);
       
   175       len = m_address.GetLength ();
       
   176     }
       
   177   else
       
   178     {
       
   179       len = 0;
       
   180     }
       
   181   //netlink align
       
   182   start.WriteU8 (0, NETLINK_MSG_ALIGN (len) - len);
       
   183 }
       
   184 
       
   185 uint32_t
       
   186 NetlinkAttributeValue::DeserializeWithType (Buffer::Iterator& start, 
       
   187                                             NetlinkAttributeValueType_e type, uint16_t remaining)
       
   188 {
       
   189   uint32_t len =0;
       
   190   m_type = type;  
       
   191 
       
   192   if (m_type == U8)
       
   193     {
       
   194       m_u8 = start.ReadU8 ();
       
   195       len = 1;
       
   196     }
       
   197   else if (m_type == U16)
       
   198     {
       
   199       m_u16 = start.ReadU16 ();
       
   200       len = 2;
       
   201     }
       
   202   else if (m_type == U32)
       
   203     {
       
   204       m_u32 = start.ReadU32 ();
       
   205       len = 4;
       
   206     }
       
   207   else if (m_type == U64)
       
   208     {
       
   209       m_u64 = start.ReadU64 ();
       
   210     }  
       
   211   else if (m_type == STRING)
       
   212     {
       
   213       char buf[512];
       
   214       uint32_t i = 0;
       
   215       do 
       
   216       {
       
   217         buf[i] = start.ReadU8 ();
       
   218       } while (buf[i++]);
       
   219       
       
   220       m_string = std::string (buf);
       
   221       len = (uint32_t)m_string.size () + 1;
       
   222     }
       
   223   else if (m_type == ADDRESS)
       
   224     {
       
   225       ReadFrom (start, m_address, remaining);
       
   226       len = m_address.GetLength ();
       
   227     }
       
   228   else
       
   229     {
       
   230       len = 0;
       
   231     }
       
   232 
       
   233   //netlink align
       
   234   uint8_t buf[4];
       
   235   start.Read (buf, NETLINK_MSG_ALIGN (len) - len);
       
   236   
       
   237   return NETLINK_MSG_ALIGN (len);
       
   238 }
       
   239 
       
   240 uint32_t
       
   241 NetlinkAttributeValue::GetSerializedSize ()const
       
   242 {
       
   243   return NETLINK_MSG_ALIGN (GetSize ());
       
   244 }
       
   245 
       
   246 uint32_t
       
   247 NetlinkAttributeValue::GetSize () const
       
   248 {
       
   249   uint32_t len;
       
   250 
       
   251   if (m_type == U8)
       
   252     {
       
   253       len = 1;
       
   254     }
       
   255   else if(m_type == U16)
       
   256     {
       
   257       len =  2;
       
   258     }
       
   259   else if(m_type == U32)
       
   260     {
       
   261       len =  4;
       
   262     }
       
   263   else if (m_type == STRING)
       
   264     {
       
   265       len =  uint32_t (m_string.size () + 1);
       
   266     }
       
   267   else if (m_type == ADDRESS)
       
   268     {
       
   269       len =  m_address.GetLength ();
       
   270     }
       
   271   else
       
   272     {
       
   273       len = 0;
       
   274     }
       
   275 
       
   276     return len;
       
   277 }
       
   278 
       
   279 void
       
   280 NetlinkAttributeValue::Print (std::ostream &os) const
       
   281 {
       
   282   os << "NetlinkAttributeValue (type= " << m_type <<", v= ";
       
   283   if (m_type == U8)
       
   284     {
       
   285       os << m_u8;
       
   286     }
       
   287   else if (m_type == U16)
       
   288     {
       
   289       os << m_u16;
       
   290     }
       
   291   else if (m_type == U32)
       
   292     {
       
   293       os << m_u32;
       
   294     }
       
   295   else if (m_type == U64)
       
   296     {
       
   297       os << m_u64;
       
   298     }  
       
   299   else if (m_type == STRING)
       
   300     {
       
   301       os << m_string;
       
   302     }
       
   303   else if (m_type == ADDRESS)
       
   304     {
       
   305       os << "address(" << m_address <<")";
       
   306     }
       
   307   else
       
   308     {
       
   309       os << "NULL";
       
   310     }
       
   311   os <<")";
       
   312 }
       
   313 
       
   314 
       
   315 /***********************************************************************************
       
   316 * \ NetlinkAttribute
       
   317 ***********************************************************************************/
       
   318 
       
   319 NetlinkAttribute::NetlinkAttribute ()
       
   320   : m_len(4),
       
   321     m_type (0)
       
   322 {
       
   323 }
       
   324 
       
   325 NetlinkAttribute::NetlinkAttribute (uint16_t type, NetlinkAttributeValueType payloadtype,  uint8_t payload)
       
   326 {
       
   327   m_payload = NetlinkAttributeValue (payloadtype, payload);
       
   328   m_len = NETLINK_MSG_ATTR_SIZE + m_payload.GetSize ();
       
   329   m_type = type;
       
   330 }
       
   331 
       
   332 NetlinkAttribute::NetlinkAttribute (uint16_t type, NetlinkAttributeValueType payloadtype,  uint16_t payload)
       
   333 {
       
   334   m_payload = NetlinkAttributeValue (payloadtype, payload);
       
   335   m_len = NETLINK_MSG_ATTR_SIZE + m_payload.GetSize ();
       
   336   m_type = type;
       
   337 }
       
   338 NetlinkAttribute::NetlinkAttribute (uint16_t type, NetlinkAttributeValueType payloadtype,  uint32_t payload)
       
   339 {
       
   340   m_payload = NetlinkAttributeValue (payloadtype, payload);
       
   341   m_len = NETLINK_MSG_ATTR_SIZE + m_payload.GetSize ();
       
   342   m_type = type;;
       
   343 }
       
   344 NetlinkAttribute::NetlinkAttribute (uint16_t type, NetlinkAttributeValueType payloadtype,  uint64_t payload)
       
   345 {
       
   346   m_payload = NetlinkAttributeValue (payloadtype, payload);
       
   347   m_len = NETLINK_MSG_ATTR_SIZE + m_payload.GetSize ();
       
   348   m_type = type;
       
   349 }
       
   350 NetlinkAttribute::NetlinkAttribute (uint16_t type, NetlinkAttributeValueType payloadtype,  std::string payload)
       
   351 {
       
   352   m_payload = NetlinkAttributeValue (payloadtype, payload);
       
   353   m_len = NETLINK_MSG_ATTR_SIZE + m_payload.GetSize ();
       
   354   m_type = type;
       
   355 }
       
   356 NetlinkAttribute::NetlinkAttribute (uint16_t type, NetlinkAttributeValueType payloadtype,  Address payload)
       
   357 {
       
   358   m_payload = NetlinkAttributeValue (payloadtype, payload);
       
   359   m_len = NETLINK_MSG_ATTR_SIZE + m_payload.GetSize ();
       
   360   m_type = type;
       
   361 }
       
   362 
       
   363 void
       
   364 NetlinkAttribute::SetAttrLen (uint16_t v)
       
   365 {
       
   366   m_len = v;
       
   367 }
       
   368 void 
       
   369 NetlinkAttribute::SetAttrType (uint16_t v)
       
   370 {
       
   371   m_type = v;
       
   372 }
       
   373 void
       
   374 NetlinkAttribute::SetAttrPayload (NetlinkAttributeValue v)
       
   375 {
       
   376   m_payload = v;
       
   377 }
       
   378 uint16_t
       
   379 NetlinkAttribute::GetAttrLen () const
       
   380 {
       
   381   return m_len;
       
   382 }
       
   383 uint16_t
       
   384 NetlinkAttribute::GetAttrType () const
       
   385 {
       
   386   return m_type;
       
   387 }
       
   388 NetlinkAttributeValue
       
   389 NetlinkAttribute::GetAttrPayload() const
       
   390 {
       
   391   return m_payload;
       
   392 }
       
   393 
       
   394 void 
       
   395 NetlinkAttribute::Print (std::ostream &os) const
       
   396 {  
       
   397   os << "NetlinkAttribute "
       
   398      << "len: " << m_len << " "
       
   399      << "type: " << m_type<<" "
       
   400      << "payload:[";
       
   401   m_payload.Print(os);
       
   402   os<<"]";
       
   403 }
       
   404 
       
   405 uint32_t 
       
   406 NetlinkAttribute::GetSerializedSize (void) const
       
   407 {
       
   408   /* this is the size of an nlattr payload. */
       
   409   return NETLINK_MSG_ATTR_SIZE + m_payload.GetSerializedSize ();
       
   410 }
       
   411 
       
   412 void
       
   413 NetlinkAttribute::Serialize (Buffer::Iterator& start) const
       
   414 {
       
   415   start.WriteU16 (m_len);
       
   416   start.WriteU16 (m_type);
       
   417   m_payload.Serialize (start);
       
   418 }
       
   419 
       
   420 uint32_t
       
   421 NetlinkAttribute::Deserialize (Buffer::Iterator& start, NetlinkAttributeValueType vtypes[])
       
   422 {
       
   423   NetlinkAttributeValueType type;
       
   424 
       
   425   m_len = start.ReadU16 ();
       
   426   m_type = start.ReadU16 ();
       
   427   type = vtypes[m_type];
       
   428   m_payload.DeserializeWithType (start, type, m_len - 4);
       
   429 
       
   430   return GetSerializedSize ();
       
   431 }
       
   432 
       
   433 }; // namespace ns3