aboutsummaryrefslogtreecommitdiff
path: root/network.F90
blob: 8e77b9d45d6206724f803696f46ade865515787b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
! Copyright (c) 2020 Jeffrey Armstrong <jeff@rainbow-100.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

module network 
use iso_c_binding
implicit none

    integer(kind=c_int), parameter::AF_INET  = 2
    integer(kind=c_int), parameter::AF_INET6 = 10
    integer(kind=c_int), parameter::AF_UNIX  = 1
    
    integer(kind=c_int), parameter::SOCK_STREAM = 1
    
    integer, parameter::socket_timeout_ms    = 3000
    
#ifdef WINDOWS
    integer, parameter::hostent_int_kind = c_short
#else
    integer, parameter::hostent_int_kind = c_int
#endif
    
    type, bind(c) :: in_addr
        integer(kind=c_int32_t)::s_addr
    end type
    
    type, bind(c) :: sockaddr_in
        integer(kind=c_short)::sin_family
        integer(kind=c_int16_t)::sin_port
        type(in_addr)::sin_addr
        !integer(kind=c_int32_t)::s_addr
    end type
    
    type, bind(c) :: hostent_c
        type(c_ptr)::h_name                             !official name of host */
        type(c_ptr)::h_aliases                          !alias list */
        integer(kind=hostent_int_kind):: h_addrtype     !host address type */
        integer(kind=hostent_int_kind):: h_length       !length of address */
        type(c_ptr)::h_addr_list                        !list of addresses */
    end type 
    
    ! Let's keep this simple...
    type :: simple_hostent
        character(len=:), allocatable::h_name
        integer::h_addrtype
        integer(kind=c_int32_t)::h_addr4
        integer(kind=c_int64_t)::h_addr6
    end type 
    
#ifndef WINDOWS
    type, bind(c) :: timeval
        integer(kind=c_long)::seconds
        integer(kind=c_long)::useconds
    end type
#endif
    
    integer(kind=c_size_t), parameter::sockaddr_size = 56
    
    interface
        function socket_c(i, j, k) bind(c, name="socket")
        use iso_c_binding
        integer(kind=c_int), value::i, j, k
        integer(kind=c_int)::socket_c
        end function socket_c
        
        function inet_addr_c(str) bind(c, name="inet_addr")
        use iso_c_binding
        type(c_ptr), value::str
        integer(c_int32_t)::inet_addr_c
        end function inet_addr_c
        
        function inet_ntoa_c(ip) bind(c, name="inet_ntoa")
        use iso_c_binding
        type(c_ptr)::inet_ntoa_c
        integer(c_int32_t), value::ip
        end function inet_ntoa_c
        
        function htons(i) bind(c)
        use iso_c_binding
        integer(kind=c_int32_t), value::i
        integer(kind=c_int32_t)::htons
        end function htons
        
        function connect_c(sockfd, sock_addr, socklen) bind(c, name="connect")
        use iso_c_binding
        import::sockaddr_in
        integer(kind=c_int), value::sockfd
        type(c_ptr), value::sock_addr
        integer(kind=c_size_t), value::socklen
        integer(kind=c_int)::connect_c
        end function connect_c
        
        function gethostbyname_c(host) bind(c, name="gethostbyname")
        use iso_c_binding
        type(c_ptr), value::host
        type(c_ptr)::gethostbyname_c
        end function gethostbyname_c
        
        function close_c(s) bind(c, name="close")
        use iso_c_binding
        integer(kind=c_int), value::s
        integer(kind=c_int)::close_c
        end function close_c
        
    end interface
    
    contains
    
    function socket(domain, stype, protocol)
    use iso_c_binding, only: c_int
    implicit none
    
        integer::socket
        integer, intent(in)::domain, stype, protocol
        integer::ignored
        
#ifdef WINDOWS
        integer(kind=c_int32_t), target::timeout
        integer(kind=c_int), parameter::timeout_size=c_int32_t
#else
        type(timeval), target::timeout
        integer(kind=c_int), parameter::timeout_size=2*c_long
#endif
        

        ! Set up a timeout on the socket that's sensible
        interface
            function setsockopt(s, level, optname, optval, optlen) bind(c, name="setsockopt")
            use iso_c_binding
            integer(kind=c_int)::setsockopt
            integer(kind=c_int), value::s
            integer(kind=c_int), value::level, optname, optlen
            type(c_ptr), value::optval
            end function
        end interface

#ifdef WINDOWS
        integer, parameter::SOL_SOCKET  = 65535
        integer, parameter::SO_RCVTIMEO = 4102
        timeout = socket_timeout_ms
#else
        integer, parameter::SOL_SOCKET  = 1
        integer, parameter::SO_RCVTIMEO = 20
        timeout%useconds = 0
        timeout%seconds = socket_timeout_ms/1000
#endif        

        socket = socket_c(int(domain, c_int), int(stype, c_int), int(protocol, c_int))

        ! Timeout call
        ignored = setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, c_loc(timeout), timeout_size) 
        
        
    end function socket
    
    subroutine close_socket(s)
    use iso_c_binding
    implicit none
    
        integer::s
        integer::ignored
        
        ignored = close_c(int(s, kind=c_int))
        
    end subroutine close_socket
    
    function inet_addr(str)
    use iso_c_binding
    implicit none
        
        character(*), intent(in)::str
        integer(c_int32_t)::inet_addr
        
        character(kind=c_char), dimension(:), allocatable, target::cstr
        integer::i
        
        allocate(cstr(len_trim(str)+1))
        
        do i=1, len_trim(str)
            cstr(i) = str(i:i)
        end do
        cstr(len_trim(str)+1) = c_null_char
        
        inet_addr = inet_addr_c(c_loc(cstr))
        
        deallocate(cstr)
    
    end function inet_addr
    
    function inet_ntoa(ip) result(res)
    use iso_c_binding
    implicit none
    
        integer(kind=c_int32_t), intent(in)::ip
        character(15)::res
        
        type(c_ptr)::cptr
        character(kind=c_char), dimension(:), pointer::cres
        integer::i
        
        res = " "
        cptr = inet_ntoa_c(ip)
        if(c_associated(cptr)) then
            call c_f_pointer(cptr, cres, [1])
        
            i = 1
            do while(cres(i) /= c_null_char)
                res(i:i) = cres(i)
                i = i + 1
            end do
        end if
    
    end function inet_ntoa
    
    function connect(sockfd, sock_addr)
    use iso_c_binding
    implicit none

        integer::sockfd
        type(sockaddr_in), target::sock_addr
        logical::connect

        !print *, c_sizeof(sock_addr)

        connect = (connect_c(int(sockfd, kind=c_int), &
                             c_loc(sock_addr), &
                             sockaddr_size) .eq. 0)
    
    end function connect
    
    function gethostbyname(host, success) result(res)
    use iso_c_binding
    implicit none
    
        character(*)::host
        type(simple_hostent)::res
        
        type(hostent_c), pointer::cres
        type(c_ptr)::callres
        
        logical, intent(out), optional::success
        
        ! To get the host to C
        character(kind=c_char), dimension(:), allocatable, target::chost
        integer::i
        
        ! To process h_name
        character(kind=c_char), dimension(:), pointer::h_name
        integer::hnamelen
        interface
            function strlen_c(cstr) bind(c, name="strlen")
            use iso_c_binding
            type(c_ptr), value::cstr
            integer(kind=c_size_t)::strlen_c
            end function strlen_c
        end interface
        
        
        ! To process h_addr
        type(c_ptr), dimension(:), pointer::addrptr
        integer(kind=c_int32_t), pointer::addr32
        integer(kind=c_int64_t), pointer::addr64
        
        allocate(chost(len_trim(host)+1))
        
        do i=1, len_trim(host)
            chost(i) = host(i:i)
        end do
        chost(len_trim(host)+1) = c_null_char
        
        callres = gethostbyname_c(c_loc(chost))
        if(c_associated(callres)) then
            call c_f_pointer(callres, cres)
            
            ! Extract the name
            hnamelen = strlen_c(cres%h_name)
            call c_f_pointer(cres%h_name, h_name, [hnamelen])
            allocate(character(len=i) :: res%h_name)
            do i = 1, hnamelen
                res%h_name(i:i) = h_name(i)
            end do
            
            ! And address
            res%h_addr4 = 0
            res%h_addr6 = 0
            
            res%h_addrtype = cres%h_addrtype
            call c_f_pointer(cres%h_addr_list, addrptr, [1])
            if(res%h_addrtype == AF_INET) then
                call c_f_pointer(addrptr(1), addr32)
                res%h_addr4 = addr32
            else if(res%h_addrtype == AF_INET6) then
                call c_f_pointer(addrptr(1), addr64)
                res%h_addr6 = addr64
            end if

            if(present(success)) then
                success = .TRUE.
            end if
        else
            if(present(success)) then
                success = .FALSE.
            end if
        end if
        
    end function gethostbyname
    
end module network