Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit using the example code

  • Loading branch information...
commit a0cc5e3254c0e7f9a039251637cd27add1450e79 1 parent 97f2d11
Michael Santos authored
2  .gitignore
... ... @@ -0,0 +1,2 @@
  1 +*.[oa]
  2 +*.beam
23 Makefile
... ... @@ -0,0 +1,23 @@
  1 +
  2 +REBAR=$(shell which rebar || echo ./rebar)
  3 +
  4 +all: dirs deps compile
  5 +
  6 +./rebar:
  7 + erl -noshell -s inets start \
  8 + -eval 'httpc:request(get, {"https://github.com/downloads/basho/rebar/rebar", []}, [], [{stream, "./rebar"}])' \
  9 + -s inets stop -s init stop
  10 + chmod +x ./rebar
  11 +
  12 +dirs:
  13 + @mkdir -p priv/tmp
  14 +
  15 +compile: $(REBAR)
  16 + @$(REBAR) compile
  17 +
  18 +clean: $(REBAR)
  19 + @$(REBAR) clean
  20 +
  21 +deps: $(REBAR)
  22 + @$(REBAR) check-deps || $(REBAR) get-deps
  23 +
23 README.md
Source Rendered
... ... @@ -1,6 +1,29 @@
1 1
2 2 # Erlang interface for manipulating 802.11 wireless interfaces
3 3
  4 +## NOTE ON PRIVILEGES
  5 +
  6 +To run this code, Erlang will have to either run as root or have
  7 +CAP\_NET\_ADMIN privileges:
  8 +
  9 + setcap cap_net_admin=ep /path/to/beam
  10 +
  11 +
  12 +## QUICK SETUP
  13 +
  14 + cd wierl
  15 + make
  16 +
  17 + sudo setcap cap_net_admin=ep /path/to/beam
  18 +
  19 + ./start.sh
  20 + % Scan using the "wlan0" interface
  21 + wierl_scan:start(<<"wlan0">>).
  22 +
  23 + # If you want to remove the privs
  24 + sudo setcap -r /path/to/beam
  25 +
  26 +
4 27 ## TODO
5 28
6 29 * put interfaces into monitor mode
15 ebin/wierl.app
... ... @@ -0,0 +1,15 @@
  1 +{application, wierl,
  2 + [
  3 + {description, "Erlang interface for manipulating 802.11 devices"},
  4 + {vsn, "0.01"},
  5 + {modules, [
  6 + wierl_scan
  7 + ]},
  8 + {registered, []},
  9 + {applications, [
  10 + kernel,
  11 + stdlib
  12 + ]},
  13 + {env, []}
  14 + ]}.
  15 +
68 include/wierl_scan.hrl
... ... @@ -0,0 +1,68 @@
  1 +%% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
  2 +%% All rights reserved.
  3 +%%
  4 +%% Redistribution and use in source and binary forms, with or without
  5 +%% modification, are permitted provided that the following conditions
  6 +%% are met:
  7 +%%
  8 +%% Redistributions of source code must retain the above copyright
  9 +%% notice, this list of conditions and the following disclaimer.
  10 +%%
  11 +%% Redistributions in binary form must reproduce the above copyright
  12 +%% notice, this list of conditions and the following disclaimer in the
  13 +%% documentation and/or other materials provided with the distribution.
  14 +%%
  15 +%% Neither the name of the author nor the names of its contributors
  16 +%% may be used to endorse or promote products derived from this software
  17 +%% without specific prior written permission.
  18 +%%
  19 +%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20 +%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21 +%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22 +%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23 +%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24 +%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25 +%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26 +%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27 +%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28 +%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29 +%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30 +%% POSSIBILITY OF SUCH DAMAGE.
  31 +-define(UINT16, 2/native-unsigned-integer-unit:8).
  32 +-define(UINT32, 4/native-unsigned-integer-unit:8).
  33 +-define(UINT64, 8/native-unsigned-integer-unit:8).
  34 +
  35 +-define(IFNAMSIZ, 16).
  36 +-define(SIOCSIWSCAN, 16#8B18). % Trigger a scan
  37 +-define(SIOCGIWSCAN, 16#8B19). % Retrieve the scan results
  38 +
  39 +% Event types
  40 +-define(SIOCGIWAP, 16#8B15).
  41 +-define(SIOCGIWAPLIST, 16#8B17).
  42 +-define(SIOCGIWAUTH, 16#8B33).
  43 +-define(SIOCGIWENCODE, 16#8B2B).
  44 +-define(SIOCGIWENCODEEXT, 16#8B35).
  45 +-define(SIOCGIWESSID, 16#8B1B).
  46 +-define(SIOCGIWFRAG, 16#8B25).
  47 +-define(SIOCGIWFREQ, 16#8B05).
  48 +-define(SIOCGIWGENIE, 16#8B31).
  49 +-define(SIOCGIWMODE, 16#8B07).
  50 +-define(SIOCGIWMODUL, 16#8B2F).
  51 +-define(SIOCGIWNAME, 16#8B01).
  52 +-define(SIOCGIWNICKN, 16#8B1D).
  53 +-define(SIOCGIWNWID, 16#8B03).
  54 +-define(SIOCGIWPOWER, 16#8B2D).
  55 +-define(SIOCGIWPRIV, 16#8B0D).
  56 +-define(SIOCGIWRANGE, 16#8B0B).
  57 +-define(SIOCGIWRATE, 16#8B21).
  58 +-define(SIOCGIWRETRY, 16#8B29).
  59 +-define(SIOCGIWRTS, 16#8B23).
  60 +-define(SIOCGIWSENS, 16#8B09).
  61 +-define(SIOCGIWSPY, 16#8B11).
  62 +-define(SIOCGIWSTATS, 16#8B0F).
  63 +-define(SIOCGIWTHRSPY, 16#8B13).
  64 +-define(SIOCGIWTXPOW, 16#8B27).
  65 +
  66 +-define(IWEVGENIE, 16#8C05).
  67 +-define(IWEVQUAL, 16#8C01).
  68 +-define(IWEVCUSTOM, 16#8C02).
3  rebar.config
... ... @@ -0,0 +1,3 @@
  1 +{deps, [
  2 + {procket, ".*", {git, "git://github.com/msantos/procket.git", "master"}}
  3 + ]}.
123 src/wierl_scan.erl
... ... @@ -0,0 +1,123 @@
  1 +%% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
  2 +%% All rights reserved.
  3 +%%
  4 +%% Redistribution and use in source and binary forms, with or without
  5 +%% modification, are permitted provided that the following conditions
  6 +%% are met:
  7 +%%
  8 +%% Redistributions of source code must retain the above copyright
  9 +%% notice, this list of conditions and the following disclaimer.
  10 +%%
  11 +%% Redistributions in binary form must reproduce the above copyright
  12 +%% notice, this list of conditions and the following disclaimer in the
  13 +%% documentation and/or other materials provided with the distribution.
  14 +%%
  15 +%% Neither the name of the author nor the names of its contributors
  16 +%% may be used to endorse or promote products derived from this software
  17 +%% without specific prior written permission.
  18 +%%
  19 +%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20 +%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21 +%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22 +%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23 +%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24 +%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25 +%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26 +%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27 +%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28 +%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29 +%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30 +%% POSSIBILITY OF SUCH DAMAGE.
  31 +-module(wierl_scan).
  32 +-export([start/1]).
  33 +
  34 +-include("wierl_scan.hrl").
  35 +
  36 +
  37 +%%
  38 +%% Open an unprivileged, datagram socket using
  39 +%% procket
  40 +%%
  41 +
  42 +start(Dev) when is_binary(Dev) ->
  43 + {ok, Socket} = procket:socket(inet, dgram, 0),
  44 + scan(Dev, Socket),
  45 + procket:close(Socket).
  46 +
  47 +%%
  48 +%% Initiate the scan
  49 +%%
  50 +
  51 +scan(Dev, Socket) when byte_size(Dev) < ?IFNAMSIZ ->
  52 + Req = <<Dev/binary, 0:((?IFNAMSIZ - byte_size(Dev))*8), 0:(16*8)>>,
  53 + {ok, _Req} = procket:ioctl(Socket, ?SIOCSIWSCAN, Req),
  54 + result(Dev, Socket).
  55 +
  56 +%%
  57 +%% Retrieve the scan results by specifying a buffer for the
  58 +%% kernel to return the results
  59 +%%
  60 +
  61 +result(Dev, Socket) ->
  62 + {ok, Req, [Res]} = procket:alloc([
  63 + <<Dev/bytes, 0:( (?IFNAMSIZ - byte_size(Dev))*8)>>,
  64 + {ptr, 4096},
  65 + <<4096:?UINT16, 0:?UINT16>>
  66 + ]),
  67 + case procket:ioctl(Socket, ?SIOCGIWSCAN, Req) of
  68 + {ok, <<_Ifname:16/bytes, _Ptr:?UINT32, Len:?UINT16, _Flag:?UINT16>>} ->
  69 + {ok, <<Stream:Len/bytes, _/binary>>} = procket:buf(Res),
  70 + event(Stream);
  71 +
  72 + % Poll the socket for the results
  73 + {error, eagain} ->
  74 + timer:sleep(1000),
  75 + result(Dev, Socket);
  76 +
  77 + Error ->
  78 + Error
  79 + end.
  80 +
  81 +% The events are returned in the form: length, command, data
  82 +% (length bytes)
  83 +%
  84 +% We believe the length returned in the packet and print out
  85 +% the binary data. If the length is wrong, we'll just crash.
  86 +event(<<>>) ->
  87 + ok;
  88 +event(<<Len:?UINT16, Cmd:?UINT16, Bin/binary>>) ->
  89 + EventLen = Len - 4,
  90 + <<Event:EventLen/bytes, Rest/binary>> = Bin,
  91 + io:format("~p:~p~n", [cmd(Cmd), Event]),
  92 + event(Rest).
  93 +
  94 +% Convert the integer command values to almost human readable atoms
  95 +cmd(?SIOCGIWNAME) -> name;
  96 +cmd(?SIOCGIWNWID) -> nwid;
  97 +cmd(?SIOCGIWFREQ) -> freq;
  98 +cmd(?SIOCGIWMODE) -> mode;
  99 +cmd(?SIOCGIWSENS) -> sens;
  100 +cmd(?SIOCGIWRANGE) -> range;
  101 +cmd(?SIOCGIWPRIV) -> priv;
  102 +cmd(?SIOCGIWSTATS) -> stats;
  103 +cmd(?SIOCGIWSPY) -> spy;
  104 +cmd(?SIOCGIWTHRSPY) -> thrspy;
  105 +cmd(?SIOCGIWAP) -> bssid;
  106 +cmd(?SIOCGIWAPLIST) -> aplist;
  107 +cmd(?SIOCGIWESSID) -> essid;
  108 +cmd(?SIOCGIWNICKN) -> nickn;
  109 +cmd(?SIOCGIWRATE) -> rate;
  110 +cmd(?SIOCGIWRTS) -> rts;
  111 +cmd(?SIOCGIWFRAG) -> frag;
  112 +cmd(?SIOCGIWTXPOW) -> txpow;
  113 +cmd(?SIOCGIWRETRY) -> retry;
  114 +cmd(?SIOCGIWENCODE) -> encode;
  115 +cmd(?SIOCGIWPOWER) -> power;
  116 +cmd(?SIOCGIWMODUL) -> modul;
  117 +cmd(?SIOCGIWGENIE) -> genie;
  118 +cmd(?SIOCGIWAUTH) -> auth;
  119 +cmd(?SIOCGIWENCODEEXT) -> encodeext;
  120 +cmd(?IWEVGENIE) -> genie;
  121 +cmd(?IWEVQUAL) -> qual;
  122 +cmd(?IWEVCUSTOM) -> custom;
  123 +cmd(Unknown) -> {unknown, Unknown}.
3  start.sh
... ... @@ -0,0 +1,3 @@
  1 +#!/bin/sh
  2 +
  3 +erl -boot start_sasl -pa $PWD/ebin $PWD/deps/*/ebin

0 comments on commit a0cc5e3

Please sign in to comment.
Something went wrong with that request. Please try again.