Package zephir :: Package monitor :: Package agents :: Module ifconfig
[frames] | no frames]

Source Code for Module zephir.monitor.agents.ifconfig

  1  # -*- coding: UTF-8 -*- 
  2  # 
  3  # Copyright (C) 2005, TUBITAK/UEKAE 
  4  # 
  5  # This program is free software; you can redistribute it and/or modify it 
  6  # under the terms of the GNU General Public License as published by the 
  7  # Free Software Foundation; either version 2 of the License, or (at your 
  8  # option) any later version. Please read the COPYING file. 
  9  # 
 10   
 11  import array 
 12  import fcntl 
 13  import struct 
 14  import socket 
 15   
 16   
17 -class ifconfig:
18 """ ioctl stuff """ 19 20 IFNAMSIZ = 16 # interface name size 21 22 # From <bits/ioctls.h> 23 24 SIOCGIFADDR = 0x8915 # get PA address 25 SIOCGIFBRDADDR = 0x8919 # get broadcast PA address 26 SIOCGIFCONF = 0x8912 # get iface list 27 SIOCGIFFLAGS = 0x8913 # get flags 28 SIOCGIFMTU = 0x8921 # get MTU size 29 SIOCGIFNETMASK = 0x891b # get network PA mask 30 SIOCSIFADDR = 0x8916 # set PA address 31 SIOCSIFBRDADDR = 0x891a # set broadcast PA address 32 SIOCSIFFLAGS = 0x8914 # set flags 33 SIOCSIFMTU = 0x8922 # set MTU size 34 SIOCSIFNETMASK = 0x891c # set network PA mask 35 36 # From <net/if.h> 37 38 IFF_UP = 0x1 # Interface is up. 39 IFF_BROADCAST = 0x2 # Broadcast address valid. 40 IFF_DEBUG = 0x4 # Turn on debugging. 41 IFF_LOOPBACK = 0x8 # Is a loopback net. 42 IFF_POINTOPOINT = 0x10 # Interface is point-to-point link. 43 IFF_NOTRAILERS = 0x20 # Avoid use of trailers. 44 IFF_RUNNING = 0x40 # Resources allocated. 45 IFF_NOARP = 0x80 # No address resolution protocol. 46 IFF_PROMISC = 0x100 # Receive all packets. 47 IFF_ALLMULTI = 0x200 # Receive all multicast packets. 48 IFF_MASTER = 0x400 # Master of a load balancer. 49 IFF_SLAVE = 0x800 # Slave of a load balancer. 50 IFF_MULTICAST = 0x1000 # Supports multicast. 51 IFF_PORTSEL = 0x2000 # Can set media type. 52 IFF_AUTOMEDIA = 0x4000 # Auto media select active. 53 54
55 - def __init__(self):
56 # create a socket to communicate with system 57 self.sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
58
59 - def _ioctl(self, func, args):
60 return fcntl.ioctl(self.sockfd.fileno(), func, args)
61
62 - def _call(self, ifname, func, ip = None):
63 64 if ip is None: 65 data = (ifname + '\0'*32)[:32] 66 else: 67 ifreq = (ifname + '\0' * self.IFNAMSIZ)[:self.IFNAMSIZ] 68 data = struct.pack("16si4s10x", ifreq, socket.AF_INET, socket.inet_aton(ip)) 69 70 try: 71 result = self._ioctl(func, data) 72 except IOError: 73 return None 74 75 return result
76
77 - def getInterfaceList(self):
78 """ Get all interface names in a list """ 79 # get interface list 80 buffer = array.array('c', '\0' * 1024) 81 ifconf = struct.pack("iP", buffer.buffer_info()[1], buffer.buffer_info()[0]) 82 result = self._ioctl(self.SIOCGIFCONF, ifconf) 83 84 # loop over interface names 85 iflist = [] 86 size, ptr = struct.unpack("iP", result) 87 for idx in range(0, size, 32): 88 ifconf = buffer.tostring()[idx:idx+32] 89 name, dummy = struct.unpack("16s16s", ifconf) 90 name, dummy = name.split('\0', 1) 91 iflist.append(name) 92 93 return iflist
94
95 - def getAddr(self, ifname):
96 """ Get the inet addr for an interface """ 97 result = self._call(ifname, self.SIOCGIFADDR) 98 return socket.inet_ntoa(result[20:24])
99
100 - def getNetmask(self, ifname):
101 """ Get the netmask for an interface """ 102 result = self._call(ifname, self.SIOCGIFNETMASK) 103 return socket.inet_ntoa(result[20:24])
104
105 - def getBroadcast(self, ifname):
106 """ Get the broadcast addr for an interface """ 107 result = self._call(ifname, self.SIOCGIFBRDADDR) 108 return socket.inet_ntoa(result[20:24])
109
110 - def getStatus(self, ifname):
111 """ Check whether interface is UP """ 112 result = self._call(ifname, self.SIOCGIFFLAGS) 113 flags, = struct.unpack('H', result[16:18]) 114 return (flags & self.IFF_UP) != 0
115
116 - def getMTU(self, ifname):
117 """ Get the MTU size of an interface """ 118 data = self._call(ifname, self.SIOCGIFMTU) 119 mtu = struct.unpack("16si12x", data)[1] 120 return mtu
121