Skip to content

Commit

Permalink
Added support for compiling under Win32 (thanks David Allsopp).
Browse files Browse the repository at this point in the history
  • Loading branch information
smimram committed Oct 16, 2011
1 parent 949f58a commit ecd9a54
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
=====
* Added write_certificate function.
* Remove support for SSLv2, which was dropped upstream (thanks Dario Teixeira).
* Added support for compiling under Win32 (thanks David Allsopp), see
README.win32.

0.4.5 (2011-03-01)
=====
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
OCaml-ssl - OCaml bindings for the libssl
OCaml-SSL - OCaml bindings for the libssl
=========================================

Author: Samuel Mimram <samuel.mimram@ens-lyon.org>
Expand Down
36 changes: 36 additions & 0 deletions README.win32
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
OCaml-SSL - OCaml bindings for the libssl
=========================================

Windows Installation Instructions
=================================

Author: David Allsopp <david.allsopp@metastack.com>

Note - these instructions are only for the MinGW port of OCaml (tested with
3.12.0 and OpenSSL 1.0.0d).


Dependencies
============

These instructions allow compilation of OpenSSL from sources using the MinGW
compiler instead of Cygwin's. You will need an installation of Cygwin with at
least the core GCC suite (with MinGW compiler), GNU Make and Perl

Extract the OpenSSL sources from the tarball and from that directory run:

perl Configure mingw shared --prefix=C:/ocamlmgw
make
make install

This will install an OCaml compiled OpenSSL library to C:\ocamlmgw


Installation
============

To compile the OCaml-SSL, run:

./configure CC='gcc -mno-cygwin' LDFLAGS=-LC:/ocamlmgw/lib CFLAGS=-IC:/ocamlmgw/include
make
make install
11 changes: 6 additions & 5 deletions src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#
# by Samuel Mimram

# $Id$


export OCAMLMAKEFILE = OCamlMakefile

Expand All @@ -16,15 +14,20 @@ export PS2PDF = @PS2PDF@
export BEST = @BEST@
export OCAMLLIBPATH = @CAMLLIBPATH@

ifeq (Win32, $(shell ocamlc -config | fgrep os_type | sed -e "s/.*: //"))
export WIN32 = 1
endif

#SOURCES = ssl_stubs.c ssl.mli ssl.ml ssl_threads.ml
export RES_CLIB_SUF = _stubs
export RESULT = ssl
export LIBINSTALL_FILES = $(wildcard *.mli *.cmi *.cma *.cmxa *.cmx *.a *.so)
export LIBINSTALL_FILES = $(wildcard *.mli *.cmi *.cma *.cmxa *.cmx *$(shell ocamlc -config | fgrep ext_lib | sed -e "s/.*: //") *$(shell ocamlc -config | fgrep ext_dll | sed -e "s/.*: //"))
export OCAMLLDFLAGS =
export OCAMLDOCFLAGS = -stars
export CLIBS = crypto ssl
export CC = @CC@
export CFLAGS = @CFLAGS@ -ansi -DCAML_NAME_SPACE -Wall
export CLIBFLAGS = @LDFLAGS@
export CPPFLAGS = @CPPFLAGS@
export NO_CUSTOM = yes
OCAMLFLAGS = @OCAMLFLAGS@
Expand Down Expand Up @@ -63,5 +66,3 @@ update: uninstall install
# Catch-all target will be applied to all subprojects automatically
%:
@$(MAKE) -f $(OCAMLMAKEFILE) subprojs SUBTARGET=$@

#-include $(OCAMLMAKEFILE)
2 changes: 1 addition & 1 deletion src/OCamlMakefile
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ ifneq ($(strip $(OBJ_LINK)),)
endif

ifdef WIN32
DLLSONAME := $(CLIB_BASE).dll
DLLSONAME := dll$(CLIB_BASE).dll
else
DLLSONAME := dll$(CLIB_BASE).so
endif
Expand Down
67 changes: 67 additions & 0 deletions src/ssl_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@
#include <caml/memory.h>
#include <caml/mlvalues.h>
#include <caml/signals.h>
#include <caml/unixsupport.h>

#include <openssl/ssl.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/crypto.h>

#ifdef WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif

static int client_verify_callback(int, X509_STORE_CTX *);

Expand Down Expand Up @@ -101,6 +106,52 @@ static struct custom_operations socket_ops =
* Initialization *
******************/

#ifdef WIN32
struct CRYPTO_dynlock_value
{
HANDLE mutex;
};

static HANDLE *mutex_buf = NULL;

static void locking_function(int mode, int n, const char *file, int line)
{
if (mode & CRYPTO_LOCK)
WaitForSingleObject(mutex_buf[n], INFINITE);
else
ReleaseMutex(mutex_buf[n]);
}

static struct CRYPTO_dynlock_value *dyn_create_function(const char *file, int line)
{
struct CRYPTO_dynlock_value *value;

value = malloc(sizeof(struct CRYPTO_dynlock_value));
if (!value)
return NULL;
if (!(value->mutex = CreateMutex(NULL, FALSE, NULL)))
{
free(value);
return NULL;
}

return value;
}

static void dyn_lock_function(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)
{
if (mode & CRYPTO_LOCK)
WaitForSingleObject(l->mutex, INFINITE);
else
ReleaseMutex(l->mutex);
}

static void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char *file, int line)
{
CloseHandle(l->mutex);
free(l);
}
#else
struct CRYPTO_dynlock_value
{
pthread_mutex_t mutex;
Expand Down Expand Up @@ -146,6 +197,7 @@ static void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char *fil
pthread_mutex_destroy(&l->mutex);
free(l);
}
#endif

CAMLprim value ocaml_ssl_init(value use_threads)
{
Expand All @@ -156,12 +208,23 @@ CAMLprim value ocaml_ssl_init(value use_threads)

if(Int_val(use_threads))
{
#ifdef WIN32
mutex_buf = malloc(CRYPTO_num_locks() * sizeof(HANDLE));
#else
mutex_buf = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
#endif
assert(mutex_buf);
for (i = 0; i < CRYPTO_num_locks(); i++)
#ifdef WIN32
mutex_buf[i] = CreateMutex(NULL, FALSE, NULL);
#else
pthread_mutex_init(&mutex_buf[i], NULL);
#endif
CRYPTO_set_locking_callback(locking_function);
#ifndef WIN32
/* Windows does not require id_function, see threads(3) */
CRYPTO_set_id_callback(id_function);
#endif
CRYPTO_set_dynlock_create_callback(dyn_create_function);
CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);
Expand Down Expand Up @@ -669,7 +732,11 @@ CAMLprim value ocaml_ssl_embed_socket(value socket_, value context)
{
CAMLparam1(context);
CAMLlocal1(block);
#ifdef Socket_val
SOCKET socket = Socket_val(socket_);
#else
int socket = Int_val(socket_);
#endif
SSL_CTX *ctx = Ctx_val(context);
SSL *ssl;

Expand Down

0 comments on commit ecd9a54

Please sign in to comment.