123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212(* OCaml promise library
* http://www.ocsigen.org/lwt
* Copyright (C) 2005-2008 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, with linking exceptions;
* either version 2.1 of the License, or (at your option) any later
* version. See COPYING file for details.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*)openLwt.Infixtypet=Plain|SSLofSsl.sockettypesocket=Lwt_unix.file_descr*ttypeuninitialized_socket=Lwt_unix.file_descr*Ssl.socketletssl_socket(_fd,kind)=matchkindwith|Plain->None|SSLsocket->Somesocketletssl_socket_of_uninitialized_socket(_fd,socket)=socketletis_ssls=matchsndswithPlain->false|SSL_->trueletwrap_callf()=tryf()with(Ssl.Connection_errorerr|Ssl.Accept_errorerr|Ssl.Read_errorerr|Ssl.Write_errorerr)ase->(matcherrwithSsl.Error_want_read->raiseLwt_unix.Retry_read|Ssl.Error_want_write->raiseLwt_unix.Retry_write|_->raisee)[@ocaml.warning"-4"]letrepeat_callfdf=(* See https://github.com/ocsigen/lwt/issues/530. *)Lwt_unix.blockingfd>>=fun_blocking->tryLwt.return(wrap_callf())withLwt_unix.Retry_read->Lwt_unix.register_actionLwt_unix.Readfd(wrap_callf)|Lwt_unix.Retry_write->Lwt_unix.register_actionLwt_unix.Writefd(wrap_callf)|e->Lwt.faile(****)letplainfd=(fd,Plain)letembed_socketfdcontext=(fd,SSL(Ssl.embed_socket(Lwt_unix.unix_file_descrfd)context))letembed_uninitialized_socketfdcontext=(fd,Ssl.embed_socket(Lwt_unix.unix_file_descrfd)context)letssl_acceptfdctx=letsocket=Ssl.embed_socket(Lwt_unix.unix_file_descrfd)ctxinLwt.bind(repeat_callfd(fun()->Ssl.acceptsocket))(fun()->Lwt.return(fd,SSLsocket))letssl_connectfdctx=letsocket=Ssl.embed_socket(Lwt_unix.unix_file_descrfd)ctxinLwt.bind(repeat_callfd(fun()->Ssl.connectsocket))(fun()->Lwt.return(fd,SSLsocket))letssl_accept_handshake(fd,socket)=Lwt.bind(repeat_callfd(fun()->Ssl.acceptsocket))(fun()->Lwt.return(fd,SSLsocket))letssl_perform_handshake(fd,socket)=Lwt.bind(repeat_callfd(fun()->Ssl.connectsocket))(fun()->Lwt.return(fd,SSLsocket))letread(fd,s)bufposlen=matchswith|Plain->Lwt_unix.readfdbufposlen|SSLs->iflen=0thenLwt.return0elserepeat_callfd(fun()->trySsl.readsbufposlenwithSsl.Read_errorSsl.Error_zero_return->0)letread_bytes(fd,s)bufposlen=matchswith|Plain->Lwt_bytes.readfdbufposlen|SSLs->iflen=0thenLwt.return0elserepeat_callfd(fun()->trySsl.read_into_bigarraysbufposlenwithSsl.Read_errorSsl.Error_zero_return->0)letwrite(fd,s)bufposlen=matchswith|Plain->Lwt_unix.writefdbufposlen|SSLs->iflen=0thenLwt.return0elserepeat_callfd(fun()->Ssl.writesbufposlen)letwrite_bytes(fd,s)bufposlen=matchswith|Plain->Lwt_bytes.writefdbufposlen|SSLs->iflen=0thenLwt.return0elserepeat_callfd(fun()->Ssl.write_bigarraysbufposlen)letwait_read(fd,s)=matchswithPlain->Lwt_unix.wait_readfd|SSL_->Lwt.pause()letwait_write(fd,s)=matchswithPlain->Lwt_unix.wait_writefd|SSL_->Lwt.pause()letssl_shutdown(fd,s)=matchswithPlain->Lwt.return_unit|SSLs->repeat_callfd(fun()->Ssl.shutdowns)letshutdown(fd,_)cmd=Lwt_unix.shutdownfdcmdletclose_notify=function|(_,Plain)ass->shutdownsUnix.SHUTDOWN_SEND;Lwt.return_true|(fd,SSLs)->repeat_callfd(fun()->Ssl.close_notifys)letclose(fd,_)=Lwt_unix.closefdletabort(fd,_)=Lwt_unix.abortfdletshutdown_and_closes=ssl_shutdowns>>=fun()->Lwt.wrap2shutdownsUnix.SHUTDOWN_ALL>>=fun()->closesletout_channel_of_descr?buffers=Lwt_io.make?buffer~mode:Lwt_io.output~close:(fun()->shutdown_and_closes)(funbufposlen->write_bytessbufposlen)letin_channel_of_descr?buffers=Lwt_io.make?buffer~mode:Lwt_io.input~close:(fun()->shutdown_and_closes)(funbufposlen->read_bytessbufposlen)letget_fd(fd,_socket)=fdletget_unix_fd(fd,socket)=matchsocketwith|Plain->Lwt_unix.unix_file_descrfd|SSLsocket->(Ssl.file_descr_of_socketsocket)letgetsocknames=Unix.getsockname(get_unix_fds)letgetpeernames=Unix.getpeername(get_unix_fds)