1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253(*
* Copyright (c) 2015-2022 Christiano F. Haesbaert <haesbaert@haesbaert.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)[@@@warning"-32-37"]moduleLowlevel=Rawlink_lowleveltypet={fd:Unix.file_descr;packets:Cstruct.tlistref;buffer:Cstruct.t;}letdhcp_server_filter=Lowlevel.dhcp_server_filterletdhcp_client_filter=Lowlevel.dhcp_client_filterletopen_link?filter?(promisc=false)ifname={fd=Lowlevel.opensock?filter~promiscifname;packets=ref[];buffer=Cstruct.create65536}letclose_linkt=Unix.closet.fdletsend_packettbuf=letlen=Cstruct.lengthbufinletn=Unix.writet.fd(Cstruct.to_bytesbuf)0leninifn=0thenraise(Unix.Unix_error(Unix.EPIPE,"send_packet: socket closed",""))elseifn<>lenthenraise(Unix.Unix_error(Unix.ENOBUFS,"send_packet: short write",""))letrecread_packett=match!(t.packets)with|hd::tl->t.packets:=tl;hd|[]->letn=Lowlevel.unix_bytes_readt.fdt.buffer.Cstruct.buffer0t.buffer.Cstruct.leninifn=0thenfailwith"Link socket closed";t.packets:=Lowlevel.process_inputt.buffern;read_packett