[erlang-questions] subnet masking with binary matching

Robert Baruch <>
Thu Jan 18 06:02:36 CET 2007


The binary you're matching to has to total a multiple of 8 bits long.  
Maybe Erlang is assuming _Host is some multiple of 8 bits, and  
therefore since Maskbits is not a multiple of 8 bits, the match fails?

--Rob

On Jan 17, 2007, at 11:08 PM, jm wrote:

> 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