Package elisa :: Package core :: Package utils :: Module network
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.utils.network

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