text
stringlengths
0
601k
module Make ( Random : Mirage_random . S ) ( Time : Mirage_time . S ) ( Net : Mirage_net . S ) = struct open Lwt . Infix type lease = Dhcp_wire . pkt type t = lease Lwt_stream . t let connect ( ? renew = true ) ? xid ( ? requests : Dhcp_wire . option_code list option ) net = let sleep_interval = Duration . of_sec 4 in ...
let default_lease_time = 60 * 60 * 1
let max_lease_time = 60 * 60 * 24
let ip_address = ip " 192 . 168 . 1 . 5 "
let network = net " 192 . 168 . 1 . 5 / 24 "
let range = Some ( ip " 192 . 168 . 1 . 70 " , ip " 192 . 168 . 1 . 100 " )
let options = [ Routers [ ip " 192 . 168 . 1 . 5 " ] ; Dns_servers [ ip " 192 . 168 . 1 . 5 " ] ; Domain_name " pampas " ; ]
let hosts = [ ] }
let static_host_2 = { Dhcp_server . Config . hostname = " Static host 2 " ; options = [ ] ; hw_addr = mac " 7a : bb : c1 : aa : 50 : 02 " ; fixed_addr = Some ( ip " 192 . 168 . 1 . 152 " ) ; }
let hosts = [ static_host_1 ; static_host_2 ] ) *
module Config = struct open Sexplib . Std type host = { hostname : string ; options : Dhcp_wire . dhcp_option list ; fixed_addr : Ipaddr_sexp . V4 . t option ; hw_addr : Macaddr_sexp . t ; } [ @@ deriving sexp ] type t = { options : Dhcp_wire . dhcp_option list ; hostname : string ; default_lease_time : int32 ; max_lea...
module Lease = struct open Sexplib . Conv open Sexplib . Std module Client_id = struct open Dhcp_wire type t = client_id let compare a b = match a , b with | Hwaddr maca , Hwaddr macb -> Macaddr . compare maca macb | Id ( htype , ida ) , Id ( htype ' , idb ) -> begin match compare htype htype ' with | 0 -> String . com...
module Input = struct open Config open Dhcp_wire let bad_packet fmt = Printf . ksprintf ( fun s -> invalid_arg s ) fmt type result = | Silence | Update of Lease . database | Reply of Dhcp_wire . pkt * Lease . database | Warning of string | Error of string let host_of_mac config mac = Util . find_some @@ fun ( ) -> List...
let ( let * ) = Result . bind
let guard p e = if p then Result . Ok ( ) else Result . Error e
let some_or_invalid f v = match f v with | Some x -> x | None -> invalid_arg ( " Invalid value " ^ ( string_of_int v ) )
let find_option f t = let rec loop = function | [ ] -> None | x :: l -> match f x with | None -> loop l | Some _ as r -> r in loop t
let filter_map f l = List . rev @@ List . fold_left ( fun a v -> match f v with Some v ' -> v ' :: a | None -> a ) [ ] l
let string_nul b = let len = String . length b in let rec loop i = if i = len then true else if ( String . get b i ) <> ( Char . chr 0 ) then false else loop ( succ i ) in loop 0
let cstruct_copy_normalized f buf = let b = f buf in if string_nul b then " " else b
let string_extend_if_le s m = let n = String . length s in if n > m then invalid_arg ( " string is too damn big : " ^ ( string_of_int n ) ) ; s ^ String . make ( m - n ) ( Char . chr 0 ) [ %% cstruct
type dhcp = { op : uint8_t ; htype : uint8_t ; hlen : uint8_t ; hops : uint8_t ; xid : uint32_t ; secs : uint16_t ; flags : uint16_t ; ciaddr : uint32_t ; yiaddr : uint32_t ; siaddr : uint32_t ; giaddr : uint32_t ; chaddr : uint8_t [ @ len 16 ] ; sname : uint8_t [ @ len 64 ] ; file : uint8_t [ @ len 128 ] ; ] [ %% cenu...
type op = | BOOTREQUEST [ @ id 1 ] | BOOTREPLY [ @ id 2 ] [ @@ uint8_t ] [ @@ sexp ] ]
let int_to_op_exn v = some_or_invalid int_to_op v [ %% cenum
type msgtype = | DHCPDISCOVER [ @ id 1 ] | DHCPOFFER | DHCPREQUEST | DHCPDECLINE | DHCPACK | DHCPNAK | DHCPRELEASE | DHCPINFORM | DHCPFORCERENEW | DHCPLEASEQUERY | DHCPLEASEUNASSIGNED | DHCPLEASEUNKNOWN | DHCPLEASEACTIVE | DHCPBULKLEASEQUERY | DHCPLEASEQUERYDONE [ @@ uint8_t ] [ @@ sexp ] ]
let int_to_msgtype_exn v = some_or_invalid int_to_msgtype v [ %% cenum
type option_code = | PAD [ @ id 0 ] | SUBNET_MASK [ @ id 1 ] | TIME_OFFSET [ @ id 2 ] | ROUTERS [ @ id 3 ] | TIME_SERVERS [ @ id 4 ] | NAME_SERVERS [ @ id 5 ] | DNS_SERVERS [ @ id 6 ] | LOG_SERVERS [ @ id 7 ] | COOKIE_SERVERS [ @ id 8 ] | LPR_SERVERS [ @ id 9 ] | IMPRESS_SERVERS [ @ id 10 ] | RSCLOCATION_SERVERS [ @ id...
let int_to_option_code_exn v = some_or_invalid int_to_option_code v
type htype = | Ethernet_10mb | Other [ @@ deriving sexp ]
type flags = | Broadcast | Unicast [ @@ deriving sexp ]
type client_id = | Hwaddr of Macaddr_sexp . t | Id of int * string [ @@ deriving sexp ]
type dhcp_option = | Pad | Subnet_mask of Ipaddr_sexp . V4 . t | Time_offset of int32 | Routers of Ipaddr_sexp . V4 . t list | Time_servers of Ipaddr_sexp . V4 . t list | Name_servers of Ipaddr_sexp . V4 . t list | Dns_servers of Ipaddr_sexp . V4 . t list | Log_servers of Ipaddr_sexp . V4 . t list | Cookie_servers of I...
type pkt = { srcmac : Macaddr_sexp . t ; dstmac : Macaddr_sexp . t ; srcip : Ipaddr_sexp . V4 . t ; dstip : Ipaddr_sexp . V4 . t ; srcport : int ; dstport : int ; op : op ; htype : htype ; hlen : int ; hops : int ; xid : int32 ; secs : int ; flags : flags ; ciaddr : Ipaddr_sexp . V4 . t ; yiaddr : Ipaddr_sexp . V4 . t ...
let options_of_buf buf buf_len = let rec collect buf options = let code = Cstruct . get_uint8 buf 0 in let padding ( ) = collect ( Cstruct . shift buf 1 ) options in match int_to_option_code_exn code with | PAD -> padding ( ) | END -> options | _ -> let len = Cstruct . get_uint8 buf 1 in let body = Cstruct . shift buf ...
let buf_of_options sbuf options = let open Cstruct in let put_code code buf = set_uint8 buf 0 code ; shift buf 1 in let put_len len buf = if len > 255 then invalid_arg ( " option len is too big : " ^ ( string_of_int len ) ) ; set_uint8 buf 0 len ; shift buf 1 in let put_8 v buf = set_uint8 buf 0 v ; shift buf 1 in let ...
let pkt_of_buf buf len = let open Printf in let wrap ( ) = let min_len = sizeof_dhcp + Ethernet . Packet . sizeof_ethernet + Ipv4_wire . sizeof_ipv4 + Udp_wire . sizeof_udp in let * ( ) = guard ( len >= min_len ) ( sprintf " packet is too small : % d < % d " len min_len ) in let * eth_header , eth_payload = Ethernet . ...
let pkt_into_buf pkt buf = let eth , rest = Cstruct . split buf Ethernet . Packet . sizeof_ethernet in let ip , rest ' = Cstruct . split rest Ipv4_wire . sizeof_ipv4 in let udp , dhcp = Cstruct . split rest ' Udp_wire . sizeof_udp in set_dhcp_op dhcp ( op_to_int pkt . op ) ; set_dhcp_htype dhcp ( if pkt . htype = Ether...
let buf_of_pkt pkg = let dhcp = Cstruct . create 2048 in let l = pkt_into_buf pkg dhcp in Cstruct . sub dhcp 0 l
let is_dhcp buf _len = let aux buf = let * eth_header , eth_payload = Ethernet . Packet . of_cstruct buf in match eth_header . Ethernet . Packet . ethertype with | ` ARP | ` IPv6 -> Ok false | ` IPv4 -> let * ipv4_header , ipv4_payload = Ipv4_packet . Unmarshal . of_cstruct eth_payload in match Ipv4_packet . Unmarshal ...
let collect_options f options = filter_map f options |> List . flatten
let client_id_of_pkt pkt = match find_option ( function Client_id id -> Some id | _ -> None ) pkt . options with | Some id -> id | None -> Hwaddr pkt . chaddr
let to_hum f x = Sexplib . Sexp . to_string_hum ( f x )
let client_id_to_string = to_hum sexp_of_client_id
let pkt_to_string = to_hum sexp_of_pkt
let dhcp_option_to_string = to_hum sexp_of_dhcp_option
let find_subnet_mask = find_option ( function Subnet_mask x -> Some x | _ -> None )
let find_time_offset = find_option ( function Time_offset x -> Some x | _ -> None )
let collect_routers = collect_options ( function Routers x -> Some x | _ -> None )
let collect_time_servers = collect_options ( function Time_servers x -> Some x | _ -> None )
let collect_name_servers = collect_options ( function Name_servers x -> Some x | _ -> None )
let collect_dns_servers = collect_options ( function Dns_servers x -> Some x | _ -> None )
let collect_log_servers = collect_options ( function Log_servers x -> Some x | _ -> None )
let collect_cookie_servers = collect_options ( function Cookie_servers x -> Some x | _ -> None )
let collect_lpr_servers = collect_options ( function Lpr_servers x -> Some x | _ -> None )
let collect_impress_servers = collect_options ( function Impress_servers x -> Some x | _ -> None )
let collect_rsc_location_servers = collect_options ( function Rsclocation_servers x -> Some x | _ -> None )
let find_hostname = find_option ( function Hostname x -> Some x | _ -> None )
let find_bootfile_size = find_option ( function Bootfile_size x -> Some x | _ -> None )
let find_merit_dumpfile = find_option ( function Merit_dumpfile x -> Some x | _ -> None )
let find_domain_name = find_option ( function Domain_name x -> Some x | _ -> None )
let find_swap_server = find_option ( function Swap_server x -> Some x | _ -> None )
let find_root_path = find_option ( function Root_path x -> Some x | _ -> None )
let find_extension_path = find_option ( function Extension_path x -> Some x | _ -> None )
let find_ipforwarding = find_option ( function Ipforwarding x -> Some x | _ -> None )
let find_nlsr = find_option ( function Nlsr x -> Some x | _ -> None )
let collect_policy_filters = collect_options ( function Policy_filters x -> Some x | _ -> None )
let find_max_datagram = find_option ( function Max_datagram x -> Some x | _ -> None )
let find_default_ip_ttl = find_option ( function Default_ip_ttl x -> Some x | _ -> None )
let find_pmtu_ageing_timo = find_option ( function Pmtu_ageing_timo x -> Some x | _ -> None )
let find_pmtu_plateau_table = find_option ( function Pmtu_plateau_table x -> Some x | _ -> None )
let find_interface_mtu = find_option ( function Interface_mtu x -> Some x | _ -> None )
let find_all_subnets_local = find_option ( function All_subnets_local x -> Some x | _ -> None )
let find_broadcast_addr = find_option ( function Broadcast_addr x -> Some x | _ -> None )
let find_perform_mask_discovery = find_option ( function Perform_mask_discovery x -> Some x | _ -> None )
let find_mask_supplier = find_option ( function Mask_supplier x -> Some x | _ -> None )
let find_perform_router_disc = find_option ( function Perform_router_disc x -> Some x | _ -> None )
let find_router_sol_addr = find_option ( function Router_sol_addr x -> Some x | _ -> None )
let collect_static_routes = collect_options ( function Static_routes x -> Some x | _ -> None )
let find_trailer_encapsulation = find_option ( function Trailer_encapsulation x -> Some x | _ -> None )
let find_arp_cache_timo = find_option ( function Arp_cache_timo x -> Some x | _ -> None )
let find_ethernet_encapsulation = find_option ( function Ethernet_encapsulation x -> Some x | _ -> None )
let find_tcp_default_ttl = find_option ( function Tcp_default_ttl x -> Some x | _ -> None )
let find_tcp_keepalive_interval = find_option ( function Tcp_keepalive_interval x -> Some x | _ -> None )
let find_tcp_keepalive_garbage = find_option ( function Tcp_keepalive_garbage x -> Some x | _ -> None )
let find_nis_domain = find_option ( function Nis_domain x -> Some x | _ -> None )
let collect_nis_servers = collect_options ( function Nis_servers x -> Some x | _ -> None )
let collect_ntp_servers = collect_options ( function Ntp_servers x -> Some x | _ -> None )
let find_vendor_specific = find_option ( function Vendor_specific x -> Some x | _ -> None )
let collect_netbios_name_servers = collect_options ( function Netbios_name_servers x -> Some x | _ -> None )
let collect_netbios_datagram_distrib_servers = collect_options ( function Netbios_datagram_distrib_servers x -> Some x | _ -> None )
let find_netbios_node = find_option ( function Netbios_node x -> Some x | _ -> None )
let find_netbios_scope = find_option ( function Netbios_scope x -> Some x | _ -> None )
let collect_xwindow_font_servers = collect_options ( function Xwindow_font_servers x -> Some x | _ -> None )
let collect_xwindow_display_managers = collect_options ( function Xwindow_display_managers x -> Some x | _ -> None )
let find_request_ip = find_option ( function Request_ip x -> Some x | _ -> None )
let find_ip_lease_time = find_option ( function Ip_lease_time x -> Some x | _ -> None )
let find_option_overload = find_option ( function Option_overload x -> Some x | _ -> None )
let find_message_type = find_option ( function Message_type x -> Some x | _ -> None )
let find_server_identifier = find_option ( function Server_identifier x -> Some x | _ -> None )
let find_parameter_requests = find_option ( function Parameter_requests x -> Some x | _ -> None )