123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107openLwt.Infixletsocket_path="/var/run/current-iptables-daemon/current-iptables-daemon.sock"moduleWire=structletsafe_closefd=Lwt.catch(fun()->Lwt_unix.closefd)(fun_->Lwt.return_unit)letconnect()=letsockaddr=Lwt_unix.ADDR_UNIXsocket_pathinletc=Lwt_unix.(socketPF_UNIXSOCK_STREAM0)inLwt_unix.set_close_on_execc;Lwt.catch(fun()->Lwt_unix.(connectcsockaddr)>|=fun()->c)(fune->Logs.warn(funm->m"error %s connecting to socket %s"(Printexc.to_stringe)socket_path);safe_closec>|=fun()->raisee)letwrite~socketdata=letopenLwt.Infixinletwrite_rawbuf=letrecwoffl=Lwt.catch(fun()->Lwt_unix.sendsocketbufoffl[]>>=funn->ifn=lthenLwt.return()elsew(off+n)(l-n))(fune->Logs.err(funm->m"exception %s while writing"(Printexc.to_stringe));safe_closesocket>|=fun()->raisee)inw0(Bytes.lengthbuf)inletdlen=Cstruct.create4inCstruct.BE.set_uint32dlen0(Int32.of_int(Cstruct.lengthdata));letbytes=Cstruct.(to_bytes(appenddlendata))inwrite_rawbytesletread~socket=letopenLwt.Infixinletbuf=Bytes.create4inletrecrbil=Lwt.catch(fun()->Lwt_unix.readsocketbil>>=function|0->Logs.debug(funm->m"end of file while reading");Lwt.return(Error`Eof)|nwhenn==l->Lwt.return(Ok())|nwhenn<l->rb(i+n)(l-n)|_->Logs.err(funm->m"read too much, shouldn't happen)");Lwt.return(Error`Toomuch))(fune->leterr=Printexc.to_stringeinLogs.err(funm->m"exception %s while reading"err);safe_closesocket>|=fun()->Error`Exception)inrbuf04>>=function|Errore->Lwt.return(Errore)|Ok()->letlen=Cstruct.BE.get_uint32(Cstruct.of_bytesbuf)0iniflen>0lthenletb=Bytes.create(Int32.to_intlen)inrb0(Int32.to_intlen)>|=function|Errore->Errore|Ok()->Ok(Cstruct.of_bytesb)elseLwt.return(Error`Eof)endtypewire_error=[`Eof|`Toomuch|`Exception|`Parseofstring]typesocket=Lwt_unix.file_descrletqueryrpc~socketx=letopenLwt.Syntaxinlet(let**)=Lwt_result.bindinletmoduleRpc=Iptables_daemon_api.Rpcinlettag=Rpc.Tag.vrpcinletinj,proj=Rpc.get_clientrpcinletreq=injx|>Rpc.Tag.(addtag)inlet*()=Wire.write~socketreqinlet**response=Wire.read~socketinlettag',response=Rpc.Tag.stripresponseinassert(tag=tag');Lwt.return(projresponse|>Result.map_error(fune->(e:>wire_error)))moduleIpManager=structmoduleSpec=Iptables_daemon_api.Specletlist=querySpec.IpManager.listletrequest=querySpec.IpManager.requestletremove=querySpec.IpManager.freeendmoduleDeployments=structmoduleSpec=Iptables_daemon_api.Specletlist=querySpec.Deployments.listletcreate=querySpec.Deployments.createletremove=querySpec.Deployments.deleteendletconnect=Wire.connectletclose=Wire.safe_close