aboutsummaryrefslogtreecommitdiff
path: root/test_ssl.f90
blob: 9c228951d91e1b27a6fba34a4dde4d680132eb50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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("gemini.circumlunar.space")
    !"rainbow-100.com")
    
    if(allocated(hent%h_name)) then
        Print *, "host: ", hent%h_name
        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
    sa%sin_port = htons(1965)  !443) 
    
    s = socket(AF_INET, SOCK_STREAM, 0);
    if(.not. connect(s, sa)) then
        Print *, "Connection failed", IERRNO()
        stop
    end if
    
    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