Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Tom Wilkie <tom@acunu.com>
To: Pierre Chopin <pierrchp@free.fr>
Cc: Tom Wilkie <tom@acunu.com>, caml-list@inria.fr
Subject: Re: [Caml-list] ocaml-ssl
Date: Fri, 25 Feb 2011 21:29:32 +0000	[thread overview]
Message-ID: <1FC332F1-8F6C-49B2-8224-368EB1A4B602@acunu.com> (raw)
In-Reply-To: <72F1256E-1783-45EC-8A18-EA286B290E51@free.fr>

You shouldn't do the first accept; I think you should do it like this in the server:

let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in

Unix.bind fd addr;
Unix.listen fd 10;

while true do
	let sock, _ = Unix.accept fd in
	let ssl = Ssl.embed_socket sock ctx in
		Ssl.accept ssl;
		Ssl.read ssl etc...
done

And the client should do:

let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
	Unix.connect fd address;

	let ssl = Ssl.embed_socket fd context in
		Ssl.connect ssl;
		Ssl.write ssl etc...

HTH

Tom

On 24 Feb 2011, at 19:11, Pierre Chopin wrote:

> Hi,
> 
> I am trying to get familiar with the ocaml-ssl bindings for Opensll. 
> 
> Thus, I am trying to establish an SSL connection between a server and a client which are on my computer.
> 
> I  therefore create two sockets, establish a tcp connection between then, and then fail to establish SSL communication.
> 
> The function Ssl.accept on the server will return Accept_error Error_want_read, which I believe is due to the fact that somehow the socket is used in non-blocking mode. 
> 
> I therefore made a loop to constantly check for accepting connection. I am not familiar with non blocking mode so I don't know if it is right. Here is the code for the server.
> 
> 
> (*
> #directory "/opt/local/lib/ocaml/site-lib/ssl/";;
> #load "ssl.cma" ;;
> #load "unix.cma"
> *)
> 
> open Ssl ;;
> open Unix;;
> init () ;;
> let domain = PF_INET ;;
> let ty = SOCK_STREAM ;; 
> 
> let usock = socket domain ty 0;;
> let name =Unix.gethostname () ;;
> let h = Unix.gethostbyname name ;;
> let inet = h.h_addr_list.(0) ;;
> let cont = create_context SSLv3 Server_context ;;
> let sock_addr = Unix.ADDR_INET (inet,22211) ;;
> bind usock sock_addr ;;
> listen usock 3 ;;
> accept usock ;;
> print_string "TCP connection established\n" ;;
> Pervasives.flush Pervasives.stdout ;;
> let sock = embed_socket usock cont ;;
> 
> 
> 
> while true do
> try
> Ssl.accept sock ;;
> print_endline "bing" ;
> Pervasives.flush Pervasives.stdout
> with Ssl.Accept_error Error_want_read ->
> sleep 1;
> print_endline "looping" ;Pervasives.flush Pervasives.stdout
> done;
> 
> 
> 
> Concerning the client, it's pretty much a mirror of the server, except for the loop:
> 
> 
> #directory "/opt/local/lib/ocaml/site-lib/ssl/";;
> #load "/opt/local/lib/ocaml/site-lib/ssl/ssl.cma" ;;
> #load "unix.cma"
> 
> open Ssl ;;
> open Unix;;
> init () ;;
> let domain = PF_INET ;;
> let ty = SOCK_STREAM ;; 
> let usock = socket domain ty 0;;
> let name =Unix.gethostname () ;;
> let h = Unix.gethostbyname name ;;
> let inet = h.h_addr_list.(0) ;;
> let cont = create_context SSLv3 Client_context ;;
> let sock_addr = Unix.ADDR_INET (inet,22211) ;;
> connect usock sock_addr;;
> 
> let sock =embed_socket usock cont  ;;
> let usock2 = file_descr_of_socket sock ;;
> Ssl.connect sock ;;
> 
> That last function never returns, and the server keeps "looping", until i kill the process. Any idea why?
> 
> Sincerly, Pierre
> 
> 
> 
> 
> -- 
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 



  reply	other threads:[~2011-02-25 21:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-24 19:11 Pierre Chopin
2011-02-25 21:29 ` Tom Wilkie [this message]
2011-02-27 20:48 ` Gregory Bellier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1FC332F1-8F6C-49B2-8224-368EB1A4B602@acunu.com \
    --to=tom@acunu.com \
    --cc=caml-list@inria.fr \
    --cc=pierrchp@free.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox