mldonkey-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Mldonkey-users] Re: IP-Ranges for Access


From: Pierre Etchemaite
Subject: Re: [Mldonkey-users] Re: IP-Ranges for Access
Date: Fri, 8 Nov 2002 15:44:00 +0100

Le Fri, 08 Nov 2002 12:23:24 +0100, Sven Hartge <address@hidden>
a écrit :

> But I vote for a better parser for this value, since 123.45.255.255 _is_
> a valid IP adress. Using the IP/mask scheme would avoid this and allow
> "uneven" subnets like 123.45.0.0/22  to be adressed.

I started something some days ago, but sometimes ocaml compile errors aren't
very clear to my newbie eyes:
File "lib/netmask.mli", line 25, characters 14-16:
Unbound type constructor ip
(that's Ip.of_string, that supposed to be exported...?)

netmask.mli:
type t

val of_string : string -> t

val to_string : t -> string

val matches : ip -> t list -> bool

val option : t Options.option_class

val value_to_netmask : Options.option_value -> t
val netmask_to_value : t -> Options.option_value

netmask.ml:
open Ip
open String2

type t =  Ip.t * Ip.t (* address, netmask *)

let single_netmask = Ip.of_string "255.255.255.255"

let of_string s  =
  let l = String2.split s '/' in
  match l with
    [a] -> (Ip.of_string a, single_netmask)
  | a :: [m] -> (Ip.of_string a, Ip.of_string m)
  | _ -> failwith "bad netmask"

let to_string ((a, m) as t) =
  Printf.sprintf "%s/%s" (Ip.to_string a) (Ip.to_string m)

open Int32

let matches a netmasks =
  let ai = Ip.to_int32 a in
  let rec matches_sub ai netmasks =
    match netmasks with
      [] -> false
    | (b, m) :: tail ->
        let bi = Ip.to_int32 b and
            mi = Ip.to_int32 m in
        ((Int32.logand mi (Int32.logxor ai bi)) = 0)
      || (matches_sub ai tail) in
  matches_sub ai netmasks

open Options

let value_to_netmask v = of_string (value_to_string v)

let netmask_to_value ip = string_to_value (to_string netmask)

let option = define_option_class "Netmask" value_to_netmask netmask_to_value




reply via email to

[Prev in Thread] Current Thread [Next in Thread]