[erlang-questions] subnet masking with binary matching

jm <>
Thu Jan 18 05:08:22 CET 2007


Using binary matching to perform subnet masking with the following code, 
which should work with both IPv4 and IPv6 as far as I can tell,

mask_address(Addr, Maskbits) ->
   B = list_to_binary(tuple_to_list(Addr)),
   io:format("address as binary: ~p ~p~n", [B,Maskbits]),
   <<Subnet:Maskbits, _Host>> = B,
   Subnet.

which errors with

{badmatch,<<4 bytes>>}

when called by

   mask_address({192,168,1,128}, 25).

yet works when called by

   mask_address({192,168,1,0}, 24).

Can anyone tell me how to fix this?

Jeff.



More information about the erlang-questions mailing list