From c8eded0ba8ca02baeed5497bb590c840d62ac3fb Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Mon, 14 Sep 2009 16:42:33 +0100
Subject: [PATCH] AsyncUDPSocket: Move to a well defined UDP size

Currently we read maximum 4K packets, and don't check packets when
sending them. With this patch we move to a well defined maximum size of
60K.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/constants.py | 7 +++++++
 lib/daemon.py    | 5 ++++-
 lib/errors.py    | 6 ++++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/constants.py b/lib/constants.py
index fddb30c6f..123994826 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -681,3 +681,10 @@ CONFD_CONFIG_RELOAD_TIMEOUT = 60
 # polling every RATELIMIT seconds, rather than relying on inotify, to be able
 # to serve more requests.
 CONFD_CONFIG_RELOAD_RATELIMIT = 2
+
+# Maximum UDP datagram size.
+# On IPv4: 64K - 20 (ip header size) - 8 (udp header size) = 65507
+# On IPv6: 64K - 40 (ip6 header size) - 8 (udp header size) = 65487
+#   (assuming we can't use jumbo frames)
+# We just set this to 60K, which should be enough
+MAX_UDP_DATA_SIZE = 61440
diff --git a/lib/daemon.py b/lib/daemon.py
index 9467bb97b..ac250cd1d 100644
--- a/lib/daemon.py
+++ b/lib/daemon.py
@@ -95,7 +95,7 @@ class AsyncUDPSocket(asyncore.dispatcher):
   def handle_read(self):
     try:
       try:
-        payload, address = self.recvfrom(4096)
+        payload, address = self.recvfrom(constants.MAX_UDP_DATA_SIZE)
       except socket.error, err:
         if err.errno == errno.EINTR:
           # we got a signal while trying to read. no need to do anything,
@@ -148,6 +148,9 @@ class AsyncUDPSocket(asyncore.dispatcher):
     """Enqueue a datagram to be sent when possible
 
     """
+    if len(payload) > constants.MAX_UDP_DATA_SIZE:
+      raise errors.UdpDataSizeError('Packet too big: %s > %s' % (len(payload),
+                                    constants.MAX_UDP_DATA_SIZE))
     self._out_queue.append((ip, port, payload))
 
 
diff --git a/lib/errors.py b/lib/errors.py
index ec154f738..663bc02f3 100644
--- a/lib/errors.py
+++ b/lib/errors.py
@@ -297,6 +297,12 @@ class ConfdRequestError(GenericError):
   """
 
 
+class UdpDataSizeError(GenericError):
+  """UDP payload too big.
+
+  """
+
+
 # errors should be added above
 
 
-- 
GitLab