WIP network bindings

This commit is contained in:
Quentin 2020-07-09 12:51:53 +02:00
parent 772a87dc24
commit b10b219003
2 changed files with 45 additions and 7 deletions

View File

@ -55,10 +55,19 @@
((SO_REUSEADDR) #x2)
))
(define (alloc size fx)
(let ([v (foreign-alloc size)])
(fx v)
(foreign-free v)))
(define (inet_pton af src dst)
((foreign-procedure
"inet_pton"
(int string void*)
int)
(domain->int af)
src dst))
; htons
(define (htons host)
((foreign-procedure
"htons"
(
(define (setsockopt sockfd level optname optval optlen)
((foreign-procedure
@ -70,4 +79,10 @@
(optname->int optname)
optval optlen))
(define (bind sockfd address address_len)
((foreign-procedure
"bind"
(int void* int)
int) sockfd address address_len))

View File

@ -7,6 +7,11 @@
(raise msg))
(#t ret)))
(define (alloc size fx)
(let ([v (foreign-alloc size)])
(fx v)
(foreign-free v)))
(define (udpsock-create fx)
(fx
(check-err
@ -27,7 +32,25 @@
(ftype-sizeof int))
"Unable to set REUSE ADDRESS"))))
(define (udpsock-bind sock addr port)
(alloc
(ftype-sizeof sockaddr_in)
(lambda (addr)
(ftype-set! sockaddr_in (sin_family) addr (domain->int 'AF_INET))
(ftype-set! sockaddr_in (sin_port) addr (htons port))
(check-err
(inet_pton
'AF_INET
addr
(ftype-&ref sockaddr_in (sin_addr) addr))
"Unable to convert your IP address to binary")
(bind
sock
addr
(ftype-sizeof addr)))))
(udpsock-create
(lambda (s)
(udpsock-reuseaddr s)
(printf "~a~%" s)))
(lambda (sock)
(udpsock-reuseaddr sock)
(udpsock-bind sock "0.0.0.0" 1337)
(printf "~a~%" sock)))