Making the addrinfo argument to open-net-io and with-net-io default to TCP/IP.

This commit is contained in:
Alex Shinn 2011-08-06 15:20:17 +09:00
parent ddb6f31d58
commit 50ace689b4
3 changed files with 22 additions and 10 deletions

View file

@ -6,7 +6,8 @@
address-info-family address-info-socket-type address-info-protocol address-info-family address-info-socket-type address-info-protocol
address-info-address address-info-address-length address-info-next address-info-address address-info-address-length address-info-next
address-family/unix address-family/inet address-family/unix address-family/inet
socket-type/stream socket-type/datagram socket-type/raw) socket-type/stream socket-type/datagram socket-type/raw
ip-proto/tcp ip-proto/udp)
(import (scheme)) (import (scheme))
(import (chibi filesystem)) (import (chibi filesystem))
(include-shared "net") (include-shared "net")

View file

@ -1,6 +1,20 @@
;; Copyright (c) 2010-2011 Alex Shinn. All rights reserved. ;; Copyright (c) 2010-2011 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt ;; BSD-style license: http://synthcode.com/license.txt
;;> @subsubsubsection{@scheme{(get-address-info host service [addrinfo])}}
;;> Create and return a new addrinfo structure for the given host
;;> and service. @var{host} should be a string and @var{service} a
;;> string or integer. The optional @var{addrinfo} defaults to
;;> a TCP/IP stream setting.
(define (get-address-info host service . o)
(%get-address-info host
(if (integer? service) (number->string service) service)
(make-address-info address-family/inet
socket-type/stream
ip-proto/tcp)))
;;> Opens a client net connection to @var{host}, a string, ;;> Opens a client net connection to @var{host}, a string,
;;> on port @var{service}, which can be a string such as ;;> on port @var{service}, which can be a string such as
;;> @scheme{"http"} or an integer. Returns a list of two ;;> @scheme{"http"} or an integer. Returns a list of two
@ -8,11 +22,7 @@
;;> or @scheme{#f} on failure. ;;> or @scheme{#f} on failure.
(define (open-net-io host service) (define (open-net-io host service)
(let lp ((addr (get-address-info host (let lp ((addr (get-address-info host service #f)))
(if (integer? service)
(number->string service)
service)
#f)))
(if (not addr) (if (not addr)
(error "couldn't find address" host service) (error "couldn't find address" host service)
(let ((sock (socket (address-info-family addr) (let ((sock (socket (address-info-family addr)

View file

@ -20,10 +20,7 @@
;;> The addrinfo struct accessors. ;;> The addrinfo struct accessors.
;;/ ;;/
;;> Create and return a new addrinfo structure for the (define-c errno (%get-address-info getaddrinfo)
;;> given host and service.
(define-c errno (get-address-info getaddrinfo)
(string string (maybe-null addrinfo) (result free addrinfo))) (string string (maybe-null addrinfo) (result free addrinfo)))
;;> Bind a name to a socket. ;;> Bind a name to a socket.
@ -51,4 +48,8 @@
(define-c-const int (socket-type/stream "SOCK_STREAM")) (define-c-const int (socket-type/stream "SOCK_STREAM"))
(define-c-const int (socket-type/datagram "SOCK_DGRAM")) (define-c-const int (socket-type/datagram "SOCK_DGRAM"))
(define-c-const int (socket-type/raw "SOCK_RAW")) (define-c-const int (socket-type/raw "SOCK_RAW"))
(define-c-const int (ip-proto/tcp "IPPROTO_TCP"))
(define-c-const int (ip-proto/udp "IPPROTO_UDP"))
;;> The constants for the addrinfo struct.
;;/