Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #12 from sdebnath/master

Provide a meaningful default network interface
  • Loading branch information...
commit 8db4e07a00041b8fd3a8288c9fbb8721d1b60c09 2 parents 525c03d + ea1b66b
@jen20 jen20 authored
Showing with 24 additions and 1 deletion.
  1. +2 −1  apps/flake/src/flake_sup.erl
  2. +22 −0 apps/flake/src/flake_util.erl
View
3  apps/flake/src/flake_sup.erl
@@ -57,7 +57,8 @@ upgrade() ->
%% @spec init([]) -> SupervisorTree
%% @doc supervisor callback.
init([]) ->
- If = flake:get_config_value(interface, "eth0"),
+ DefaultIf = flake_util:get_default_if(),
+ If = flake:get_config_value(interface, DefaultIf),
error_logger:info_msg("starting flake with hardware address of ~p as worker id~n", [If]),
{ok,WorkerId} = flake_util:get_if_hw_int(If),
error_logger:info_msg("using worker id: ~p~n", [WorkerId]),
View
22 apps/flake/src/flake_util.erl
@@ -19,6 +19,7 @@
-export ([
as_list/2,
+ get_default_if/0,
get_if_hw_int/1,
hw_addr_to_int/1,
curr_time_millis/0,
@@ -27,6 +28,27 @@
-include_lib("eunit/include/eunit.hrl").
+%% get a reasonable default interface that has a valid mac address
+get_default_if() ->
+ {ok, SysIfs} = inet:getifaddrs(),
+ Ifs = [I || {I, Props} <- SysIfs, filter_if(Props)],
+ hd(Ifs).
+
+% filter network interfaces
+filter_if(Props) ->
+ HwAddr = proplists:get_value(hwaddr, Props),
+ filter_hwaddr(HwAddr).
+
+% we exclude interfaces without a MAC address
+filter_hwaddr(undefined) ->
+ false;
+% we exclude interfaces with a null MAC address, ex: loopback devices
+filter_hwaddr([0,0,0,0,0,0]) ->
+ false;
+% all others are valid interfaces to pick from
+filter_hwaddr(_) ->
+ true.
+
%% get the mac/hardware address of the given interface as a 48-bit integer
get_if_hw_int(undefined) ->
{error, if_not_found};
Please sign in to comment.
Something went wrong with that request. Please try again.