From 6f49424fb0d8adee31448ca447286204f8fdacf4 Mon Sep 17 00:00:00 2001 From: Quentin Date: Thu, 9 Jul 2020 14:16:50 +0200 Subject: [PATCH] Implement bind --- bindings/libc.scm | 4 ++-- examples/udp_server.scm | 30 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/bindings/libc.scm b/bindings/libc.scm index 00e753a..cc8ee6f 100644 --- a/bindings/libc.scm +++ b/bindings/libc.scm @@ -63,11 +63,11 @@ (domain->int af) src dst)) -; htons (define (htons host) ((foreign-procedure "htons" - ( + (short) + short) host)) (define (setsockopt sockfd level optname optval optlen) ((foreign-procedure diff --git a/examples/udp_server.scm b/examples/udp_server.scm index 16648ac..4ac9121 100644 --- a/examples/udp_server.scm +++ b/examples/udp_server.scm @@ -32,22 +32,24 @@ (ftype-sizeof int)) "Unable to set REUSE ADDRESS")))) -(define (udpsock-bind sock addr port) +(define (udpsock-bind sock host 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))))) + (lambda (raw-addr) + (let ([addr (make-ftype-pointer sockaddr_in raw-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 + host + (ftype-pointer-address + (ftype-&ref sockaddr_in (sin_addr) addr))) + "Unable to convert your IP address to binary") + (bind + sock + (ftype-pointer-address addr) + (ftype-sizeof sockaddr_in)))))) (udpsock-create (lambda (sock)