From 9748ab353916bb1780acee97a139e9a54afc7ccb Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Tue, 15 Sep 2009 13:50:19 +0100
Subject: [PATCH] Move fourcc packing/unpacking to main confd module

This way it can be used by the client as well

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 daemons/ganeti-confd  | 18 ++++++------------
 lib/confd/__init__.py | 30 +++++++++++++++++++++++++++++-
 lib/errors.py         |  8 ++++++++
 3 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/daemons/ganeti-confd b/daemons/ganeti-confd
index f64c7965b..22e16374a 100755
--- a/daemons/ganeti-confd
+++ b/daemons/ganeti-confd
@@ -43,6 +43,7 @@ from ganeti import daemon
 from ganeti import ssconf
 from ganeti.asyncnotifier import AsyncNotifier
 from ganeti.confd.server import ConfdProcessor
+from ganeti.confd import PackMagic, UnpackMagic
 
 
 class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
@@ -69,23 +70,16 @@ class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
 
   # this method is overriding a daemon.AsyncUDPSocket method
   def handle_datagram(self, payload_in, ip, port):
-
-    if len(payload_in) < len(constants.CONFD_MAGIC_FOURCC):
-      logging.debug("Received a query which is too short to be true")
-      return
-
-    magic_number = payload_in[:4]
-    query = payload_in[4:]
-
-    if magic_number != constants.CONFD_MAGIC_FOURCC:
-      logging.debug("Received a query with an unknown magic number")
+    try:
+      query = UnpackMagic(payload_in)
+    except errors.ConfdMagicError, err:
+      logging.debug(err)
       return
 
     answer =  self.processor.ExecQuery(query, ip, port)
     if answer is not None:
-      payload_out = ''.join([constants.CONFD_MAGIC_FOURCC, answer])
       try:
-        self.enqueue_send(ip, port, payload_out)
+        self.enqueue_send(ip, port, PackMagic(answer))
       except errors.UdpDataSizeError:
         logging.error("Reply too big to fit in an udp packet.")
 
diff --git a/lib/confd/__init__.py b/lib/confd/__init__.py
index 786de1bed..4a8ac78a6 100644
--- a/lib/confd/__init__.py
+++ b/lib/confd/__init__.py
@@ -19,6 +19,34 @@
 # 02110-1301, USA.
 
 
-"""Ganeti confd library
+"""Ganeti confd client/server library
 
 """
+
+from ganeti import constants
+
+
+_FOURCC_LEN = 4
+
+
+def PackMagic(payload):
+  """Prepend the confd magic fourcc to a payload.
+
+  """
+  return ''.join([constants.CONFD_MAGIC_FOURCC, payload])
+
+
+def UnpackMagic(payload):
+  """Unpack and check the confd magic fourcc from a payload.
+
+  """
+  if len(payload) < _FOURCC_LEN:
+    raise errors.ConfdMagicError("UDP payload too short to contain the"
+                                 " fourcc code")
+
+  magic_number = payload[:_FOURCC_LEN]
+  if magic_number != constants.CONFD_MAGIC_FOURCC:
+    raise errors.ConfdMagicError("UDP payload contains an unkown fourcc")
+
+  return payload[_FOURCC_LEN:]
+
diff --git a/lib/errors.py b/lib/errors.py
index 663bc02f3..9136c5eff 100644
--- a/lib/errors.py
+++ b/lib/errors.py
@@ -297,6 +297,14 @@ class ConfdRequestError(GenericError):
   """
 
 
+class ConfdMagicError(GenericError):
+  """A magic fourcc error in Ganeti confd.
+
+  Errors processing the fourcc in ganeti confd datagrams.
+
+  """
+
+
 class UdpDataSizeError(GenericError):
   """UDP payload too big.
 
-- 
GitLab