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