bug 210: must take device mtu into account.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 04 Jun 2008 10:59:30 -0700
changeset 3219 49dcb3f0bd34
parent 3218 503c67d93dab
child 3220 8c4fcd2a187d
bug 210: must take device mtu into account.
src/node/packet-socket.cc
src/node/packet-socket.h
--- a/src/node/packet-socket.cc	Wed Jun 04 10:47:34 2008 -0700
+++ b/src/node/packet-socket.cc	Wed Jun 04 10:59:30 2008 -0700
@@ -27,6 +27,8 @@
 #include "ns3/uinteger.h"
 #include "ns3/trace-source-accessor.h"
 
+#include <algorithm>
+
 NS_LOG_COMPONENT_DEFINE ("PacketSocket");
 
 namespace ns3 {
@@ -240,11 +242,35 @@
   return SendTo (p, m_destAddr);
 }
 
-// XXX must limit it to interface MTU
+uint32_t
+PacketSocket::GetMinMtu (PacketSocketAddress ad) const
+{
+  if (ad.IsSingleDevice ())
+    {
+      Ptr<NetDevice> device = m_node->GetDevice (ad.GetSingleDevice ());
+      return device->GetMtu ();
+    }
+  else
+    {
+      uint32_t minMtu = 0xffff;
+      for (uint32_t i = 0; i < m_node->GetNDevices (); i++)
+        {
+          Ptr<NetDevice> device = m_node->GetDevice (i);
+          minMtu = std::min (minMtu, (uint32_t)device->GetMtu ());
+        }
+      return minMtu;
+    }
+}
+
 uint32_t 
 PacketSocket::GetTxAvailable (void) const
 {
-  // Use 65536 for now
+  if (m_state == STATE_CONNECTED)
+    {
+      PacketSocketAddress ad = PacketSocketAddress::ConvertFrom (m_destAddr);      
+      return GetMinMtu (ad);
+    }
+  // If we are not connected, we return a 'safe' value by default.
   return 0xffff;
 }
 
@@ -271,12 +297,12 @@
       m_errno = ERROR_AFNOSUPPORT;
       return -1;
     }
-  if (p->GetSize () > GetTxAvailable ())
+  ad = PacketSocketAddress::ConvertFrom (address);
+  if (p->GetSize () > GetMinMtu (ad))
     {
       m_errno = ERROR_MSGSIZE;
       return -1;
     }
-  ad = PacketSocketAddress::ConvertFrom (address);
   
   bool error = false;
   Address dest = ad.GetPhysicalAddress ();
--- a/src/node/packet-socket.h	Wed Jun 04 10:47:34 2008 -0700
+++ b/src/node/packet-socket.h	Wed Jun 04 10:59:30 2008 -0700
@@ -105,6 +105,7 @@
   void ForwardUp (Ptr<NetDevice> device, Ptr<Packet> packet, 
                   uint16_t protocol, const Address &from);
   int DoBind (const PacketSocketAddress &address);
+  uint32_t GetMinMtu (PacketSocketAddress ad) const;
   virtual void DoDispose (void);
 
   enum State {