WIP network bindings
This commit is contained in:
parent
772a87dc24
commit
b10b219003
2 changed files with 45 additions and 7 deletions
|
@ -55,10 +55,19 @@
|
||||||
((SO_REUSEADDR) #x2)
|
((SO_REUSEADDR) #x2)
|
||||||
))
|
))
|
||||||
|
|
||||||
(define (alloc size fx)
|
(define (inet_pton af src dst)
|
||||||
(let ([v (foreign-alloc size)])
|
((foreign-procedure
|
||||||
(fx v)
|
"inet_pton"
|
||||||
(foreign-free v)))
|
(int string void*)
|
||||||
|
int)
|
||||||
|
(domain->int af)
|
||||||
|
src dst))
|
||||||
|
|
||||||
|
; htons
|
||||||
|
(define (htons host)
|
||||||
|
((foreign-procedure
|
||||||
|
"htons"
|
||||||
|
(
|
||||||
|
|
||||||
(define (setsockopt sockfd level optname optval optlen)
|
(define (setsockopt sockfd level optname optval optlen)
|
||||||
((foreign-procedure
|
((foreign-procedure
|
||||||
|
@ -70,4 +79,10 @@
|
||||||
(optname->int optname)
|
(optname->int optname)
|
||||||
optval optlen))
|
optval optlen))
|
||||||
|
|
||||||
|
(define (bind sockfd address address_len)
|
||||||
|
((foreign-procedure
|
||||||
|
"bind"
|
||||||
|
(int void* int)
|
||||||
|
int) sockfd address address_len))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,11 @@
|
||||||
(raise msg))
|
(raise msg))
|
||||||
(#t ret)))
|
(#t ret)))
|
||||||
|
|
||||||
|
(define (alloc size fx)
|
||||||
|
(let ([v (foreign-alloc size)])
|
||||||
|
(fx v)
|
||||||
|
(foreign-free v)))
|
||||||
|
|
||||||
(define (udpsock-create fx)
|
(define (udpsock-create fx)
|
||||||
(fx
|
(fx
|
||||||
(check-err
|
(check-err
|
||||||
|
@ -27,7 +32,25 @@
|
||||||
(ftype-sizeof int))
|
(ftype-sizeof int))
|
||||||
"Unable to set REUSE ADDRESS"))))
|
"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
|
(udpsock-create
|
||||||
(lambda (s)
|
(lambda (sock)
|
||||||
(udpsock-reuseaddr s)
|
(udpsock-reuseaddr sock)
|
||||||
(printf "~a~%" s)))
|
(udpsock-bind sock "0.0.0.0" 1337)
|
||||||
|
(printf "~a~%" sock)))
|
||||||
|
|
Loading…
Reference in a new issue