Permalink
Browse files

Guess name server and default interface, if not provided

spood will try to guess the default device, if it's not given. If there
is more than one active interface, spood will probably guess wrong.

The nameserver was grabbed from resolv.conf and also manually specified.
Make consistent the name server lookup.
  • Loading branch information...
1 parent ec69339 commit 987d979abeb77935a653545e34c54faad0fbd504 @msantos committed Jun 26, 2010
Showing with 20 additions and 2 deletions.
  1. +1 −1 src/snuff.erl
  2. +19 −1 src/spood.erl
View
@@ -38,7 +38,7 @@
service(Dev, NS) ->
{ok, Socket} = packet:socket(),
ok = packet:promiscuous(Socket, packet:ifindex(Socket, Dev)),
- error_logger:info_report({ns,NS}),
+ error_logger:info_report([{dev, Dev}, {ns,NS}]),
loop(Socket, NS).
loop(Socket, NS) ->
View
@@ -31,12 +31,13 @@
-module(spood).
-export([start/0,start/1]).
+-export([device/1,nameserver/1]).
start() ->
start([]).
start(Options) ->
- Dev = proplists:get_value(dev, Options, "ath0"),
+ Dev = device(proplists:get_value(dev, Options)),
Smac = proplists:get_value(srcmac, Options),
Dmac = proplists:get_value(dstmac, Options),
@@ -56,4 +57,21 @@ nameserver(undefined) ->
nameserver(NS) ->
NS.
+device(undefined) ->
+ {ok, S} = procket:listen(0, [{protocol, udp}, {family, inet}, {type, dgram}]),
+ Dev = hd([ If || If <- packet:iflist(), ipcheck(S, If) ]),
+ procket:close(S),
+ Dev;
+device(Dev) ->
+ Dev.
+
+ipcheck(S, If) ->
+ try packet:ipv4address(S, If) of
+ {127,_,_,_} -> false;
+ {169,_,_,_} -> false;
+ _ -> true
+ catch
+ error:_ -> false
+ end.
+

0 comments on commit 987d979

Please sign in to comment.