netlink/netlink-message-route.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 05 May 2011 09:28:21 +0200
changeset 66 2fe1f3e576c9
parent 63 model/netlink-message-route.cc@e89dca438df6
permissions -rw-r--r--
make it somewhat build sanely
     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-message-route.h"
    23 #include "netlink-message.h"
    24 
    25 namespace ns3 {
    26 
    27 /***********************************************************************************
    28 * \ NetlinkPayload
    29 ***********************************************************************************/
    30 TypeId 
    31 NetlinkPayload::GetTypeId (void)
    32 {
    33   static TypeId tid = TypeId ("ns3::NetlinkPayload")
    34     .SetParent<ObjectBase> ()
    35     ;
    36   return tid;
    37 }
    38 
    39 
    40 /***********************************************************************************
    41 * \ GeneralMessage
    42 ***********************************************************************************/
    43 
    44 NS_OBJECT_ENSURE_REGISTERED (GeneralMessage);
    45 NS_OBJECT_ENSURE_REGISTERED (InterfaceInfoMessage);
    46 NS_OBJECT_ENSURE_REGISTERED (InterfaceAddressMessage);
    47 NS_OBJECT_ENSURE_REGISTERED (RouteMessage);
    48 
    49 GeneralMessage::GeneralMessage ()
    50   : m_family(0)
    51 {}
    52 GeneralMessage::~GeneralMessage ()
    53 {}
    54 
    55 void
    56 GeneralMessage::SetFamily (uint8_t v)
    57 {
    58   m_family = v;
    59 }
    60 uint8_t
    61 GeneralMessage::GetFamily (void) const
    62 {
    63   return m_family;
    64 }
    65 
    66 TypeId 
    67 GeneralMessage::GetTypeId (void)
    68 {
    69   static TypeId tid = TypeId ("ns3::GeneralMessage")
    70     .SetParent<NetlinkPayload> ()
    71     .AddConstructor<GeneralMessage> ()
    72     ;
    73   return tid;
    74 }
    75 
    76 TypeId 
    77 GeneralMessage::GetInstanceTypeId (void) const
    78 {
    79   return GetTypeId ();
    80 }
    81 void 
    82 GeneralMessage::Print (std::ostream &os) const
    83 {
    84   os << " ----GeneralMessage ("
    85      << "family: " << (uint32_t)m_family << ")"; 
    86 }
    87 
    88 uint32_t 
    89 GeneralMessage::GetSerializedSize (void) const
    90 {
    91   /* this is the size of an nlmsghdr payload. */
    92   return NETLINK_MSG_ALIGN (NETLINK_GENMSG_SIZE);
    93 }
    94 
    95 
    96 void
    97 GeneralMessage::Serialize (Buffer::Iterator& start) const
    98 {
    99   start.WriteU8 (m_family);
   100   start.WriteU8 (0, 3);
   101 }
   102 
   103 uint32_t
   104 GeneralMessage::Deserialize (Buffer::Iterator& start)
   105 {
   106   uint8_t buf[3];
   107   m_family = start.ReadU8 ();
   108   start.Read (buf, 3);
   109   return GetSerializedSize ();
   110 }
   111 uint32_t
   112 GeneralMessage::Deserialize (Buffer::Iterator& start, uint32_t len)
   113 {
   114   uint8_t buf[3];
   115   m_family = start.ReadU8 ();
   116   start.Read (buf, 3);
   117   return GetSerializedSize ();
   118 }
   119 
   120 
   121 uint32_t
   122 GeneralMessage::GetNNetlinkAttribute (void)const
   123 {
   124   return m_attributes.size ();
   125 }
   126 NetlinkAttribute
   127 GeneralMessage::GetNetlinkAttribute (uint32_t index)const
   128 {
   129   NS_ASSERT(index < GetNNetlinkAttribute ());
   130   return m_attributes[index];
   131 }
   132 
   133 uint32_t
   134 GeneralMessage::GetAttributeSerializedSize (void) const
   135 {
   136   uint32_t size = 0;
   137 
   138   for (uint32_t i = 0; i < m_attributes.size (); i ++)
   139     {
   140       size += m_attributes[i].GetSerializedSize ();
   141     }
   142   return size;
   143 }
   144 bool
   145 GeneralMessage::GetAttributeByType (NetlinkAttribute& attr, uint16_t type)
   146 {
   147   for (uint32_t i = 0; i < m_attributes.size (); i ++)
   148     {
   149       if (type == m_attributes[i].GetAttrType ())
   150         {
   151           attr = m_attributes[i];
   152           return true;
   153         }
   154     }
   155   return false;  
   156 }
   157 void
   158 GeneralMessage::AppendAttribute (NetlinkAttribute v)
   159 {
   160   m_attributes.push_back (v);
   161 }
   162 
   163 void
   164 GeneralMessage::SerializeAttribute (Buffer::Iterator& start) const
   165 {
   166   for (uint32_t i = 0; i < m_attributes.size (); i ++)
   167     {
   168       m_attributes[i].Serialize (start);
   169     }
   170 }
   171 
   172 void
   173 GeneralMessage::PrintAttribute (std::ostream &os) const
   174 {
   175   for (uint32_t i = 0; i < m_attributes.size (); i ++)
   176     {
   177       os << " ----Attribute (" << i << "):";
   178       m_attributes[i].Print(os);
   179     }
   180 }
   181 
   182 /***********************************************************************************
   183 * \ InterfaceInfoMessage
   184 ***********************************************************************************/
   185 InterfaceInfoMessage::InterfaceInfoMessage ()
   186   : m_reserved (0),
   187     m_deviceType (0),
   188     m_interfaceIndex(0),
   189     m_deviceFlags (0),
   190     m_changeMask (0)
   191 {
   192   memset ((void*)m_attributeTypes, 0, sizeof (m_attributeTypes));
   193   m_attributeTypes[IFL_A_UNSPEC] = UNSPEC;
   194   m_attributeTypes[IFL_A_ADDRESS] = ADDRESS;
   195   m_attributeTypes[IFL_A_BROADCAST] = ADDRESS;
   196   m_attributeTypes[IFL_A_IFNAME] = STRING;
   197   m_attributeTypes[IFL_A_MTU] = U32;
   198   m_attributeTypes[IFL_A_LINK] = U32;
   199   m_attributeTypes[IFL_A_QDISC] = U8;
   200   m_attributeTypes[IFL_A_STATS] = UNSPEC;
   201   m_attributeTypes[IFL_A_COST] = UNSPEC;
   202 }
   203 InterfaceInfoMessage::~InterfaceInfoMessage ()
   204 {}
   205 void
   206 InterfaceInfoMessage::SetDeviceType (uint16_t type)
   207 {
   208   m_deviceType = type;
   209 }
   210 void
   211 InterfaceInfoMessage::SetInterfaceIndex (int32_t index)
   212 {
   213   m_interfaceIndex = index;
   214 }
   215 void
   216 InterfaceInfoMessage::SetDeviceFlags (uint32_t flags)
   217 {
   218   m_deviceFlags = flags;
   219 }
   220 void
   221 InterfaceInfoMessage::SetChangeMask (uint32_t mask)
   222 {
   223   m_changeMask = mask;
   224 }
   225 uint16_t
   226 InterfaceInfoMessage::GetDeviceType (void) const
   227 {
   228   return m_deviceType;
   229 }
   230 int32_t
   231 InterfaceInfoMessage::GetInterfaceIndex (void) const
   232 {
   233   return m_interfaceIndex;
   234 }
   235 uint32_t
   236 InterfaceInfoMessage::GetDeviceFlags (void) const
   237 {
   238   return m_deviceFlags;
   239 }
   240 uint32_t
   241 InterfaceInfoMessage::GetChangeMask (void) const
   242 {
   243   return m_changeMask;
   244 }
   245 TypeId 
   246 InterfaceInfoMessage::GetTypeId (void)
   247 {
   248   static TypeId tid = TypeId ("ns3::InterfaceInfoMessage")
   249     .SetParent<GeneralMessage> ()
   250     .AddConstructor<InterfaceInfoMessage> ()
   251     ;
   252   return tid;
   253 }
   254 TypeId 
   255 InterfaceInfoMessage::GetInstanceTypeId (void) const
   256 {
   257   return GetTypeId ();
   258 }
   259 void 
   260 InterfaceInfoMessage::Print (std::ostream &os) const
   261 {  
   262   os << " ----InterfaceInfoMessage ("
   263      << "deviceType: " << m_deviceType << " "
   264      << "interfaceIndex: " << m_interfaceIndex << " "
   265      << "deviceFlags: " << m_deviceFlags << " "
   266      << "changeMask: " << m_changeMask << ")" ;
   267   PrintAttribute (os);
   268 }
   269 uint32_t 
   270 InterfaceInfoMessage::GetSerializedSize (void) const
   271 {
   272   return NETLINK_INTERFACE_SIZE + GetAttributeSerializedSize ();
   273 }
   274 
   275 void
   276 InterfaceInfoMessage::Serialize (Buffer::Iterator& start) const
   277 {
   278   start.WriteU8 (m_family);
   279   start.WriteU8 (m_reserved);
   280   start.WriteU16 (m_deviceType);
   281   start.WriteU32 (m_interfaceIndex);
   282   start.WriteU32 (m_deviceFlags);
   283   start.WriteU32 (m_changeMask);
   284 
   285   SerializeAttribute (start);
   286 }
   287 uint32_t
   288 InterfaceInfoMessage::Deserialize (Buffer::Iterator& start, uint32_t len)
   289 {
   290   m_family = start.ReadU8 ();
   291   m_reserved = start.ReadU8 ();
   292   m_deviceType = start.ReadU16 ();
   293   m_interfaceIndex = start.ReadU32 ();
   294   m_deviceFlags = start.ReadU32 ();
   295   m_changeMask = start.ReadU32 ();
   296 
   297   len -= NETLINK_INTERFACE_SIZE;
   298 
   299   while (len)
   300     {
   301       NetlinkAttribute attr;
   302 
   303       len -= attr.Deserialize (start, m_attributeTypes);
   304       m_attributes.push_back (attr);
   305     }
   306 
   307   return GetSerializedSize ();
   308 }
   309 
   310 
   311 
   312 /***********************************************************************************
   313 * \InterfaceAddressMessage
   314 ***********************************************************************************/
   315 InterfaceAddressMessage::InterfaceAddressMessage ()
   316   : m_length (0),
   317     m_flags (0),
   318     m_scope (0),
   319     m_index(0)
   320 {
   321   memset ((void*)m_attributeTypes, 0, sizeof (m_attributeTypes));
   322   m_attributeTypes[IF_A_UNSPEC] = UNSPEC;
   323   m_attributeTypes[IF_A_ADDRESS] = ADDRESS;
   324   m_attributeTypes[IF_A_LOCAL] = ADDRESS;
   325   m_attributeTypes[IF_A_LABEL] = STRING;
   326   m_attributeTypes[IF_A_BROADCAST] = ADDRESS;
   327   m_attributeTypes[IF_A_ANYCAST] = ADDRESS;
   328   m_attributeTypes[IF_A_CACHEINFO] = UNSPEC;
   329   m_attributeTypes[IF_A_MULTICAST] = ADDRESS;
   330 }
   331 InterfaceAddressMessage::~InterfaceAddressMessage ()
   332 {
   333 }
   334 void
   335 InterfaceAddressMessage::SetFamily (uint8_t family)
   336 {
   337   m_family = family;
   338 }
   339 void
   340 InterfaceAddressMessage::SetLength (uint8_t length)
   341 {
   342   m_length = length;
   343 }
   344 void
   345 InterfaceAddressMessage::SetFlags (uint8_t flags)
   346 {
   347   m_flags = flags;
   348 }
   349 void
   350 InterfaceAddressMessage::SetScope (uint8_t scope)
   351 {
   352   m_scope = scope;
   353 }
   354 void
   355 InterfaceAddressMessage::SetInterfaceIndex (int32_t index)
   356 {
   357   m_index = index;
   358 }
   359 
   360 uint8_t
   361 InterfaceAddressMessage::GetFamily (void) const
   362 {
   363   return m_family;
   364 }
   365 uint8_t
   366 InterfaceAddressMessage::GetLength (void) const
   367 {
   368   return m_length;
   369 }
   370 uint8_t
   371 InterfaceAddressMessage::GetFlags (void) const
   372 {
   373   return m_flags;
   374 }
   375 uint8_t
   376 InterfaceAddressMessage::GetScope (void) const
   377 {
   378   return m_scope;
   379 }
   380 int32_t
   381 InterfaceAddressMessage::GetInterfaceIndex (void) const
   382 {
   383   return m_index;
   384 }
   385 
   386 TypeId 
   387 InterfaceAddressMessage::GetTypeId (void)
   388 {
   389   static TypeId tid = TypeId ("ns3::InterfaceAddressMessage")
   390     .SetParent<GeneralMessage> ()
   391     .AddConstructor<InterfaceAddressMessage> ()
   392     ;
   393   return tid;
   394 }
   395 TypeId 
   396 InterfaceAddressMessage::GetInstanceTypeId (void) const
   397 {
   398   return GetTypeId ();
   399 }
   400 void 
   401 InterfaceAddressMessage::Print (std::ostream &os) const
   402 {  
   403   os << " ----InterfaceAddressMessage ("
   404      << "family: " << (uint32_t)m_family << " "
   405      << "length: " << (uint32_t)m_length << " "
   406      << "flags: " << (uint32_t)m_flags << " "
   407      << "scope: " << (uint32_t)m_scope << " "
   408      << "index: " << m_index << ")";
   409   PrintAttribute (os);
   410 }
   411 uint32_t 
   412 InterfaceAddressMessage::GetSerializedSize (void) const
   413 {
   414   return NETLINK_ADDRESS_SIZE + GetAttributeSerializedSize ();
   415 }
   416 
   417 void
   418 InterfaceAddressMessage::Serialize (Buffer::Iterator& start) const
   419 {
   420   start.WriteU8 (m_family);
   421   start.WriteU8 (m_length);
   422   start.WriteU8 (m_flags);
   423   start.WriteU8 (m_scope);
   424   start.WriteU32 (m_index);
   425 
   426   SerializeAttribute(start);
   427 }
   428 
   429 uint32_t
   430 InterfaceAddressMessage::Deserialize (Buffer::Iterator& start, uint32_t len)
   431 {
   432   m_family = start.ReadU8 ();
   433   m_length = start.ReadU8 ();
   434   m_flags = start.ReadU8 ();
   435   m_scope = start.ReadU8 ();
   436   m_index = start.ReadU32 ();
   437 
   438   len -= NETLINK_ADDRESS_SIZE;
   439 
   440   while (len)
   441     {
   442       NetlinkAttribute attr;
   443 
   444       len -= attr.Deserialize (start, m_attributeTypes);
   445       m_attributes.push_back (attr);
   446     }
   447 
   448   return GetSerializedSize ();
   449 }
   450 
   451 
   452 
   453 /***********************************************************************************
   454 * \ RouteMessage
   455 ***********************************************************************************/
   456 RouteMessage::RouteMessage ()
   457   : m_dstLen (0),
   458     m_srcLen (0),
   459     m_tos (0),
   460     m_tableId (0),
   461     m_protocol(0),
   462     m_scope (0),
   463     m_type (0),
   464     m_flags (0)
   465 {
   466   memset ((void*)m_attributeTypes, 0, sizeof (m_attributeTypes));
   467   m_attributeTypes[RT_A_UNSPEC] = UNSPEC;
   468   m_attributeTypes[RT_A_DST] = ADDRESS;
   469   m_attributeTypes[RT_A_SRC] = ADDRESS;
   470   m_attributeTypes[RT_A_IIF] = U32;
   471   m_attributeTypes[RT_A_OIF] = U32;
   472   m_attributeTypes[RT_A_GATEWAY] = ADDRESS;
   473   m_attributeTypes[RT_A_PRIORITY] = U8;
   474   m_attributeTypes[RT_A_PREFSRC] = ADDRESS;
   475   m_attributeTypes[RT_A_METRICS] = UNSPEC;
   476   //others default value UNSPEC
   477 }
   478 RouteMessage::~RouteMessage ()
   479 {}
   480 
   481 void
   482 RouteMessage::SetFamily (uint8_t v)
   483 {
   484   m_family = v;
   485 }
   486 void
   487 RouteMessage::SetDstLength (uint8_t v)
   488 {
   489   m_dstLen = v;
   490 }
   491 void
   492 RouteMessage::SetSrcLength (uint8_t v)
   493 {
   494   m_srcLen = v;
   495 }
   496 void
   497 RouteMessage::SetTos (uint8_t v)
   498 {
   499   m_tos = v;
   500 }
   501 void
   502 RouteMessage::SetTableId (uint8_t v)
   503 {
   504   m_tableId = v;
   505 }
   506 void
   507 RouteMessage::SetProtocol (uint8_t v)
   508 {
   509   m_protocol = v;
   510 }
   511 void
   512 RouteMessage::SetScope (uint8_t v)
   513 {
   514   m_scope = v;
   515 }
   516 void
   517 RouteMessage::SetType (uint8_t v)
   518 {
   519   m_type = v;
   520 }
   521 void
   522 RouteMessage::SetFlags (uint32_t v)
   523 {
   524   m_flags = v;
   525 }
   526 uint8_t
   527 RouteMessage::GetFamily (void) const
   528 {
   529   return m_family;
   530 }
   531 uint8_t
   532 RouteMessage::GetDstLength (void) const
   533 {
   534   return m_dstLen;
   535 }
   536 uint8_t
   537 RouteMessage::GetSrcLength (void) const
   538 {
   539   return m_srcLen;
   540 }
   541 uint8_t
   542 RouteMessage::GetTos (void) const
   543 {
   544   return m_tos;
   545 }
   546 uint8_t
   547 RouteMessage::GetTableId (void) const
   548 {
   549   return m_tableId;
   550 }
   551 uint8_t
   552 RouteMessage::GetProtocol (void) const
   553 {
   554   return m_protocol;
   555 }
   556 uint8_t
   557 RouteMessage::GetType (void) const
   558 {
   559   return m_type;
   560 }
   561 uint8_t
   562 RouteMessage::GetScope (void) const
   563 {
   564   return m_scope;
   565 }
   566 uint32_t
   567 RouteMessage::GetFlags (void) const
   568 {
   569   return m_flags;
   570 }
   571 
   572 TypeId 
   573 RouteMessage::GetTypeId (void)
   574 {
   575   static TypeId tid = TypeId ("ns3::RouteMessage")
   576     .SetParent<GeneralMessage> ()
   577     .AddConstructor<RouteMessage> ()
   578     ;
   579   return tid;
   580 }
   581 TypeId 
   582 RouteMessage::GetInstanceTypeId (void) const
   583 {
   584   return GetTypeId ();
   585 }
   586 void 
   587 RouteMessage::Print (std::ostream &os) const
   588 {  
   589   os << " ----RouteMessage ("
   590      << "family: " << (uint32_t)m_family << " "
   591      << "dstLen: " << (uint32_t)m_dstLen << " "
   592      << "srcLen: " << (uint32_t)m_srcLen << " "
   593      << "tos: " << (uint32_t)m_tos << " "
   594      << "tableId: " << (uint32_t)m_tableId << " "
   595      << "protocol: " << (uint32_t)m_protocol << " "
   596      << "scope: " << (uint32_t)m_scope << " "
   597      << "type: " << (uint32_t)m_type << " "
   598      << "flags: " << m_flags<< ")" ;
   599   PrintAttribute (os);
   600 }
   601 uint32_t 
   602 RouteMessage::GetSerializedSize (void) const
   603 {
   604   return NETLINK_ROUTE_SIZE + GetAttributeSerializedSize ();
   605 }
   606 
   607 void
   608 RouteMessage::Serialize (Buffer::Iterator& start) const
   609 {
   610   start.WriteU8 (m_family);
   611   start.WriteU8 (m_dstLen);
   612   start.WriteU8 (m_srcLen);
   613   start.WriteU8 (m_tos);
   614   start.WriteU8 (m_tableId);
   615   start.WriteU8 (m_protocol);
   616   start.WriteU8 (m_scope);
   617   start.WriteU8 (m_type);
   618   start.WriteU32 (m_flags);
   619 
   620   SerializeAttribute (start);
   621 }
   622 uint32_t
   623 RouteMessage::Deserialize (Buffer::Iterator& start, uint32_t len)
   624 {
   625   m_family = start.ReadU8 ();
   626   m_dstLen = start.ReadU8 ();
   627   m_srcLen = start.ReadU8 ();
   628   m_tos = start.ReadU8 ();
   629   m_tableId = start.ReadU8 ();
   630   m_protocol = start.ReadU8 ();
   631   m_scope = start.ReadU8 ();
   632   m_type = start.ReadU8 ();
   633   m_flags = start.ReadU32 ();
   634 
   635   len -= NETLINK_ROUTE_SIZE;
   636 
   637   while (len)
   638     {
   639       NetlinkAttribute attr;
   640 
   641       len -= attr.Deserialize (start, m_attributeTypes);
   642       m_attributes.push_back (attr);
   643     }
   644 
   645   return GetSerializedSize ();
   646 }
   647 }; // namespace ns3