Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add random mac generator for g_ether so networking tools and scripts …
…on host side are easier to use/automate

Signed-off-by: Philippe De Swert <philippe.deswert@jollamobile.com>
  • Loading branch information
philippedeswert committed Jan 17, 2013
1 parent e4e0be4 commit 7f05c41
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
6 changes: 6 additions & 0 deletions debian/changelog
@@ -1,3 +1,9 @@
usb-moded (0.57) unstable; urgency=low

* Add random mac generator so host based network tools always get the same mac

-- Philippe De Swert <philippedeswert@jollamobile.com> Thu, 17 Jan 2013 19:50:37 +0200

usb-moded (0.56) unstable; urgency=low

* Fix possible compiltation issue with N900 support and android which are mutually exclusive
Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.am
Expand Up @@ -26,7 +26,9 @@ usb_moded_SOURCES = \
usb_moded-network.c \
usb_moded-network.h \
usb_moded-modesetting.c \
usb_moded-modesetting.h
usb_moded-modesetting.h \
usb_moded-mac.c \
usb_moded-mac.h

if !ANDROID
usb_moded_SOURCES += \
Expand Down
56 changes: 56 additions & 0 deletions src/usb_moded-mac.c
@@ -0,0 +1,56 @@
/**
@file usb_moded-mac.c
Copyright (C) 2013 Jolla. All rights reserved.
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
This program 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 Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/

#include <stdio.h>
#include "usb_moded-mac.h"

static void random_ether_addr(unsigned char *addr)
{
FILE *random;

random = fopen("/dev/urandom", "r");
fread(addr, 1, 6, random);
fclose(random);

addr [0] &= 0xfe; /* clear multicast bit */
addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
}

void generate_random_mac (void)
{
unsigned char addr[6];
int i;
FILE *g_ether;

printf("Getting random usb ethernet mac\n");
random_ether_addr(addr);

g_ether = fopen("/etc/modprobe.d/g_ether.conf", "w");
fprintf(g_ether, "options g_ether host_addr=");

for(i=0; i<5; i++)
{
fprintf(g_ether, "%02x:",addr[i]);
}
fprintf(g_ether, "%02x",addr[i]);
fclose(g_ether);
}
23 changes: 23 additions & 0 deletions src/usb_moded-mac.h
@@ -0,0 +1,23 @@
/**
@file usb_moded-mac.h
Copyright (C) 2013 Jolla. All rights reserved.
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
This program 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 Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/

void generate_random_mac (void);
7 changes: 6 additions & 1 deletion src/usb_moded.c
Expand Up @@ -49,6 +49,7 @@
#include "usb_moded-config.h"
#include "usb_moded-config-private.h"
#include "usb_moded-network.h"
#include "usb_moded-mac.h"

/* global definitions */

Expand Down Expand Up @@ -180,7 +181,7 @@ void set_usb_connected_state(void)
#ifdef MEEGOLOCK
int act_dead = 0;
/* check if we are in acting dead or not, /tmp/USER will not exist in acting dead */
act_dead = access("/tmp/USER", R_OK);
act_dead = accesa("/tmp/USER", R_OK);
if(mode_to_set && !export && !act_dead)
#else
if(mode_to_set)
Expand Down Expand Up @@ -455,6 +456,10 @@ static void usb_moded_init(void)
ctx = kmod_new(NULL, NULL);
kmod_load_resources(ctx);

if(!access("/etc/modprobe.d/g_ether.conf", F_OK))
{
generate_random_mac();
}
/* TODO: add more start-up clean-up and init here if needed */
}

Expand Down

0 comments on commit 7f05c41

Please sign in to comment.