From c7a908cc5adede6ca34519059f2e324dc6737ff6 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Fri, 1 May 2020 11:21:05 -0400 Subject: SSL network connections now seem to work, including on windows --- test_ssl.f90 | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'test_ssl.f90') diff --git a/test_ssl.f90 b/test_ssl.f90 index 04386d4..9c22895 100644 --- a/test_ssl.f90 +++ b/test_ssl.f90 @@ -1,28 +1,44 @@ program test_ssl +use iso_c_binding use jessl use network +use request use wsa_network, only: windows_network_startup => startup implicit none type(sockaddr_in), target::sa integer::s + character(*), parameter:: request_attempt = & + "gemini://gemini.circumlinar.space/"//C_CARRIAGE_RETURN//C_NEW_LINE + + !"GET / HTTP/1.1"//C_CARRIAGE_RETURN//C_NEW_LINE& + !//"Host: rainbow-100.com"//C_CARRIAGE_RETURN//C_NEW_LINE& + !//C_CARRIAGE_RETURN//C_NEW_LINE + type(simple_hostent)::hent + type(c_ptr)::ssl_method, ssl_ctx, ssl + + character, dimension(bufsize)::buffer + integer::bytes_received, i + call windows_network_startup() - hent = gethostbyname("google.com") + hent = gethostbyname("gemini.circumlunar.space") + !"rainbow-100.com") + if(allocated(hent%h_name)) then Print *, "host: ", hent%h_name - Print *, "addr: ", hent%h_addr4 + Print *, "addr: ", inet_ntoa(hent%h_addr4) else Print *, "Failure" stop end if sa%sin_family = AF_INET - sa%sin_addr%s_addr = hent%h_addr4 !inet_addr("192.168.2.90") - sa%sin_port = htons(80) + sa%sin_addr%s_addr = hent%h_addr4 + sa%sin_port = htons(1965) !443) s = socket(AF_INET, SOCK_STREAM, 0); if(.not. connect(s, sa)) then @@ -32,5 +48,41 @@ implicit none Print *, "socket opened!" + call library_init() + ssl_method = tls_v1_3_client_method() + ssl_ctx = ctx_new(ssl_method) + ssl = ssl_new(ssl_ctx) + + if(set_fd(ssl, s) == 1) then + Print *, "FD set" + if(ssl_connect(ssl) == 1) then + Print *, "Connected via SSL" + + + if(send_string(ssl, request_attempt, trimming=.false.)) then + Print *, "Message sent" + + bytes_received = retrieve_characters(ssl, buffer) + do while(bytes_received > 0) + do i=1, bytes_received + write(*, '(A1)', advance='no') buffer(i) + end do + bytes_received = retrieve_characters(ssl, buffer) + end do + + Print *, " " + Print *, "DONE" + else + Print *, "Could not send request" + end if + + else + Print *, "Did not complete the ssl handshake..." + end if + else + Print *, "No fd set" + end if + + call close_socket(s) end program test_ssl \ No newline at end of file -- cgit v1.2.3