netlink/netlink-message.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.h@e89dca438df6
permissions -rw-r--r--
make it somewhat build sanely
mathieu@0
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@0
     2
/*
mathieu@0
     3
 * Copyright (c) 2008 Liu Jian
mathieu@0
     4
 *
mathieu@0
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@0
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@0
     7
 * published by the Free Software Foundation;
mathieu@0
     8
 *
mathieu@0
     9
 * This program is distributed in the hope that it will be useful,
mathieu@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@0
    12
 * GNU General Public License for more details.
mathieu@0
    13
 *
mathieu@0
    14
 * You should have received a copy of the GNU General Public License
mathieu@0
    15
 * along with this program; if not, write to the Free Software
mathieu@0
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@0
    17
 *
mathieu@0
    18
 * Author: Liu Jian <liujatp@gmail.com>
mathieu@0
    19
 *         Hajime Tazaki <tazaki@sfc.wide.ad.jp>
mathieu@0
    20
 */
mathieu@0
    21
mathieu@0
    22
#ifndef NETLINK_MESSAGE_H
mathieu@0
    23
#define NETLINK_MESSAGE_H
mathieu@0
    24
mathieu@0
    25
#include "ns3/header.h"
mathieu@0
    26
#include "ns3/address.h"
mathieu@0
    27
#include "netlink-message-route.h"
mathieu@0
    28
#include "netlink-attribute.h"
mathieu@0
    29
mathieu@0
    30
mathieu@0
    31
namespace ns3 {
mathieu@0
    32
  class NetlinkPayload;
mathieu@0
    33
  class GeneralMessage;
mathieu@0
    34
  class InterfaceAddressMessage;
mathieu@0
    35
  class InterfaceInfoMessage;
mathieu@0
    36
  class RouteMessage;
mathieu@0
    37
  class MultipartNetlinkMessage;
mathieu@0
    38
mathieu@0
    39
/**
mathieu@0
    40
* \brief The Netlink message structure for an netlink packet
mathieu@0
    41
* 
mathieu@0
    42
There are three levels to a Netlink message: The general Netlink
mathieu@0
    43
message header, the IP service specific template, and the IP service
mathieu@0
    44
specific data.
mathieu@0
    45
mathieu@0
    46
0                   1                   2                   3
mathieu@0
    47
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
mathieu@0
    48
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
mathieu@0
    49
|                                                               |
mathieu@0
    50
|                   Netlink message header                      |
mathieu@0
    51
|                                                               |
mathieu@0
    52
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
mathieu@0
    53
|                                                               |
mathieu@0
    54
|                  IP Service Template                          |
mathieu@0
    55
|                                                               |
mathieu@0
    56
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
mathieu@0
    57
|                                                               |
mathieu@0
    58
|                  IP Service specific data in TLVs             |
mathieu@0
    59
|                                                               |
mathieu@0
    60
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
mathieu@0
    61
*/
mathieu@0
    62
mathieu@0
    63
  enum NetlinkMessageFlag
mathieu@0
    64
  {
mathieu@0
    65
    NETLINK_MSG_F_REQUEST = 1,  // It is request message.
mathieu@0
    66
    NETLINK_MSG_F_MULTI = 2,    // Multipart message, terminated by NETLINK_MSG_DONE
mathieu@0
    67
    NETLINK_MSG_F_ACK = 4,      // Reply with ack, with zero or error code
mathieu@0
    68
    NETLINK_MSG_F_ECHO = 8,     // Echo this request 
mathieu@0
    69
mathieu@0
    70
    /* Modifiers to Get request */
mathieu@0
    71
    NETLINK_MSG_F_ROOT = 0x100,        // specify tree root
mathieu@0
    72
    NETLINK_MSG_F_MATCH = 0x200,       // return all matching
mathieu@0
    73
    NETLINK_MSG_F_ATOMIC = 0x400,      // atomic Get =
mathieu@0
    74
    NETLINK_MSG_F_DUMP = (NETLINK_MSG_F_ROOT|NETLINK_MSG_F_MATCH),
mathieu@0
    75
mathieu@0
    76
    /* Modifiers to NEW request */
mathieu@0
    77
    NETLINK_MSG_F_REPLACE = 0x100, // Override existing = 
mathieu@0
    78
    NETLINK_MSG_F_EXCL = 0x200,   // Do not touch, if it exists
mathieu@0
    79
    NETLINK_MSG_F_CREATE = 0x400,  // Create, if it does not exist
mathieu@0
    80
    NETLINK_MSG_F_APPEND = 0x800,  // Add to end of list = 
mathieu@0
    81
  };
mathieu@0
    82
mathieu@0
    83
  enum NetlinkMessageType
mathieu@0
    84
  {
mathieu@0
    85
    NETLINK_MSG_NOOP = 0x1,          // Nothing.
mathieu@0
    86
    NETLINK_MSG_ERROR = 0x2,         // Error
mathieu@0
    87
    NETLINK_MSG_DONE = 0x3,          // End of a dump
mathieu@0
    88
    NETLINK_MSG_OVERRUN = 0x4,       // Data lost
mathieu@0
    89
    NETLINK_MSG_MIN_TYPE = 0x10,     // < 0x10: reserved control messages
mathieu@0
    90
  };
mathieu@0
    91
mathieu@0
    92
#define NETLINK_MSG_ALIGNTO 4
mathieu@0
    93
#define NETLINK_MSG_ALIGN(X)    (((X)+NETLINK_MSG_ALIGNTO-1) & ~(NETLINK_MSG_ALIGNTO-1) )
mathieu@0
    94
mathieu@0
    95
class NetlinkMessageHeader : public ObjectBase
mathieu@0
    96
{
mathieu@0
    97
public:
mathieu@0
    98
  NetlinkMessageHeader ();
mathieu@0
    99
  NetlinkMessageHeader (uint16_t type, uint16_t flags, uint32_t seq, uint32_t pid);
mathieu@0
   100
mathieu@0
   101
  static TypeId GetTypeId (void);
mathieu@0
   102
  virtual TypeId GetInstanceTypeId (void) const;
mathieu@0
   103
  void Print (std::ostream &os) const;
mathieu@0
   104
  uint32_t GetSerializedSize (void) const;
mathieu@0
   105
  void Serialize (Buffer::Iterator& start) const;
mathieu@0
   106
  uint32_t Deserialize (Buffer::Iterator& start);
mathieu@0
   107
mathieu@0
   108
  void SetMsgLen (uint32_t v);
mathieu@0
   109
  void SetMsgFlags (uint16_t v);
mathieu@0
   110
  void SetMsgType (uint16_t v);
mathieu@0
   111
  void SetMsgSeq (uint32_t v);
mathieu@0
   112
  void SetMsgPid (uint32_t v);
mathieu@0
   113
  uint32_t GetMsgLen (void) const;
mathieu@0
   114
  uint16_t GetMsgFlags (void) const;
mathieu@0
   115
  uint16_t GetMsgType (void) const;
mathieu@0
   116
  uint32_t GetMsgSeq (void) const;
mathieu@0
   117
  uint32_t GetMsgPid (void) const;
mathieu@0
   118
mathieu@0
   119
  static uint32_t GetHeaderSize ();
mathieu@0
   120
  uint32_t GetPayloadSize (void) const;
mathieu@0
   121
mathieu@0
   122
private:
mathieu@0
   123
  static const uint32_t NETLINK_MSG_HEADER_SIZE = 16; /* size of the nlmsghdr field*/
mathieu@0
   124
  uint32_t m_nlmsgLen;	/* Length of message including header */
mathieu@0
   125
  uint16_t m_nlmsgType;	/* Message content */
mathieu@0
   126
  uint16_t m_nlmsgFlags;	/* Additional flags */
mathieu@0
   127
  uint32_t m_nlmsgSeq;	/* Sequence number */
mathieu@0
   128
  uint32_t m_nlmsgPid;	/* Sending process PID */
mathieu@0
   129
};
mathieu@0
   130
mathieu@0
   131
/**
mathieu@0
   132
* \brief The struct nlmsgerr
mathieu@0
   133
*/
mathieu@0
   134
class NetlinkMessageError : public NetlinkPayload
mathieu@0
   135
{
mathieu@0
   136
public:
mathieu@0
   137
  NetlinkMessageError ();
mathieu@0
   138
  virtual ~NetlinkMessageError();
mathieu@0
   139
mathieu@0
   140
  static TypeId GetTypeId (void);
mathieu@0
   141
  virtual TypeId GetInstanceTypeId (void) const;
mathieu@0
   142
  virtual void Serialize (Buffer::Iterator& start) const;
mathieu@0
   143
  virtual uint32_t Deserialize (Buffer::Iterator& start);
mathieu@0
   144
  virtual void Print (std::ostream &os) const;
mathieu@0
   145
  virtual uint32_t GetSerializedSize (void) const;
mathieu@0
   146
mathieu@0
   147
  void SetError (int32_t v);
mathieu@0
   148
  int32_t GetError (void) const;
mathieu@0
   149
  void SetMsg(NetlinkMessageHeader v);
mathieu@0
   150
  NetlinkMessageHeader GetMsg (void) const;
mathieu@0
   151
mathieu@0
   152
private:
mathieu@0
   153
  static const int NETLINK_MSG_ERROR_SIZE = 20; /* size of the nlmsgerror field*/
mathieu@0
   154
  int32_t m_error;
mathieu@0
   155
  NetlinkMessageHeader m_msg;        
mathieu@0
   156
};
mathieu@0
   157
mathieu@0
   158
mathieu@0
   159
class NetlinkMessage : public ObjectBase
mathieu@0
   160
{
mathieu@0
   161
public:
mathieu@0
   162
  NetlinkMessage ();
mathieu@0
   163
mathieu@0
   164
  static TypeId GetTypeId (void);
mathieu@0
   165
  TypeId GetInstanceTypeId (void) const;
mathieu@0
   166
  void Print (std::ostream &os) const;
mathieu@0
   167
  uint32_t GetSerializedSize (void) const;
mathieu@0
   168
  void Serialize (Buffer::Iterator& start) const;
mathieu@0
   169
  uint32_t Deserialize (Buffer::Iterator& start);
mathieu@0
   170
mathieu@0
   171
  operator MultipartNetlinkMessage (void) const;
mathieu@0
   172
mathieu@0
   173
  uint32_t GetTotalSize (void) const;  //length of netlink message including padding
mathieu@0
   174
  uint32_t GetMsgSize (void) const;    //length of netlink message not including padding
mathieu@0
   175
  uint32_t GetPayloadSize (void) const; //length of message payload
mathieu@0
   176
  uint16_t GetMsgType (void) const;
mathieu@0
   177
  uint8_t GetFamily(void) const;
mathieu@0
   178
mathieu@0
   179
  void SetHeader (NetlinkMessageHeader hdr);
mathieu@0
   180
  NetlinkMessageHeader GetHeader (void) const;
mathieu@0
   181
mathieu@0
   182
  //before set message body, should set header first
mathieu@0
   183
  void SetErrorMessage (NetlinkMessageError errmsg);
mathieu@0
   184
  void SetGeneralMessage (GeneralMessage genmsg);
mathieu@0
   185
  void SetInterfaceInfoMessage (InterfaceInfoMessage v);
mathieu@0
   186
  void SetInterfaceAddressMessage (InterfaceAddressMessage v);
mathieu@0
   187
  void SetRouteMessage (RouteMessage v);
mathieu@0
   188
  NetlinkMessageError GetErrorMessage (void) const;
mathieu@0
   189
  GeneralMessage GetGeneralMessage (void) const;
mathieu@0
   190
  InterfaceInfoMessage GetInterfaceInfoMessage (void) const;
mathieu@0
   191
  InterfaceAddressMessage GetInterfaceAddressMessage (void) const;
mathieu@0
   192
  RouteMessage GetRouteMessage (void) const;
mathieu@0
   193
mathieu@0
   194
  /**
mathieu@0
   195
  * \returns true if type was control type, false otherwise.
mathieu@0
   196
  */
mathieu@0
   197
  static bool IsMessageNetlinkControl (uint16_t type);
mathieu@0
   198
  /**
mathieu@0
   199
  * \returns true if type was netlink route, false otherwise.
mathieu@0
   200
  */
mathieu@0
   201
  static bool IsMessageNetlinkRoute (uint16_t type);
mathieu@0
   202
  static bool IsMessageAddress (uint16_t type);
mathieu@0
   203
  static bool IsMessageInterface (uint16_t type);
mathieu@0
   204
  static bool IsMessageRoute (uint16_t type);
mathieu@0
   205
  /**
mathieu@0
   206
  * \returns true if type was GETxxx , false otherwise.
mathieu@0
   207
  */
mathieu@0
   208
  static bool IsMessageTypeGet (uint16_t type);
mathieu@0
   209
  /**
mathieu@0
   210
  * \returns true if flag has ack , false otherwise.
mathieu@0
   211
  */
mathieu@0
   212
  static bool IsMessageFlagsAck (uint16_t flags);
mathieu@0
   213
  /**
mathieu@0
   214
  * \returns true if flag has request , false otherwise.
mathieu@0
   215
  */
mathieu@0
   216
  static bool IsMessageFlagsRequest (uint16_t flags);
mathieu@0
   217
  /**
mathieu@0
   218
  * \returns true if flag has dump , false otherwise.
mathieu@0
   219
  */
mathieu@0
   220
  static bool IsMessageFlagsDump (uint16_t flags);
mathieu@0
   221
mathieu@0
   222
private:
mathieu@0
   223
  NetlinkMessageHeader m_hdr;
mathieu@0
   224
mathieu@0
   225
  //only one type of messages below exists in real world application
mathieu@0
   226
  NetlinkMessageError m_errorMessage;  
mathieu@0
   227
  GeneralMessage m_genmsg;
mathieu@0
   228
  InterfaceInfoMessage m_interfaceTemplate;
mathieu@0
   229
  InterfaceAddressMessage m_addressTemplate;
mathieu@0
   230
  RouteMessage m_routeTemplate;
mathieu@0
   231
};
mathieu@0
   232
mathieu@0
   233
class MultipartNetlinkMessage : public Header
mathieu@0
   234
{
mathieu@0
   235
public:
mathieu@0
   236
  MultipartNetlinkMessage ();
mathieu@0
   237
mathieu@0
   238
  static TypeId GetTypeId (void);
mathieu@0
   239
  virtual TypeId GetInstanceTypeId (void) const;
mathieu@0
   240
  virtual void Print (std::ostream &os) const;
mathieu@0
   241
  virtual uint32_t GetSerializedSize (void) const;
mathieu@0
   242
  virtual void Serialize (Buffer::Iterator start) const;
mathieu@0
   243
  virtual uint32_t Deserialize (Buffer::Iterator start);
mathieu@0
   244
mathieu@0
   245
  void AppendMessage (NetlinkMessage nlmsg);
mathieu@0
   246
  void Clear();
mathieu@0
   247
  uint32_t GetNMessages (void) const;
mathieu@0
   248
  NetlinkMessage GetMessage (uint32_t index) const;
mathieu@0
   249
mathieu@0
   250
private:
mathieu@0
   251
  std::vector<NetlinkMessage> m_netlinkMessages;
mathieu@0
   252
};
mathieu@0
   253
mathieu@0
   254
}; // namespace ns3
mathieu@0
   255
mathieu@0
   256
#endif /* NETLINK_MESSAGE_H */