From c2f00c82e7b2ba3789e58d772d6cde41f651ed9d Mon Sep 17 00:00:00 2001 From: Ryan Sundberg Date: Sat, 14 Jan 2023 09:20:20 -0800 Subject: [PATCH 3/4] web: Support binding to IPv6 addresses with --listen * src/cuirass/http.scm (run-cuirass-server): Detect AF_INET6 addresses * src/web/server/fiberized.scm (make-default-socket): Ditto --- src/cuirass/http.scm | 19 +++++++++++++++---- src/web/server/fiberized.scm | 5 ++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/cuirass/http.scm b/src/cuirass/http.scm index a9fc3ea..07359c2 100644 --- a/src/cuirass/http.scm +++ b/src/cuirass/http.scm @@ -1148,10 +1148,19 @@ passed, only display JOBS targeting this SYSTEM." (_ (respond-not-found (uri->string (request-uri request)))))) +(define (lookup-host host) + (cond + ;; Detect IPv6 addresses and skip DNS resolution + ((string-index host #\:) + (values AF_INET6 host)) + + (else + (let* ((host-info (gethostbyname host)) + (family (hostent:addrtype host-info))) + (values family (inet-ntop family (car (hostent:addr-list host-info)))))))) + (define* (run-cuirass-server #:key (host "localhost") (port 8080)) - (let* ((host-info (gethostbyname host)) - (address (inet-ntop (hostent:addrtype host-info) - (car (hostent:addr-list host-info))))) + (let-values (((family address) (lookup-host host))) (log-info "listening on ~A:~A" address port) ;; Here we use our own web backend, call 'fiberized'. We cannot use the @@ -1168,7 +1177,9 @@ passed, only display JOBS targeting this SYSTEM." ;; ;; XXX: We don't do 'call-with-sigint' like 'run-server' does. (let* ((impl (lookup-server-impl 'fiberized)) - (server (open-server impl `(#:host ,address #:port ,port)))) + (server (open-server impl `(#:host ,address + #:family ,family + #:port ,port)))) (let loop () (let-values (((client request body) (read-client impl server))) diff --git a/src/web/server/fiberized.scm b/src/web/server/fiberized.scm index 23a2bd9..78edab5 100644 --- a/src/web/server/fiberized.scm +++ b/src/web/server/fiberized.scm @@ -49,7 +49,10 @@ #:use-module (cuirass utils)) (define (make-default-socket family addr port) - (let ((sock (socket PF_INET SOCK_STREAM 0))) + (let* ((proto-family (cond + ((= family AF_INET) PF_INET) + ((= family AF_INET6) PF_INET6))) + (sock (socket proto-family SOCK_STREAM 0))) (setsockopt sock SOL_SOCKET SO_REUSEADDR 1) (fcntl sock F_SETFD FD_CLOEXEC) (bind sock family addr port) -- 2.37.2