""" This file is part of Mokonnect. Mokonnect is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License Version 3 as published by the Free Software Foundation. Mokonnect 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 Mokonnect. If not, see . """ # # mkdev_usbnet.py # mokonnect device usb network # import mkbase import socket import struct import fcntl class IFacePanel(mkbase.MKPanel): def __init__(self,win): mkbase.MKPanel.__init__(self,win) self.gui = { "type": "frame", "label": "Interface Configuration", "align": (-1.0,0.0), "content": { "type": "box", "content": [ {"type":"check","label":"Default","config_link":"default"}, {"type":"check","label":"Use DHCP","config_link":"dhcp","panel_link":"ipconfig","panel_link_reverse":None}, ] } } self.config = {"default":True,"dhcp": True} class IPConfigPanel(mkbase.MKPanel): def __init__(self,win): mkbase.MKPanel.__init__(self,win) self.gui = { "name": "ipconfig", "type": "frame", "label": "IP Configuration", "align": (-1.0,0.0), "content": { "type": "table", "cols": 3, "content": [ [ {"type":"label","label":"IP Address"}, {"type":"box","align":(-1.0,0.0),"weight":(1,1)}, {"type":"entry","config_link":"ip"} ], [ {"type":"label","label":"Netmask"}, {"type":"box","align":(-1.0,0.0),"weight":(1,1)}, {"type":"entry","config_link":"netmask"} ], [ {"type":"label","label":"Gateway"}, {"type":"box","align":(-1.0,0.0),"weight":(1,1)}, {"type":"entry","config_link":"gateway"} ], [ {"type":"label","label":"DNS Server"}, {"type":"box","align":(-1.0,0.0),"weight":(1,1)}, {"type":"entry","config_link":"nameserver"} ], ] } } self.config = {"ip": "192.168.0.202","netmask":"255.255.255.0","gateway":"192.168.0.200","nameserver":"208.67.222.222"} class USBNetDevice(mkbase.MKDevice): def __init__(self,win,pager,qdbus): mkbase.MKDevice.__init__(self,win) self.panels = [IFacePanel(win),IPConfigPanel(win)] self.name = "USB Network" self.bus = qdbus self.powered = False self.active_timer = None self.powered_down_once = False self.timeout_connect = 10 def _ConfigStatic(self,ifname): ifname = str(ifname) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) iface = struct.pack('256s', ifname[:15]) # Setting IP info = fcntl.ioctl(sock.fileno(), 0x8915, iface) # SIOCGIFADDR info = info[:20] + socket.inet_aton(self.panels[1].config["ip"]) + info[24:] info = fcntl.ioctl(sock.fileno(), 0x8916, info) # SIOCSIFADDR self.log("IP: %s" % self.panels[1].config["ip"]) # Setting Netmask info = fcntl.ioctl(sock.fileno(), 0x891b, iface) # SIOCGIFNETMASK info = info[:20] + socket.inet_aton(self.panels[1].config["netmask"]) + info[24:] info = fcntl.ioctl(sock.fileno(), 0x891c, info) # SIOCSIFNETMASK self.log("Netmask: %s" % self.panels[1].config["netmask"]) # Setting broadcast bcast = self._GetBroadcast() info = fcntl.ioctl(sock.fileno(), 0x8919, iface) # SIOCGIFBRDADDR info = info[:20] + socket.inet_aton(bcast) + info[24:] info = fcntl.ioctl(sock.fileno(), 0x891a, info) # SIOCSIFBRDADDR # gateway for show self.log("Gateway: %s" % self.panels[1].config["gateway"]) # nameserver append to the /etc/resolv.conf fh = file("/etc/resolv.conf","at") fh.write("nameserver %s\n" % self.panels[1].config["nameserver"]) fh.close() self.log("Nameserver: %s" % self.panels[1].config["nameserver"]) # done closing sock.close() self.log("Done!") self.log("__DONE__") def _GetBroadcast(self): ipb = socket.inet_aton(self.panels[1].config["ip"]) maskb = socket.inet_aton(self.panels[1].config["netmask"]) ipl = struct.unpack("!L",ipb)[0] maskl = struct.unpack("!L",maskb)[0] rmaskl = 0xFFFFFFFF & (~maskl) bcastl = ipl | rmaskl bcastb = struct.pack("!L",bcastl) return socket.inet_ntoa(bcastb) def _TimePassed(self,*args,**kargs): self.log("Timeout reached, connection failed...") self.log("__DONE__") self.active_timer = None return False def DbusSignal(self,*args,**kargs): if kargs["signal"] == "PropertyChanged": #print "Property changed! %s %s" % (str(args),str(kargs)) if args[0] == "Connections": if self.sent_done: return # find the connection belonging to this device... dname = self.device.iface.object_path for conpath in args[1]: if dname in conpath: # found our connecion # cancel timeout if self.active_timer: self.active_timer.delete() self.active_timer = None # print con details and flag done con = self.bus["connman"][conpath]["Connection"] cprop = con.GetProperties() #print cprop self.log("IP: %s" % cprop["IPv4.Address"]) self.log("Netmask: %s" % cprop["IPv4.Netmask"]) self.log("Gateway: %s" % cprop["IPv4.Gateway"]) self.log("Nameserver: %s" % cprop["IPv4.Nameserver"]) self.log("Done!") self.log("__DONE__") self.sent_done = True return if args[0] == "Powered": if args[1] == False: if self.powered_down_once: return self.powered_down_once = True # applying config # TODO: apply the default connection somehow... self.log("Configuring using dhcp...") self.device["IPv4.Method"] = "dhcp" self.active_timer = mkbase.SetTimeout(self.timeout_connect,self._TimePassed) self.device["Powered"] = True #else: # print "debug signal! %s %s" % (str(args),str(kargs)) def _GetDevice(self): self.device = None try: for dpath in self.bus["connman"]["/"]["Manager"]["Devices"]: device = self.bus["connman"][dpath]["Device"] if device["Type"] == "ethernet": if device["Interface"].startswith("usb"): # annoying thing self.device = device return True except: pass return False def Apply(self,log): # find the ethernet device self.powered_down_once = False if not self._GetDevice(): log("Ethernet device was not found in connman.") log("Do you have connman-plugin-ethernet installed?") log("__DONE__") return log("Ethernet device: %s" % self.device["Interface"]) self.sent_done = False self.log = log self.bus["connman"].AddSignalFunction(self.DbusSignal) if self.panels[0].config["dhcp"]: # powering down, setting stuff on callback of powerdown log("Powering down device...") self.device["Powered"] = False else: self.log("Configuring using static details...") self.device["IPv4.Method"] = "static" # connman doesnt support static yet, we fall back to socket configuration... try: self._ConfigStatic(self.device["Interface"]) except Exception,e: self.log("Error configuring static information: %s" % str(e)) self.log("__DONE__") return def PowerOff(self,log): log("Ethernet has no power control.") log("__DONE__") return True def PowerOn(self,log): log("Ethernet has no power control.") return True