summaryrefslogtreecommitdiff
path: root/deps/netlink/src/netlink_drv.erl
blob: e390d128311047a786d5de87fbc729d0c33d444c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
%%%---- BEGIN COPYRIGHT -------------------------------------------------------
%%%
%%% Copyright (C) 2012 Feuerlabs, Inc. All rights reserved.
%%%
%%% This Source Code Form is subject to the terms of the Mozilla Public
%%% License, v. 2.0. If a copy of the MPL was not distributed with this
%%% file, You can obtain one at http://mozilla.org/MPL/2.0/.
%%%
%%%---- END COPYRIGHT ---------------------------------------------------------
%%% @author Tony Rogvall <tony@rogvall.se>
%%% @doc
%%%    launch netlink_drv
%%% @end
%%% Created : 30 Nov 2011 by Tony Rogvall <tony@rogvall.se>

-module(netlink_drv).
-export([open/1, close/1, send/2]).
-export([add_membership/2, 
	 drop_membership/2,
	 set_rcvbuf/2, set_sndbuf/2,
	 get_rcvbuf/1, get_sndbuf/1,
	 get_sizeof/1,
	 deactivate/1,
	 activate/1, 
	 activate/2,
	 debug/2]).

%% deugging
-compile(export_all).

-include("netlink.hrl").

-define(CMD_ADD_MEMBERSHIP,   1).
-define(CMD_DROP_MEMBERSHIP,  2).
-define(CMD_ACTIVE,           3).
-define(CMD_DEBUG,            4).
-define(CMD_SET_RCVBUF,       5).
-define(CMD_SET_SNDBUF,       6).
-define(CMD_GET_RCVBUF,       7).
-define(CMD_GET_SNDBUF,       8).
-define(CMD_GET_SIZEOF,       9).

-define(DLOG_DEBUG,     7).
-define(DLOG_INFO,      6).
-define(DLOG_NOTICE,    5).
-define(DLOG_WARNING,   4).
-define(DLOG_ERROR,     3).
-define(DLOG_CRITICAL,  2).
-define(DLOG_ALERT,     1).
-define(DLOG_EMERGENCY, 0).
-define(DLOG_NONE,     -1).

add_membership(Port,Msg) when is_integer(Msg) ->
    port_call(Port, ?CMD_ADD_MEMBERSHIP, <<Msg:32>>).

drop_membership(Port,Msg) when is_integer(Msg) ->
    port_call(Port, ?CMD_DROP_MEMBERSHIP, <<Msg:32>>).

set_rcvbuf(Port,Size) when is_integer(Size), Size >= 0 ->
    port_call(Port, ?CMD_SET_RCVBUF, <<Size:32>>).

set_sndbuf(Port,Size) when is_integer(Size), Size >= 0 ->
    port_call(Port, ?CMD_SET_SNDBUF, <<Size:32>>).

get_rcvbuf(Port) ->
    port_call(Port, ?CMD_GET_RCVBUF, <<>>).

get_sndbuf(Port) ->
    port_call(Port, ?CMD_GET_SNDBUF, <<>>).

get_sizeof(Port) ->
    port_call(Port, ?CMD_GET_SIZEOF, <<>>).

deactivate(Port) ->
    activate(Port, 0).    

activate(Port) ->
    activate(Port, -1).

activate(Port, N) when is_integer(N), N >= -1, N < 16#7fffffff ->
    port_call(Port, ?CMD_ACTIVE, <<N:32>>).

debug(Port,Level) when is_atom(Level) ->
    L = level(Level),
    port_call(Port, ?CMD_DEBUG, <<L:32>>).

open(Protocol) when is_integer(Protocol), Protocol >= 0 ->
    Driver = "netlink_drv",
    Path = code:priv_dir(netlink),
    io:format("load_driver '~s' from: '~s'\n", [Driver, Path]),
    case erl_ddll:load_driver(Path, Driver) of
	ok ->
	    Arg = integer_to_list(Protocol),
	    erlang:open_port({spawn_driver, Driver++" "++Arg}, [binary]);
	{error,Error} ->
	    io:format("Error: ~s\n", [erl_ddll:format_error_int(Error)]),
	    erlang:error(Error)
    end.

close(Port) ->
    erlang:port_close(Port).

send(Port, Command) ->
    erlang:port_command(Port, Command).

port_call(Port, Cmd, Data) ->
    case erlang:port_control(Port, Cmd, Data) of
	<<0>> ->
	    ok;
	<<255,E/binary>> -> 
	    {error, erlang:binary_to_atom(E, latin1)};
	<<254,E/binary>> -> 
	    {error, binary_to_list(E)};
	<<1,Y:8>> -> {ok,Y};
	<<1,Y:16/native-unsigned>> -> {ok, Y};
	<<1,Y:32/native-unsigned>> -> {ok, Y};
	<<1,Y:64/native-unsigned>> -> {ok, Y};
	<<2,X:32/native-unsigned,Y:32/native-unsigned>> -> {ok,{X,Y}};
	<<3,X/binary>> -> {ok,X};
	<<4,X/binary>> -> {ok,binary_to_list(X)}
    end.
	
%% convert symbolic to numeric level
level(true)  -> ?DLOG_DEBUG;
level(false) -> ?DLOG_NONE;
level(debug) -> ?DLOG_DEBUG;
level(info)  -> ?DLOG_INFO;
level(notice) -> ?DLOG_NOTICE;
level(warning) -> ?DLOG_WARNING;
level(error) -> ?DLOG_ERROR;
level(critical) -> ?DLOG_CRITICAL;
level(alert) -> ?DLOG_ALERT;
level(emergency) -> ?DLOG_EMERGENCY;
level(none) -> ?DLOG_NONE.