netutils.py 16.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#
#

# Copyright (C) 2010 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Ganeti network utility module.

This module holds functions that can be used in both daemons (all) and
the command line scripts.

"""


import errno
31
import os
32
33
34
35
import re
import socket
import struct
import IN
36
import logging
37
38
39

from ganeti import constants
from ganeti import errors
40
from ganeti import utils
41
42
43
44
45
46
47
48
49
50
51
52
53

# Structure definition for getsockopt(SOL_SOCKET, SO_PEERCRED, ...):
# struct ucred { pid_t pid; uid_t uid; gid_t gid; };
#
# The GNU C Library defines gid_t and uid_t to be "unsigned int" and
# pid_t to "int".
#
# IEEE Std 1003.1-2008:
# "nlink_t, uid_t, gid_t, and id_t shall be integer types"
# "blksize_t, pid_t, and ssize_t shall be signed integer types"
_STRUCT_UCRED = "iII"
_STRUCT_UCRED_SIZE = struct.calcsize(_STRUCT_UCRED)

54
55
56
57
58
59
60
# Regexes used to find IP addresses in the output of ip.
_IP_RE_TEXT = r"[.:a-z0-9]+"      # separate for testing purposes
_IP_FAMILY_RE = re.compile(r"(?P<family>inet6?)\s+(?P<ip>%s)/" % _IP_RE_TEXT,
                           re.IGNORECASE)

# Dict used to convert from a string representing an IP family to an IP
# version
Michael Hanselmann's avatar
Michael Hanselmann committed
61
_NAME_TO_IP_VER = {
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  "inet": constants.IP4_VERSION,
  "inet6": constants.IP6_VERSION,
  }


def _GetIpAddressesFromIpOutput(ip_output):
  """Parses the output of the ip command and retrieves the IP addresses and
  version.

  @param ip_output: string containing the output of the ip command;
  @rtype: dict; (int, list)
  @return: a dict having as keys the IP versions and as values the
           corresponding list of addresses found in the IP output.

  """
  addr = dict((i, []) for i in _NAME_TO_IP_VER.values())

  for row in ip_output.splitlines():
    match = _IP_FAMILY_RE.search(row)
    if match and IPAddress.IsValid(match.group("ip")):
      addr[_NAME_TO_IP_VER[match.group("family")]].append(match.group("ip"))

  return addr

86
87
88
89
90
91
92
93
94
95
96
97
98
99

def GetSocketCredentials(sock):
  """Returns the credentials of the foreign process connected to a socket.

  @param sock: Unix socket
  @rtype: tuple; (number, number, number)
  @return: The PID, UID and GID of the connected foreign process.

  """
  peercred = sock.getsockopt(socket.SOL_SOCKET, IN.SO_PEERCRED,
                             _STRUCT_UCRED_SIZE)
  return struct.unpack(_STRUCT_UCRED, peercred)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def IsValidInterface(ifname):
  """Validate an interface name.

  @type ifname: string
  @param ifname: Name of the network interface
  @return: boolean indicating whether the interface name is valid or not.

  """
  return os.path.exists(utils.PathJoin("/sys/class/net", ifname))


def GetInterfaceIpAddresses(ifname):
  """Returns the IP addresses associated to the interface.

  @type ifname: string
  @param ifname: Name of the network interface
  @return: A dict having for keys the IP version (either
           L{constants.IP4_VERSION} or L{constants.IP6_VERSION}) and for
           values the lists of IP addresses of the respective version
           associated to the interface

  """
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "-o", "addr", "show",
                         ifname])

  if result.failed:
    logging.error("Error running the ip command while getting the IP"
                  " addresses of %s", ifname)
    return None

  return _GetIpAddressesFromIpOutput(result.output)


133
134
def GetHostname(name=None, family=None):
  """Returns a Hostname object.
135

136
137
138
139
140
141
  @type name: str
  @param name: hostname or None
  @type family: int
  @param family: AF_INET | AF_INET6 | None
  @rtype: L{Hostname}
  @return: Hostname object
142
  @raise errors.OpPrereqError: in case of errors in resolving
143
144

  """
145
  try:
146
    return Hostname(name=name, family=family)
147
148
149
150
151
  except errors.ResolverError, err:
    raise errors.OpPrereqError("The given name (%s) does not resolve: %s" %
                               (err[0], err[2]), errors.ECODE_RESOLVER)


152
153
class Hostname:
  """Class implementing resolver and hostname functionality.
154
155

  """
156
157
  _VALID_NAME_RE = re.compile("^[a-z0-9._-]{1,255}$")

158
  def __init__(self, name=None, family=None):
159
160
    """Initialize the host name object.

161
162
163
164
165
166
    If the name argument is None, it will use this system's name.

    @type family: int
    @param family: AF_INET | AF_INET6 | None
    @type name: str
    @param name: hostname or None
167
168

    """
169
    self.name = self.GetNormalizedName(self.GetFqdn(name))
170
    self.ip = self.GetIP(self.name, family=family)
171

172
173
174
175
176
177
178
  @classmethod
  def GetSysName(cls):
    """Legacy method the get the current system's name.

    """
    return cls.GetFqdn()

179
  @staticmethod
180
181
182
183
  def GetFqdn(hostname=None):
    """Return fqdn.

    If hostname is None the system's fqdn is returned.
184

185
186
187
188
    @type hostname: str
    @param hostname: name to be fqdn'ed
    @rtype: str
    @return: fqdn of given name, if it exists, unmodified name otherwise
189
190

    """
191
192
193
194
    if hostname is None:
      return socket.getfqdn()
    else:
      return socket.getfqdn(hostname)
195
196

  @staticmethod
197
198
199
200
  def GetIP(hostname, family=None):
    """Return IP address of given hostname.

    Supports both IPv4 and IPv6.
201
202
203

    @type hostname: str
    @param hostname: hostname to look up
204
205
206
207
    @type family: int
    @param family: AF_INET | AF_INET6 | None
    @rtype: str
    @return: IP address
208
209
210
211
    @raise errors.ResolverError: in case of errors in resolving

    """
    try:
212
213
214
      if family in (socket.AF_INET, socket.AF_INET6):
        result = socket.getaddrinfo(hostname, None, family)
      else:
Manuel Franceschini's avatar
Manuel Franceschini committed
215
        result = socket.getaddrinfo(hostname, None)
216
217
218
219
220
    except (socket.gaierror, socket.herror, socket.error), err:
      # hostname not found in DNS, or other socket exception in the
      # (code, description format)
      raise errors.ResolverError(hostname, err.args[0], err.args[1])

221
222
223
    # getaddrinfo() returns a list of 5-tupes (family, socktype, proto,
    # canonname, sockaddr). We return the first tuple's first address in
    # sockaddr
224
225
226
227
    try:
      return result[0][4][0]
    except IndexError, err:
      raise errors.ResolverError("Unknown error in getaddrinfo(): %s" % err)
228

229
230
  @classmethod
  def GetNormalizedName(cls, hostname):
231
232
233
234
235
236
237
238
    """Validate and normalize the given hostname.

    @attention: the validation is a bit more relaxed than the standards
        require; most importantly, we allow underscores in names
    @raise errors.OpPrereqError: when the name is not valid

    """
    hostname = hostname.lower()
239
    if (not cls._VALID_NAME_RE.match(hostname) or
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
        # double-dots, meaning empty label
        ".." in hostname or
        # empty initial label
        hostname.startswith(".")):
      raise errors.OpPrereqError("Invalid hostname '%s'" % hostname,
                                 errors.ECODE_INVAL)
    if hostname.endswith("."):
      hostname = hostname.rstrip(".")
    return hostname


def TcpPing(target, port, timeout=10, live_port_needed=False, source=None):
  """Simple ping implementation using TCP connect(2).

  Check if the given IP is reachable by doing attempting a TCP connect
  to it.

  @type target: str
258
  @param target: the IP to ping
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  @type port: int
  @param port: the port to connect to
  @type timeout: int
  @param timeout: the timeout on the connection attempt
  @type live_port_needed: boolean
  @param live_port_needed: whether a closed port will cause the
      function to return failure, as if there was a timeout
  @type source: str or None
  @param source: if specified, will cause the connect to be made
      from this specific source address; failures to bind other
      than C{EADDRNOTAVAIL} will be ignored

  """
  try:
273
    family = IPAddress.GetAddressFamily(target)
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  except errors.GenericError:
    return False

  sock = socket.socket(family, socket.SOCK_STREAM)
  success = False

  if source is not None:
    try:
      sock.bind((source, 0))
    except socket.error, (errcode, _):
      if errcode == errno.EADDRNOTAVAIL:
        success = False

  sock.settimeout(timeout)

  try:
    sock.connect((target, port))
    sock.close()
    success = True
  except socket.timeout:
    success = False
  except socket.error, (errcode, _):
    success = (not live_port_needed) and (errcode == errno.ECONNREFUSED)

  return success


def GetDaemonPort(daemon_name):
  """Get the daemon port for this cluster.

  Note that this routine does not read a ganeti-specific file, but
  instead uses C{socket.getservbyname} to allow pre-customization of
  this parameter outside of Ganeti.

  @type daemon_name: string
  @param daemon_name: daemon name (in constants.DAEMONS_PORTS)
  @rtype: int

  """
  if daemon_name not in constants.DAEMONS_PORTS:
    raise errors.ProgrammerError("Unknown daemon: %s" % daemon_name)

  (proto, default_port) = constants.DAEMONS_PORTS[daemon_name]
  try:
    port = socket.getservbyname(daemon_name, proto)
  except socket.error:
    port = default_port

  return port
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419


class IPAddress(object):
  """Class that represents an IP address.

  """
  iplen = 0
  family = None
  loopback_cidr = None

  @staticmethod
  def _GetIPIntFromString(address):
    """Abstract method to please pylint.

    """
    raise NotImplementedError

  @classmethod
  def IsValid(cls, address):
    """Validate a IP address.

    @type address: str
    @param address: IP address to be checked
    @rtype: bool
    @return: True if valid, False otherwise

    """
    if cls.family is None:
      try:
        family = cls.GetAddressFamily(address)
      except errors.IPAddressError:
        return False
    else:
      family = cls.family

    try:
      socket.inet_pton(family, address)
      return True
    except socket.error:
      return False

  @classmethod
  def Own(cls, address):
    """Check if the current host has the the given IP address.

    This is done by trying to bind the given address. We return True if we
    succeed or false if a socket.error is raised.

    @type address: str
    @param address: IP address to be checked
    @rtype: bool
    @return: True if we own the address, False otherwise

    """
    if cls.family is None:
      try:
        family = cls.GetAddressFamily(address)
      except errors.IPAddressError:
        return False
    else:
      family = cls.family

    s = socket.socket(family, socket.SOCK_DGRAM)
    success = False
    try:
      try:
        s.bind((address, 0))
        success = True
      except socket.error:
        success = False
    finally:
      s.close()
    return success

  @classmethod
  def InNetwork(cls, cidr, address):
    """Determine whether an address is within a network.

    @type cidr: string
    @param cidr: Network in CIDR notation, e.g. '192.0.2.0/24', '2001:db8::/64'
    @type address: str
    @param address: IP address
    @rtype: bool
    @return: True if address is in cidr, False otherwise

    """
    address_int = cls._GetIPIntFromString(address)
    subnet = cidr.split("/")
    assert len(subnet) == 2
    try:
      prefix = int(subnet[1])
    except ValueError:
      return False

    assert 0 <= prefix <= cls.iplen
    target_int = cls._GetIPIntFromString(subnet[0])
    # Convert prefix netmask to integer value of netmask
Michael Hanselmann's avatar
Michael Hanselmann committed
420
    netmask_int = (2 ** cls.iplen) - 1 ^ ((2 ** cls.iplen) - 1 >> prefix)
421
    # Calculate hostmask
Michael Hanselmann's avatar
Michael Hanselmann committed
422
    hostmask_int = netmask_int ^ (2 ** cls.iplen) - 1
423
424
425
426
427
428
429
430
431
432
433
434
435
436
    # Calculate network address by and'ing netmask
    network_int = target_int & netmask_int
    # Calculate broadcast address by or'ing hostmask
    broadcast_int = target_int | hostmask_int

    return network_int <= address_int <= broadcast_int

  @staticmethod
  def GetAddressFamily(address):
    """Get the address family of the given address.

    @type address: str
    @param address: ip address whose family will be returned
    @rtype: int
437
    @return: C{socket.AF_INET} or C{socket.AF_INET6}
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
    @raise errors.GenericError: for invalid addresses

    """
    try:
      return IP4Address(address).family
    except errors.IPAddressError:
      pass

    try:
      return IP6Address(address).family
    except errors.IPAddressError:
      pass

    raise errors.IPAddressError("Invalid address '%s'" % address)

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
  @staticmethod
  def GetVersionFromAddressFamily(family):
    """Convert an IP address family to the corresponding IP version.

    @type family: int
    @param family: IP address family, one of socket.AF_INET or socket.AF_INET6
    @return: an int containing the IP version, one of L{constants.IP4_VERSION}
             or L{constants.IP6_VERSION}
    @raise errors.ProgrammerError: for unknown families

    """
    if family == socket.AF_INET:
      return constants.IP4_VERSION
    elif family == socket.AF_INET6:
      return constants.IP6_VERSION

    raise errors.ProgrammerError("%s is not a valid IP address family" % family)

  @staticmethod
  def GetAddressFamilyFromVersion(version):
    """Convert an IP version to the corresponding IP address family.

    @type version: int
    @param version: IP version, one of L{constants.IP4_VERSION} or
                    L{constants.IP6_VERSION}
    @return: an int containing the IP address family, one of C{socket.AF_INET}
             or C{socket.AF_INET6}
    @raise errors.ProgrammerError: for unknown IP versions

    """
    if version == constants.IP4_VERSION:
      return socket.AF_INET
    elif version == constants.IP6_VERSION:
      return socket.AF_INET6

    raise errors.ProgrammerError("%s is not a valid IP version" % version)

490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
  @classmethod
  def IsLoopback(cls, address):
    """Determine whether it is a loopback address.

    @type address: str
    @param address: IP address to be checked
    @rtype: bool
    @return: True if loopback, False otherwise

    """
    try:
      return cls.InNetwork(cls.loopback_cidr, address)
    except errors.IPAddressError:
      return False


class IP4Address(IPAddress):
  """IPv4 address class.

  """
  iplen = 32
  family = socket.AF_INET
  loopback_cidr = "127.0.0.0/8"

  def __init__(self, address):
    """Constructor for IPv4 address.

    @type address: str
    @param address: IP address
    @raises errors.IPAddressError: if address invalid

    """
    IPAddress.__init__(self)
    if not self.IsValid(address):
      raise errors.IPAddressError("IPv4 Address %s invalid" % address)

    self.address = address

  @staticmethod
  def _GetIPIntFromString(address):
    """Get integer value of IPv4 address.

    @type address: str
533
    @param address: IPv6 address
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
    @rtype: int
    @return: integer value of given IP address

    """
    address_int = 0
    parts = address.split(".")
    assert len(parts) == 4
    for part in parts:
      address_int = (address_int << 8) | int(part)

    return address_int


class IP6Address(IPAddress):
  """IPv6 address class.

  """
  iplen = 128
  family = socket.AF_INET6
  loopback_cidr = "::1/128"

  def __init__(self, address):
    """Constructor for IPv6 address.

    @type address: str
    @param address: IP address
    @raises errors.IPAddressError: if address invalid

    """
    IPAddress.__init__(self)
    if not self.IsValid(address):
      raise errors.IPAddressError("IPv6 Address [%s] invalid" % address)
    self.address = address

  @staticmethod
  def _GetIPIntFromString(address):
    """Get integer value of IPv6 address.

    @type address: str
573
    @param address: IPv6 address
574
575
576
577
578
579
580
581
582
583
    @rtype: int
    @return: integer value of given IP address

    """
    doublecolons = address.count("::")
    assert not doublecolons > 1
    if doublecolons == 1:
      # We have a shorthand address, expand it
      parts = []
      twoparts = address.split("::")
Iustin Pop's avatar
Iustin Pop committed
584
585
      sep = len(twoparts[0].split(":")) + len(twoparts[1].split(":"))
      parts = twoparts[0].split(":")
586
      [parts.append("0") for _ in range(8 - sep)]
Iustin Pop's avatar
Iustin Pop committed
587
      parts += twoparts[1].split(":")
588
589
590
591
592
    else:
      parts = address.split(":")

    address_int = 0
    for part in parts:
Iustin Pop's avatar
Iustin Pop committed
593
      address_int = (address_int << 16) + int(part or "0", 16)
594
595

    return address_int
596

597

598
def FormatAddress(address, family=None):
599
600
601
602
  """Format a socket address

  @type address: family specific (usually tuple)
  @param address: address, as reported by this class
603
604
  @type family: integer
  @param family: socket family (one of socket.AF_*) or None
605
606

  """
607
608
609
610
611
612
  if family is None:
    try:
      family = IPAddress.GetAddressFamily(address[0])
    except errors.IPAddressError:
      raise errors.ParameterError(address)

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
  if family == socket.AF_UNIX and len(address) == 3:
    return "pid=%s, uid=%s, gid=%s" % address

  if family in (socket.AF_INET, socket.AF_INET6) and len(address) == 2:
    host, port = address
    if family == socket.AF_INET6:
      res = "[%s]" % host
    else:
      res = host

    if port is not None:
      res += ":%s" % port

    return res

  raise errors.ParameterError(family, address)