ray 0.0.1 → 0.1.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. data/.gemtest +0 -0
  2. data/.yardopts +4 -0
  3. data/README.md +17 -21
  4. data/Rakefile +18 -139
  5. data/VERSION +1 -1
  6. data/ext/audio.cpp +723 -0
  7. data/ext/{color.c → color.cpp} +25 -13
  8. data/ext/drawable.cpp +91 -0
  9. data/ext/event.cpp +460 -0
  10. data/ext/extconf.rb +5 -104
  11. data/ext/font.cpp +190 -0
  12. data/ext/image.cpp +733 -0
  13. data/ext/input.cpp +74 -0
  14. data/ext/ray.cpp +168 -0
  15. data/ext/ray.hpp +356 -0
  16. data/ext/{rect.c → rect.cpp} +51 -37
  17. data/ext/shader.cpp +169 -0
  18. data/ext/shape.cpp +409 -0
  19. data/ext/sprite.cpp +306 -0
  20. data/ext/text.cpp +181 -0
  21. data/ext/vector.cpp +215 -0
  22. data/guide.md +619 -0
  23. data/lib/ray/audio.rb +0 -41
  24. data/lib/ray/color.rb +32 -10
  25. data/lib/ray/drawable.rb +16 -0
  26. data/lib/ray/dsl/event_listener.rb +25 -2
  27. data/lib/ray/dsl/event_runner.rb +33 -5
  28. data/lib/ray/dsl/event_translator.rb +66 -30
  29. data/lib/ray/dsl/handler.rb +3 -2
  30. data/lib/ray/dsl/matcher.rb +58 -14
  31. data/lib/ray/font.rb +38 -96
  32. data/lib/ray/font_set.rb +8 -8
  33. data/lib/ray/game.rb +87 -66
  34. data/lib/ray/helper.rb +105 -10
  35. data/lib/ray/image.rb +150 -24
  36. data/lib/ray/image_set.rb +3 -1
  37. data/lib/ray/input.rb +10 -0
  38. data/lib/ray/music_set.rb +5 -3
  39. data/lib/ray/ray.rb +21 -9
  40. data/lib/ray/rect.rb +48 -7
  41. data/lib/ray/rmagick.rb +41 -0
  42. data/lib/ray/scene.rb +99 -43
  43. data/lib/ray/scene_list.rb +67 -0
  44. data/lib/ray/shape.rb +132 -0
  45. data/lib/ray/sound_set.rb +4 -2
  46. data/lib/ray/sprite.rb +49 -111
  47. data/lib/ray/text.rb +101 -0
  48. data/lib/ray/text_helper.rb +37 -0
  49. data/lib/ray/turtle.rb +215 -0
  50. data/lib/ray/vector.rb +226 -0
  51. data/samples/audio/spacial.rb +44 -0
  52. data/samples/hello_world/hello.rb +9 -13
  53. data/samples/hello_world/hello_dsl.rb +8 -12
  54. data/samples/hello_world/text.rb +15 -0
  55. data/samples/opengl/binding.rb +38 -0
  56. data/samples/opengl/image.rb +32 -0
  57. data/samples/opengl/opengl.rb +34 -0
  58. data/samples/opengl/shader.rb +42 -0
  59. data/samples/pong/pong.rb +14 -10
  60. data/samples/run_scene.rb +53 -0
  61. data/samples/shaders/scene.rb +40 -0
  62. data/samples/shaders/shaders.rb +42 -0
  63. data/samples/shaders/shape.rb +34 -0
  64. data/samples/sokoban/sokoban.rb +18 -18
  65. data/samples/test/actual_scene.rb +41 -0
  66. data/samples/test/scene_riot.rb +39 -0
  67. data/samples/test/scene_spec.rb +32 -0
  68. data/samples/test/scene_test_unit.rb +25 -0
  69. data/samples/turtle/byzantium.rb +45 -0
  70. data/samples/turtle/hilbert.rb +48 -0
  71. data/samples/turtle/koch.rb +55 -0
  72. data/samples/turtle/mandala.rb +61 -0
  73. data/samples/turtle/tree.rb +57 -0
  74. data/test/audio_test.rb +69 -0
  75. data/test/color_test.rb +77 -0
  76. data/test/drawable_test.rb +19 -0
  77. data/test/dsl_test.rb +93 -0
  78. data/test/font_test.rb +57 -0
  79. data/test/helpers.rb +94 -0
  80. data/test/image_test.rb +82 -0
  81. data/test/ray_test.rb +25 -0
  82. data/test/rect_test.rb +121 -0
  83. data/{spec → test}/res/VeraMono.ttf +0 -0
  84. data/{spec → test}/res/aqua.bmp +0 -0
  85. data/{spec → test}/res/aqua.png +0 -0
  86. data/{spec → test}/res/aqua2.bmp +0 -0
  87. data/{spec → test}/res/not_a_jpeg.jpeg +0 -0
  88. data/{spec → test}/res/pop.wav +0 -0
  89. data/test/resource_set_test.rb +99 -0
  90. data/test/run_all.rb +7 -0
  91. data/test/shape_test.rb +101 -0
  92. data/test/sprite_test.rb +89 -0
  93. data/test/text_test.rb +78 -0
  94. data/test/turtle_test.rb +176 -0
  95. data/test/vector_test.rb +111 -0
  96. data/yard_ext.rb +0 -28
  97. metadata +95 -139
  98. data/.gitignore +0 -23
  99. data/.gitmodules +0 -3
  100. data/.rspec +0 -3
  101. data/ext/audio.c +0 -473
  102. data/ext/event.c +0 -557
  103. data/ext/font.c +0 -287
  104. data/ext/image.c +0 -933
  105. data/ext/joystick.c +0 -145
  106. data/ext/ray.c +0 -489
  107. data/ext/ray.h +0 -245
  108. data/ext/ray_osx.m +0 -161
  109. data/lib/ray/joystick.rb +0 -30
  110. data/psp/SDL_psp_main.c +0 -84
  111. data/psp/bigdecimal/README +0 -60
  112. data/psp/bigdecimal/bigdecimal.c +0 -4697
  113. data/psp/bigdecimal/bigdecimal.h +0 -216
  114. data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +0 -85
  115. data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +0 -84
  116. data/psp/bigdecimal/lib/bigdecimal/math.rb +0 -235
  117. data/psp/bigdecimal/lib/bigdecimal/newton.rb +0 -77
  118. data/psp/bigdecimal/lib/bigdecimal/util.rb +0 -65
  119. data/psp/digest/bubblebabble/bubblebabble.c +0 -142
  120. data/psp/digest/defs.h +0 -20
  121. data/psp/digest/digest.c +0 -643
  122. data/psp/digest/digest.h +0 -32
  123. data/psp/digest/lib/digest.rb +0 -50
  124. data/psp/digest/lib/md5.rb +0 -27
  125. data/psp/digest/lib/sha1.rb +0 -27
  126. data/psp/digest/md5/md5.c +0 -420
  127. data/psp/digest/md5/md5.h +0 -80
  128. data/psp/digest/md5/md5init.c +0 -40
  129. data/psp/digest/rmd160/rmd160.c +0 -457
  130. data/psp/digest/rmd160/rmd160.h +0 -56
  131. data/psp/digest/rmd160/rmd160init.c +0 -40
  132. data/psp/digest/sha1/sha1.c +0 -269
  133. data/psp/digest/sha1/sha1.h +0 -39
  134. data/psp/digest/sha1/sha1init.c +0 -40
  135. data/psp/digest/sha2/lib/sha2.rb +0 -73
  136. data/psp/digest/sha2/sha2.c +0 -919
  137. data/psp/digest/sha2/sha2.h +0 -109
  138. data/psp/digest/sha2/sha2init.c +0 -52
  139. data/psp/enumerator/enumerator.c +0 -298
  140. data/psp/etc/etc.c +0 -559
  141. data/psp/ext.c +0 -289
  142. data/psp/fcntl/fcntl.c +0 -187
  143. data/psp/lib/rbconfig.rb +0 -178
  144. data/psp/nkf/lib/kconv.rb +0 -367
  145. data/psp/nkf/nkf-utf8/config.h +0 -88
  146. data/psp/nkf/nkf-utf8/nkf.c +0 -6040
  147. data/psp/nkf/nkf-utf8/utf8tbl.c +0 -8500
  148. data/psp/nkf/nkf-utf8/utf8tbl.h +0 -34
  149. data/psp/nkf/nkf.c +0 -654
  150. data/psp/socket/addrinfo.h +0 -173
  151. data/psp/socket/getaddrinfo.c +0 -676
  152. data/psp/socket/getnameinfo.c +0 -270
  153. data/psp/socket/pspsocket.c +0 -71
  154. data/psp/socket/pspsocket.h +0 -28
  155. data/psp/socket/socket.c +0 -4662
  156. data/psp/socket/sockport.h +0 -76
  157. data/psp/stringio/stringio.c +0 -1306
  158. data/psp/strscan/strscan.c +0 -1320
  159. data/psp/syck/bytecode.c +0 -1166
  160. data/psp/syck/emitter.c +0 -1242
  161. data/psp/syck/gram.c +0 -1894
  162. data/psp/syck/gram.h +0 -79
  163. data/psp/syck/handler.c +0 -174
  164. data/psp/syck/implicit.c +0 -2990
  165. data/psp/syck/node.c +0 -408
  166. data/psp/syck/rubyext.c +0 -2367
  167. data/psp/syck/syck.c +0 -504
  168. data/psp/syck/syck.h +0 -456
  169. data/psp/syck/token.c +0 -2725
  170. data/psp/syck/yaml2byte.c +0 -257
  171. data/psp/syck/yamlbyte.h +0 -170
  172. data/psp/thread/thread.c +0 -1175
  173. data/psp/zlib/zlib.c +0 -3547
  174. data/script.rb +0 -10
  175. data/spec/ray/audio_spec.rb +0 -146
  176. data/spec/ray/color_spec.rb +0 -57
  177. data/spec/ray/event_spec.rb +0 -80
  178. data/spec/ray/font_spec.rb +0 -93
  179. data/spec/ray/image_set_spec.rb +0 -48
  180. data/spec/ray/image_spec.rb +0 -162
  181. data/spec/ray/joystick_spec.rb +0 -21
  182. data/spec/ray/matcher_spec.rb +0 -50
  183. data/spec/ray/ray_spec.rb +0 -88
  184. data/spec/ray/rect_spec.rb +0 -154
  185. data/spec/ray/resource_set_spec.rb +0 -105
  186. data/spec/ray/sprite_spec.rb +0 -163
  187. data/spec/spec.opts +0 -4
  188. data/spec/spec_helper.rb +0 -8
@@ -1,173 +0,0 @@
1
- /*
2
- * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions
7
- * are met:
8
- * 1. Redistributions of source code must retain the above copyright
9
- * notice, this list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright
11
- * notice, this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- * 3. Neither the name of the project nor the names of its contributors
14
- * may be used to endorse or promote products derived from this software
15
- * without specific prior written permission.
16
- *
17
- * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
- * SUCH DAMAGE.
28
- */
29
-
30
- #ifndef ADDR_INFO_H
31
- #define ADDR_INFO_H
32
-
33
- #ifndef HAVE_GETADDRINFO
34
-
35
- /* special compatibility hack */
36
- #undef EAI_ADDRFAMILY
37
- #undef EAI_AGAIN
38
- #undef EAI_BADFLAGS
39
- #undef EAI_FAIL
40
- #undef EAI_FAMILY
41
- #undef EAI_MEMORY
42
- #undef EAI_NODATA
43
- #undef EAI_NONAME
44
- #undef EAI_SERVICE
45
- #undef EAI_SOCKTYPE
46
- #undef EAI_SYSTEM
47
- #undef EAI_BADHINTS
48
- #undef EAI_PROTOCOL
49
- #undef EAI_MAX
50
-
51
- #undef AI_PASSIVE
52
- #undef AI_CANONNAME
53
- #undef AI_NUMERICHOST
54
- #undef AI_ALL
55
- #undef AI_ADDRCONFIG
56
- #undef AI_V4MAPPED
57
- #undef AI_DEFAULT
58
-
59
- #undef NI_NOFQDN
60
- #undef NI_NUMERICHOST
61
- #undef NI_NAMEREQD
62
- #undef NI_NUMERICSERV
63
- #undef NI_DGRAM
64
-
65
- #define addrinfo addrinfo__compat
66
- #define getaddrinfo getaddrinfo__compat
67
- #define getnameinfo getnameinfo__compat
68
- #define freehostent freehostent__compat
69
- #define freeaddrinfo freeaddrinfo__compat
70
-
71
- #ifndef __P
72
- # ifdef HAVE_PROTOTYPES
73
- # define __P(args) args
74
- # else
75
- # define __P(args) args
76
- # endif
77
- #endif
78
-
79
- /* special compatibility hack -- end*/
80
-
81
-
82
- /*
83
- * Error return codes from getaddrinfo()
84
- */
85
- #define EAI_ADDRFAMILY 1 /* address family for hostname not supported */
86
- #define EAI_AGAIN 2 /* temporary failure in name resolution */
87
- #define EAI_BADFLAGS 3 /* invalid value for ai_flags */
88
- #define EAI_FAIL 4 /* non-recoverable failure in name resolution */
89
- #define EAI_FAMILY 5 /* ai_family not supported */
90
- #define EAI_MEMORY 6 /* memory allocation failure */
91
- #define EAI_NODATA 7 /* no address associated with hostname */
92
- #define EAI_NONAME 8 /* hostname nor servname provided, or not known */
93
- #define EAI_SERVICE 9 /* servname not supported for ai_socktype */
94
- #define EAI_SOCKTYPE 10 /* ai_socktype not supported */
95
- #define EAI_SYSTEM 11 /* system error returned in errno */
96
- #define EAI_BADHINTS 12
97
- #define EAI_PROTOCOL 13
98
- #define EAI_MAX 14
99
-
100
- /*
101
- * Flag values for getaddrinfo()
102
- */
103
- #define AI_PASSIVE 0x00000001 /* get address to use bind() */
104
- #define AI_CANONNAME 0x00000002 /* fill ai_canonname */
105
- #define AI_NUMERICHOST 0x00000004 /* prevent name resolution */
106
- /* valid flags for addrinfo */
107
- #define AI_MASK (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST)
108
-
109
- #define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
110
- #define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */
111
- #define AI_ADDRCONFIG 0x00000400 /* only if any address is assigned */
112
- #define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */
113
- /* special recommended flags for getipnodebyname */
114
- #define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG)
115
-
116
- /*
117
- * Constants for getnameinfo()
118
- */
119
- #ifndef NI_MAXHOST
120
- #define NI_MAXHOST 1025
121
- #define NI_MAXSERV 32
122
- #endif
123
-
124
- /*
125
- * Flag values for getnameinfo()
126
- */
127
- #define NI_NOFQDN 0x00000001
128
- #define NI_NUMERICHOST 0x00000002
129
- #define NI_NAMEREQD 0x00000004
130
- #define NI_NUMERICSERV 0x00000008
131
- #define NI_DGRAM 0x00000010
132
-
133
- struct addrinfo {
134
- int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
135
- int ai_family; /* PF_xxx */
136
- int ai_socktype; /* SOCK_xxx */
137
- int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
138
- size_t ai_addrlen; /* length of ai_addr */
139
- char *ai_canonname; /* canonical name for hostname */
140
- struct sockaddr *ai_addr; /* binary address */
141
- struct addrinfo *ai_next; /* next structure in linked list */
142
- };
143
-
144
- extern int getaddrinfo __P((
145
- const char *hostname, const char *servname,
146
- const struct addrinfo *hints,
147
- struct addrinfo **res));
148
-
149
- extern int getnameinfo __P((
150
- const struct sockaddr *sa,
151
- size_t salen,
152
- char *host,
153
- size_t hostlen,
154
- char *serv,
155
- size_t servlen,
156
- int flags));
157
-
158
- extern void freehostent __P((struct hostent *));
159
- extern void freeaddrinfo __P((struct addrinfo *));
160
- #if defined __UCLIBC__
161
- const
162
- #endif
163
- extern char *gai_strerror __P((int));
164
-
165
- /* In case there is no definition of offsetof() provided - though any proper
166
- Standard C system should have one. */
167
-
168
- #ifndef offsetof
169
- #define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field))
170
- #endif
171
-
172
- #endif
173
- #endif
@@ -1,676 +0,0 @@
1
- /*
2
- * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions
7
- * are met:
8
- * 1. Redistributions of source code must retain the above copyright
9
- * notice, this list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright
11
- * notice, this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- * 3. Neither the name of the project nor the names of its contributors
14
- * may be used to endorse or promote products derived from this software
15
- * without specific prior written permission.
16
- *
17
- * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
- * SUCH DAMAGE.
28
- */
29
-
30
- /*
31
- * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
32
- *
33
- * Issues to be discussed:
34
- * - Thread safe-ness must be checked.
35
- * - Return values. There are nonstandard return values defined and used
36
- * in the source code. This is because RFC2133 is silent about which error
37
- * code must be returned for which situation.
38
- * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag.
39
- */
40
-
41
- #include <ruby/config.h>
42
- #include <sys/types.h>
43
- #include <sys/param.h>
44
- #include <sys/socket.h>
45
- #include <netinet/in.h>
46
- #include <arpa/inet.h>
47
- #include <netdb.h>
48
- #include <stdio.h>
49
-
50
- #include <unistd.h>
51
- #include <netdb.h>
52
-
53
- #include <string.h>
54
- #include <stdio.h>
55
- #include <stdlib.h>
56
- #include <stddef.h>
57
- #include <ctype.h>
58
-
59
- #include "pspsocket.h"
60
-
61
- #include "addrinfo.h"
62
- #include "sockport.h"
63
-
64
- #if defined(__KAME__) && defined(INET6)
65
- # define FAITH
66
- #endif
67
-
68
- #define SUCCESS 0
69
- #define ANY 0
70
- #define YES 1
71
- #define NO 0
72
-
73
- #ifdef FAITH
74
- static int translate = NO;
75
- static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
76
- #endif
77
-
78
- static const char in_addrany[] = { 0, 0, 0, 0 };
79
- static const char in6_addrany[] = {
80
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
81
- };
82
- static const char in_loopback[] = { 127, 0, 0, 1 };
83
- static const char in6_loopback[] = {
84
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
85
- };
86
-
87
- struct sockinet {
88
- u_char si_len;
89
- u_char si_family;
90
- u_short si_port;
91
- };
92
-
93
- static const struct afd {
94
- int a_af;
95
- int a_addrlen;
96
- int a_socklen;
97
- int a_off;
98
- const char *a_addrany;
99
- const char *a_loopback;
100
- } afdl [] = {
101
- #ifdef INET6
102
- #define N_INET6 0
103
- {PF_INET6, sizeof(struct in6_addr),
104
- sizeof(struct sockaddr_in6),
105
- offsetof(struct sockaddr_in6, sin6_addr),
106
- in6_addrany, in6_loopback},
107
- #define N_INET 1
108
- #else
109
- #define N_INET 0
110
- #endif
111
- {PF_INET, sizeof(struct in_addr),
112
- sizeof(struct sockaddr_in),
113
- offsetof(struct sockaddr_in, sin_addr),
114
- in_addrany, in_loopback},
115
- {0, 0, 0, 0, NULL, NULL},
116
- };
117
-
118
- #ifdef INET6
119
- #define PTON_MAX 16
120
- #else
121
- #define PTON_MAX 4
122
- #endif
123
-
124
- static int get_name __P((const char *, const struct afd *,
125
- struct addrinfo **, char *, struct addrinfo *,
126
- int));
127
- static int get_addr __P((const char *, int, struct addrinfo **,
128
- struct addrinfo *, int));
129
- static int str_isnumber __P((const char *));
130
-
131
- static const char *const ai_errlist[] = {
132
- "success.",
133
- "address family for hostname not supported.", /* EAI_ADDRFAMILY */
134
- "temporary failure in name resolution.", /* EAI_AGAIN */
135
- "invalid value for ai_flags.", /* EAI_BADFLAGS */
136
- "non-recoverable failure in name resolution.", /* EAI_FAIL */
137
- "ai_family not supported.", /* EAI_FAMILY */
138
- "memory allocation failure.", /* EAI_MEMORY */
139
- "no address associated with hostname.", /* EAI_NODATA */
140
- "hostname nor servname provided, or not known.",/* EAI_NONAME */
141
- "servname not supported for ai_socktype.", /* EAI_SERVICE */
142
- "ai_socktype not supported.", /* EAI_SOCKTYPE */
143
- "system error returned in errno.", /* EAI_SYSTEM */
144
- "invalid value for hints.", /* EAI_BADHINTS */
145
- "resolved protocol is unknown.", /* EAI_PROTOCOL */
146
- "unknown error.", /* EAI_MAX */
147
- };
148
-
149
- #define GET_CANONNAME(ai, str) \
150
- if (pai->ai_flags & AI_CANONNAME) {\
151
- if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
152
- strcpy((ai)->ai_canonname, (str));\
153
- } else {\
154
- error = EAI_MEMORY;\
155
- goto free;\
156
- }\
157
- }
158
-
159
- #define GET_AI(ai, afd, addr, port) {\
160
- char *p;\
161
- if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
162
- ((afd)->a_socklen)))\
163
- == NULL) {\
164
- error = EAI_MEMORY;\
165
- goto free;\
166
- }\
167
- memcpy(ai, pai, sizeof(struct addrinfo));\
168
- (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
169
- memset((ai)->ai_addr, 0, (afd)->a_socklen);\
170
- SET_SA_LEN((ai)->ai_addr, (ai)->ai_addrlen = (afd)->a_socklen);\
171
- (ai)->ai_addr->sa_family = (ai)->ai_family = (afd)->a_af;\
172
- ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
173
- p = (char *)((ai)->ai_addr);\
174
- memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
175
- }
176
-
177
- #define ERR(err) { error = (err); goto bad; }
178
-
179
- #if defined __UCLIBC__
180
- const
181
- #endif
182
- char *
183
- gai_strerror(ecode)
184
- int ecode;
185
- {
186
- if (ecode < 0 || ecode > EAI_MAX)
187
- ecode = EAI_MAX;
188
- return (char *)ai_errlist[ecode];
189
- }
190
-
191
- void
192
- freeaddrinfo(ai)
193
- struct addrinfo *ai;
194
- {
195
- struct addrinfo *next;
196
-
197
- do {
198
- next = ai->ai_next;
199
- if (ai->ai_canonname)
200
- free(ai->ai_canonname);
201
- /* no need to free(ai->ai_addr) */
202
- free(ai);
203
- } while ((ai = next) != NULL);
204
- }
205
-
206
- static int
207
- str_isnumber(p)
208
- const char *p;
209
- {
210
- char *q = (char *)p;
211
- while (*q) {
212
- if (! isdigit(*q))
213
- return NO;
214
- q++;
215
- }
216
- return YES;
217
- }
218
-
219
- #define HAVE_INET_PTON
220
- #ifndef HAVE_INET_PTON
221
-
222
- static int
223
- inet_pton(af, hostname, pton)
224
- int af;
225
- const char *hostname;
226
- void *pton;
227
- {
228
- struct in_addr in;
229
-
230
- #ifdef HAVE_INET_ATON
231
- if (!inet_aton(hostname, &in))
232
- return 0;
233
- #else
234
- int d1, d2, d3, d4;
235
- char ch;
236
-
237
- if (sscanf(hostname, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
238
- 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
239
- 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
240
- in.s_addr = htonl(
241
- ((long) d1 << 24) | ((long) d2 << 16) |
242
- ((long) d3 << 8) | ((long) d4 << 0));
243
- }
244
- else {
245
- return 0;
246
- }
247
- #endif
248
- memcpy(pton, &in, sizeof(in));
249
- return 1;
250
- }
251
- #endif
252
-
253
- int
254
- getaddrinfo(hostname, servname, hints, res)
255
- const char *hostname, *servname;
256
- const struct addrinfo *hints;
257
- struct addrinfo **res;
258
- {
259
- struct addrinfo sentinel;
260
- struct addrinfo *top = NULL;
261
- struct addrinfo *cur;
262
- int i, error = 0;
263
- char pton[PTON_MAX];
264
- struct addrinfo ai;
265
- struct addrinfo *pai;
266
- u_short port;
267
-
268
- #ifdef FAITH
269
- static int firsttime = 1;
270
-
271
- if (firsttime) {
272
- /* translator hack */
273
- {
274
- char *q = getenv("GAI");
275
- if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
276
- translate = YES;
277
- }
278
- firsttime = 0;
279
- }
280
- #endif
281
-
282
- /* initialize file static vars */
283
- sentinel.ai_next = NULL;
284
- cur = &sentinel;
285
- pai = &ai;
286
- pai->ai_flags = 0;
287
- pai->ai_family = PF_UNSPEC;
288
- pai->ai_socktype = ANY;
289
- pai->ai_protocol = ANY;
290
- pai->ai_addrlen = 0;
291
- pai->ai_canonname = NULL;
292
- pai->ai_addr = NULL;
293
- pai->ai_next = NULL;
294
- port = ANY;
295
-
296
- if (hostname == NULL && servname == NULL)
297
- return EAI_NONAME;
298
- if (hints) {
299
- /* error check for hints */
300
- if (hints->ai_addrlen || hints->ai_canonname ||
301
- hints->ai_addr || hints->ai_next)
302
- ERR(EAI_BADHINTS); /* xxx */
303
- if (hints->ai_flags & ~AI_MASK)
304
- ERR(EAI_BADFLAGS);
305
- switch (hints->ai_family) {
306
- case PF_UNSPEC:
307
- case PF_INET:
308
- #ifdef INET6
309
- case PF_INET6:
310
- #endif
311
- break;
312
- default:
313
- ERR(EAI_FAMILY);
314
- }
315
- memcpy(pai, hints, sizeof(*pai));
316
- switch (pai->ai_socktype) {
317
- case ANY:
318
- switch (pai->ai_protocol) {
319
- case ANY:
320
- break;
321
- case IPPROTO_UDP:
322
- pai->ai_socktype = SOCK_DGRAM;
323
- break;
324
- case IPPROTO_TCP:
325
- pai->ai_socktype = SOCK_STREAM;
326
- break;
327
- default:
328
- #if defined(SOCK_RAW)
329
- pai->ai_socktype = SOCK_RAW;
330
- #endif
331
- break;
332
- }
333
- break;
334
- #if defined(SOCK_RAW)
335
- case SOCK_RAW:
336
- break;
337
- #endif
338
- case SOCK_DGRAM:
339
- if (pai->ai_protocol != IPPROTO_UDP &&
340
- pai->ai_protocol != ANY)
341
- ERR(EAI_BADHINTS); /*xxx*/
342
- pai->ai_protocol = IPPROTO_UDP;
343
- break;
344
- case SOCK_STREAM:
345
- if (pai->ai_protocol != IPPROTO_TCP &&
346
- pai->ai_protocol != ANY)
347
- ERR(EAI_BADHINTS); /*xxx*/
348
- pai->ai_protocol = IPPROTO_TCP;
349
- break;
350
- default:
351
- ERR(EAI_SOCKTYPE);
352
- break;
353
- }
354
- }
355
-
356
- /*
357
- * service port
358
- */
359
- if (servname) {
360
- if (str_isnumber(servname)) {
361
- if (pai->ai_socktype == ANY) {
362
- /* caller accept *ANY* socktype */
363
- pai->ai_socktype = SOCK_DGRAM;
364
- pai->ai_protocol = IPPROTO_UDP;
365
- }
366
- port = htons((unsigned short)atoi(servname));
367
- } else {
368
- struct servent *sp;
369
- char *proto;
370
-
371
- proto = NULL;
372
- switch (pai->ai_socktype) {
373
- case ANY:
374
- proto = NULL;
375
- break;
376
- case SOCK_DGRAM:
377
- proto = "udp";
378
- break;
379
- case SOCK_STREAM:
380
- proto = "tcp";
381
- break;
382
- default:
383
- fprintf(stderr, "panic!\n");
384
- break;
385
- }
386
- if ((sp = getservbyname((char*)servname, proto)) == NULL)
387
- ERR(EAI_SERVICE);
388
- port = sp->s_port;
389
- if (pai->ai_socktype == ANY)
390
- if (strcmp(sp->s_proto, "udp") == 0) {
391
- pai->ai_socktype = SOCK_DGRAM;
392
- pai->ai_protocol = IPPROTO_UDP;
393
- } else if (strcmp(sp->s_proto, "tcp") == 0) {
394
- pai->ai_socktype = SOCK_STREAM;
395
- pai->ai_protocol = IPPROTO_TCP;
396
- } else
397
- ERR(EAI_PROTOCOL); /*xxx*/
398
- }
399
- }
400
-
401
- /*
402
- * hostname == NULL.
403
- * passive socket -> anyaddr (0.0.0.0 or ::)
404
- * non-passive socket -> localhost (127.0.0.1 or ::1)
405
- */
406
- if (hostname == NULL) {
407
- const struct afd *afd;
408
- int s;
409
-
410
- for (afd = &afdl[0]; afd->a_af; afd++) {
411
- if (!(pai->ai_family == PF_UNSPEC
412
- || pai->ai_family == afd->a_af)) {
413
- continue;
414
- }
415
-
416
- /*
417
- * filter out AFs that are not supported by the kernel
418
- * XXX errno?
419
- */
420
- s = socket(afd->a_af, SOCK_DGRAM, 0);
421
- if (s < 0)
422
- continue;
423
- #if defined(HAVE_CLOSESOCKET)
424
- closesocket(s);
425
- #else
426
- close(s);
427
- #endif
428
-
429
- if (pai->ai_flags & AI_PASSIVE) {
430
- GET_AI(cur->ai_next, afd, afd->a_addrany, port);
431
- /* xxx meaningless?
432
- * GET_CANONNAME(cur->ai_next, "anyaddr");
433
- */
434
- } else {
435
- GET_AI(cur->ai_next, afd, afd->a_loopback,
436
- port);
437
- /* xxx meaningless?
438
- * GET_CANONNAME(cur->ai_next, "localhost");
439
- */
440
- }
441
- cur = cur->ai_next;
442
- }
443
- top = sentinel.ai_next;
444
- if (top)
445
- goto good;
446
- else
447
- ERR(EAI_FAMILY);
448
- }
449
-
450
- /* hostname as numeric name */
451
- for (i = 0; afdl[i].a_af; i++) {
452
- if (inet_pton(afdl[i].a_af, hostname, pton)) {
453
- u_long v4a;
454
- #ifdef INET6
455
- u_char pfx;
456
- #endif
457
-
458
- switch (afdl[i].a_af) {
459
- case AF_INET:
460
- v4a = ((struct in_addr *)pton)->s_addr;
461
- if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
462
- pai->ai_flags &= ~AI_CANONNAME;
463
- v4a >>= IN_CLASSA_NSHIFT;
464
- if (v4a == 0 || v4a == IN_LOOPBACKNET)
465
- pai->ai_flags &= ~AI_CANONNAME;
466
- break;
467
- #ifdef INET6
468
- case AF_INET6:
469
- #ifdef HAVE_ADDR8
470
- pfx = ((struct in6_addr *)pton)->s6_addr8[0];
471
- #else
472
- pfx = ((struct in6_addr *)pton)->s6_addr[0];
473
- #endif
474
- if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
475
- pai->ai_flags &= ~AI_CANONNAME;
476
- break;
477
- #endif
478
- }
479
-
480
- if (pai->ai_family == afdl[i].a_af ||
481
- pai->ai_family == PF_UNSPEC) {
482
- if (! (pai->ai_flags & AI_CANONNAME)) {
483
- GET_AI(top, &afdl[i], pton, port);
484
- goto good;
485
- }
486
- /*
487
- * if AI_CANONNAME and if reverse lookup
488
- * fail, return ai anyway to pacify
489
- * calling application.
490
- *
491
- * XXX getaddrinfo() is a name->address
492
- * translation function, and it looks strange
493
- * that we do addr->name translation here.
494
- */
495
- get_name(pton, &afdl[i], &top, pton, pai, port);
496
- goto good;
497
- } else
498
- ERR(EAI_FAMILY); /*xxx*/
499
- }
500
- }
501
-
502
- if (pai->ai_flags & AI_NUMERICHOST)
503
- ERR(EAI_NONAME);
504
-
505
- /* hostname as alphabetical name */
506
- error = get_addr(hostname, pai->ai_family, &top, pai, port);
507
- if (error == 0) {
508
- if (top) {
509
- good:
510
- *res = top;
511
- return SUCCESS;
512
- } else
513
- error = EAI_FAIL;
514
- }
515
- free:
516
- if (top)
517
- freeaddrinfo(top);
518
- bad:
519
- *res = NULL;
520
- return error;
521
- }
522
-
523
- static int
524
- get_name(addr, afd, res, numaddr, pai, port0)
525
- const char *addr;
526
- const struct afd *afd;
527
- struct addrinfo **res;
528
- char *numaddr;
529
- struct addrinfo *pai;
530
- int port0;
531
- {
532
- u_short port = port0 & 0xffff;
533
- struct hostent *hp;
534
- struct addrinfo *cur;
535
- int error = 0;
536
- #ifdef INET6
537
- int h_error;
538
- #endif
539
-
540
- #ifdef INET6
541
- hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
542
- #else
543
- hp = gethostbyaddr((char*)addr, afd->a_addrlen, AF_INET);
544
- #endif
545
- if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
546
- GET_AI(cur, afd, hp->h_addr_list[0], port);
547
- GET_CANONNAME(cur, hp->h_name);
548
- } else
549
- GET_AI(cur, afd, numaddr, port);
550
-
551
- #ifdef INET6
552
- if (hp)
553
- freehostent(hp);
554
- #endif
555
- *res = cur;
556
- return SUCCESS;
557
- free:
558
- if (cur)
559
- freeaddrinfo(cur);
560
- #ifdef INET6
561
- if (hp)
562
- freehostent(hp);
563
- #endif
564
- /* bad: */
565
- *res = NULL;
566
- return error;
567
- }
568
-
569
- static int
570
- get_addr(hostname, af, res, pai, port0)
571
- const char *hostname;
572
- int af;
573
- struct addrinfo **res;
574
- struct addrinfo *pai;
575
- int port0;
576
- {
577
- u_short port = port0 & 0xffff;
578
- struct addrinfo sentinel;
579
- struct hostent *hp;
580
- struct addrinfo *top, *cur;
581
- const struct afd *afd;
582
- int i, error = 0, h_error;
583
- char *ap;
584
-
585
- top = NULL;
586
- sentinel.ai_next = NULL;
587
- cur = &sentinel;
588
- #ifdef INET6
589
- if (af == AF_UNSPEC) {
590
- hp = getipnodebyname(hostname, AF_INET6,
591
- AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
592
- } else
593
- hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
594
- #else
595
- hp = gethostbyname((char*)hostname);
596
- h_error = h_errno;
597
- #endif
598
- if (hp == NULL) {
599
- switch (h_error) {
600
- case HOST_NOT_FOUND:
601
- case NO_DATA:
602
- error = EAI_NODATA;
603
- break;
604
- case TRY_AGAIN:
605
- error = EAI_AGAIN;
606
- break;
607
- case NO_RECOVERY:
608
- default:
609
- error = EAI_FAIL;
610
- break;
611
- }
612
- goto bad;
613
- }
614
-
615
- if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
616
- (hp->h_addr_list[0] == NULL))
617
- ERR(EAI_FAIL);
618
-
619
- for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
620
- switch (af) {
621
- #ifdef INET6
622
- case AF_INET6:
623
- afd = &afdl[N_INET6];
624
- break;
625
- #endif
626
- #ifndef INET6
627
- default: /* AF_UNSPEC */
628
- #endif
629
- case AF_INET:
630
- afd = &afdl[N_INET];
631
- break;
632
- #ifdef INET6
633
- default: /* AF_UNSPEC */
634
- if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
635
- ap += sizeof(struct in6_addr) -
636
- sizeof(struct in_addr);
637
- afd = &afdl[N_INET];
638
- } else
639
- afd = &afdl[N_INET6];
640
- break;
641
- #endif
642
- }
643
- #ifdef FAITH
644
- if (translate && afd->a_af == AF_INET) {
645
- struct in6_addr *in6;
646
-
647
- GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
648
- in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
649
- memcpy(&in6->s6_addr32[0], &faith_prefix,
650
- sizeof(struct in6_addr) - sizeof(struct in_addr));
651
- memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
652
- } else
653
- #endif /* FAITH */
654
- GET_AI(cur->ai_next, afd, ap, port);
655
- if (cur == &sentinel) {
656
- top = cur->ai_next;
657
- GET_CANONNAME(top, hp->h_name);
658
- }
659
- cur = cur->ai_next;
660
- }
661
- #ifdef INET6
662
- freehostent(hp);
663
- #endif
664
- *res = top;
665
- return SUCCESS;
666
- free:
667
- if (top)
668
- freeaddrinfo(top);
669
- #ifdef INET6
670
- if (hp)
671
- freehostent(hp);
672
- #endif
673
- bad:
674
- *res = NULL;
675
- return error;
676
- }