Skip to content

Commit

Permalink
packet: add bind/2
Browse files Browse the repository at this point in the history
Binding a PF_PACKET socket to an interface will return packets only for
that device (rather than all interfaces on the host).
  • Loading branch information
msantos committed Jun 27, 2011
1 parent b6af970 commit 637acc5
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/packet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
ipv4address/2,
macaddress/2,
promiscuous/2,
bind/2,
bindtodevice/2,
filter/2, unfilter/1, unfilter/2,
send/3
Expand Down Expand Up @@ -225,6 +226,30 @@ promiscuous(Socket, Ifindex) ->
>>).


%%-------------------------------------------------------------------------
%% Bind a PF_PACKET socket to an interface.
%%-------------------------------------------------------------------------
bind(Socket, Ifindex) ->
Sockaddr_ll = <<
?PF_PACKET:16/native, % sll_family: PF_PACKET
0:16, % sll_protocol: Physical layer protocol
Ifindex:32/native, % sll_ifindex: Interface number
0:16, % sll_hatype: Header type
0:8, % sll_pkttype: Packet type
0:8, % sll_halen: address length
0:8, % sll_addr[8]: physical layer address
0:8, % sll_addr[8]: physical layer address
0:8, % sll_addr[8]: physical layer address
0:8, % sll_addr[8]: physical layer address
0:8, % sll_addr[8]: physical layer address
0:8, % sll_addr[8]: physical layer address
0:8, % sll_addr[8]: physical layer address
0:8 % sll_addr[8]: physical layer address
>>,

procket:bind(Socket, Sockaddr_ll).


%%-------------------------------------------------------------------------
%% Bind socket to interface. Equivalent to the {interface, Device} option
%% but requires running Erlang with heightened privs (CAP_NET_RAW)
Expand Down

0 comments on commit 637acc5

Please sign in to comment.