netlink/netlink-socket.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 05 May 2011 09:28:21 +0200
changeset 66 2fe1f3e576c9
parent 63 model/netlink-socket.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
#ifndef NETLINK_SOCKET_H
mathieu@0
    22
#define NETLINK_SOCKET_H
mathieu@0
    23
mathieu@0
    24
#include <stdint.h>
mathieu@0
    25
#include <queue>
mathieu@0
    26
#include "netlink-message.h"
mathieu@0
    27
#include "ns3/callback.h"
mathieu@0
    28
#include "ns3/ptr.h"
mathieu@0
    29
#include "ns3/traced-callback.h"
mathieu@0
    30
#include "ns3/socket.h"
mathieu@0
    31
#include "ns3/ipv4-address.h"
mathieu@0
    32
#include "ns3/ipv6-address.h"
mathieu@0
    33
#include "ns3/ipv6-interface.h"
mathieu@0
    34
mathieu@0
    35
namespace ns3 {
mathieu@0
    36
mathieu@0
    37
class Node;
mathieu@0
    38
class Packet;
mathieu@0
    39
class NetlinkSocketAddress;
mathieu@0
    40
mathieu@0
    41
/**
mathieu@0
    42
* \brief A NetlinkSocket is  used  to transfer information 
mathieu@0
    43
between kernel and userspace processes .
mathieu@0
    44
*
mathieu@0
    45
* here we focus on NETLINK_ROUTE: Receives routing and link
mathieu@0
    46
* updates and may be used to modify  the  routing  tables 
mathieu@0
    47
* (both IPv4 and IPv6), IP addresses, link parame- ters, neighbor
mathieu@0
    48
* setups, queueing disciplines, traffic classes and packet 
mathieu@0
    49
* classifiers (see rtnetlink (7)). This socket type is very similar
mathieu@0
    50
* to the linux and BSD "packet" sockets.
mathieu@0
    51
*
mathieu@0
    52
* Here is a summary of the semantics of this class:
mathieu@0
    53
* - Bind: Bind uses only the protocol and device fields of the 
mathieu@0
    54
*       NetlinkSocketAddress.
mathieu@0
    55
*
mathieu@0
    56
* - Send: send the input packet to the underlying kernel space
mathieu@0
    57
*       with its own address. The socket must  be bound.
mathieu@0
    58
*
mathieu@0
    59
* - Recv: receive packet from the kernel space.
mathieu@0
    60
*
mathieu@0
    61
* - Accept: not allowed
mathieu@0
    62
* - Connect: not allowed
mathieu@0
    63
*/
mathieu@0
    64
class NetlinkSocket : public Socket
mathieu@0
    65
{
mathieu@0
    66
public:
mathieu@0
    67
  static TypeId GetTypeId (void);
mathieu@0
    68
mathieu@0
    69
  NetlinkSocket ();
mathieu@0
    70
  virtual ~NetlinkSocket ();
mathieu@0
    71
mathieu@0
    72
  void SetNode (Ptr<Node> node);
mathieu@0
    73
mathieu@0
    74
  virtual enum SocketErrno GetErrno (void) const;
mathieu@63
    75
  virtual enum Socket::SocketType GetSocketType (void) const;
mathieu@0
    76
  virtual Ptr<Node> GetNode (void) const;
mathieu@0
    77
  virtual int Bind (void);
mathieu@0
    78
  virtual int Bind (const Address & address);
mathieu@0
    79
  virtual int Close (void);
mathieu@0
    80
  virtual int ShutdownSend (void);
mathieu@0
    81
  virtual int ShutdownRecv (void);
mathieu@0
    82
  virtual int Connect (const Address &address);
mathieu@0
    83
  virtual int Listen (void);
mathieu@0
    84
  virtual uint32_t GetTxAvailable (void) const;
mathieu@0
    85
  virtual int Send (Ptr<Packet> p, uint32_t flags);
mathieu@0
    86
  virtual int SendTo(Ptr<Packet> p, uint32_t flags, const Address &toAddress);
mathieu@0
    87
  virtual uint32_t GetRxAvailable (void) const;
mathieu@0
    88
  virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags);
mathieu@0
    89
  virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags, 
mathieu@0
    90
                                Address &fromAddress);
mathieu@0
    91
  virtual int GetSockName (Address &address) const; 
mathieu@0
    92
  virtual int GetPeerName (Address &address) const;
mathieu@0
    93
  virtual bool SetAllowBroadcast (bool allowBroadcast);
mathieu@0
    94
  virtual bool GetAllowBroadcast () const;
mathieu@0
    95
mathieu@0
    96
  uint32_t GetSrcPid (void) const;
mathieu@0
    97
  uint32_t GetSrcGroups (void)const;
mathieu@0
    98
  uint32_t GetDstPid (void) const;
mathieu@0
    99
  uint32_t GetDstGroups (void)const;
mathieu@0
   100
  int32_t NotifyIfAddrMessage (Ipv6Interface* interface, Ipv6Address addr, int cmd);
mathieu@0
   101
  int32_t NotifyIfLinkMessage (Address address, uint16_t type, uint8_t family);
mathieu@0
   102
  //  int32_t NotifyRouteMessage(Ojbect route, uint16_t type, uint8_t family);
mathieu@0
   103
mathieu@0
   104
private:
mathieu@0
   105
  int DoBind (const NetlinkSocketAddress &address);
mathieu@0
   106
  virtual void DoDispose (void);
mathieu@0
   107
  void ForwardUp (Ptr<Packet> p, NetlinkSocketAddress &address);
mathieu@0
   108
mathieu@0
   109
mathieu@0
   110
mathieu@0
   111
  /**
mathieu@0
   112
 * the functions below were for kernel parsing netlink message,set private here
mathieu@0
   113
 * when netlink msg sent to kernel through netlink socket, it was parsed in kernel
mathieu@0
   114
 * space, then, kernel add/del its route/interface/link table or dump all information
mathieu@0
   115
 * to user space
mathieu@0
   116
 */
mathieu@0
   117
mathieu@0
   118
  int32_t HandleMessage (const NetlinkMessage &nlmsg);
mathieu@0
   119
  /**
mathieu@0
   120
  * when kernel find the message truncated or user need an ACK response,
mathieu@0
   121
  * it send ACK back to user space, with the error code(0 for ACK, > 0 for error).
mathieu@0
   122
  */
mathieu@0
   123
  void SendAckMessage (const NetlinkMessage &nlmsg, int32_t errorcode);
mathieu@0
   124
mathieu@0
   125
  /**
mathieu@0
   126
  * \brief unicast an message to user
mathieu@0
   127
  * \param nlmsg the netlink message to transmit
mathieu@0
   128
  * \param pid the netlink pid of destination socket
mathieu@0
   129
  * \param nonbloack always true
mathieu@0
   130
  */
mathieu@0
   131
  int32_t SendMessageUnicast (const MultipartNetlinkMessage &nlmsg, 
mathieu@0
   132
                              uint32_t pid, int32_t nonblock);
mathieu@0
   133
  /**
mathieu@0
   134
  * \brief spread message to netlink group user
mathieu@0
   135
  * \param nlmsg the netlink message to transmit
mathieu@0
   136
  * \param pid the netlink pid of the kernel, always 0
mathieu@0
   137
  * \param group multicast group id
mathieu@0
   138
  */
mathieu@0
   139
  static int32_t SendMessageBroadcast (const MultipartNetlinkMessage &nlmsg, 
mathieu@0
   140
                                       uint32_t pid, uint32_t group, Ptr<Node> node);
mathieu@0
   141
mathieu@0
   142
  /**
mathieu@0
   143
  * these functions below are for NETLINK_ROUTE protocol, it handle the netlink 
mathieu@0
   144
  * message like linux kernel work.  this implementation follows the kernel code 
mathieu@0
   145
  * linux/rtnetlink.c, focus on "interface address, interface info and route entry",
mathieu@0
   146
  * now we will only simply support three types operations of  NETLINK_ROUTE 
mathieu@0
   147
  * protocol
mathieu@0
   148
  */
mathieu@0
   149
  
mathieu@0
   150
  /**
mathieu@0
   151
  * \returns 0 if messge not processed, < 0 for success or an error.
mathieu@0
   152
  * this function would call dumping/doing functions to  
mathieu@0
   153
  */
mathieu@0
   154
  int32_t HandleNetlinkRouteMessage (const NetlinkMessage &nlmsg);
mathieu@0
   155
  
mathieu@0
   156
  /**
mathieu@0
   157
  * \returns 0 if dumping operation is OK, < 0 for an error.
mathieu@0
   158
  */ 
mathieu@0
   159
  int32_t DumpNetlinkRouteMessage (const NetlinkMessage &nlmsg, 
mathieu@0
   160
                                   uint16_t type, uint8_t family);
mathieu@0
   161
  MultipartNetlinkMessage BuildInterfaceAddressDumpMessage (uint32_t pid,
mathieu@0
   162
                                                            uint32_t seq, uint8_t family);
mathieu@0
   163
  MultipartNetlinkMessage BuildInterfaceInfoDumpMessage (uint32_t pid,
mathieu@0
   164
                                                         uint32_t seq, uint8_t family);
mathieu@0
   165
  MultipartNetlinkMessage BuildRouteDumpMessage (uint32_t pid,
mathieu@0
   166
                                                 uint32_t seq, uint8_t family);
mathieu@0
   167
mathieu@0
   168
  /**
mathieu@0
   169
  * \returns 0 if doing operation(ADD/DEL/GET) is OK, < 0 for an error.
mathieu@0
   170
  */
mathieu@0
   171
  int32_t DoNetlinkRouteMessage (const NetlinkMessage &nlmsg,
mathieu@0
   172
                                 uint16_t type, uint8_t family);
mathieu@0
   173
  int32_t DoInterfaceAddressMessage (const NetlinkMessage &nlmsg, 
mathieu@0
   174
                                     uint16_t type, uint8_t family);
mathieu@0
   175
  int32_t DoInterfaceInfoMessage (const NetlinkMessage &nlmsg, 
mathieu@0
   176
                                  uint16_t type, uint8_t family);
mathieu@0
   177
  int32_t DoRouteMessage (const NetlinkMessage &nlmsg, 
mathieu@0
   178
                          uint16_t type, uint8_t family);
mathieu@0
   179
mathieu@0
   180
  int ErrnoToSimuErrno (void);
mathieu@0
   181
  Address ConvertFrom (uint8_t family, const Address &address);
mathieu@0
   182
mathieu@0
   183
  Ptr<Node> m_node;
mathieu@0
   184
  enum SocketErrno m_errno;
mathieu@0
   185
  bool m_shutdownSend;
mathieu@0
   186
  bool m_shutdownRecv;
mathieu@0
   187
mathieu@0
   188
  std::queue<Ptr<Packet> > m_dataReceiveQueue;
mathieu@0
   189
  uint32_t m_rxAvailable;
mathieu@0
   190
  TracedCallback<Ptr<const Packet> > m_dropTrace;
mathieu@0
   191
  // Socket options (attributes)
mathieu@0
   192
  uint32_t m_rcvBufSize;
mathieu@0
   193
mathieu@0
   194
  uint32_t m_srcPid;
mathieu@0
   195
  uint32_t m_srcGroups;
mathieu@0
   196
  uint32_t m_dstPid;
mathieu@0
   197
  uint32_t m_dstGroups;
mathieu@0
   198
  Callback<void, Ipv4Address,uint8_t,uint8_t,uint8_t,uint32_t> m_icmpCallback;
mathieu@0
   199
};
mathieu@0
   200
mathieu@0
   201
}//namespace ns3
mathieu@0
   202
mathieu@0
   203
#endif /* NETLINK_SOCKET_H */
mathieu@0
   204
mathieu@0
   205