Skip to content

Commit

Permalink
[usb_moded] support starting a dhcp server on the device
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe De Swert <philippe.deswert@jollamobile.com>
  • Loading branch information
philippedeswert committed Dec 10, 2013
1 parent 957a15d commit bac2fec
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions docs/usb_moded-doc.txt
Expand Up @@ -151,6 +151,7 @@ When nat=1 is given in the mode configuration this will be used to enable nat on
This is a bit complicated but related to the fact that the nat interface is usually the same on a device,
but not all usb network related profiles might want it enabled.

If you want the device to load a dhcp server you need to configure this in the mode config, just like nat. (see lower)

Functional overview
--------------------
Expand Down Expand Up @@ -264,6 +265,12 @@ mandatory for all kinds of mass_storage support)

To enable nat, you need to set nat = 1 and configure the nat_interface in the network settings.

To have dhcp server functionality on the device, set dhcp_server = 1. This will use udhcpd.
It also uses the default network address or whatever has been configured and sets up a corresponding dhcp
configuration. This way the device is always available on the same address.

Both NAT and dhcp server need a corresponding service that can be started by usb_moded. (see Appsyn feature)

Trigger support
---------------

Expand Down
1 change: 1 addition & 0 deletions src/usb_moded-dyn-config.c
Expand Up @@ -133,6 +133,7 @@ static struct mode_list_elem *read_mode_file(const gchar *filename)
//log_debug("Android extra value2 = %s\n", list_item->android_extra_sysfs_value2);
list_item->idProduct = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_IDPRODUCT, NULL);
list_item->nat = g_key_file_get_integer(settingsfile, MODE_OPTIONS_ENTRY, MODE_HAS_NAT, NULL);
list_item->dhcp_server = g_key_file_get_integer(settingsfile, MODE_OPTIONS_ENTRY, MODE_HAS_DHCP_SERVER, NULL);

g_key_file_free(settingsfile);
if(list_item->mode_name == NULL || list_item->mode_module == NULL)
Expand Down
2 changes: 2 additions & 0 deletions src/usb_moded-dyn-config.h
Expand Up @@ -50,6 +50,7 @@ android engineers prefered to have sysfs entries... go figure... */
/* For windows different modes/usb profiles need their own idProduct */
#define MODE_IDPRODUCT "idProduct"
#define MODE_HAS_NAT "nat"
#define MODE_HAS_DHCP_SERVER "dhcp_server"

/**
* Struct keeping all the data needed for the definition of a dynamic mode
Expand All @@ -75,6 +76,7 @@ typedef struct mode_list_elem
char *android_extra_sysfs_value2; /* static value that never changes that needs to be set by sysfs :( */
char *idProduct; /* product id to assign to a specific profile */
int nat; /* If NAT should be set up in this mode or not */
int dhcp_server; /* if a DHCP server needs to be configured and started or not */
/*@} */
}mode_list_elem;

Expand Down
2 changes: 1 addition & 1 deletion src/usb_moded-modesetting.c
Expand Up @@ -352,7 +352,7 @@ int set_dynamic_mode(void)

/* Needs to be called before application synching so
that the dhcp server has the right config */
if(data->nat)
if(data->nat || data->dhcp_server)
usb_network_set_up_dhcpd(data);

#ifdef APP_SYNC
Expand Down
24 changes: 17 additions & 7 deletions src/usb_moded-network.c
Expand Up @@ -110,6 +110,7 @@ static int resolv_conf_dns(ipforward_data *ipforward)

/**
* Write udhcpd.conf
* @ipforward : NULL if we want a simple config, otherwise include dns info etc...
* TODO: make this conditional ip could not have changed
*/
static int write_udhcpd_conf(ipforward_data *ipforward, struct mode_list_elem *data)
Expand Down Expand Up @@ -154,8 +155,11 @@ static int write_udhcpd_conf(ipforward_data *ipforward, struct mode_list_elem *d
fprintf(conffile, "start\t%s\n", ipstart);
fprintf(conffile, "end\t%s\n", ipend);
fprintf(conffile, "interface\t%s\n", get_interface(data));
fprintf(conffile, "opt\tdns\t%s %s\n", ipforward->dns1, ipforward->dns2);
fprintf(conffile, "opt\trouter\t%s\n", ip);
if(ipforward != NULL)
{
fprintf(conffile, "opt\tdns\t%s %s\n", ipforward->dns1, ipforward->dns2);
fprintf(conffile, "opt\trouter\t%s\n", ip);
}

free(ipstart);
free(ipend);
Expand Down Expand Up @@ -365,17 +369,23 @@ static int connman_get_connection_data(struct ipforward_data *ipforward)
*/
int usb_network_set_up_dhcpd(struct mode_list_elem *data)
{
struct ipforward_data *ipforward;
struct ipforward_data *ipforward = NULL;

ipforward = malloc(sizeof(struct ipforward_data));
/* Set up nat info only if it is required */
if(data->nat)
{
ipforward = malloc(sizeof(struct ipforward_data));
#ifdef CONNMAN
connman_get_connection_data(ipforward);
connman_get_connection_data(ipforward);
#else
resolv_conf_dns(ipforward);
resolv_conf_dns(ipforward);
#endif /*CONNMAN */
}
/* ipforward can be NULL here, which is expected and handled in this function */
write_udhcpd_conf(ipforward, data);

free(ipforward);
if(ipforward)
free(ipforward);
return(0);
}

Expand Down

0 comments on commit bac2fec

Please sign in to comment.