X-Git-Url: https://git.hcoop.net/hcoop/domtool2.git/blobdiff_plain/07cc384cf2e6e3589f2892026a9b8f9835c8eb2c..3b2676435dc4af39acd77e7fe232902e6651e42d:/src/main.sml?ds=sidebyside diff --git a/src/main.sml b/src/main.sml index bcadb63..cc5b5b5 100644 --- a/src/main.sml +++ b/src/main.sml @@ -26,12 +26,6 @@ structure SM = StringMap val dmy = ErrorMsg.dummyLoc -fun init () = (F_OpenSSL_SML_add_all_algorithms.f' (); - F_OpenSSL_SML_load_error_strings.f' (); - F_OpenSSL_SML_load_BIO_strings.f' ()) - -val () = init () - val defaultT : record ref = ref SM.empty val defaultV : (unit -> exp) SM.map ref = ref SM.empty @@ -139,111 +133,88 @@ fun eval fname = Eval.exec (SM.map (fn f => f ()) (!defaultV)) body' | NONE => () -val dispatcher : C.rw ZString.zstring' = - ZString.dupML' (Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort) - -val listenOn : C.rw ZString.zstring' = - ZString.dupML' ("localhost:" ^ Int.toString Config.dispatcherPort) +val dispatcher = + Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort fun ssl_err s = let val err = F_OpenSSL_SML_get_error.f () + + val lib = F_OpenSSL_SML_lib_error_string.f err + val func = F_OpenSSL_SML_func_error_string.f err + val reason = F_OpenSSL_SML_reason_error_string.f err in print s; print "\nReason: "; - print (ZString.toML (F_OpenSSL_SML_lib_error_string.f err)); - print ":"; - print (ZString.toML (F_OpenSSL_SML_func_error_string.f err)); - print ":"; - print (ZString.toML (F_OpenSSL_SML_reason_error_string.f err)); + if C.Ptr.isNull lib then + () + else + (print (ZString.toML lib); + print ":"); + if C.Ptr.isNull func then + () + else + (print (ZString.toML func); + print ":"); + if C.Ptr.isNull reason then + () + else + print (ZString.toML reason); print "\n" end -exception OpenSSL of string - -val readBuf : (C.uchar, C.rw) C.obj C.ptr' = C.alloc' C.S.uchar (Word.fromInt Config.bufSize) -val bufSize = Int32.fromInt Config.bufSize -fun readOne bio = +fun request fname = let - val r = F_OpenSSL_SML_read.f' (bio, C.Ptr.inject' readBuf, bufSize) - in - if r = 0 then - NONE - else if r < 0 then - raise OpenSSL "BIO_read failed" - else - SOME (CharVector.tabulate (Int32.toInt r, - fn i => chr (Word32.toInt (C.Get.uchar' - (C.Ptr.sub' C.S.uchar (readBuf, i)))))) - end + val context = OpenSSL.context ("/home/adamc/fake/clientcert.pem", + "/home/adamc/fake/clientkey.pem", + Config.trustStore) -fun writeAll (bio, s) = - let - val buf = ZString.dupML' s + val bio = OpenSSL.connect (context, dispatcher) - fun loop (buf, len) = - let - val r = F_OpenSSL_SML_write.f' (bio, C.Ptr.inject' buf, len) - in - if r = len then - () - else if r <= 0 then - (C.free' buf; - raise OpenSSL "BIO_write failed") - else - loop (C.Ptr.|+! C.S.uchar (buf, Int32.toInt r), Int32.- (len, r)) - end - in - loop (buf, Int32.fromInt (size s)); - C.free' buf - end + val _ = print ("Subject: " ^ OpenSSL.peerCN bio ^ "\n") -fun request fname = - let - val bio = F_OpenSSL_SML_new_connect.f' dispatcher + val inf = TextIO.openIn fname + + fun loop () = + case TextIO.inputLine inf of + NONE => () + | SOME line => (OpenSSL.writeAll (bio, line); + loop ()) in - if C.Ptr.isNull' bio then - (ssl_err ("Error initializating connection to dispatcher at " ^ Config.dispatcher); - F_OpenSSL_SML_free_all.f' bio) - else if F_OpenSSL_SML_do_connect.f' bio <= 0 then - (ssl_err ("Error connecting to dispatcher at " ^ Config.dispatcher); - F_OpenSSL_SML_free_all.f' bio) - else let - val inf = TextIO.openIn fname - - fun loop () = - case TextIO.inputLine inf of - NONE => () - | SOME line => (writeAll (bio, line); - loop ()) - in - loop (); - TextIO.closeIn inf; - F_OpenSSL_SML_free_all.f' bio - end + loop (); + TextIO.closeIn inf; + OpenSSL.close bio end -fun serviceOne () = +fun service () = let - val bio = F_OpenSSL_SML_new_accept.f' listenOn + val context = OpenSSL.context (Config.serverCert, + Config.serverKey, + Config.trustStore) + + val sock = OpenSSL.listen (Config.dispatcherPort, Config.queueSize) + + fun loop () = + case OpenSSL.accept (context, sock) of + NONE => () + | SOME bio => + let + val _ = print ("Subject: " ^ OpenSSL.peerCN bio ^ "\n") + + fun loop' () = + case OpenSSL.readOne bio of + NONE => () + | SOME line => (print line; + loop' ()) + in + loop' (); + OpenSSL.close bio; + loop () + end in - if C.Ptr.isNull' bio then - (ssl_err "Error initializating listener"; - F_OpenSSL_SML_free_all.f' bio) - else if F_OpenSSL_SML_do_accept.f' bio <= 0 then - (ssl_err "Error accepting connection"; - F_OpenSSL_SML_free_all.f' bio) - else let - fun loop () = - case readOne bio of - NONE => () - | SOME line => (print line; - loop ()) - in - loop (); - F_OpenSSL_SML_free_all.f' bio - end + loop (); + OpenSSL.shutdown sock end end