msantos / procket

Erlang interface to low level socket operations
http://blog.listincomprehension.com/search/label/procket
BSD 3-Clause "New" or "Revised" License
283 stars 80 forks source link

Custom IP Header #43

Open Ryanauger95 opened 6 years ago

Ryanauger95 commented 6 years ago

Hi,

I am using pkt to create a custom ipv4 header:

 IpHeader_ = #ipv4{p = 1, saddr = {127,0,0,1}, daddr = {192,168,7,83}, len = bytesize(Icmp) + 20},  IpHeader = pkt:ipv4(IpHeader#ipv4{sum =  pkt:makesum(IpHeader_)})

Using a static icmp echo packet:  Packet = <<8,0,140,250,29,138,0,0,255,253,243,182,73,166,226,218,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>>,

However, I am having issues sending the raw packet without the underlying protocol adding the header. I am following this blog here:http://blog.listincomprehension.com/2010/06/fun-with-raw-sockets-in-erlang-sending.html

which uses {ok, FD} = procket:listen(0, [{protocol, icmp}, {type, raw}, {family, inet}]) and then ok = procket:sendto(S, Packet, 0, << ?PF_INET:16/native, % sin_family 0:16, % sin_port IP1:8, IP2:8, IP3:8, IP4:8, % sin_addr 0:64 % sin_zero

),

But attempting to use procket:listen with those arguments gives an error, it seems like this repository has changed a bit since 2010!

So instead I've been using procket:open with those same options, {ok, FD} = procket:open(0, [{protocol, icmp}, {type, raw}, {family, packet}]),

but when i try to write or sendto i get an error enxio which corresponds to the device not existing...

I'm curious what you all think is the best route from here. I've hit a wall.

Thanks for your help!

msantos commented 6 years ago

Hey @PiedPieper ! Your code worked without any changes for me on linux:

-module(icmpt).                                                                 

-include("procket.hrl").                                                        

-export([t/0, t/1]).                                                            

%%%                                                                             
%%% erlc -I include icmpt.erl                                                   
%%%                                                                             

t() ->                                                                          
  t({192,168,7,83}).                                                            

t({IP1, IP2, IP3, IP4}) ->                                                      
%  IpHeader_ = #ipv4{p = 1,                                                     
%                    saddr = {127,0,0,1},                                       
%                    daddr = {192,168,7,83},                                    
%                    len = byte_size(Icmp) + 20                                 
%                   },                                                          
%  IpHeader = pkt:ipv4(IpHeader_#ipv4{sum =  pkt:makesum(IpHeader_)})           

  Packet = <<8,0,140,250,29,138,0,0,255,253,243,182,73,166,226,218,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>>,           

  {ok, FD} = procket:open(0, [{protocol, icmp}, {type, raw}, {family, inet}]),  
  ok = procket:sendto(FD, Packet, 0,                                            
                      <<                                                        
                        ?PF_INET:16/native,             % sin_family            
                        0:16,                           % sin_port              
                        IP1:8, IP2:8, IP3:8, IP4:8,     % sin_addr              
                        0:64                            % sin_zero              
                      >>                                                        
                     ).

Running it:

1> icmp:t().
ok

The corresponding tcpdump:

$ sudo tcpdump -n -s0 -X icmp                 
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode      
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes       
15:57:46.378413 IP 192.168.214.41 > 192.168.7.83: ICMP echo request, id 7562, seq 0, length 64
        0x0000:  4500 0054 4901 4000 4001 92da c0a8 d629  E..TI.@.@......)      
        0x0010:  c0a8 0753 0800 8cfa 1d8a 0000 fffd f3b6  ...S............      
        0x0020:  49a6 e2da 2021 2223 2425 2627 2829 2a2b  I....!"#$%&'()*+      
        0x0030:  2c2d 2e2f 3031 3233 3435 3637 3839 3a3b  ,-./0123456789:;      
        0x0040:  3c3d 3e3f 4041 4243 4445 4647 4849 4a4b  <=>?@ABCDEFGHIJK      
        0x0050:  4c4d 4e4f                                LMNO 

Ping'ing the router:

2> icmp:t({192,168,214,1}).

And the tcpdump shows an ICMP echo reply:

15:58:47.642236 IP 192.168.214.41 > 192.168.214.1: ICMP echo request, id 7562, seq 0, length 64
        0x0000:  4500 0054 c43f 4000 4001 48ed c0a8 d629  E..T.?@.@.H....)      
        0x0010:  c0a8 d601 0800 8cfa 1d8a 0000 fffd f3b6  ................      
        0x0020:  49a6 e2da 2021 2223 2425 2627 2829 2a2b  I....!"#$%&'()*+      
        0x0030:  2c2d 2e2f 3031 3233 3435 3637 3839 3a3b  ,-./0123456789:;      
        0x0040:  3c3d 3e3f 4041 4243 4445 4647 4849 4a4b  <=>?@ABCDEFGHIJK      
        0x0050:  4c4d 4e4f                                LMNO                  
15:58:47.645710 IP 192.168.214.1 > 192.168.214.41: ICMP echo reply, id 7562, seq 0, length 64
        0x0000:  4500 0054 9b82 0000 4001 b1aa c0a8 d601  E..T....@.......      
        0x0010:  c0a8 d629 0000 94fa 1d8a 0000 fffd f3b6  ...)............      
        0x0020:  49a6 e2da 2021 2223 2425 2627 2829 2a2b  I....!"#$%&'()*+      
        0x0030:  2c2d 2e2f 3031 3233 3435 3637 3839 3a3b  ,-./0123456789:;      
        0x0040:  3c3d 3e3f 4041 4243 4445 4647 4849 4a4b  <=>?@ABCDEFGHIJK      
        0x0050:  4c4d 4e4f                                LMNO 

What OS are you testing with?

But attempting to use procket:listen with those arguments gives an error, it seems like this repository has changed a bit since 2010!

I'm curious what you all think is the best route from here. I've hit a wall.

I've been meaning to update those old blog posts forever now. Sorry about that! Feel free to ask if you have any problems!