Bug 2035 - write(2)/send(2) crashes with connected UDP socket
authorHajime Tazaki <tazaki@sfc.wide.ad.jp>
Sun, 21 Dec 2014 09:56:37 +0900
changeset 645 4747e90c6d5c
parent 644 8821dc9478e2
child 646 0fd32ca088db
Bug 2035 - write(2)/send(2) crashes with connected UDP socket
RELEASE_NOTES
model/unix-datagram-socket-fd.cc
model/unix-datagram-socket-fd.h
test/test-socket.cc
--- a/RELEASE_NOTES	Sat Dec 20 17:01:00 2014 +0900
+++ b/RELEASE_NOTES	Sun Dec 21 09:56:37 2014 +0900
@@ -22,6 +22,7 @@
 ----------
 - Bug 2009 - update code for RandomVariable deprecation
 - Bug 2015 - static/optimized build fails to execute with a script under myscripts
+- Bug 2035 - write(2)/send(2) crashes with connected UDP socket
 
 Release dce-1.4
 ===============
--- a/model/unix-datagram-socket-fd.cc	Sat Dec 20 17:01:00 2014 +0900
+++ b/model/unix-datagram-socket-fd.cc	Sun Dec 21 09:56:37 2014 +0900
@@ -370,7 +370,10 @@
         }
       else
         {
-          result = m_socket->Send (packet);
+          TaskManager *manager = TaskManager::Current ();
+          result = -1;
+          manager->ExecOnMain (MakeEvent (&UnixDatagramSocketFd::MainSend,
+                                          this, &result, packet));
         }
       if (result == -1)
         {
@@ -454,4 +457,9 @@
 {
   *r = m_socket->SendTo (p, f, ad);
 }
+void
+UnixDatagramSocketFd::MainSend (int *r, Ptr<Packet> p)
+{
+  *r = m_socket->Send (p);
+}
 } // namespace ns3
--- a/model/unix-datagram-socket-fd.h	Sat Dec 20 17:01:00 2014 +0900
+++ b/model/unix-datagram-socket-fd.h	Sun Dec 21 09:56:37 2014 +0900
@@ -31,6 +31,7 @@
   void QueueErr (sock_extended_err ee, struct sockaddr_in offender, uint8_t ttl);
   void CopyMacAddress (const Address &a,  uint8_t* const buf);
   void MainSendTo (int *r, Ptr<Packet> p, uint32_t f, Address ad);
+  void MainSend (int *r, Ptr<Packet> p);
 
   struct Error
   {
--- a/test/test-socket.cc	Sat Dec 20 17:01:00 2014 +0900
+++ b/test/test-socket.cc	Sun Dec 21 09:56:37 2014 +0900
@@ -188,7 +188,7 @@
   TEST_ASSERT_UNEQUAL (sock, -1);
 }
 
-void test_udp (void)
+void test_udp (int connected)
 {
   int sock;
   const char buf[12] = "0123456789\0";
@@ -220,9 +220,18 @@
   msg.msg_control = NULL;
   msg.msg_controllen = 0;
 
-  ret = sendmsg (sock, &msg, 0);
+  if (connected)
+    {
+      ret = connect (sock, (const struct sockaddr *) &dst, sizeof (dst));
+      TEST_ASSERT_EQUAL (ret, 0);
+      ret = send (sock, buf, sizeof (buf), 0);
+    }
+  else
+    {
+      ret = sendmsg (sock, &msg, 0);
+    }
   TEST_ASSERT_EQUAL (ret, sizeof (buf));
-  OUTPUT ("UDP send ret = " << ret);
+  OUTPUT ("UDP (" << (connected ? "connected" : "non-connected") << ") send ret = " << ret);
 
   // recvmsg
   iov.iov_base = (void *) buf;
@@ -515,7 +524,8 @@
   test_timestamp ();
   test_raw ();
   test_raw6 ();
-  test_udp ();
+  test_udp (0);
+  test_udp (1);
   test_tcp ();
   test_netlink ();