address-info

Check-in [c3946a8616]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:initial working code
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c3946a8616534b47558417c70d247412637f863e4cee1bbe8acf876dd1bfea6f
User & Date: murphy 2017-08-21 22:24:58
Context
2017-08-21
22:54
initial documentation check-in: e677329d3d user: murphy tags: trunk
22:24
initial working code check-in: c3946a8616 user: murphy tags: trunk
22:00
initial empty check-in check-in: 6e6a257bf2 user: murphy tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added LICENSE.txt.







































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Copyright (C) 2017 Thomas Chust <chust@web.de>.  All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the Software),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Added address-info.meta.



















>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
((category net)
 (synopsis "Network address information access")
 (license "BSD")
 (author "Thomas Chust")
 (doc-from-wiki)
 (needs)
 (test-depends test))

;; vim: set ai et ts=4 sts=2 sw=2 ft=scheme: ;;

Added address-info.release-info.













>
>
>
>
>
>
1
2
3
4
5
6
(repo fossil "http://chust.org/fossils/{egg-name}")

(uri targz "http://chust.org/fossils/{egg-name}/tarball/{egg-name}.tar.gz?uuid=v{egg-release}")
(release "1.0.0")

;; vim: set ai et ts=4 sts=2 sw=2 ft=scheme: ;;

Added address-info.scm.







































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
;; This file is part of address-info for CHICKEN
;; Copyright (c) 2017 by Thomas Chust.  All rights reserved.
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the Software), to deal in the Software without restriction,
;; including without limitation the rights to use, copy, modify,
;; merge, publish, distribute, sublicense, and/or sell copies of the
;; Software, and to permit persons to whom the Software is furnished
;; to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.

(module address-info
  (make-address-info address-info?
   address-info-family address-info-type address-info-host address-info-port
   address-infos)

(import
  scheme chicken foreign
  (only srfi-1 unfold) lolevel)

(require-library
  srfi-1 lolevel)

(declare
  (usual-integrations)
  (no-procedure-checks-for-usual-bindings))

#>
#include <errno.h>
#ifdef _WIN32
# ifdef _MSC_VER
#   include <winsock2.h>
# else
#   include <ws2tcpip.h>
# endif
#else
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
# include <arpa/inet.h>
#endif
<#

(define-foreign-type addrinfo
  (c-pointer (struct "addrinfo")))

(define (type-error loc msg . args)
  (abort
    (make-composite-condition
      (make-property-condition 'exn
        'location loc
        'message msg
        'arguments args)
      (make-property-condition 'type))))

(define-foreign-type addrfamily
  int
  (lambda (family)
    (case family
      [(ipv4) (foreign-value "AF_INET" int)]
      [(ipv6) (foreign-value "AF_INET6" int)]
      [(#f) (foreign-value "AF_UNSPEC" int)]
      [else (type-error 'address-infos "bad address family" family)]))
  (lambda (family)
    (cond
      [(= family (foreign-value "AF_INET" int)) 'ipv4]
      [(= family (foreign-value "AF_INET6" int)) 'ipv6]
      [else #f])))

(define-foreign-type socktype
  int
  (lambda (type)
    (case type
      [(tcp stream) (foreign-value "SOCK_STREAM" int)]
      [(udp datagram) (foreign-value "SOCK_DGRAM" int)]
      [(raw) (foreign-value "SOCK_RAW" int)]
      [(#f) 0]
      [else (type-error 'address-infos "bad socket type" type)]))
  (lambda (type)
    (cond
      [(= type (foreign-value "SOCK_STREAM" int)) 'tcp]
      [(= type (foreign-value "SOCK_DGRAM" int)) 'udp]
      [(= type (foreign-value "SOCK_RAW" int)) 'raw]
      [else #f])))

(define addrinfo-get
  (foreign-lambda* addrinfo ([c-string node] [c-string service] [addrfamily family] [socktype type] [bool server] [bool numeric])
    "struct addrinfo hints;"
    "struct addrinfo *info = NULL;\n"
    "memset(&hints, 0, sizeof(struct addrinfo));\n"
    "hints.ai_family = family;\n"
    "hints.ai_socktype = type;\n"
    "hints.ai_protocol = 0;\n"
    "hints.ai_flags = (server ? AI_PASSIVE : 0) | (numeric ? AI_NUMERICHOST : 0);\n"
    "if (getaddrinfo(node, service, &hints, &info) != 0 && info) {\n"
    "  freeaddrinfo(info);\n"
    "  info = NULL;\n"
    "}\n"
    "C_return(info);\n"))

(define addrinfo-free!
  (foreign-lambda void "freeaddrinfo" addrinfo))

(define addrinfo-next
  (foreign-lambda* addrinfo ([addrinfo info])
    "C_return(info->ai_next);"))

(define addrinfo-family
  (foreign-lambda* addrfamily ([addrinfo info])
    "C_return(info->ai_family);"))

(define addrinfo-type
  (foreign-lambda* socktype ([addrinfo info])
    "C_return(info->ai_socktype);"))

(define addrinfo-host
  (foreign-lambda* c-string ([addrinfo info] [scheme-pointer buf])
    "socklen_t len = info->ai_addrlen;\n"
    "const void *src = info->ai_addr;\n"
    "switch (info->ai_family) {\n"
    "case AF_INET:\n"
    "  src = &((struct sockaddr_in *)src)->sin_addr;\n"
    "  len = sizeof(struct sockaddr_in);\n"
    "  break;\n"
    "case AF_INET6:\n"
    "  src = &((struct sockaddr_in6 *)src)->sin6_addr;\n"
    "  len = sizeof(struct sockaddr_in6);\n"
    "  break;\n"
    "}\n"
    "C_return(inet_ntop(info->ai_family, src, (char *)buf, len));"))

(define addrinfo-port
  (foreign-lambda* scheme-object ([addrinfo info])
    "uint16_t port = 0;\n"
    "switch (info->ai_family) {\n"
    "case AF_INET:\n"
    "  port = ((struct sockaddr_in *)info->ai_addr)->sin_port;\n"
    "  break;\n"
    "case AF_INET6:\n"
    "  port = ((struct sockaddr_in6 *)info->ai_addr)->sin6_port;\n"
    "  break;\n"
    "}\n"
    "if (port > 0)\n"
    "  C_return(C_fix(ntohs(port)));\n"
    "else\n"
    "  C_return(C_SCHEME_FALSE);\n"))

(define-record-type address-info
  (make-address-info family type host port)
  address-info?
  [family address-info-family]
  [type address-info-type]
  [host address-info-host]
  [port address-info-port])

(define-record-printer (address-info info out)
  (display "#<address-info" out)
  (cond
    [(address-info-type info)
     => (lambda (type)
          (let ([host (address-info-host info)]
                [port (address-info-port info)])
            (display #\space out)
            (display type out)
            (display "://" out)
            (if host
              (if (eq? (address-info-family info) 'ipv6)
                (begin
                  (display #\[ out)
                  (display host out)
                  (display #\] out))
                (display host out))
              (display #\* out))
            (when port
              (display #\: out)
              (display port out))))])
  (display #\> out))

(define (addrinfo->address-info info)
  (make-address-info
    (addrinfo-family info)
    (addrinfo-type info)
    (addrinfo-host info (make-string 64))
    (addrinfo-port info)))

(define (address-infos host #!key [port #f] [family #f] [type #f] [server? (not host)] [numeric? #f])
  (when (number? port)
    (set! port (number->string port)))
  (and-let* ([info (addrinfo-get host port family type server? numeric?)])
    (dynamic-wind
      void
      (lambda ()
        (unfold not addrinfo->address-info addrinfo-next info))
      (lambda ()
        (addrinfo-free! info)))))

)

;; vim: set ai et ts=4 sts=2 sw=2 ft=scheme: ;;

Added address-info.setup.























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(compile -s -O2 -d1 "address-info.scm" -j address-info)

(cond-expand
 (enable-static
  (compile -c -O2 -d1 "address-info.scm" -unit address-info))
 (else
  ))

(compile -s -O2 -d0 "address-info.import.scm")

(install-extension
 'address-info
 `("address-info.so"
   ,@(cond-expand
      (enable-static
       '("address-info.o"))
      (else
       '()))
   "address-info.import.so")
 `((version "1.0.0")
   ,@(cond-expand
      (enable-static
       '((static "address-info.o")))
      (else
       '()))))

;; vim: set ai et ts=4 sts=2 sw=2 ft=scheme: ;;

Added test/run.scm.















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
;; This file is part of address-info for CHICKEN
;; Copyright (c) 2017 by Thomas Chust.  All rights reserved.
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the Software), to deal in the Software without restriction,
;; including without limitation the rights to use, copy, modify,
;; merge, publish, distribute, sublicense, and/or sell copies of the
;; Software, and to permit persons to whom the Software is furnished
;; to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.

(require-extension
  address-info test)

(test-group "client mode"
  (test-assert "bad hostname" (not (address-infos "!foo/bar.invalid")))
  (test-assert "symbolic localhost" (list? (address-infos "localhost")))
  (test-assert "numeric ipv4 localhost" (member (make-address-info 'ipv4 'tcp "127.0.0.1" #f) (address-infos "127.0.0.1" type: 'stream numeric?: #t)))
  (test-assert "numeric ipv6 localhost" (member (make-address-info 'ipv6 'tcp "::1" #f) (address-infos "::1" type: 'stream numeric?: #t)))
  (test "port lookup" 80 (address-info-port (car (address-infos "example.com" port: "http")))))

(test-group "server mode"
  (test-assert "bad port" (not (address-infos #f port: "!foo/bar")))
  (test "port lookup" 80 (address-info-port (car (address-infos #f port: "http"))))
  (test "ipv4 wildcard lookup" "0.0.0.0" (address-info-host (car (address-infos #f port: "http" family: 'ipv4))))
  (test "ipv6 wildcard lookup" "::" (address-info-host (car (address-infos #f port: "http" family: 'ipv6)))))