Code and Stuff

Aug 11, 2011

Sharing Ethernet over WLAN (Buggy)

Requirements: A capable WIFI card, Ubuntu 11.04
Packages: hostapd, dhcp server


In an attempt to share my wired network to my android cell phone using wifi I found the following solution. It is still quite rough and not very stable, but fits my needs.

The solution is composed of 3 parts: hostapd, dhcp and a script.

hostapd

Hostapd is a software to create an accesspoint with a wifi card (exactly what I need). The configuration is quite simple and well documented. The config I used is the following (/etc/hostapd/hostapd.conf):
interface=wlan0
channel=1
driver=nl80211
ssid=THE_SSID

auth_algs=1

wpa=3
wpa_passphrase=THE_PASSWORD_GOES_HERE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
You might have to change it a little to match your config (Especially the driver, interface, ssid and wpa_passphrase). You can get a list of the interfaces with ifconfig

DHCP server

Next thing is to setup the DHCP server. This will give devices an IP when they connect to the WIFI network. My config file (/etc/dhcp/dhcpd.conf) looks like this:
# listen on wlan0
DHCPDARGS=wlan0; 

ddns-update-style none;

# change this according to your needs
option domain-name-servers 192.168.1.1;

default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet 192.168.9.0 netmask 255.255.255.0 {
  range 192.168.9.10 192.168.9.254;
  option broadcast-address 192.168.9.254;
  option routers 192.168.9.1;
}
You might have to change it a little to match your environment especially the DNS. I'm using the 192.168.9.0/24 network for the Wireless network. The machine that is running the server has IP 192.168.9.1. The wlan interface has to be configured to this IP, or you will get an error.

The script

The last part is the script that starts it all. I've placed it in the /usr/local/sbin folder and set my path to look in there for executables.
#!/bin/bash

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# set the interface's IP
ifconfig wlan0 inet 192.168.9.1 netmask 255.255.255.0

# start the AP
hostapd /etc/hostapd/hostapd.conf > /dev/null &

# restart the DHCP server
/etc/init.d/isc-dhcp-server restart

here as well you will have to change the IP and interfaces if needed. This script could also be linked from some runlevel folder to be started at boot (you probably have to add the start/stop cases.

Problems

One major problem of this approach is when the computer goes into sleep mode. I never checked what happens exactly, but looks like the interface goes down and hostapd isn't that happy about it.

Other solution

I read on various forums that ubuntu's NetworkManager should be able to share the LAN as well.


Aug 10, 2011

Graphiti: Using Graphiti in an Eclipse RCP application

UPDATE 9 Aug. 2013

Information in this post is outdated and probably not best practice. I'm busy writing a new version of the post that should be simpler and work with recent version of Graphiti. I'll post a link here as soon as it is ready.



As many others I implemented the nice eclipse Graphiti tutorial and ran it as a plugin in eclipse. This works just fine and shows your diagram as expected.
Using it inside an RCP application was not described in the tutorial. Many posts on various forum suggested to use the code of the example org.eclipse.graphiti.examples.common.ui.CreateDiagramWizard. It works and it can be made also a little easier.
I assume that the diagram type is already declared in the plugin.

A custom Graphiti DiagramEditor

To have some more expansion possibilities I created a custom Graphiti editor by subclassing DiagramEditor and declaring it in the plugin.xml using the editor extension point.
<extension point="org.eclipse.ui.editors">
 <editor class="ch.idsia.diagram.MyGraphitiEditor" 
  id="ch.idsia.diagram.myDiagramEditor" 
  name="A diagram editor">
 </editor>
</extension>

Creating the diagram

Creating a new diagram will require the creation of the DiagramEditingDomain and the DiagramEditorInput. The actual creation of the editor is done the usual was via the openEditor method of the Workbech Page.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException { 
 Diagram diagram = Graphiti.getPeCreateService().createDiagram(DIAG_TYPE_ID, "Name", true);
 String editorID = "ch.idsia.diagram.myDiagramEditor";
   
 TransactionalEditingDomain domain = createEmfFileForDiagram(diagram);
 String providerId = GraphitiUi.getExtensionManager().getDiagramTypeProviderId(diagram.getDiagramTypeId());
 DiagramEditorInput editorInput = new DiagramEditorInput(EcoreUtil.getURI(diagram), domain, providerId, true);
  
 try {
  HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().openEditor(editorInput, editorID);
 } catch (PartInitException e) {
  // TODO let the user know
  return false;
 }
 return true;
}

Differently than the example I'm not using a FileService class an I simply added a method to the handler that creates the TransactionalEditingDomain.
/**
 * This code is mostly obtained from the org.eclipse.graphiti.examples.common.FileService class.
 */
public static TransactionalEditingDomain createEmfFileForDiagram(final Diagram diagram) {
 // Create a resource set and EditingDomain
 TransactionalEditingDomain editingDomain = DiagramEditorFactory.createResourceSetAndEditingDomain();
 ResourceSet resourceSet = editingDomain.getResourceSet();

 // Create a resource for this file.
 URI uri = URI.createFileURI("<untitled>.ext");  
 final Resource resource = resourceSet.createResource(uri);
  
 editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) {
  @Override
  protected void doExecute() {
   resource.setTrackingModification(true);
   resource.getContents().add(diagram);
  }
 });

 return editingDomain;
}

Opening a Diagram

To open a diagram a handler can use the openEditor method with an editor input and the ID of our declared editor as parameters. The editor input, however, must be a DiagramEditorInput, which is obtained by providing a URIEditorInput to the DiagramEditorInputFactory (other EditorInputs might be provided to the factory, have a look at the factory code for more info).
@Override
public void execute(ExecutionEvent event) throws ExecutionException {
 ... 
 // prompt the user for the filepath
 ...
 
 URI uri = URI.createFileURI(filePath);
 DiagramEditorFactory factory = new DiagramEditorFactory();
 DiagramEditorInput input = null;

 try {
  input = factory.createEditorInput(new URIEditorInput(uri));
 } catch(Exception exception) {
  // TODO let the user know that we did not manage to open the uri
  return false;
 }

 try {
  page.openEditor(input, "ch.idsia.diagram.MyDiagramEditor");
 } catch (PartInitException exception) {
  // TODO show a message
  return false;
 }
}