Erlangでソフトウェアルータを作ってる話8. bitstring
•
% bitstring
<<69,16,0,60,227,255,64,0,63,6,255,108,172,31,0,2,172,17,0,13,198,22,31,64,
73,33,85,221,0,0,0,0,160,2,114,16,88,110,0,0,2,4,5,180,4,2,8,10,8,17,182,34
,0,0,0,0,1,3,3,7>>
%
<<DestMacAddr:48, SourceMacAddr:48, Type:16, Data/bitstring>> = Buf,
% bitstring
SendData = <<Ver:4, Len:4, ServiceType, Packetlen:16, IdentificationNumber:
16, Flg:3, Offset:13, CountTTL, Protocol, SourceIp:32, DestIp:32>>,
10. •
1> N = 9. % N 9
9
2> N.
9
3> N = 0. %
** exception error: no match of right hand side value 0
4> 9 = N. %
9
5> {N, M} = {9, 0}. % 9 N M 0
{9,0}
6> {N, L} = {0, 8}. % 9 N
** exception error: no match of right hand side value {0,8}
11. •
•
handle_event(Buf, Fd) ->
<<_:48, SourceMacAddr:48, Type:16, Data/bitstring>> = Buf,
ethernet_type(Type, Data);
% IP Protocol
ethernet_type(16#0800, Data) -> % Type 16 0800
ip:packet(Data);
% ARP Protocol
ethernet_type(16#0806, Data) -> % Type 16 0806
arp:packet(Data);
ethernet_type(_Type, _Data) -> % _
false;
12. • bitstring
% IP check sump
% bitstring
checksum(<<>>, Sum) ->
Sum bxor 16#FFFF;
% 16bit
checksum(<<A:16, Other/bitstring>>, Sum) ->
Check = A + Sum,
Res = (Check band 16#FFFF) + (Check bsr 16),
checksum(Other, Res).
18. ARP
% source_ip_addr IP
% dest_ip_addr IP
% dest_mac_addr Mac
% type
-record(arp_table, {
source_ip_addr,
dest_ip_addr,
dest_mac_addr,
type
}).
21. % source_route
% dest_route {192.168.100.0}
% dest_route_int dest_route
% subnetmask dest_route {255, 255, 255, 0}
% subnetmask_int subnetmask
-record(routing_table, {
source_route,
dest_route,
dest_route_int,
subnetmask,
subnetmask_int,
ad,
metric,
nexthop,
age,
out_interface
}).
22. % ad
% metric
% nexthop dest_route IP
% age
% out_interface
-record(routing_table, {
source_route,
dest_route,
dest_route_int,
subnetmask,
subnetmask_int,
ad,
metric,
nexthop,
age,
out_interface
}).