OLD | NEW |
(Empty) | |
| 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 "ns3/callback.h" |
| 27 #include "ns3/ptr.h" |
| 28 #include "ns3/traced-callback.h" |
| 29 #include "ns3/socket.h" |
| 30 #include "ns3/netlink-message.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 Ptr<Node> GetNode (void) const; |
| 76 virtual int Bind (void); |
| 77 virtual int Bind (const Address & address); |
| 78 virtual int Close (void); |
| 79 virtual int ShutdownSend (void); |
| 80 virtual int ShutdownRecv (void); |
| 81 virtual int Connect (const Address &address); |
| 82 virtual int Listen (void); |
| 83 virtual uint32_t GetTxAvailable (void) const; |
| 84 virtual int Send (Ptr<Packet> p, uint32_t flags); |
| 85 virtual int SendTo(Ptr<Packet> p, uint32_t flags, const Address &toAddress); |
| 86 virtual uint32_t GetRxAvailable (void) const; |
| 87 virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags); |
| 88 virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags,· |
| 89 Address &fromAddress); |
| 90 virtual int GetSockName (Address &address) const;· |
| 91 virtual int GetPeerName (Address &address) const; |
| 92 |
| 93 uint32_t GetSrcPid (void) const; |
| 94 uint32_t GetSrcGroups (void)const; |
| 95 uint32_t GetDstPid (void) const; |
| 96 uint32_t GetDstGroups (void)const; |
| 97 int32_t NotifyIfAddrMessage (Ipv6Interface* interface, Ipv6Address addr, int c
md); |
| 98 int32_t NotifyIfLinkMessage (Address address, uint16_t type, uint8_t family); |
| 99 // int32_t NotifyRouteMessage(Ojbect route, uint16_t type, uint8_t family); |
| 100 |
| 101 private: |
| 102 int DoBind (const NetlinkSocketAddress &address); |
| 103 virtual void DoDispose (void); |
| 104 void ForwardUp (Ptr<Packet> p, NetlinkSocketAddress &address); |
| 105 |
| 106 |
| 107 |
| 108 /** |
| 109 * the functions below were for kernel parsing netlink message,set private here |
| 110 * when netlink msg sent to kernel through netlink socket, it was parsed in kern
el |
| 111 * space, then, kernel add/del its route/interface/link table or dump all inform
ation |
| 112 * to user space |
| 113 */ |
| 114 |
| 115 int32_t HandleMessage (const NetlinkMessage &nlmsg); |
| 116 /** |
| 117 * when kernel find the message truncated or user need an ACK response, |
| 118 * it send ACK back to user space, with the error code(0 for ACK, > 0 for error
). |
| 119 */ |
| 120 void SendAckMessage (const NetlinkMessage &nlmsg, int32_t errorcode); |
| 121 |
| 122 /** |
| 123 * \brief unicast an message to user |
| 124 * \param nlmsg the netlink message to transmit |
| 125 * \param pid the netlink pid of destination socket |
| 126 * \param nonbloack always true |
| 127 */ |
| 128 int32_t SendMessageUnicast (const MultipartNetlinkMessage &nlmsg,· |
| 129 uint32_t pid, int32_t nonblock); |
| 130 /** |
| 131 * \brief spread message to netlink group user |
| 132 * \param nlmsg the netlink message to transmit |
| 133 * \param pid the netlink pid of the kernel, always 0 |
| 134 * \param group multicast group id |
| 135 */ |
| 136 static int32_t SendMessageBroadcast (const MultipartNetlinkMessage &nlmsg,· |
| 137 uint32_t pid, uint32_t group, Ptr<Node> n
ode); |
| 138 |
| 139 /** |
| 140 * these functions below are for NETLINK_ROUTE protocol, it handle the netlink· |
| 141 * message like linux kernel work. this implementation follows the kernel code······· |
| 142 * linux/rtnetlink.c, focus on "interface address, interface info and route ent
ry", |
| 143 * now we will only simply support three types operations of NETLINK_ROUTE· |
| 144 * protocol |
| 145 */ |
| 146 ·· |
| 147 /** |
| 148 * \returns 0 if messge not processed, < 0 for success or an error. |
| 149 * this function would call dumping/doing functions to·· |
| 150 */ |
| 151 int32_t HandleNetlinkRouteMessage (const NetlinkMessage &nlmsg); |
| 152 ·· |
| 153 /** |
| 154 * \returns 0 if dumping operation is OK, < 0 for an error. |
| 155 */· |
| 156 int32_t DumpNetlinkRouteMessage (const NetlinkMessage &nlmsg,· |
| 157 uint16_t type, uint8_t family); |
| 158 MultipartNetlinkMessage BuildInterfaceAddressDumpMessage (uint32_t pid, |
| 159 uint32_t seq, uint8_
t family); |
| 160 MultipartNetlinkMessage BuildInterfaceInfoDumpMessage (uint32_t pid, |
| 161 uint32_t seq, uint8_t f
amily); |
| 162 MultipartNetlinkMessage BuildRouteDumpMessage (uint32_t pid, |
| 163 uint32_t seq, uint8_t family); |
| 164 |
| 165 /** |
| 166 * \returns 0 if doing operation(ADD/DEL/GET) is OK, < 0 for an error. |
| 167 */ |
| 168 int32_t DoNetlinkRouteMessage (const NetlinkMessage &nlmsg, |
| 169 uint16_t type, uint8_t family); |
| 170 int32_t DoInterfaceAddressMessage (const NetlinkMessage &nlmsg,· |
| 171 uint16_t type, uint8_t family); |
| 172 int32_t DoInterfaceInfoMessage (const NetlinkMessage &nlmsg,· |
| 173 uint16_t type, uint8_t family); |
| 174 int32_t DoRouteMessage (const NetlinkMessage &nlmsg,· |
| 175 uint16_t type, uint8_t family); |
| 176 |
| 177 int ErrnoToSimuErrno (void); |
| 178 Address ConvertFrom (uint8_t family, const Address &address); |
| 179 |
| 180 Ptr<Node> m_node; |
| 181 enum SocketErrno m_errno; |
| 182 bool m_shutdownSend; |
| 183 bool m_shutdownRecv; |
| 184 |
| 185 std::queue<Ptr<Packet> > m_dataReceiveQueue; |
| 186 uint32_t m_rxAvailable; |
| 187 TracedCallback<Ptr<const Packet> > m_dropTrace; |
| 188 // Socket options (attributes) |
| 189 uint32_t m_rcvBufSize; |
| 190 |
| 191 uint32_t m_srcPid; |
| 192 uint32_t m_srcGroups; |
| 193 uint32_t m_dstPid; |
| 194 uint32_t m_dstGroups; |
| 195 Callback<void, Ipv4Address,uint8_t,uint8_t,uint8_t,uint32_t> m_icmpCallback; |
| 196 }; |
| 197 |
| 198 }//namespace ns3 |
| 199 |
| 200 #endif /* NETLINK_SOCKET_H */ |
| 201 |
| 202 |
OLD | NEW |