[pkg-fso-commits] [SCM] FSO frameworkd Debian packaging branch, master, updated. milestone4-368-g700ab82
Michael 'Mickey' Lauer
mickey at vanille-media.de
Mon Feb 2 18:51:22 UTC 2009
The following commit has been merged in the master branch:
commit 9d7524a2c72d5488ecfb7dbaeedfca74c182f043
Author: Michael 'Mickey' Lauer <mickey at vanille-media.de>
Date: Sat Nov 22 19:17:29 2008 +0100
onetworkd: connection sharing convenience (WIP, there is no such thing as a Network subsystem...)
diff --git a/etc/dbus-1/system.d/frameworkd.conf b/etc/dbus-1/system.d/frameworkd.conf
index 23a8d64..1211302 100644
--- a/etc/dbus-1/system.d/frameworkd.conf
+++ b/etc/dbus-1/system.d/frameworkd.conf
@@ -8,6 +8,12 @@
<allow receive_sender="org.freesmartphone.Testing"/>
</policy>
<policy user="root">
+ <allow own="org.freesmartphone.onetworkd"/>
+ <allow send_path="/org/freesmartphone/Network"/>
+ <allow send_destination="org.freesmartphone.Network"/>
+ <allow receive_sender="org.freesmartphone.Network"/>
+ </policy>
+ <policy user="root">
<allow own="org.freesmartphone.frameworkd"/>
<allow send_path="/org/freesmartphone/Framework"/>
<allow send_destination="org.freesmartphone.Framework"/>
diff --git a/framework/subsystems/odeviced/helpers.py b/framework/subsystems/onetworkd/helpers.py
similarity index 70%
copy from framework/subsystems/odeviced/helpers.py
copy to framework/subsystems/onetworkd/helpers.py
index e59905d..a85131f 100644
--- a/framework/subsystems/odeviced/helpers.py
+++ b/framework/subsystems/onetworkd/helpers.py
@@ -1,10 +1,21 @@
-DBUS_INTERFACE_PREFIX = "org.freesmartphone.Device"
-DBUS_PATH_PREFIX = "/org/freesmartphone/Device"
-
from string import maketrans
import logging
-logger = logging.getLogger('odeviced')
+logger = logging.getLogger('onetworkd')
+
+#============================================================================#
+def isValidAddress( address ):
+#============================================================================#
+ parts = address.split( '.' )
+ if len( parts ) != 4:
+ return False
+ try:
+ for part in parts:
+ if 0 > int( part ) or 255 < int ( part ):
+ return False
+ return True
+ except ValueError:
+ return False
#============================================================================#
def readFromFile( path ):
diff --git a/framework/subsystems/onetworkd/network.py b/framework/subsystems/onetworkd/network.py
new file mode 100644
index 0000000..1e44943
--- /dev/null
+++ b/framework/subsystems/onetworkd/network.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+"""
+Network
+
+(C) 2008 Michael 'Mickey' Lauer <mlauer at vanille-media.de>
+(C) 2008 Openmoko, Inc.
+GPLv2 or later
+
+Package: onetworkd
+Module: network
+"""
+
+MODULE_NAME = "onetworkd"
+__version__ = "0.0.0"
+
+import gobject
+
+import os
+import socket
+import fcntl
+import struct
+
+import logging
+logger = logging.getLogger( MODULE_NAME )
+
+#============================================================================#
+class Network( dict ):
+#============================================================================#
+ def __init__( self ):
+ gobject.idle_add( self._sync )
+
+ def _sync( self ):
+ # FIXME add listener so that this gets called whenever a change in
+ # interfaces occurs
+ interfaces = os.listdir( "/sys/class/net" )
+ # first pass: remove
+ for interface in self:
+ if interface not in interfaces:
+ logger.debug( "interface %s no longer present -- removing" % interface )
+ del self[interface]
+ # second pass: add
+ for interface in os.listdir( "/sys/class/net" ):
+ if interface not in self:
+ logger.debug( "new interface %s -- adding" % interface )
+ self[interface] = Interface( interface )
+ return False
+
+#============================================================================#
+class Interface( object ):
+#============================================================================#
+ def __init__( self, name ):
+ self._name = name
+
+ def ipAddress4( self ):
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ return socket.inet_ntoa(fcntl.ioctl(
+ s.fileno(),
+ 0x8915, # SIOCGIFADDR
+ struct.pack('256s', self._name[:15])
+ )[20:24])
+
+#============================================================================#
+theNetwork = Network()
+#============================================================================#
+
+if __name__ == "__main__":
+ pass
diff --git a/framework/subsystems/onetworkd/sharing.py b/framework/subsystems/onetworkd/sharing.py
index 8c0cf06..bac7c17 100644
--- a/framework/subsystems/onetworkd/sharing.py
+++ b/framework/subsystems/onetworkd/sharing.py
@@ -13,10 +13,15 @@ Module: sharing
MODULE_NAME = "onetworkd"
__version__ = "0.0.0"
+from network import theNetwork
+from helpers import isValidAddress, writeToFile
+
import dbus
import dbus.service
import gobject
+import os, subprocess
+
import logging
logger = logging.getLogger( MODULE_NAME )
@@ -24,7 +29,22 @@ DBUS_INTERFACE_NETWORK = "org.freesmartphone.Network"
DBUS_OBJECT_PATH = "/org/freesmartphone/Network"
#============================================================================#
-class ConnectionSharing(dbus.service.Object):
+class NoInterface( dbus.DBusException ):
+#============================================================================#
+ _dbus_error_name = "org.freesmartphone.Network.NoInterface"
+
+#============================================================================#
+class NoAddress( dbus.DBusException ):
+#============================================================================#
+ _dbus_error_name = "org.freesmartphone.Network.NoAddress"
+
+#============================================================================#
+class InternalError( dbus.DBusException ):
+#============================================================================#
+ _dbus_error_name = "org.freesmartphone.Network.InternalError"
+
+#============================================================================#
+class ConnectionSharing( dbus.service.Object ):
#============================================================================#
def __init__( self, bus ):
self.path = DBUS_OBJECT_PATH
@@ -34,10 +54,49 @@ class ConnectionSharing(dbus.service.Object):
#
# dbus org.freesmartphone.Network
#
- @dbus.service.method( DBUS_INTERFACE_NETWORK, "s", "",
+ @dbus.service.method( DBUS_INTERFACE_NETWORK, "ss", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
- def ShareConnectionsForInterface( self, interface, dbus_ok, dbus_error ):
- # enable forwarding and launch dhcp server listening on said interface
+ def ShareConnection( self, interface, address, dbus_ok, dbus_error ):
+ """
+
+ This should be roughly equivalent to:
+
+ #!/bin/sh
+ iptables -I INPUT 1 -s 192.168.0.200 -j ACCEPT
+ iptables -I OUTPUT 1 -s 192.168.0.202 -j ACCEPT
+ iptables -A POSTROUTING -t nat -j MASQUERADE -s 192.168.0.0/24
+ echo 1 > /proc/sys/net/ipv4/ip_forward
+ """
+
+ try:
+ iface = theNetwork[ str(interface) ]
+ except KeyError:
+ dbus_error( NoInterface( "%s is not a valid interface. Known interfaces are %s" % ( interface, theNetwork.keys() ) ) )
+ return
+
+ source_address = iface.ipAddress4()
+
+ if not isValidAddress( address ):
+ dbus_error( NoAddress( "%s is not a valid IPv4 address." % address ) )
+ return
+
+ target_address = address
+
+ # FIXME use dhcp daemon
+
+ commands = []
+ commands.append( "iptables -I INPUT 1 -s %s -j ACCEPT" % target_address )
+ commands.append( "iptables -I OUTPUT 1 -s %s -j ACCEPT" % source_address )
+ commands.append( "iptables -A POSTROUTING -t nat -j MASQUERADE -s 192.168.0.0/24" )
+
+ for command in commands:
+ logging.debug( "issuing command '%s'" % command )
+ result = subprocess.call( command.split( ' ' ) )
+ logging.debug( "command result = %d" % result )
+ if result != 0:
+ dbus_error( InternalError( "%s gave returncode %d" % ( command, result ) ) )
+ return
+ writeToFile( "/proc/sys/net/ipv4/ip_forward", "1" )
dbus_ok()
#============================================================================#
diff --git a/tools/cli-framework b/tools/cli-framework
index 3d6e187..bfd4394 100755
--- a/tools/cli-framework
+++ b/tools/cli-framework
@@ -94,6 +94,12 @@ gps = getObject( "org.freesmartphone.ogpsd", "/org/freedesktop/Gypsy" )
# usage device object
usage = getObject( "org.freesmartphone.ousaged", "/org/freesmartphone/Usage" )
+# network
+network = getInterface( \
+ "org.freesmartphone.onetworkd",
+ "/org/freesmartphone/Network",
+ "org.freesmartphone.Network" )
+
# testing
testing = getInterface( \
"org.freesmartphone.testing",
--
FSO frameworkd Debian packaging
More information about the pkg-fso-commits
mailing list