Statistics
| Branch: | Tag: | Revision:

root / lib / confd / __init__.py @ cea881e5

History | View | Annotate | Download (1.4 kB)

1 12426ae1 Guido Trotter
#
2 12426ae1 Guido Trotter
#
3 12426ae1 Guido Trotter
4 12426ae1 Guido Trotter
# Copyright (C) 2009 Google Inc.
5 12426ae1 Guido Trotter
#
6 12426ae1 Guido Trotter
# This program is free software; you can redistribute it and/or modify
7 12426ae1 Guido Trotter
# it under the terms of the GNU General Public License as published by
8 12426ae1 Guido Trotter
# the Free Software Foundation; either version 2 of the License, or
9 12426ae1 Guido Trotter
# (at your option) any later version.
10 12426ae1 Guido Trotter
#
11 12426ae1 Guido Trotter
# This program is distributed in the hope that it will be useful, but
12 12426ae1 Guido Trotter
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 12426ae1 Guido Trotter
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 12426ae1 Guido Trotter
# General Public License for more details.
15 12426ae1 Guido Trotter
#
16 12426ae1 Guido Trotter
# You should have received a copy of the GNU General Public License
17 12426ae1 Guido Trotter
# along with this program; if not, write to the Free Software
18 12426ae1 Guido Trotter
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 12426ae1 Guido Trotter
# 02110-1301, USA.
20 12426ae1 Guido Trotter
21 12426ae1 Guido Trotter
22 9748ab35 Guido Trotter
"""Ganeti confd client/server library
23 12426ae1 Guido Trotter

24 12426ae1 Guido Trotter
"""
25 9748ab35 Guido Trotter
26 9748ab35 Guido Trotter
from ganeti import constants
27 6855f043 Guido Trotter
from ganeti import errors
28 9748ab35 Guido Trotter
29 9748ab35 Guido Trotter
30 9748ab35 Guido Trotter
_FOURCC_LEN = 4
31 9748ab35 Guido Trotter
32 9748ab35 Guido Trotter
33 9748ab35 Guido Trotter
def PackMagic(payload):
34 9748ab35 Guido Trotter
  """Prepend the confd magic fourcc to a payload.
35 9748ab35 Guido Trotter

36 9748ab35 Guido Trotter
  """
37 9748ab35 Guido Trotter
  return ''.join([constants.CONFD_MAGIC_FOURCC, payload])
38 9748ab35 Guido Trotter
39 9748ab35 Guido Trotter
40 9748ab35 Guido Trotter
def UnpackMagic(payload):
41 9748ab35 Guido Trotter
  """Unpack and check the confd magic fourcc from a payload.
42 9748ab35 Guido Trotter

43 9748ab35 Guido Trotter
  """
44 9748ab35 Guido Trotter
  if len(payload) < _FOURCC_LEN:
45 9748ab35 Guido Trotter
    raise errors.ConfdMagicError("UDP payload too short to contain the"
46 9748ab35 Guido Trotter
                                 " fourcc code")
47 9748ab35 Guido Trotter
48 9748ab35 Guido Trotter
  magic_number = payload[:_FOURCC_LEN]
49 9748ab35 Guido Trotter
  if magic_number != constants.CONFD_MAGIC_FOURCC:
50 9748ab35 Guido Trotter
    raise errors.ConfdMagicError("UDP payload contains an unkown fourcc")
51 9748ab35 Guido Trotter
52 9748ab35 Guido Trotter
  return payload[_FOURCC_LEN:]