netlink/netlink-socket.h
changeset 66 2fe1f3e576c9
parent 63 e89dca438df6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netlink/netlink-socket.h	Thu May 05 09:28:21 2011 +0200
     1.3 @@ -0,0 +1,205 @@
     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 +#ifndef NETLINK_SOCKET_H
    1.25 +#define NETLINK_SOCKET_H
    1.26 +
    1.27 +#include <stdint.h>
    1.28 +#include <queue>
    1.29 +#include "netlink-message.h"
    1.30 +#include "ns3/callback.h"
    1.31 +#include "ns3/ptr.h"
    1.32 +#include "ns3/traced-callback.h"
    1.33 +#include "ns3/socket.h"
    1.34 +#include "ns3/ipv4-address.h"
    1.35 +#include "ns3/ipv6-address.h"
    1.36 +#include "ns3/ipv6-interface.h"
    1.37 +
    1.38 +namespace ns3 {
    1.39 +
    1.40 +class Node;
    1.41 +class Packet;
    1.42 +class NetlinkSocketAddress;
    1.43 +
    1.44 +/**
    1.45 +* \brief A NetlinkSocket is  used  to transfer information 
    1.46 +between kernel and userspace processes .
    1.47 +*
    1.48 +* here we focus on NETLINK_ROUTE: Receives routing and link
    1.49 +* updates and may be used to modify  the  routing  tables 
    1.50 +* (both IPv4 and IPv6), IP addresses, link parame- ters, neighbor
    1.51 +* setups, queueing disciplines, traffic classes and packet 
    1.52 +* classifiers (see rtnetlink (7)). This socket type is very similar
    1.53 +* to the linux and BSD "packet" sockets.
    1.54 +*
    1.55 +* Here is a summary of the semantics of this class:
    1.56 +* - Bind: Bind uses only the protocol and device fields of the 
    1.57 +*       NetlinkSocketAddress.
    1.58 +*
    1.59 +* - Send: send the input packet to the underlying kernel space
    1.60 +*       with its own address. The socket must  be bound.
    1.61 +*
    1.62 +* - Recv: receive packet from the kernel space.
    1.63 +*
    1.64 +* - Accept: not allowed
    1.65 +* - Connect: not allowed
    1.66 +*/
    1.67 +class NetlinkSocket : public Socket
    1.68 +{
    1.69 +public:
    1.70 +  static TypeId GetTypeId (void);
    1.71 +
    1.72 +  NetlinkSocket ();
    1.73 +  virtual ~NetlinkSocket ();
    1.74 +
    1.75 +  void SetNode (Ptr<Node> node);
    1.76 +
    1.77 +  virtual enum SocketErrno GetErrno (void) const;
    1.78 +  virtual enum Socket::SocketType GetSocketType (void) const;
    1.79 +  virtual Ptr<Node> GetNode (void) const;
    1.80 +  virtual int Bind (void);
    1.81 +  virtual int Bind (const Address & address);
    1.82 +  virtual int Close (void);
    1.83 +  virtual int ShutdownSend (void);
    1.84 +  virtual int ShutdownRecv (void);
    1.85 +  virtual int Connect (const Address &address);
    1.86 +  virtual int Listen (void);
    1.87 +  virtual uint32_t GetTxAvailable (void) const;
    1.88 +  virtual int Send (Ptr<Packet> p, uint32_t flags);
    1.89 +  virtual int SendTo(Ptr<Packet> p, uint32_t flags, const Address &toAddress);
    1.90 +  virtual uint32_t GetRxAvailable (void) const;
    1.91 +  virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags);
    1.92 +  virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags, 
    1.93 +                                Address &fromAddress);
    1.94 +  virtual int GetSockName (Address &address) const; 
    1.95 +  virtual int GetPeerName (Address &address) const;
    1.96 +  virtual bool SetAllowBroadcast (bool allowBroadcast);
    1.97 +  virtual bool GetAllowBroadcast () const;
    1.98 +
    1.99 +  uint32_t GetSrcPid (void) const;
   1.100 +  uint32_t GetSrcGroups (void)const;
   1.101 +  uint32_t GetDstPid (void) const;
   1.102 +  uint32_t GetDstGroups (void)const;
   1.103 +  int32_t NotifyIfAddrMessage (Ipv6Interface* interface, Ipv6Address addr, int cmd);
   1.104 +  int32_t NotifyIfLinkMessage (Address address, uint16_t type, uint8_t family);
   1.105 +  //  int32_t NotifyRouteMessage(Ojbect route, uint16_t type, uint8_t family);
   1.106 +
   1.107 +private:
   1.108 +  int DoBind (const NetlinkSocketAddress &address);
   1.109 +  virtual void DoDispose (void);
   1.110 +  void ForwardUp (Ptr<Packet> p, NetlinkSocketAddress &address);
   1.111 +
   1.112 +
   1.113 +
   1.114 +  /**
   1.115 + * the functions below were for kernel parsing netlink message,set private here
   1.116 + * when netlink msg sent to kernel through netlink socket, it was parsed in kernel
   1.117 + * space, then, kernel add/del its route/interface/link table or dump all information
   1.118 + * to user space
   1.119 + */
   1.120 +
   1.121 +  int32_t HandleMessage (const NetlinkMessage &nlmsg);
   1.122 +  /**
   1.123 +  * when kernel find the message truncated or user need an ACK response,
   1.124 +  * it send ACK back to user space, with the error code(0 for ACK, > 0 for error).
   1.125 +  */
   1.126 +  void SendAckMessage (const NetlinkMessage &nlmsg, int32_t errorcode);
   1.127 +
   1.128 +  /**
   1.129 +  * \brief unicast an message to user
   1.130 +  * \param nlmsg the netlink message to transmit
   1.131 +  * \param pid the netlink pid of destination socket
   1.132 +  * \param nonbloack always true
   1.133 +  */
   1.134 +  int32_t SendMessageUnicast (const MultipartNetlinkMessage &nlmsg, 
   1.135 +                              uint32_t pid, int32_t nonblock);
   1.136 +  /**
   1.137 +  * \brief spread message to netlink group user
   1.138 +  * \param nlmsg the netlink message to transmit
   1.139 +  * \param pid the netlink pid of the kernel, always 0
   1.140 +  * \param group multicast group id
   1.141 +  */
   1.142 +  static int32_t SendMessageBroadcast (const MultipartNetlinkMessage &nlmsg, 
   1.143 +                                       uint32_t pid, uint32_t group, Ptr<Node> node);
   1.144 +
   1.145 +  /**
   1.146 +  * these functions below are for NETLINK_ROUTE protocol, it handle the netlink 
   1.147 +  * message like linux kernel work.  this implementation follows the kernel code 
   1.148 +  * linux/rtnetlink.c, focus on "interface address, interface info and route entry",
   1.149 +  * now we will only simply support three types operations of  NETLINK_ROUTE 
   1.150 +  * protocol
   1.151 +  */
   1.152 +  
   1.153 +  /**
   1.154 +  * \returns 0 if messge not processed, < 0 for success or an error.
   1.155 +  * this function would call dumping/doing functions to  
   1.156 +  */
   1.157 +  int32_t HandleNetlinkRouteMessage (const NetlinkMessage &nlmsg);
   1.158 +  
   1.159 +  /**
   1.160 +  * \returns 0 if dumping operation is OK, < 0 for an error.
   1.161 +  */ 
   1.162 +  int32_t DumpNetlinkRouteMessage (const NetlinkMessage &nlmsg, 
   1.163 +                                   uint16_t type, uint8_t family);
   1.164 +  MultipartNetlinkMessage BuildInterfaceAddressDumpMessage (uint32_t pid,
   1.165 +                                                            uint32_t seq, uint8_t family);
   1.166 +  MultipartNetlinkMessage BuildInterfaceInfoDumpMessage (uint32_t pid,
   1.167 +                                                         uint32_t seq, uint8_t family);
   1.168 +  MultipartNetlinkMessage BuildRouteDumpMessage (uint32_t pid,
   1.169 +                                                 uint32_t seq, uint8_t family);
   1.170 +
   1.171 +  /**
   1.172 +  * \returns 0 if doing operation(ADD/DEL/GET) is OK, < 0 for an error.
   1.173 +  */
   1.174 +  int32_t DoNetlinkRouteMessage (const NetlinkMessage &nlmsg,
   1.175 +                                 uint16_t type, uint8_t family);
   1.176 +  int32_t DoInterfaceAddressMessage (const NetlinkMessage &nlmsg, 
   1.177 +                                     uint16_t type, uint8_t family);
   1.178 +  int32_t DoInterfaceInfoMessage (const NetlinkMessage &nlmsg, 
   1.179 +                                  uint16_t type, uint8_t family);
   1.180 +  int32_t DoRouteMessage (const NetlinkMessage &nlmsg, 
   1.181 +                          uint16_t type, uint8_t family);
   1.182 +
   1.183 +  int ErrnoToSimuErrno (void);
   1.184 +  Address ConvertFrom (uint8_t family, const Address &address);
   1.185 +
   1.186 +  Ptr<Node> m_node;
   1.187 +  enum SocketErrno m_errno;
   1.188 +  bool m_shutdownSend;
   1.189 +  bool m_shutdownRecv;
   1.190 +
   1.191 +  std::queue<Ptr<Packet> > m_dataReceiveQueue;
   1.192 +  uint32_t m_rxAvailable;
   1.193 +  TracedCallback<Ptr<const Packet> > m_dropTrace;
   1.194 +  // Socket options (attributes)
   1.195 +  uint32_t m_rcvBufSize;
   1.196 +
   1.197 +  uint32_t m_srcPid;
   1.198 +  uint32_t m_srcGroups;
   1.199 +  uint32_t m_dstPid;
   1.200 +  uint32_t m_dstGroups;
   1.201 +  Callback<void, Ipv4Address,uint8_t,uint8_t,uint8_t,uint32_t> m_icmpCallback;
   1.202 +};
   1.203 +
   1.204 +}//namespace ns3
   1.205 +
   1.206 +#endif /* NETLINK_SOCKET_H */
   1.207 +
   1.208 +