Add upnp.h to tincd SOURCES.
[tinc] / gui / tinc-gui
index 89a8afa..0a6370a 100755 (executable)
@@ -1,7 +1,8 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 
 # tinc-gui -- GUI for controlling a running tincd
-# Copyright (C) 2009-2012 Guus Sliepen <guus@tinc-vpn.org>
+# Copyright (C) 2009-2014 Guus Sliepen <guus@tinc-vpn.org>
+#                    2014 Dennis Joachimsthaler <dennis@efjot.de>
 #
 # 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
@@ -27,7 +28,7 @@ import time
 from wx.lib.mixins.listctrl import ColumnSorterMixin
 from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
 
-if platform.system == 'Windows':
+if platform.system() == 'Windows':
        import _winreg
 
 # Classes to interface with a running tinc daemon
@@ -77,8 +78,8 @@ class Edge:
                self.to = args[1]
                self.address = args[2]
                self.port = args[4]
-               self.options = int(args[5], 16)
-               self.weight = int(args[6])
+               self.options = int(args[-2], 16)
+               self.weight = int(args[-1])
 
 class Subnet:
        def parse(self, args):
@@ -111,11 +112,32 @@ class VPN:
        piddir = '/var/run/'
 
        def connect(self):
+               # read the pidfile
                f = open(self.pidfile)
                info = string.split(f.readline())
                f.close()
-               s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-               s.connect((info[2], int(info[4])))
+
+               # check if there is a UNIX socket as well
+               if self.pidfile.endswith(".pid"):
+                       unixfile = self.pidfile.replace(".pid", ".socket");
+               else:
+                       unixfile = self.pidfile + ".socket";
+
+               if os.path.exists(unixfile):
+                       # use it if it exists
+                       print(unixfile + " exists!");
+                       s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+                       s.connect(unixfile)
+               else:
+                       # otherwise connect via TCP
+                       print(unixfile + " does not exist.");
+                       if ':' in info[2]:
+                               af = socket.AF_INET6
+                       else:
+                               af = socket.AF_INET
+                       s = socket.socket(af, socket.SOCK_STREAM)
+                       s.connect((info[2], int(info[4])))
+
                self.sf = s.makefile()
                s.close()
                hello = string.split(self.sf.readline())
@@ -170,6 +192,8 @@ class VPN:
                                subnet.parse(resp[2:])
                                subnet.visited = True
                                self.subnets[(resp[2], resp[3])] = subnet
+                               if subnet.owner == "(broadcast)":
+                                       continue
                                self.nodes[subnet.owner].subnets[resp[2]] = subnet
                        elif resp[1] == '6':
                                if len(resp) < 9:
@@ -216,10 +240,16 @@ class VPN:
                return int(resp[2])
 
        def __init__(self, netname = None, pidfile = None):
-               if platform.system == 'Windows':
+               if platform.system() == 'Windows':
+                       sam = _winreg.KEY_READ
+                       if platform.machine().endswith('64'):
+                               sam = sam | _winreg.KEY_WOW64_64KEY
                        try:
-                               reg = _winreg.ConnectRegistry(None, HKEY_LOCAL_MACHINE)
-                               key = _winreg.OpenKey(reg, "SOFTWARE\\tinc")
+                               reg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
+                               try:
+                                       key = _winreg.OpenKey(reg, "SOFTWARE\\tinc", 0, sam)
+                               except WindowsError:
+                                       key = _winreg.OpenKey(reg, "SOFTWARE\\Wow6432Node\\tinc", 0, sam)
                                VPN.confdir = _winreg.QueryValue(key, None)
                        except WindowsError:
                                pass
@@ -235,7 +265,7 @@ class VPN:
                if pidfile != None:
                        self.pidfile = pidfile
                else:
-                       if platform.system == 'Windows':
+                       if platform.system() == 'Windows':
                                self.pidfile = os.path.join(self.confbase, 'pid')
                        else:
                                if netname:
@@ -287,7 +317,7 @@ class SuperListCtrl(wx.ListCtrl, ColumnSorterMixin, ListCtrlAutoWidthMixin):
     def __init__(self, parent, style):
         wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES)
        ListCtrlAutoWidthMixin.__init__(self)
-        ColumnSorterMixin.__init__(self, 14)
+        ColumnSorterMixin.__init__(self, 16)
 
     def GetListCtrl(self):
         return self
@@ -300,12 +330,12 @@ class SettingsPage(wx.Panel):
        def __init__(self, parent, id):
                wx.Panel.__init__(self, parent, id)
                grid = wx.FlexGridSizer(cols = 2)
-               grid.AddGrowableCol(0, 1)
+               grid.AddGrowableCol(1, 1)
 
                namelabel = wx.StaticText(self, -1, 'Name:')
                self.name = wx.TextCtrl(self, -1, vpn.name)
                grid.Add(namelabel)
-               grid.Add(self.name)
+               grid.Add(self.name, 1, wx.EXPAND)
 
                portlabel = wx.StaticText(self, -1, 'Port:')
                self.port = wx.TextCtrl(self, -1, vpn.port)
@@ -328,7 +358,7 @@ class SettingsPage(wx.Panel):
 class ConnectionsPage(wx.Panel):
        def __init__(self, parent, id):
                wx.Panel.__init__(self, parent, id)
-               self.list = wx.ListCtrl(self, id, style=wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES)
+               self.list = SuperListCtrl(self, id)
                self.list.InsertColumn(0, 'Name')
                self.list.InsertColumn(1, 'Address')
                self.list.InsertColumn(2, 'Port')
@@ -358,6 +388,7 @@ class ConnectionsPage(wx.Panel):
                self.PopupMenu(self.ContextMenu(self.list.itemDataMap[event.GetIndex()]), event.GetPosition())
 
        def refresh(self):
+               sortstate = self.list.GetSortState()
                self.list.itemDataMap = {}
                i = 0
 
@@ -372,11 +403,13 @@ class ConnectionsPage(wx.Panel):
                        self.list.SetStringItem(i, 4, str(connection.weight))
                        self.list.itemDataMap[i] = (connection.name, connection.address, connection.port, connection.options, connection.weight)
                        self.list.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.OnContext)
+                       self.list.SetItemData(i, i)
                        i += 1
 
                while self.list.GetItemCount() > i:
                        self.list.DeleteItem(self.list.GetItemCount() - 1)
 
+               self.list.SortListItems(sortstate[0], sortstate[1])
 
 class NodesPage(wx.Panel):
        def __init__(self, parent, id):
@@ -405,6 +438,7 @@ class NodesPage(wx.Panel):
                self.refresh()
 
        def refresh(self):
+               sortstate = self.list.GetSortState()
                self.list.itemDataMap = {}
                i = 0
 
@@ -432,17 +466,19 @@ class NodesPage(wx.Panel):
                        else:
                                since = "never"
                        self.list.SetStringItem(i, 15, since)
-                       self.list.itemDataMap[i] = (node.name, node.address, node.port, node.cipher, node.digest, node.maclength, node.compression, node.options, node.status, node.nexthop, node.via, node.distance, node.pmtu, node.minmtu, node.maxmtu)
+                       self.list.itemDataMap[i] = (node.name, node.address, node.port, node.cipher, node.digest, node.maclength, node.compression, node.options, node.status, node.nexthop, node.via, node.distance, node.pmtu, node.minmtu, node.maxmtu, since)
                        self.list.SetItemData(i, i)
                        i += 1
 
                while self.list.GetItemCount() > i:
                        self.list.DeleteItem(self.list.GetItemCount() - 1)
 
+               self.list.SortListItems(sortstate[0], sortstate[1])
+
 class EdgesPage(wx.Panel):
        def __init__(self, parent, id):
                wx.Panel.__init__(self, parent, id)
-               self.list = wx.ListCtrl(self, id, style=wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES)
+               self.list = SuperListCtrl(self, id)
                self.list.InsertColumn(0, 'From')
                self.list.InsertColumn(1, 'To')
                self.list.InsertColumn(2, 'Address')
@@ -456,6 +492,7 @@ class EdgesPage(wx.Panel):
                self.refresh()
 
        def refresh(self):
+               sortstate = self.list.GetSortState()
                self.list.itemDataMap = {}
                i = 0
 
@@ -470,11 +507,14 @@ class EdgesPage(wx.Panel):
                        self.list.SetStringItem(i, 4, format(edge.options, "x"))
                        self.list.SetStringItem(i, 5, str(edge.weight))
                        self.list.itemDataMap[i] = (edge.fr, edge.to, edge.address, edge.port, edge.options, edge.weight)
+                       self.list.SetItemData(i, i)
                        i += 1
 
                while self.list.GetItemCount() > i:
                        self.list.DeleteItem(self.list.GetItemCount() - 1)
 
+               self.list.SortListItems(sortstate[0], sortstate[1])
+
 class SubnetsPage(wx.Panel):
        def __init__(self, parent, id):
                wx.Panel.__init__(self, parent, id)
@@ -488,6 +528,7 @@ class SubnetsPage(wx.Panel):
                self.refresh()
 
        def refresh(self):
+               sortstate = self.list.GetSortState()
                self.list.itemDataMap = {}
                i = 0
 
@@ -496,14 +537,17 @@ class SubnetsPage(wx.Panel):
                                self.list.InsertStringItem(i, subnet.address + '/' + subnet.prefixlen)
                        else:
                                self.list.SetStringItem(i, 0, subnet.address + '/' + subnet.prefixlen)
-                       self.list.SetStringItem(i, 1, subnet.weight)
+                       self.list.SetStringItem(i, 1, str(subnet.weight))
                        self.list.SetStringItem(i, 2, subnet.owner)
                        self.list.itemDataMap[i] = (subnet.address + '/' + subnet.prefixlen, subnet.weight, subnet.owner)
-                       i = i + 1
+                       self.list.SetItemData(i, i)
+                       i += 1
 
                while self.list.GetItemCount() > i:
                        self.list.DeleteItem(self.list.GetItemCount() - 1)
 
+               self.list.SortListItems(sortstate[0], sortstate[1])
+
 class StatusPage(wx.Panel):
        def __init__(self, parent, id):
                wx.Panel.__init__(self, parent, id)