me
/
guix
Archived
1
0
Fork 0

ftp-client: Try all the addresses returned by `getaddrinfo'.

* guix/ftp-client.scm (ftp-open): Upon connection failure, try the other
  addresses returned by `getaddrinfo'.
master
Ludovic Courtès 2012-10-13 21:18:16 +02:00
parent 568717fd90
commit 4004f95379
1 changed files with 32 additions and 16 deletions

View File

@ -81,24 +81,40 @@
(else (throw 'ftp-error port command code message)))))) (else (throw 'ftp-error port command code message))))))
(define (ftp-open host) (define (ftp-open host)
"Open an FTP connection to HOST, and return it."
(catch 'getaddrinfo-error (catch 'getaddrinfo-error
(lambda () (lambda ()
(let* ((ai (car (getaddrinfo host "ftp"))) (define addresses
(getaddrinfo host "ftp"))
(let loop ((addresses addresses))
(let* ((ai (car addresses))
(s (socket (addrinfo:fam ai) (addrinfo:socktype ai) (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
(addrinfo:protocol ai)))) (addrinfo:protocol ai))))
(catch 'system-error
(lambda ()
(connect s (addrinfo:addr ai)) (connect s (addrinfo:addr ai))
(setvbuf s _IOLBF) (setvbuf s _IOLBF)
(let-values (((code message) (%ftp-listen s))) (let-values (((code message) (%ftp-listen s)))
(if (eqv? code 220) (if (eqv? code 220)
(begin (begin
;(%ftp-command "OPTS UTF8 ON" 200 s) ;;(%ftp-command "OPTS UTF8 ON" 200 s)
(%ftp-login "anonymous" "ludo@example.com" s) (%ftp-login "anonymous" "guix@example.com" s)
(%make-ftp-connection s ai)) (%make-ftp-connection s ai))
(begin (begin
(format (current-error-port) "FTP to `~a' failed: ~A: ~A~%" (format (current-error-port)
"FTP to `~a' failed: ~A: ~A~%"
host code message) host code message)
(close s) (close s)
#f))))) #f))))
(lambda args
;; Connection failed, so try one of the other addresses.
(close s)
(if (null? addresses)
(apply throw args)
(loop (cdr addresses))))))))
(lambda (key errcode) (lambda (key errcode)
(format (current-error-port) "failed to resolve `~a': ~a~%" (format (current-error-port) "failed to resolve `~a': ~a~%"
host (gai-strerror errcode)) host (gai-strerror errcode))