netlink/netlink-message.cc
changeset 66 2fe1f3e576c9
parent 63 e89dca438df6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netlink/netlink-message.cc	Thu May 05 09:28:21 2011 +0200
     1.3 @@ -0,0 +1,672 @@
     1.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     1.5 +/*
     1.6 + * Copyright (c) 2008 Liu Jian
     1.7 + *
     1.8 + * This program is free software; you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License version 2 as
    1.10 + * published by the Free Software Foundation;
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program; if not, write to the Free Software
    1.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.20 + *
    1.21 + * Author: Liu Jian <liujatp@gmail.com>
    1.22 + *         Hajime Tazaki <tazaki@sfc.wide.ad.jp>
    1.23 + */
    1.24 +
    1.25 +#include "netlink-message.h"
    1.26 +#include "ns3/address-utils.h"
    1.27 +#include "ns3/log.h"
    1.28 +
    1.29 +NS_LOG_COMPONENT_DEFINE ("NetlinkMessage");
    1.30 +
    1.31 +namespace ns3 {
    1.32 +
    1.33 +/***********************************************************************************
    1.34 +* \ NetlinkMessageHeader
    1.35 +***********************************************************************************/
    1.36 +NS_OBJECT_ENSURE_REGISTERED (NetlinkMessageHeader);
    1.37 +NS_OBJECT_ENSURE_REGISTERED (NetlinkMessage);
    1.38 +
    1.39 +NetlinkMessageHeader::NetlinkMessageHeader ()
    1.40 +  : m_nlmsgLen (16),
    1.41 +    m_nlmsgType (0),
    1.42 +    m_nlmsgFlags (0),
    1.43 +    m_nlmsgSeq (0),
    1.44 +    m_nlmsgPid (0)
    1.45 +{}
    1.46 +
    1.47 +NetlinkMessageHeader::NetlinkMessageHeader (uint16_t type, uint16_t flags, uint32_t seq, uint32_t pid)
    1.48 +  : m_nlmsgLen (16),
    1.49 +    m_nlmsgType (type),
    1.50 +    m_nlmsgFlags (flags),
    1.51 +    m_nlmsgSeq (seq),
    1.52 +    m_nlmsgPid (pid)
    1.53 +{}
    1.54 +
    1.55 +void
    1.56 +NetlinkMessageHeader::SetMsgLen (uint32_t v)
    1.57 +{
    1.58 +  m_nlmsgLen = v;
    1.59 +}
    1.60 +void
    1.61 +NetlinkMessageHeader::SetMsgFlags (uint16_t v)
    1.62 +{
    1.63 +  m_nlmsgFlags = v;
    1.64 +}
    1.65 +void
    1.66 +NetlinkMessageHeader::SetMsgType (uint16_t v)
    1.67 +{
    1.68 +  m_nlmsgType = v;
    1.69 +}
    1.70 +void
    1.71 +NetlinkMessageHeader::SetMsgSeq (uint32_t v)
    1.72 +{
    1.73 +  m_nlmsgSeq = v;
    1.74 +}
    1.75 +void
    1.76 +NetlinkMessageHeader::SetMsgPid (uint32_t v)
    1.77 +{
    1.78 +  m_nlmsgPid = v;
    1.79 +}
    1.80 +uint16_t
    1.81 +NetlinkMessageHeader::GetMsgFlags (void) const
    1.82 +{
    1.83 +  return m_nlmsgFlags;
    1.84 +}
    1.85 +uint32_t 
    1.86 +NetlinkMessageHeader::GetMsgLen (void) const
    1.87 +{
    1.88 +  return m_nlmsgLen;
    1.89 +}
    1.90 +uint16_t 
    1.91 +NetlinkMessageHeader::GetMsgType (void) const
    1.92 +{
    1.93 +  return m_nlmsgType;
    1.94 +}
    1.95 +uint32_t
    1.96 +NetlinkMessageHeader::GetMsgSeq (void) const
    1.97 +{
    1.98 +  return m_nlmsgSeq;
    1.99 +}
   1.100 +uint32_t 
   1.101 +NetlinkMessageHeader::GetMsgPid (void) const
   1.102 +{
   1.103 +  return m_nlmsgPid;
   1.104 +}
   1.105 +uint32_t
   1.106 +NetlinkMessageHeader::GetHeaderSize ()
   1.107 +{
   1.108 +  return NETLINK_MSG_HEADER_SIZE;
   1.109 +}
   1.110 +uint32_t
   1.111 +NetlinkMessageHeader::GetPayloadSize (void) const
   1.112 +{
   1.113 +  return NETLINK_MSG_ALIGN (m_nlmsgLen - NETLINK_MSG_HEADER_SIZE);
   1.114 +}
   1.115 +
   1.116 +TypeId 
   1.117 +NetlinkMessageHeader::GetTypeId (void)
   1.118 +{
   1.119 +  static TypeId tid = TypeId ("ns3::NetlinkMessageHeader")
   1.120 +    .SetParent<Header> ()
   1.121 +    .AddConstructor<NetlinkMessageHeader> ()
   1.122 +    ;
   1.123 +  return tid;
   1.124 +}
   1.125 +TypeId 
   1.126 +NetlinkMessageHeader::GetInstanceTypeId (void) const
   1.127 +{
   1.128 +  return GetTypeId ();
   1.129 +}
   1.130 +void 
   1.131 +NetlinkMessageHeader::Print (std::ostream &os) const
   1.132 +{
   1.133 +  os << "NetlinkMessageHeader "
   1.134 +     << "len: " << m_nlmsgLen << " "
   1.135 +     << "flags: " << m_nlmsgFlags << " "
   1.136 +     << "type: " << m_nlmsgType << " "
   1.137 +     << "seq: " << m_nlmsgSeq << " "
   1.138 +     << "pid: " << m_nlmsgPid;
   1.139 +}
   1.140 +
   1.141 +uint32_t 
   1.142 +NetlinkMessageHeader::GetSerializedSize (void) const
   1.143 +{
   1.144 +  /* this is the size of an nlmsghdr payload. */
   1.145 +  return NETLINK_MSG_HEADER_SIZE;
   1.146 +}
   1.147 +
   1.148 +void
   1.149 +NetlinkMessageHeader::Serialize (Buffer::Iterator& start) const
   1.150 +{
   1.151 +  start.WriteU32 (m_nlmsgLen);
   1.152 +  start.WriteU16 (m_nlmsgType);
   1.153 +  start.WriteU16 (m_nlmsgFlags);
   1.154 +  start.WriteU32 (m_nlmsgSeq);
   1.155 +  start.WriteU32 (m_nlmsgPid);
   1.156 +}
   1.157 +
   1.158 +uint32_t
   1.159 +NetlinkMessageHeader::Deserialize (Buffer::Iterator& start)
   1.160 +{
   1.161 +  m_nlmsgLen = start.ReadU32 ();
   1.162 +  m_nlmsgType = start.ReadU16 ();
   1.163 +  m_nlmsgFlags = start.ReadU16 ();
   1.164 +  m_nlmsgSeq = start.ReadU32 ();
   1.165 +  m_nlmsgPid = start.ReadU32 ();
   1.166 +
   1.167 +  return GetSerializedSize ();
   1.168 +}
   1.169 +
   1.170 +
   1.171 +
   1.172 +
   1.173 +/***********************************************************************************
   1.174 +* \ NetlinkMessageError
   1.175 +***********************************************************************************/
   1.176 +
   1.177 +NetlinkMessageError::NetlinkMessageError ()
   1.178 +  : m_error (0)
   1.179 +{
   1.180 +}
   1.181 +NetlinkMessageError::~NetlinkMessageError ()
   1.182 +{}
   1.183 +void 
   1.184 +NetlinkMessageError::SetError (int32_t v)
   1.185 +{
   1.186 +  m_error = v;
   1.187 +}
   1.188 +int32_t
   1.189 +NetlinkMessageError::GetError (void) const
   1.190 +{
   1.191 +  return m_error;
   1.192 +}
   1.193 +void
   1.194 +NetlinkMessageError::SetMsg (NetlinkMessageHeader v)
   1.195 +{
   1.196 +  m_msg = v;
   1.197 +}
   1.198 +NetlinkMessageHeader
   1.199 +NetlinkMessageError::GetMsg (void) const
   1.200 +{
   1.201 +  return m_msg;
   1.202 +}
   1.203 +
   1.204 +TypeId 
   1.205 +NetlinkMessageError::GetTypeId (void)
   1.206 +{
   1.207 +  static TypeId tid = TypeId ("ns3::NetlinkMessageError")
   1.208 +    .SetParent<NetlinkPayload> ()
   1.209 +    .AddConstructor<NetlinkMessageError> ()
   1.210 +    ;
   1.211 +  return tid;
   1.212 +}
   1.213 +
   1.214 +TypeId 
   1.215 +NetlinkMessageError::GetInstanceTypeId (void) const
   1.216 +{
   1.217 +  return GetTypeId ();
   1.218 +}
   1.219 +void 
   1.220 +NetlinkMessageError::Print (std::ostream &os) const
   1.221 +{  
   1.222 +  os << "----NetlinkMessageError "
   1.223 +     << "error: " << m_error << " "
   1.224 +     << "msg:( ";
   1.225 +  m_msg.Print(os);
   1.226 +  os << " )";
   1.227 +}
   1.228 +
   1.229 +uint32_t 
   1.230 +NetlinkMessageError::GetSerializedSize (void) const
   1.231 +{
   1.232 +  /* this is the size of an nlmsgerr payload. */
   1.233 +  return NETLINK_MSG_ERROR_SIZE;
   1.234 +}
   1.235 +
   1.236 +void
   1.237 +NetlinkMessageError::Serialize (Buffer::Iterator& start) const
   1.238 +{
   1.239 +  start.WriteU32 (m_error);
   1.240 +  m_msg.Serialize (start);
   1.241 +}
   1.242 +
   1.243 +uint32_t
   1.244 +NetlinkMessageError::Deserialize (Buffer::Iterator& start)
   1.245 +{  
   1.246 +  m_error = start.ReadU32 ();
   1.247 +  m_msg.Deserialize (start);
   1.248 +  
   1.249 +  return GetSerializedSize ();
   1.250 +}
   1.251 +
   1.252 +
   1.253 +
   1.254 +
   1.255 +/***********************************************************************************
   1.256 +* \ NetlinkMessage
   1.257 +***********************************************************************************/
   1.258 +NetlinkMessage::NetlinkMessage ()
   1.259 +{}
   1.260 +
   1.261 +void
   1.262 +NetlinkMessage::SetHeader (NetlinkMessageHeader hdr)
   1.263 +{
   1.264 +  m_hdr = hdr;
   1.265 +}
   1.266 +NetlinkMessageHeader
   1.267 +NetlinkMessage::GetHeader (void)const
   1.268 +{
   1.269 +  return m_hdr;
   1.270 +}
   1.271 +void
   1.272 +NetlinkMessage::SetGeneralMessage (GeneralMessage genmsg)
   1.273 +{
   1.274 +  m_genmsg = genmsg;
   1.275 +  m_hdr.SetMsgLen (m_hdr.GetSerializedSize () + genmsg.GetSerializedSize ());
   1.276 +}
   1.277 +void
   1.278 +NetlinkMessage::SetErrorMessage (NetlinkMessageError errmsg)
   1.279 +{
   1.280 +  m_errorMessage = errmsg;
   1.281 +  m_hdr.SetMsgLen(m_hdr.GetSerializedSize () + errmsg.GetSerializedSize ());
   1.282 +}
   1.283 +// the type is one of RTM_NEWLINK,RTM_DELLINK,RTM_GETLINK
   1.284 +void
   1.285 +NetlinkMessage::SetInterfaceInfoMessage (InterfaceInfoMessage v)
   1.286 +{
   1.287 +  m_hdr.SetMsgLen (m_hdr.GetSerializedSize () + v.GetSerializedSize ());
   1.288 +  m_interfaceTemplate = v;
   1.289 +}
   1.290 +// the type is one of RTM_NEWADDR,RTM_DELADDR,RTM_GETADDR
   1.291 +void NetlinkMessage::SetInterfaceAddressMessage (InterfaceAddressMessage v)
   1.292 +{
   1.293 +  m_hdr.SetMsgLen (m_hdr.GetSerializedSize () + v.GetSerializedSize ());
   1.294 +  m_addressTemplate = v;
   1.295 +}
   1.296 +// the type  is one of RTM_NEWROUTE,RTM_DELROUTE,RTM_GETROUTE
   1.297 +void NetlinkMessage::SetRouteMessage (RouteMessage v)
   1.298 +{
   1.299 +  m_hdr.SetMsgLen (m_hdr.GetSerializedSize () + v.GetSerializedSize ());
   1.300 +  m_routeTemplate = v;
   1.301 +}
   1.302 +GeneralMessage
   1.303 +NetlinkMessage::GetGeneralMessage (void) const
   1.304 +{
   1.305 +  return m_genmsg;
   1.306 +}
   1.307 +NetlinkMessageError
   1.308 +NetlinkMessage::GetErrorMessage (void) const
   1.309 +{
   1.310 +  return m_errorMessage;
   1.311 +}
   1.312 +InterfaceInfoMessage
   1.313 +NetlinkMessage::GetInterfaceInfoMessage (void) const
   1.314 +{
   1.315 +  return m_interfaceTemplate;
   1.316 +}
   1.317 +InterfaceAddressMessage
   1.318 +NetlinkMessage::GetInterfaceAddressMessage (void) const
   1.319 +{
   1.320 +  return m_addressTemplate;
   1.321 +}
   1.322 +RouteMessage
   1.323 +NetlinkMessage::GetRouteMessage (void) const
   1.324 +{
   1.325 +  return m_routeTemplate;
   1.326 +}
   1.327 +bool
   1.328 +NetlinkMessage::IsMessageNetlinkControl (uint16_t type)
   1.329 +{
   1.330 +  return (type < NETLINK_RTM_BASE);
   1.331 +}
   1.332 +bool
   1.333 +NetlinkMessage::IsMessageNetlinkRoute (uint16_t type)
   1.334 +{
   1.335 +  return (type >= NETLINK_RTM_BASE && type < NETLINK_RTM_MAX);
   1.336 +}
   1.337 +bool
   1.338 +NetlinkMessage::IsMessageAddress (uint16_t type)
   1.339 +{
   1.340 +  return (type >= NETLINK_RTM_NEWADDR && type <= NETLINK_RTM_GETADDR);
   1.341 +}
   1.342 +bool
   1.343 +NetlinkMessage::IsMessageInterface (uint16_t type)
   1.344 +{
   1.345 +  return (type >= NETLINK_RTM_NEWLINK && type <= NETLINK_RTM_SETLINK);
   1.346 +}
   1.347 +bool
   1.348 +NetlinkMessage::IsMessageRoute (uint16_t type)
   1.349 +{
   1.350 +  return (type >= NETLINK_RTM_NEWROUTE && type <= NETLINK_RTM_GETROUTE);
   1.351 +}
   1.352 +bool
   1.353 +NetlinkMessage::IsMessageTypeGet (uint16_t type)
   1.354 +{
   1.355 +  return ((( type - NETLINK_RTM_BASE)&3) == 2);
   1.356 +}
   1.357 +bool
   1.358 +NetlinkMessage::IsMessageFlagsAck (uint16_t flags)
   1.359 +{
   1.360 +  return (flags & NETLINK_MSG_F_ACK) ? true : false;
   1.361 +}
   1.362 +bool
   1.363 +NetlinkMessage::IsMessageFlagsRequest (uint16_t flags)
   1.364 +{
   1.365 +  return (flags & NETLINK_MSG_F_REQUEST) ? true : false;
   1.366 +}
   1.367 +bool
   1.368 +NetlinkMessage::IsMessageFlagsDump (uint16_t flags)
   1.369 +{
   1.370 +  return (flags & NETLINK_MSG_F_DUMP) ? true : false;
   1.371 +}
   1.372 +
   1.373 +NetlinkMessage::operator MultipartNetlinkMessage (void) const
   1.374 +{
   1.375 +  MultipartNetlinkMessage multi_nlmsg;
   1.376 +  multi_nlmsg.AppendMessage (*this);
   1.377 +  return multi_nlmsg;
   1.378 +}
   1.379 +
   1.380 +TypeId 
   1.381 +NetlinkMessage::GetTypeId (void)
   1.382 +{
   1.383 +  static TypeId tid = TypeId ("ns3::NetlinkMessage")
   1.384 +    .SetParent<Header> ()
   1.385 +    .AddConstructor<NetlinkMessage> ()
   1.386 +    ;
   1.387 +  return tid;
   1.388 +}
   1.389 +TypeId 
   1.390 +NetlinkMessage::GetInstanceTypeId (void) const
   1.391 +{
   1.392 +  return GetTypeId ();
   1.393 +}
   1.394 +
   1.395 +uint32_t
   1.396 +NetlinkMessage::GetTotalSize (void) const
   1.397 +{
   1.398 +  return NETLINK_MSG_ALIGN (m_hdr.GetMsgLen ());
   1.399 +}
   1.400 +
   1.401 +uint32_t
   1.402 +NetlinkMessage::GetMsgSize (void) const
   1.403 +{
   1.404 +  return m_hdr.GetMsgLen ();
   1.405 +}
   1.406 +
   1.407 +uint32_t
   1.408 +NetlinkMessage::GetPayloadSize (void) const
   1.409 +{
   1.410 +  return m_hdr.GetPayloadSize ();
   1.411 +}
   1.412 +uint16_t
   1.413 +NetlinkMessage::GetMsgType (void) const
   1.414 +{
   1.415 +  return m_hdr.GetMsgType ();
   1.416 +}
   1.417 +
   1.418 +uint8_t
   1.419 +NetlinkMessage::GetFamily(void) const
   1.420 +{
   1.421 +  if (IsMessageTypeGet (GetMsgType ()))
   1.422 +    {
   1.423 +      NS_LOG_DEBUG ("TypeGetMsg");
   1.424 +    }
   1.425 +  if (IsMessageAddress (m_hdr.GetMsgType ()))
   1.426 +    {
   1.427 +      return m_addressTemplate.GetFamily ();
   1.428 +    }
   1.429 +  else if (IsMessageInterface(m_hdr.GetMsgType ()))
   1.430 +    {
   1.431 +      return m_interfaceTemplate.GetFamily ();
   1.432 +    }
   1.433 +  else if (IsMessageRoute(m_hdr.GetMsgType ()))
   1.434 +    {
   1.435 +      return m_routeTemplate.GetFamily ();
   1.436 +    }
   1.437 +  else if (IsMessageFlagsDump (m_hdr.GetMsgFlags ()))
   1.438 +    {
   1.439 +      return m_genmsg.GetFamily (); //value is said to be always set to AF_UNSPEC
   1.440 +    }
   1.441 +  else
   1.442 +    {
   1.443 +      NS_LOG_WARN ("Netlink message type not supported, return AF_UNSPEC");
   1.444 +      return 0;
   1.445 +    }
   1.446 +}
   1.447 +
   1.448 +void 
   1.449 +NetlinkMessage::Print (std::ostream &os) const
   1.450 +{
   1.451 +  uint16_t type = m_hdr.GetMsgType ();
   1.452 +
   1.453 +  os << "NetlinkMessage  ";
   1.454 +  os << " ----Header:(";
   1.455 +  m_hdr.Print(os);
   1.456 +  os << ")";
   1.457 +
   1.458 +  if (type == NETLINK_MSG_DONE )
   1.459 +    {
   1.460 +      os << "multipart message ends here";
   1.461 +    }
   1.462 +  else if (type == NETLINK_MSG_ERROR )
   1.463 +    {
   1.464 +      m_errorMessage.Print (os);
   1.465 +    }
   1.466 +  else if (type == NETLINK_RTM_GETROUTE || type == NETLINK_RTM_GETADDR || type == NETLINK_RTM_GETLINK)
   1.467 +    {
   1.468 +      m_genmsg.Print (os);
   1.469 +    }  
   1.470 +  else if (type == NETLINK_RTM_NEWROUTE || type == NETLINK_RTM_DELROUTE)
   1.471 +    {
   1.472 +      m_routeTemplate.Print (os);
   1.473 +    }
   1.474 +  else if (type == NETLINK_RTM_NEWADDR || type == NETLINK_RTM_DELADDR)
   1.475 +    {
   1.476 +      m_addressTemplate.Print (os);
   1.477 +    }
   1.478 +  else if (type == NETLINK_RTM_NEWLINK || type == NETLINK_RTM_DELLINK)
   1.479 +    {
   1.480 +      m_interfaceTemplate.Print (os);
   1.481 +    }
   1.482 +  else
   1.483 +    {
   1.484 +      os << "service not supported yet( " << type <<")";
   1.485 +    }
   1.486 +}
   1.487 +uint32_t 
   1.488 +NetlinkMessage::GetSerializedSize (void) const
   1.489 +{
   1.490 +  return NETLINK_MSG_ALIGN (m_hdr.GetMsgLen ());
   1.491 +}
   1.492 +
   1.493 +void
   1.494 +NetlinkMessage::Serialize (Buffer::Iterator& start) const
   1.495 +{
   1.496 +  NS_LOG_FUNCTION (this);
   1.497 +  //  Print (std::cout);
   1.498 +  uint16_t type = m_hdr.GetMsgType ();
   1.499 +
   1.500 +  m_hdr.Serialize (start);
   1.501 +
   1.502 +  if (type == NETLINK_MSG_DONE)
   1.503 +    {
   1.504 +      //nothing done
   1.505 +    }
   1.506 +  else if (type == NETLINK_MSG_ERROR)
   1.507 +    {
   1.508 +      m_errorMessage.Serialize (start);
   1.509 +    }  
   1.510 +  else if (NetlinkMessage::IsMessageFlagsDump (m_hdr.GetMsgFlags ()) && 
   1.511 +           (type == NETLINK_RTM_GETROUTE || type == NETLINK_RTM_GETADDR || type == NETLINK_RTM_GETLINK))
   1.512 +    {
   1.513 +      m_genmsg.Serialize (start);
   1.514 +    }  
   1.515 +  else if (type == NETLINK_RTM_NEWROUTE || type == NETLINK_RTM_DELROUTE || type == NETLINK_RTM_GETROUTE)
   1.516 +    {
   1.517 +      m_routeTemplate.Serialize (start);
   1.518 +    }
   1.519 +  else if (type == NETLINK_RTM_NEWADDR || type == NETLINK_RTM_DELADDR)
   1.520 +    {
   1.521 +      m_addressTemplate.Serialize (start);
   1.522 +    }
   1.523 +  else if (type == NETLINK_RTM_NEWLINK || type == NETLINK_RTM_SETLINK)
   1.524 +    {
   1.525 +      m_interfaceTemplate.Serialize (start);
   1.526 +    }
   1.527 +  else
   1.528 +    {
   1.529 +    }  
   1.530 +}
   1.531 +
   1.532 +
   1.533 +uint32_t
   1.534 +NetlinkMessage::Deserialize (Buffer::Iterator&start)
   1.535 +{
   1.536 +  uint32_t remaining;
   1.537 +
   1.538 +  m_hdr.Deserialize (start);
   1.539 +  remaining = NETLINK_MSG_ALIGN (m_hdr.GetMsgLen ()) - m_hdr.GetSerializedSize ();
   1.540 +  
   1.541 +  //Deserialize service module
   1.542 +  uint16_t type = GetMsgType ();
   1.543 +  if (remaining)
   1.544 +    {        
   1.545 +      if (type == NETLINK_MSG_DONE)
   1.546 +        {
   1.547 +          //do nothing
   1.548 +        }
   1.549 +      else if (type == NETLINK_MSG_ERROR)
   1.550 +        {
   1.551 +          remaining -= m_errorMessage.Deserialize (start);
   1.552 +        }      
   1.553 +      else if (NetlinkMessage::IsMessageFlagsDump (m_hdr.GetMsgFlags()) &&
   1.554 +               (type == NETLINK_RTM_GETROUTE || type == NETLINK_RTM_GETADDR || type == NETLINK_RTM_GETLINK))
   1.555 +        {
   1.556 +          remaining -= m_genmsg.Deserialize (start, remaining);
   1.557 +        }
   1.558 +      else if (type == NETLINK_RTM_NEWROUTE || type == NETLINK_RTM_DELROUTE || type == NETLINK_RTM_GETROUTE)
   1.559 +        {
   1.560 +          remaining -= m_routeTemplate.Deserialize (start, remaining);
   1.561 +        }
   1.562 +      else if (type == NETLINK_RTM_NEWADDR || type == NETLINK_RTM_DELADDR)
   1.563 +        {
   1.564 +          remaining -= m_addressTemplate.Deserialize (start, remaining);
   1.565 +        }
   1.566 +      else if (type == NETLINK_RTM_NEWLINK || type == NETLINK_RTM_SETLINK)
   1.567 +        {
   1.568 +          remaining -= m_interfaceTemplate.Deserialize (start, remaining);
   1.569 +        }
   1.570 +      else
   1.571 +        {
   1.572 +          //do nothing
   1.573 +        }
   1.574 +    }
   1.575 +
   1.576 +  return GetSerializedSize ();
   1.577 +}
   1.578 +
   1.579 +
   1.580 +/***********************************************************************************
   1.581 +* \ MultipartNetlinkMessage
   1.582 +***********************************************************************************/
   1.583 +MultipartNetlinkMessage::MultipartNetlinkMessage ()
   1.584 +{}
   1.585 +
   1.586 +TypeId 
   1.587 +MultipartNetlinkMessage::GetTypeId (void)
   1.588 +{
   1.589 +  static TypeId tid = TypeId ("ns3::MultipartNetlinkMessage")
   1.590 +    .SetParent<Header> ()
   1.591 +    .AddConstructor<MultipartNetlinkMessage> ()
   1.592 +    ;
   1.593 +  return tid;
   1.594 +}
   1.595 +TypeId 
   1.596 +MultipartNetlinkMessage::GetInstanceTypeId (void) const
   1.597 +{
   1.598 +  return GetTypeId ();
   1.599 +}
   1.600 +
   1.601 +void
   1.602 +MultipartNetlinkMessage::Print (std::ostream &os) const
   1.603 +{
   1.604 +  for (uint32_t i = 0; i <  m_netlinkMessages.size (); i ++)
   1.605 +    {
   1.606 +      m_netlinkMessages[i].Print (os);
   1.607 +    }
   1.608 +}
   1.609 +uint32_t
   1.610 +MultipartNetlinkMessage::GetSerializedSize (void) const
   1.611 +{
   1.612 +  uint32_t len = 0;
   1.613 +
   1.614 +  for (uint32_t i = 0; i <  m_netlinkMessages.size (); i ++)
   1.615 +    {
   1.616 +      len +=  m_netlinkMessages[i].GetSerializedSize ();
   1.617 +    }
   1.618 +  return len;
   1.619 +}
   1.620 +void
   1.621 +MultipartNetlinkMessage::Serialize (Buffer::Iterator start) const
   1.622 +{
   1.623 +  NS_LOG_FUNCTION ("Multi" << this);
   1.624 +  for (uint32_t i = 0; i <  m_netlinkMessages.size (); i ++)
   1.625 +    {
   1.626 +      m_netlinkMessages[i].Serialize (start);
   1.627 +    }
   1.628 +}
   1.629 +uint32_t
   1.630 +MultipartNetlinkMessage::Deserialize (Buffer::Iterator start)
   1.631 +{
   1.632 +  while (1)
   1.633 +    {
   1.634 +      NetlinkMessage nlmsg;
   1.635 +      nlmsg.Deserialize (start);
   1.636 +      AppendMessage (nlmsg);
   1.637 +
   1.638 +      if (!(nlmsg.GetHeader ().GetMsgFlags () & NETLINK_MSG_F_MULTI))
   1.639 +        {
   1.640 +          break;
   1.641 +        }
   1.642 +
   1.643 +      if (nlmsg.GetHeader ().GetMsgType() == NETLINK_MSG_DONE)
   1.644 +        {
   1.645 +          break;
   1.646 +        }
   1.647 +    }
   1.648 +  return GetSerializedSize ();
   1.649 +}
   1.650 +
   1.651 +void
   1.652 +MultipartNetlinkMessage::AppendMessage (NetlinkMessage nlmsg)
   1.653 +{
   1.654 +  m_netlinkMessages.push_back (nlmsg);
   1.655 +}
   1.656 +
   1.657 +void
   1.658 +MultipartNetlinkMessage::Clear ()
   1.659 +{
   1.660 +  m_netlinkMessages.clear ();
   1.661 +}
   1.662 +
   1.663 +uint32_t
   1.664 +MultipartNetlinkMessage::GetNMessages (void) const
   1.665 +{
   1.666 +  return m_netlinkMessages.size ();
   1.667 +}
   1.668 +NetlinkMessage
   1.669 +MultipartNetlinkMessage::GetMessage (uint32_t index) const
   1.670 +{
   1.671 +  NS_ASSERT(index < m_netlinkMessages.size ());
   1.672 +  return m_netlinkMessages[index];
   1.673 +}
   1.674 +
   1.675 +}; // namespace ns3