Package elisa :: Package core :: Package utils :: Module network

Source Code for Module elisa.core.utils.network

  1  # -*- coding: utf-8 -*- 
  2  # Moovida - Home multimedia server 
  3  # Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Moovida with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Moovida is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Moovida" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  """ 
 18  Functions to retrieve hostname and IP address of the machine running Elisa 
 19  """ 
 20   
 21  __maintainer__ = 'Philippe Normand <philippe@fluendo.com>' 
 22   
 23  import socket 
 24  import struct 
 25  import platform 
 26   
27 -def _get_host_address_win():
28 """ 29 Retrieve host address for Win32 platform 30 31 @rtype: string 32 """ 33 s = socket.gethostname() 34 return socket.gethostbyname(s)
35
36 -def _get_host_address_linux():
37 """ 38 Retrieve host address for Linux platform 39 40 @rtype: string 41 """ 42 route_file = '/proc/net/route' 43 route = open(route_file) 44 host_address = '127.0.0.1' 45 if route: 46 tmp = route.readline() #skip first line 47 while tmp != '': 48 tmp = route.readline() 49 l = tmp.split('\t') 50 if len(l) > 2: 51 if l[2] != '00000000': #default gateway... 52 host_address = get_ip_address(l[0]) 53 break 54 55 route.close() 56 return host_address
57
58 -def get_host_address():
59 """ 60 Retrieve current host address for Win32 and Linux platforms 61 62 @raises ValueError: if current system's platform is not windows or linux 63 @rtype: string 64 """ 65 platform_type = platform.system().lower() 66 if platform_type == "windows": 67 return _get_host_address_win() 68 elif platform_type == "linux": 69 return _get_host_address_linux() 70 else: 71 raise ValueError("Unsupported platform")
72
73 -def _get_linux_ip_address(ifname):
74 """ 75 Retrieve IP address of the given network interface, on a Linux platform 76 77 @param ifname: network interface name 78 @type ifname: string 79 @rtype: string 80 """ 81 import fcntl 82 83 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 84 ip_address = socket.inet_ntoa(fcntl.ioctl( 85 s.fileno(), 86 0x8915, # SIOCGIFADDR 87 struct.pack('256s', ifname[:15]) 88 )[20:24]) 89 return ip_address
90
91 -def _get_win32_ipaddress(ifname):
92 """ 93 Retrieve IP address of the given network interface, on a Windows platform 94 95 @param ifname: network interface name 96 @type ifname: string 97 @rtype: string 98 """ 99 # TODO: implement me 100 return ""
101
102 -def _get_win32_default_iface():
103 """ 104 Retrieve name of the network interface connected to the default 105 gateway, on a Windows platform. 106 107 @rtype: string 108 """ 109 # TODO: implement me 110 return "eth0"
111
112 -def _get_linux_default_iface():
113 """ 114 Retrieve name of the network interface connected to the default 115 gateway, on a Linux platform. 116 117 @rtype: string 118 """ 119 route_file = '/proc/net/route' 120 route = open(route_file) 121 iface = 'eth0' 122 if route: 123 tmp = route.readline() #skip first line 124 while tmp != '': 125 tmp = route.readline() 126 l = tmp.split('\t') 127 if len(l) > 2: 128 if l[2] == '00000000': #default gateway... 129 iface = l[0] 130 break 131 route.close() 132 return iface
133 134
135 -def get_ip_address(ifname=None):
136 """ 137 Retrieve IP address of the given network interface, on a Windows platform 138 139 @raises ValueError: if current system's platform is not windows or linux 140 @param ifname: network interface name 141 @type ifname: string 142 @rtype: string 143 """ 144 platform_type = platform.system().lower() 145 if platform_type == "windows": 146 if ifname is None: 147 ifname = _get_win32_default_iface() 148 return _get_win32_ip_address(ifname) 149 elif platform_type == "linux": 150 if ifname is None: 151 ifname = _get_linux_default_iface() 152 return _get_linux_ip_address(ifname) 153 else: 154 raise ValueError("Unsupported platform")
155