netlink/netlink-message-route.h
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.h@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 #ifndef NETLINK_MESSAGE_ROUTE_H
    23 #define NETLINK_MESSAGE_ROUTE_H
    24 
    25 #include "ns3/object-base.h"
    26 #include "ns3/address.h"
    27 #include "netlink-attribute.h"
    28 #include <string>
    29 
    30 namespace ns3 {
    31 
    32 /*
    33 * this file define some netlink message of NETLINK_ROUTE protocol,
    34 * there are mainly three types:interface address, interface info, route entry
    35 * just implemented them for quagga porting.
    36 */
    37   
    38 
    39 
    40 /**
    41 * \Types of messages,here we only define the route message types quagga used
    42 */
    43 enum NetlinkRtmType_e {
    44   NETLINK_RTM_BASE = 16,
    45 
    46   NETLINK_RTM_NEWLINK = 16,
    47   NETLINK_RTM_DELLINK,
    48   NETLINK_RTM_GETLINK,
    49   NETLINK_RTM_SETLINK,
    50 
    51   NETLINK_RTM_NEWADDR = 20,
    52   NETLINK_RTM_DELADDR,
    53   NETLINK_RTM_GETADDR,
    54 
    55   NETLINK_RTM_NEWROUTE = 24,
    56   NETLINK_RTM_DELROUTE,
    57   NETLINK_RTM_GETROUTE,
    58 
    59   NETLINK_RTM_MAX,
    60 };  
    61 
    62 /**
    63 * \Types of netlink groups,here we only define types quagga used
    64 */
    65 enum NetlinkRtmGroup_e {
    66   NETLINK_RTM_GRP_LINK = 1,
    67   NETLINK_RTM_GRP_IPV4_IFADDR = 0x10,
    68   NETLINK_RTM_GRP_IPV4_ROUTE = 0x40,
    69   RTMGRP_IPV6_IFADDR = 0x100,
    70   RTMGRP_IPV6_ROUTE = 0x400,
    71 };
    72 
    73 class NetlinkPayload :public ObjectBase
    74 {
    75 public:
    76   static TypeId GetTypeId (void);
    77   virtual TypeId GetInstanceTypeId (void) const = 0;
    78   virtual void Serialize (Buffer::Iterator& start) const = 0;
    79   virtual void Print (std::ostream &os) const = 0;
    80   virtual uint32_t GetSerializedSize (void) const = 0;
    81 };
    82 
    83 /***
    84  General form of address family dependent message.
    85 
    86   struct rtgenmsg
    87   {
    88     unsigned char		rtgen_family;
    89   };
    90 **/
    91 
    92 class GeneralMessage : public NetlinkPayload
    93 {
    94 public:
    95   GeneralMessage ();
    96   virtual ~GeneralMessage ();
    97 
    98   static TypeId GetTypeId (void);
    99   virtual TypeId GetInstanceTypeId (void) const;
   100   virtual void Serialize (Buffer::Iterator& start) const;
   101   virtual uint32_t Deserialize (Buffer::Iterator& start);
   102   virtual uint32_t Deserialize (Buffer::Iterator& start, uint32_t len);
   103   virtual void Print (std::ostream &os) const;
   104   virtual uint32_t GetSerializedSize (void) const;
   105 
   106   
   107   virtual uint32_t GetNNetlinkAttribute (void)const;
   108   virtual NetlinkAttribute GetNetlinkAttribute (uint32_t index)const;
   109   virtual void AppendAttribute (NetlinkAttribute v);
   110   virtual void SerializeAttribute (Buffer::Iterator& start) const;
   111   virtual void PrintAttribute (std::ostream &os) const;
   112   virtual uint32_t GetAttributeSerializedSize (void) const;
   113   virtual bool GetAttributeByType (NetlinkAttribute& attr, uint16_t type);
   114 
   115 
   116   void SetFamily (uint8_t v);
   117   uint8_t GetFamily (void) const;
   118 
   119 private:
   120   static const int NETLINK_GENMSG_SIZE = 1; /* size of the struct rtgenmsg */  
   121 protected:
   122   uint8_t m_family;   //always set to AF_UNSPEC
   123   //attribute can exist or not
   124   std::vector<NetlinkAttribute> m_attributes;
   125 };
   126 
   127 
   128 
   129 /**
   130 * \brief Link layer specific messages
   131 *
   132 * struct ifinfomsg
   133 * passes link level specific information, not dependent
   134 * on network protocol.
   135 *
   136   struct ifinfomsg
   137   {
   138     unsigned char	ifi_family;
   139     unsigned char	__ifi_pad;
   140     unsigned short	ifi_type;
   141     int		ifi_index;	
   142     unsigned	ifi_flags;
   143     unsigned	ifi_change;
   144   };
   145 */
   146 class InterfaceInfoMessage : public GeneralMessage
   147 {
   148 public:
   149   InterfaceInfoMessage ();
   150   virtual ~InterfaceInfoMessage ();
   151 
   152   static TypeId GetTypeId (void);
   153   virtual TypeId GetInstanceTypeId (void) const;
   154   virtual void Serialize (Buffer::Iterator& start) const;
   155   virtual uint32_t Deserialize (Buffer::Iterator& start, uint32_t len);
   156   virtual void Print (std::ostream &os) const;
   157   virtual uint32_t GetSerializedSize (void) const;
   158 
   159   enum IflAttr_e {
   160     IFL_A_UNSPEC,
   161     IFL_A_ADDRESS,
   162     IFL_A_BROADCAST,
   163     IFL_A_IFNAME,
   164     IFL_A_MTU,
   165     IFL_A_LINK,
   166     IFL_A_QDISC,
   167     IFL_A_STATS,
   168     IFL_A_COST,
   169     IFL_A_PRIORITY,
   170     IFL_A_MASTER,
   171     IFL_A_WIRELESS,		
   172     IFL_A_PROTINFO,
   173     IFL_A_TXQLEN,
   174     IFL_A_MAP,
   175     IFL_A_WEIGHT,
   176     IFL_A_OPERSTATE,
   177     IFL_A_LINKMODE,
   178     IFL_A_MAX,
   179   };
   180 
   181   enum Type_e {
   182     UP = 1,
   183     BROADCAST = 2,
   184     DBG = 4,
   185   };
   186 
   187   void SetDeviceType (uint16_t type);
   188   void SetInterfaceIndex (int32_t index);
   189   void SetDeviceFlags (uint32_t index);
   190   void SetChangeMask (uint32_t mask);
   191 
   192   uint16_t GetDeviceType (void) const;
   193   int32_t GetInterfaceIndex (void) const;
   194   uint32_t GetDeviceFlags (void) const;
   195   uint32_t GetChangeMask (void) const;
   196 private:
   197   static const int NETLINK_INTERFACE_SIZE = 16; /* size of the struct ifinfomsg */
   198   uint8_t m_reserved; //not used
   199   uint16_t m_deviceType;
   200   int32_t m_interfaceIndex;
   201   uint32_t m_deviceFlags;
   202   uint32_t m_changeMask;
   203   NetlinkAttributeValueType m_attributeTypes[IFL_A_MAX];
   204 };
   205 
   206 
   207 
   208 
   209 /**
   210 * \brief Interface address.
   211 *
   212   struct ifaddrmsg
   213   {
   214   unsigned char	ifa_family;
   215   unsigned char	ifa_prefixlen;
   216   unsigned char	ifa_flags;
   217   unsigned char	ifa_scope;
   218   int		ifa_index;
   219   };
   220 */
   221 
   222 class InterfaceAddressMessage : public GeneralMessage
   223 {
   224 public:
   225   InterfaceAddressMessage ();
   226   virtual ~InterfaceAddressMessage ();
   227 
   228   static TypeId GetTypeId (void);
   229   virtual TypeId GetInstanceTypeId (void) const;
   230   virtual void Serialize (Buffer::Iterator& start) const;
   231   virtual uint32_t Deserialize (Buffer::Iterator& start, uint32_t len);
   232   virtual void Print (std::ostream &os) const;
   233   virtual uint32_t GetSerializedSize (void) const;
   234 
   235   enum IfAttr_e {
   236     IF_A_UNSPEC,
   237     IF_A_ADDRESS,
   238     IF_A_LOCAL,
   239     IF_A_LABEL,
   240     IF_A_BROADCAST,
   241     IF_A_ANYCAST,
   242     IF_A_CACHEINFO,
   243     IF_A_MULTICAST,
   244     IF_A_MAX
   245   };
   246 
   247   enum {
   248     F_SECONDARY = 0x01,
   249     F_PERMANENT = 0x80,
   250     F_DEPRECATED = 0x20,
   251     F_TENTATIVE = 0x40
   252   };
   253   enum {
   254     SCOPE_UNIVERSE = 0,
   255     SCOPE_SITE = 200,
   256     SCOPE_LINK = 253,
   257     SCOPE_HOST = 254
   258   };
   259 
   260 
   261   void SetFamily (uint8_t family);
   262   void SetLength (uint8_t length);
   263   void SetFlags (uint8_t flags);
   264   void SetScope (uint8_t scope);
   265   void SetInterfaceIndex (int32_t index);
   266 
   267   uint8_t GetFamily (void) const;
   268   uint8_t GetLength (void) const;
   269   uint8_t GetFlags (void) const;
   270   uint8_t GetScope (void) const;
   271   int32_t GetInterfaceIndex (void) const;
   272 
   273 private:
   274   static const int NETLINK_ADDRESS_SIZE = 8; /* size of the struct ifaddrmsg */
   275   uint8_t m_length;
   276   uint8_t m_flags;
   277   uint8_t m_scope;
   278   int32_t m_index;
   279   NetlinkAttributeValueType m_attributeTypes[IF_A_MAX];
   280 };
   281 
   282 
   283 /**
   284 * \brief Definitions used in routing table administration.
   285 *
   286   struct rtmsg
   287   {
   288     unsigned char		rtm_family;
   289     unsigned char		rtm_dst_len;
   290     unsigned char		rtm_src_len;
   291     unsigned char		rtm_tos;
   292 
   293     unsigned char		rtm_table;	// Routing table id 
   294     unsigned char		rtm_protocol;	//Routing protocol; 
   295     unsigned char		rtm_scope;	
   296     unsigned char		rtm_type;	
   297 
   298     unsigned		rtm_flags;
   299   };
   300 */
   301 
   302 class RouteMessage : public GeneralMessage
   303 {
   304 public:
   305   RouteMessage ();
   306   virtual ~RouteMessage ();
   307 
   308   static TypeId GetTypeId (void);
   309   virtual TypeId GetInstanceTypeId (void) const;
   310   virtual void Serialize (Buffer::Iterator& start) const;
   311   virtual uint32_t Deserialize (Buffer::Iterator& start, uint32_t len);
   312   virtual void Print (std::ostream &os) const;
   313   virtual uint32_t GetSerializedSize (void) const;
   314 
   315   uint8_t GetFamily (void) const;
   316   uint8_t GetDstLength (void) const;
   317   uint8_t GetSrcLength (void) const;
   318   uint8_t GetTos (void) const;
   319   uint8_t GetTableId (void) const;
   320   uint8_t GetProtocol(void) const;
   321   uint8_t GetType (void) const;
   322   uint8_t GetScope (void) const;
   323   uint32_t GetFlags (void) const;
   324   void SetFamily (uint8_t v);
   325   void SetDstLength (uint8_t v);
   326   void SetSrcLength (uint8_t v);
   327   void SetTos (uint8_t v);
   328   void SetTableId (uint8_t v);
   329   void SetProtocol (uint8_t v);
   330   void SetScope (uint8_t v);
   331   void SetType (uint8_t v);
   332   void SetFlags (uint32_t v);
   333 
   334   enum RtProtocol_e {
   335     RT_PROT_UNSPEC = 0,
   336   };
   337 
   338   enum RtFlags_e {
   339     RT_F_CLONED = 0x200,
   340   };
   341 
   342   enum RtScope_e {
   343     RT_SCOPE_UNIVERSE = 0,
   344     RT_SCOPE_LINK = 253,
   345   };
   346 
   347   enum RtClass_e {
   348     RT_TABLE_UNSPEC = 0,
   349     RT_TABLE_MAIN = 254,
   350   };
   351 
   352   enum RtAttr_e {
   353     RT_A_UNSPEC,
   354     RT_A_DST,
   355     RT_A_SRC,
   356     RT_A_IIF,
   357     RT_A_OIF,
   358     RT_A_GATEWAY,
   359     RT_A_PRIORITY,
   360     RT_A_PREFSRC,
   361     RT_A_METRICS,
   362     RT_A_MULTIPATH,
   363     RT_A_PROTOINFO,
   364     RT_A_FLOW,
   365     RT_A_CACHEINFO,
   366     RT_A_SESSION,
   367     RT_A_MP_ALGO,
   368     RT_A_TABLE,
   369     RT_A_MAX
   370   };
   371 
   372 
   373 private:
   374   static const int NETLINK_ROUTE_SIZE = 12; /* size of the struct rtmsg */
   375   uint8_t m_dstLen;
   376   uint8_t m_srcLen;
   377   uint8_t m_tos;
   378   uint8_t m_tableId;
   379   uint8_t m_protocol;
   380   uint8_t m_scope;
   381   uint8_t m_type;
   382   uint32_t m_flags;
   383   NetlinkAttributeValueType m_attributeTypes[RT_A_MAX];
   384 };
   385 
   386 }; // namespace ns3
   387 
   388 #endif /* NETLINK_MESSAGE_ROUTE_H */