passenger 5.0.8 → 5.0.9
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of passenger might be problematic. Click here for more details.
- checksums.yaml +8 -8
- checksums.yaml.gz.asc +7 -7
- data.tar.gz.asc +7 -7
- data/.editorconfig +20 -0
- data/CHANGELOG +21 -0
- data/bin/passenger-install-apache2-module +3 -1
- data/build/agents.rb +7 -5
- data/build/basics.rb +3 -3
- data/build/common_library.rb +52 -30
- data/build/cxx_tests.rb +20 -13
- data/build/misc.rb +5 -5
- data/doc/Design and Architecture.html +1 -1
- data/doc/Design and Architecture.txt +1 -1
- data/doc/Packaging.html +4 -4
- data/doc/Packaging.txt.md +4 -4
- data/doc/Users guide Apache.html +22 -9
- data/doc/Users guide Apache.idmap.txt +4 -2
- data/doc/Users guide Apache.txt +2 -0
- data/doc/Users guide Nginx.html +22 -9
- data/doc/Users guide Nginx.idmap.txt +4 -2
- data/doc/Users guide Nginx.txt +2 -0
- data/doc/Users guide Standalone.html +14 -9
- data/doc/Users guide Standalone.idmap.txt +4 -2
- data/doc/users_guide_snippets/installation.txt +10 -6
- data/ext/apache2/Hooks.cpp +13 -2
- data/ext/common/ApplicationPool2/Pool/Inspection.h +8 -3
- data/ext/common/BackgroundEventLoop.cpp +249 -67
- data/ext/common/BackgroundEventLoop.h +5 -5
- data/ext/common/Constants.h +1 -1
- data/ext/common/InstanceDirectory.h +8 -6
- data/ext/common/ServerKit/Context.h +8 -2
- data/ext/common/ServerKit/FileBufferedChannel.h +262 -226
- data/ext/common/ServerKit/HeaderTable.h +28 -3
- data/ext/common/ServerKit/HttpHeaderParser.h +37 -13
- data/ext/common/ServerKit/HttpServer.h +17 -1
- data/ext/common/ServerKit/Implementation.cpp +2 -0
- data/ext/common/ServerKit/Server.h +25 -28
- data/ext/common/Utils/IOUtils.cpp +11 -0
- data/ext/common/Utils/ProcessMetricsCollector.h +4 -0
- data/ext/common/Utils/StrIntUtils.cpp +11 -7
- data/ext/common/Utils/StrIntUtils.h +1 -1
- data/ext/common/Utils/StrIntUtilsNoStrictAliasing.cpp +21 -16
- data/ext/common/agents/Base.cpp +6 -0
- data/ext/common/agents/Base.h +2 -0
- data/ext/common/agents/HelperAgent/AdminServer.h +25 -25
- data/ext/common/agents/HelperAgent/Main.cpp +37 -12
- data/ext/common/agents/HelperAgent/RequestHandler.h +18 -20
- data/ext/common/agents/HelperAgent/RequestHandler/AppResponse.h +4 -0
- data/ext/common/agents/HelperAgent/RequestHandler/ForwardResponse.cpp +10 -6
- data/ext/common/agents/HelperAgent/RequestHandler/Hooks.cpp +2 -0
- data/ext/common/agents/HelperAgent/RequestHandler/InitRequest.cpp +1 -1
- data/ext/common/agents/HelperAgent/RequestHandler/SendRequest.cpp +1 -1
- data/ext/common/agents/HelperAgent/RequestHandler/Utils.cpp +9 -2
- data/ext/common/agents/HelperAgent/ResponseCache.h +11 -11
- data/ext/common/agents/LoggingAgent/AdminServer.h +8 -8
- data/ext/common/agents/LoggingAgent/Main.cpp +6 -5
- data/ext/common/agents/Watchdog/AdminServer.h +13 -13
- data/ext/common/agents/Watchdog/Main.cpp +8 -3
- data/ext/libuv/.gitignore +72 -0
- data/ext/libuv/AUTHORS +199 -0
- data/ext/libuv/ChangeLog +2023 -0
- data/ext/libuv/LICENSE +46 -0
- data/ext/libuv/Makefile.am +336 -0
- data/ext/libuv/README.md +197 -0
- data/ext/libuv/checksparse.sh +233 -0
- data/ext/libuv/common.gypi +210 -0
- data/ext/libuv/configure.ac +67 -0
- data/ext/libuv/gyp_uv.py +96 -0
- data/ext/libuv/include/android-ifaddrs.h +54 -0
- data/ext/libuv/include/pthread-fixes.h +72 -0
- data/ext/libuv/include/tree.h +768 -0
- data/ext/libuv/include/uv-aix.h +32 -0
- data/ext/libuv/include/uv-bsd.h +34 -0
- data/ext/libuv/include/uv-darwin.h +61 -0
- data/ext/libuv/include/uv-errno.h +418 -0
- data/ext/libuv/include/uv-linux.h +34 -0
- data/ext/libuv/include/uv-sunos.h +44 -0
- data/ext/libuv/include/uv-threadpool.h +37 -0
- data/ext/libuv/include/uv-unix.h +383 -0
- data/ext/libuv/include/uv-version.h +39 -0
- data/ext/libuv/include/uv.h +1455 -0
- data/ext/libuv/libuv.pc.in +11 -0
- data/ext/libuv/m4/.gitignore +4 -0
- data/ext/libuv/m4/as_case.m4 +21 -0
- data/ext/libuv/m4/libuv-check-flags.m4 +319 -0
- data/ext/libuv/src/fs-poll.c +255 -0
- data/ext/libuv/src/heap-inl.h +245 -0
- data/ext/libuv/src/inet.c +313 -0
- data/ext/libuv/src/queue.h +92 -0
- data/ext/libuv/src/threadpool.c +303 -0
- data/ext/libuv/src/unix/aix.c +1240 -0
- data/ext/libuv/src/unix/android-ifaddrs.c +703 -0
- data/ext/libuv/src/unix/async.c +284 -0
- data/ext/libuv/src/unix/atomic-ops.h +60 -0
- data/ext/libuv/src/unix/core.c +985 -0
- data/ext/libuv/src/unix/darwin-proctitle.c +206 -0
- data/ext/libuv/src/unix/darwin.c +331 -0
- data/ext/libuv/src/unix/dl.c +83 -0
- data/ext/libuv/src/unix/freebsd.c +435 -0
- data/ext/libuv/src/unix/fs.c +1189 -0
- data/ext/libuv/src/unix/fsevents.c +899 -0
- data/ext/libuv/src/unix/getaddrinfo.c +202 -0
- data/ext/libuv/src/unix/getnameinfo.c +120 -0
- data/ext/libuv/src/unix/internal.h +314 -0
- data/ext/libuv/src/unix/kqueue.c +418 -0
- data/ext/libuv/src/unix/linux-core.c +876 -0
- data/ext/libuv/src/unix/linux-inotify.c +257 -0
- data/ext/libuv/src/unix/linux-syscalls.c +471 -0
- data/ext/libuv/src/unix/linux-syscalls.h +158 -0
- data/ext/libuv/src/unix/loop-watcher.c +63 -0
- data/ext/libuv/src/unix/loop.c +135 -0
- data/ext/libuv/src/unix/netbsd.c +368 -0
- data/ext/libuv/src/unix/openbsd.c +384 -0
- data/ext/libuv/src/unix/pipe.c +288 -0
- data/ext/libuv/src/unix/poll.c +113 -0
- data/ext/libuv/src/unix/process.c +551 -0
- data/ext/libuv/src/unix/proctitle.c +102 -0
- data/ext/libuv/src/unix/pthread-fixes.c +103 -0
- data/ext/libuv/src/unix/signal.c +465 -0
- data/ext/libuv/src/unix/spinlock.h +53 -0
- data/ext/libuv/src/unix/stream.c +1598 -0
- data/ext/libuv/src/unix/sunos.c +763 -0
- data/ext/libuv/src/unix/tcp.c +327 -0
- data/ext/libuv/src/unix/thread.c +519 -0
- data/ext/libuv/src/unix/timer.c +172 -0
- data/ext/libuv/src/unix/tty.c +265 -0
- data/ext/libuv/src/unix/udp.c +833 -0
- data/ext/libuv/src/uv-common.c +544 -0
- data/ext/libuv/src/uv-common.h +214 -0
- data/ext/libuv/src/version.c +49 -0
- data/ext/libuv/uv.gyp +487 -0
- data/ext/nginx/ContentHandler.c +21 -10
- data/ext/nginx/ngx_http_passenger_module.c +7 -0
- data/ext/oxt/implementation.cpp +9 -2
- data/ext/oxt/initialize.hpp +5 -1
- data/lib/phusion_passenger.rb +3 -3
- data/lib/phusion_passenger/admin_tools/instance.rb +10 -6
- data/lib/phusion_passenger/admin_tools/instance_registry.rb +6 -2
- data/lib/phusion_passenger/packaging.rb +3 -4
- data/lib/phusion_passenger/platform_info.rb +13 -1
- data/lib/phusion_passenger/platform_info/apache.rb +15 -4
- data/lib/phusion_passenger/platform_info/apache_detector.rb +5 -1
- data/lib/phusion_passenger/rack/thread_handler_extension.rb +184 -99
- data/lib/phusion_passenger/request_handler/thread_handler.rb +13 -6
- data/lib/phusion_passenger/standalone/start_command.rb +2 -2
- data/resources/templates/apache2/apache_install_broken.txt.erb +2 -1
- metadata +99 -22
- metadata.gz.asc +7 -7
- data/ext/libeio/Changes +0 -76
- data/ext/libeio/LICENSE +0 -36
- data/ext/libeio/Makefile.am +0 -15
- data/ext/libeio/Makefile.in +0 -694
- data/ext/libeio/aclocal.m4 +0 -9418
- data/ext/libeio/autogen.sh +0 -3
- data/ext/libeio/config.guess +0 -1540
- data/ext/libeio/config.h.in +0 -136
- data/ext/libeio/config.sub +0 -1779
- data/ext/libeio/configure +0 -14822
- data/ext/libeio/configure.ac +0 -22
- data/ext/libeio/demo.c +0 -194
- data/ext/libeio/ecb.h +0 -714
- data/ext/libeio/eio.c +0 -2818
- data/ext/libeio/eio.h +0 -414
- data/ext/libeio/install-sh +0 -520
- data/ext/libeio/libeio.m4 +0 -195
- data/ext/libeio/ltmain.sh +0 -9636
- data/ext/libeio/missing +0 -376
- data/ext/libeio/xthread.h +0 -166
@@ -0,0 +1,245 @@
|
|
1
|
+
/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
|
2
|
+
*
|
3
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
* purpose with or without fee is hereby granted, provided that the above
|
5
|
+
* copyright notice and this permission notice appear in all copies.
|
6
|
+
*
|
7
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
8
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
10
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
12
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
13
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
14
|
+
*/
|
15
|
+
|
16
|
+
#ifndef UV_SRC_HEAP_H_
|
17
|
+
#define UV_SRC_HEAP_H_
|
18
|
+
|
19
|
+
#include <stddef.h> /* NULL */
|
20
|
+
|
21
|
+
#if defined(__GNUC__)
|
22
|
+
# define HEAP_EXPORT(declaration) __attribute__((unused)) static declaration
|
23
|
+
#else
|
24
|
+
# define HEAP_EXPORT(declaration) static declaration
|
25
|
+
#endif
|
26
|
+
|
27
|
+
struct heap_node {
|
28
|
+
struct heap_node* left;
|
29
|
+
struct heap_node* right;
|
30
|
+
struct heap_node* parent;
|
31
|
+
};
|
32
|
+
|
33
|
+
/* A binary min heap. The usual properties hold: the root is the lowest
|
34
|
+
* element in the set, the height of the tree is at most log2(nodes) and
|
35
|
+
* it's always a complete binary tree.
|
36
|
+
*
|
37
|
+
* The heap function try hard to detect corrupted tree nodes at the cost
|
38
|
+
* of a minor reduction in performance. Compile with -DNDEBUG to disable.
|
39
|
+
*/
|
40
|
+
struct heap {
|
41
|
+
struct heap_node* min;
|
42
|
+
unsigned int nelts;
|
43
|
+
};
|
44
|
+
|
45
|
+
/* Return non-zero if a < b. */
|
46
|
+
typedef int (*heap_compare_fn)(const struct heap_node* a,
|
47
|
+
const struct heap_node* b);
|
48
|
+
|
49
|
+
/* Public functions. */
|
50
|
+
HEAP_EXPORT(void heap_init(struct heap* heap));
|
51
|
+
HEAP_EXPORT(struct heap_node* heap_min(const struct heap* heap));
|
52
|
+
HEAP_EXPORT(void heap_insert(struct heap* heap,
|
53
|
+
struct heap_node* newnode,
|
54
|
+
heap_compare_fn less_than));
|
55
|
+
HEAP_EXPORT(void heap_remove(struct heap* heap,
|
56
|
+
struct heap_node* node,
|
57
|
+
heap_compare_fn less_than));
|
58
|
+
HEAP_EXPORT(void heap_dequeue(struct heap* heap, heap_compare_fn less_than));
|
59
|
+
|
60
|
+
/* Implementation follows. */
|
61
|
+
|
62
|
+
HEAP_EXPORT(void heap_init(struct heap* heap)) {
|
63
|
+
heap->min = NULL;
|
64
|
+
heap->nelts = 0;
|
65
|
+
}
|
66
|
+
|
67
|
+
HEAP_EXPORT(struct heap_node* heap_min(const struct heap* heap)) {
|
68
|
+
return heap->min;
|
69
|
+
}
|
70
|
+
|
71
|
+
/* Swap parent with child. Child moves closer to the root, parent moves away. */
|
72
|
+
static void heap_node_swap(struct heap* heap,
|
73
|
+
struct heap_node* parent,
|
74
|
+
struct heap_node* child) {
|
75
|
+
struct heap_node* sibling;
|
76
|
+
struct heap_node t;
|
77
|
+
|
78
|
+
t = *parent;
|
79
|
+
*parent = *child;
|
80
|
+
*child = t;
|
81
|
+
|
82
|
+
parent->parent = child;
|
83
|
+
if (child->left == child) {
|
84
|
+
child->left = parent;
|
85
|
+
sibling = child->right;
|
86
|
+
} else {
|
87
|
+
child->right = parent;
|
88
|
+
sibling = child->left;
|
89
|
+
}
|
90
|
+
if (sibling != NULL)
|
91
|
+
sibling->parent = child;
|
92
|
+
|
93
|
+
if (parent->left != NULL)
|
94
|
+
parent->left->parent = parent;
|
95
|
+
if (parent->right != NULL)
|
96
|
+
parent->right->parent = parent;
|
97
|
+
|
98
|
+
if (child->parent == NULL)
|
99
|
+
heap->min = child;
|
100
|
+
else if (child->parent->left == parent)
|
101
|
+
child->parent->left = child;
|
102
|
+
else
|
103
|
+
child->parent->right = child;
|
104
|
+
}
|
105
|
+
|
106
|
+
HEAP_EXPORT(void heap_insert(struct heap* heap,
|
107
|
+
struct heap_node* newnode,
|
108
|
+
heap_compare_fn less_than)) {
|
109
|
+
struct heap_node** parent;
|
110
|
+
struct heap_node** child;
|
111
|
+
unsigned int path;
|
112
|
+
unsigned int n;
|
113
|
+
unsigned int k;
|
114
|
+
|
115
|
+
newnode->left = NULL;
|
116
|
+
newnode->right = NULL;
|
117
|
+
newnode->parent = NULL;
|
118
|
+
|
119
|
+
/* Calculate the path from the root to the insertion point. This is a min
|
120
|
+
* heap so we always insert at the left-most free node of the bottom row.
|
121
|
+
*/
|
122
|
+
path = 0;
|
123
|
+
for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2)
|
124
|
+
path = (path << 1) | (n & 1);
|
125
|
+
|
126
|
+
/* Now traverse the heap using the path we calculated in the previous step. */
|
127
|
+
parent = child = &heap->min;
|
128
|
+
while (k > 0) {
|
129
|
+
parent = child;
|
130
|
+
if (path & 1)
|
131
|
+
child = &(*child)->right;
|
132
|
+
else
|
133
|
+
child = &(*child)->left;
|
134
|
+
path >>= 1;
|
135
|
+
k -= 1;
|
136
|
+
}
|
137
|
+
|
138
|
+
/* Insert the new node. */
|
139
|
+
newnode->parent = *parent;
|
140
|
+
*child = newnode;
|
141
|
+
heap->nelts += 1;
|
142
|
+
|
143
|
+
/* Walk up the tree and check at each node if the heap property holds.
|
144
|
+
* It's a min heap so parent < child must be true.
|
145
|
+
*/
|
146
|
+
while (newnode->parent != NULL && less_than(newnode, newnode->parent))
|
147
|
+
heap_node_swap(heap, newnode->parent, newnode);
|
148
|
+
}
|
149
|
+
|
150
|
+
HEAP_EXPORT(void heap_remove(struct heap* heap,
|
151
|
+
struct heap_node* node,
|
152
|
+
heap_compare_fn less_than)) {
|
153
|
+
struct heap_node* smallest;
|
154
|
+
struct heap_node** max;
|
155
|
+
struct heap_node* child;
|
156
|
+
unsigned int path;
|
157
|
+
unsigned int k;
|
158
|
+
unsigned int n;
|
159
|
+
|
160
|
+
if (heap->nelts == 0)
|
161
|
+
return;
|
162
|
+
|
163
|
+
/* Calculate the path from the min (the root) to the max, the left-most node
|
164
|
+
* of the bottom row.
|
165
|
+
*/
|
166
|
+
path = 0;
|
167
|
+
for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2)
|
168
|
+
path = (path << 1) | (n & 1);
|
169
|
+
|
170
|
+
/* Now traverse the heap using the path we calculated in the previous step. */
|
171
|
+
max = &heap->min;
|
172
|
+
while (k > 0) {
|
173
|
+
if (path & 1)
|
174
|
+
max = &(*max)->right;
|
175
|
+
else
|
176
|
+
max = &(*max)->left;
|
177
|
+
path >>= 1;
|
178
|
+
k -= 1;
|
179
|
+
}
|
180
|
+
|
181
|
+
heap->nelts -= 1;
|
182
|
+
|
183
|
+
/* Unlink the max node. */
|
184
|
+
child = *max;
|
185
|
+
*max = NULL;
|
186
|
+
|
187
|
+
if (child == node) {
|
188
|
+
/* We're removing either the max or the last node in the tree. */
|
189
|
+
if (child == heap->min) {
|
190
|
+
heap->min = NULL;
|
191
|
+
}
|
192
|
+
return;
|
193
|
+
}
|
194
|
+
|
195
|
+
/* Replace the to be deleted node with the max node. */
|
196
|
+
child->left = node->left;
|
197
|
+
child->right = node->right;
|
198
|
+
child->parent = node->parent;
|
199
|
+
|
200
|
+
if (child->left != NULL) {
|
201
|
+
child->left->parent = child;
|
202
|
+
}
|
203
|
+
|
204
|
+
if (child->right != NULL) {
|
205
|
+
child->right->parent = child;
|
206
|
+
}
|
207
|
+
|
208
|
+
if (node->parent == NULL) {
|
209
|
+
heap->min = child;
|
210
|
+
} else if (node->parent->left == node) {
|
211
|
+
node->parent->left = child;
|
212
|
+
} else {
|
213
|
+
node->parent->right = child;
|
214
|
+
}
|
215
|
+
|
216
|
+
/* Walk down the subtree and check at each node if the heap property holds.
|
217
|
+
* It's a min heap so parent < child must be true. If the parent is bigger,
|
218
|
+
* swap it with the smallest child.
|
219
|
+
*/
|
220
|
+
for (;;) {
|
221
|
+
smallest = child;
|
222
|
+
if (child->left != NULL && less_than(child->left, smallest))
|
223
|
+
smallest = child->left;
|
224
|
+
if (child->right != NULL && less_than(child->right, smallest))
|
225
|
+
smallest = child->right;
|
226
|
+
if (smallest == child)
|
227
|
+
break;
|
228
|
+
heap_node_swap(heap, child, smallest);
|
229
|
+
}
|
230
|
+
|
231
|
+
/* Walk up the subtree and check that each parent is less than the node
|
232
|
+
* this is required, because `max` node is not guaranteed to be the
|
233
|
+
* actual maximum in tree
|
234
|
+
*/
|
235
|
+
while (child->parent != NULL && less_than(child, child->parent))
|
236
|
+
heap_node_swap(heap, child->parent, child);
|
237
|
+
}
|
238
|
+
|
239
|
+
HEAP_EXPORT(void heap_dequeue(struct heap* heap, heap_compare_fn less_than)) {
|
240
|
+
heap_remove(heap, heap->min, less_than);
|
241
|
+
}
|
242
|
+
|
243
|
+
#undef HEAP_EXPORT
|
244
|
+
|
245
|
+
#endif /* UV_SRC_HEAP_H_ */
|
@@ -0,0 +1,313 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
|
3
|
+
* Copyright (c) 1996-1999 by Internet Software Consortium.
|
4
|
+
*
|
5
|
+
* Permission to use, copy, modify, and distribute this software for any
|
6
|
+
* purpose with or without fee is hereby granted, provided that the above
|
7
|
+
* copyright notice and this permission notice appear in all copies.
|
8
|
+
*
|
9
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
|
10
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
11
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
|
12
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
13
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
14
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
15
|
+
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include <stdio.h>
|
19
|
+
#include <string.h>
|
20
|
+
|
21
|
+
#if defined(_MSC_VER) && _MSC_VER < 1600
|
22
|
+
# include "stdint-msvc2008.h"
|
23
|
+
#else
|
24
|
+
# include <stdint.h>
|
25
|
+
#endif
|
26
|
+
|
27
|
+
#include "uv.h"
|
28
|
+
#include "uv-common.h"
|
29
|
+
|
30
|
+
#define UV__INET_ADDRSTRLEN 16
|
31
|
+
#define UV__INET6_ADDRSTRLEN 46
|
32
|
+
|
33
|
+
|
34
|
+
static int inet_ntop4(const unsigned char *src, char *dst, size_t size);
|
35
|
+
static int inet_ntop6(const unsigned char *src, char *dst, size_t size);
|
36
|
+
static int inet_pton4(const char *src, unsigned char *dst);
|
37
|
+
static int inet_pton6(const char *src, unsigned char *dst);
|
38
|
+
|
39
|
+
|
40
|
+
int uv_inet_ntop(int af, const void* src, char* dst, size_t size) {
|
41
|
+
switch (af) {
|
42
|
+
case AF_INET:
|
43
|
+
return (inet_ntop4(src, dst, size));
|
44
|
+
case AF_INET6:
|
45
|
+
return (inet_ntop6(src, dst, size));
|
46
|
+
default:
|
47
|
+
return UV_EAFNOSUPPORT;
|
48
|
+
}
|
49
|
+
/* NOTREACHED */
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
static int inet_ntop4(const unsigned char *src, char *dst, size_t size) {
|
54
|
+
static const char fmt[] = "%u.%u.%u.%u";
|
55
|
+
char tmp[UV__INET_ADDRSTRLEN];
|
56
|
+
int l;
|
57
|
+
|
58
|
+
#ifndef _WIN32
|
59
|
+
l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
|
60
|
+
#else
|
61
|
+
l = _snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
|
62
|
+
#endif
|
63
|
+
if (l <= 0 || (size_t) l >= size) {
|
64
|
+
return UV_ENOSPC;
|
65
|
+
}
|
66
|
+
strncpy(dst, tmp, size);
|
67
|
+
dst[size - 1] = '\0';
|
68
|
+
return 0;
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
static int inet_ntop6(const unsigned char *src, char *dst, size_t size) {
|
73
|
+
/*
|
74
|
+
* Note that int32_t and int16_t need only be "at least" large enough
|
75
|
+
* to contain a value of the specified size. On some systems, like
|
76
|
+
* Crays, there is no such thing as an integer variable with 16 bits.
|
77
|
+
* Keep this in mind if you think this function should have been coded
|
78
|
+
* to use pointer overlays. All the world's not a VAX.
|
79
|
+
*/
|
80
|
+
char tmp[UV__INET6_ADDRSTRLEN], *tp;
|
81
|
+
struct { int base, len; } best, cur;
|
82
|
+
unsigned int words[sizeof(struct in6_addr) / sizeof(uint16_t)];
|
83
|
+
int i;
|
84
|
+
|
85
|
+
/*
|
86
|
+
* Preprocess:
|
87
|
+
* Copy the input (bytewise) array into a wordwise array.
|
88
|
+
* Find the longest run of 0x00's in src[] for :: shorthanding.
|
89
|
+
*/
|
90
|
+
memset(words, '\0', sizeof words);
|
91
|
+
for (i = 0; i < (int) sizeof(struct in6_addr); i++)
|
92
|
+
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
|
93
|
+
best.base = -1;
|
94
|
+
best.len = 0;
|
95
|
+
cur.base = -1;
|
96
|
+
cur.len = 0;
|
97
|
+
for (i = 0; i < (int) ARRAY_SIZE(words); i++) {
|
98
|
+
if (words[i] == 0) {
|
99
|
+
if (cur.base == -1)
|
100
|
+
cur.base = i, cur.len = 1;
|
101
|
+
else
|
102
|
+
cur.len++;
|
103
|
+
} else {
|
104
|
+
if (cur.base != -1) {
|
105
|
+
if (best.base == -1 || cur.len > best.len)
|
106
|
+
best = cur;
|
107
|
+
cur.base = -1;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
111
|
+
if (cur.base != -1) {
|
112
|
+
if (best.base == -1 || cur.len > best.len)
|
113
|
+
best = cur;
|
114
|
+
}
|
115
|
+
if (best.base != -1 && best.len < 2)
|
116
|
+
best.base = -1;
|
117
|
+
|
118
|
+
/*
|
119
|
+
* Format the result.
|
120
|
+
*/
|
121
|
+
tp = tmp;
|
122
|
+
for (i = 0; i < (int) ARRAY_SIZE(words); i++) {
|
123
|
+
/* Are we inside the best run of 0x00's? */
|
124
|
+
if (best.base != -1 && i >= best.base &&
|
125
|
+
i < (best.base + best.len)) {
|
126
|
+
if (i == best.base)
|
127
|
+
*tp++ = ':';
|
128
|
+
continue;
|
129
|
+
}
|
130
|
+
/* Are we following an initial run of 0x00s or any real hex? */
|
131
|
+
if (i != 0)
|
132
|
+
*tp++ = ':';
|
133
|
+
/* Is this address an encapsulated IPv4? */
|
134
|
+
if (i == 6 && best.base == 0 && (best.len == 6 ||
|
135
|
+
(best.len == 7 && words[7] != 0x0001) ||
|
136
|
+
(best.len == 5 && words[5] == 0xffff))) {
|
137
|
+
int err = inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp));
|
138
|
+
if (err)
|
139
|
+
return err;
|
140
|
+
tp += strlen(tp);
|
141
|
+
break;
|
142
|
+
}
|
143
|
+
tp += sprintf(tp, "%x", words[i]);
|
144
|
+
}
|
145
|
+
/* Was it a trailing run of 0x00's? */
|
146
|
+
if (best.base != -1 && (best.base + best.len) == ARRAY_SIZE(words))
|
147
|
+
*tp++ = ':';
|
148
|
+
*tp++ = '\0';
|
149
|
+
|
150
|
+
/*
|
151
|
+
* Check for overflow, copy, and we're done.
|
152
|
+
*/
|
153
|
+
if ((size_t)(tp - tmp) > size) {
|
154
|
+
return UV_ENOSPC;
|
155
|
+
}
|
156
|
+
strcpy(dst, tmp);
|
157
|
+
return 0;
|
158
|
+
}
|
159
|
+
|
160
|
+
|
161
|
+
int uv_inet_pton(int af, const char* src, void* dst) {
|
162
|
+
if (src == NULL || dst == NULL)
|
163
|
+
return UV_EINVAL;
|
164
|
+
|
165
|
+
switch (af) {
|
166
|
+
case AF_INET:
|
167
|
+
return (inet_pton4(src, dst));
|
168
|
+
case AF_INET6: {
|
169
|
+
int len;
|
170
|
+
char tmp[UV__INET6_ADDRSTRLEN], *s, *p;
|
171
|
+
s = (char*) src;
|
172
|
+
p = strchr(src, '%');
|
173
|
+
if (p != NULL) {
|
174
|
+
s = tmp;
|
175
|
+
len = p - src;
|
176
|
+
if (len > UV__INET6_ADDRSTRLEN-1)
|
177
|
+
return UV_EINVAL;
|
178
|
+
memcpy(s, src, len);
|
179
|
+
s[len] = '\0';
|
180
|
+
}
|
181
|
+
return inet_pton6(s, dst);
|
182
|
+
}
|
183
|
+
default:
|
184
|
+
return UV_EAFNOSUPPORT;
|
185
|
+
}
|
186
|
+
/* NOTREACHED */
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
static int inet_pton4(const char *src, unsigned char *dst) {
|
191
|
+
static const char digits[] = "0123456789";
|
192
|
+
int saw_digit, octets, ch;
|
193
|
+
unsigned char tmp[sizeof(struct in_addr)], *tp;
|
194
|
+
|
195
|
+
saw_digit = 0;
|
196
|
+
octets = 0;
|
197
|
+
*(tp = tmp) = 0;
|
198
|
+
while ((ch = *src++) != '\0') {
|
199
|
+
const char *pch;
|
200
|
+
|
201
|
+
if ((pch = strchr(digits, ch)) != NULL) {
|
202
|
+
unsigned int nw = *tp * 10 + (pch - digits);
|
203
|
+
|
204
|
+
if (saw_digit && *tp == 0)
|
205
|
+
return UV_EINVAL;
|
206
|
+
if (nw > 255)
|
207
|
+
return UV_EINVAL;
|
208
|
+
*tp = nw;
|
209
|
+
if (!saw_digit) {
|
210
|
+
if (++octets > 4)
|
211
|
+
return UV_EINVAL;
|
212
|
+
saw_digit = 1;
|
213
|
+
}
|
214
|
+
} else if (ch == '.' && saw_digit) {
|
215
|
+
if (octets == 4)
|
216
|
+
return UV_EINVAL;
|
217
|
+
*++tp = 0;
|
218
|
+
saw_digit = 0;
|
219
|
+
} else
|
220
|
+
return UV_EINVAL;
|
221
|
+
}
|
222
|
+
if (octets < 4)
|
223
|
+
return UV_EINVAL;
|
224
|
+
memcpy(dst, tmp, sizeof(struct in_addr));
|
225
|
+
return 0;
|
226
|
+
}
|
227
|
+
|
228
|
+
|
229
|
+
static int inet_pton6(const char *src, unsigned char *dst) {
|
230
|
+
static const char xdigits_l[] = "0123456789abcdef",
|
231
|
+
xdigits_u[] = "0123456789ABCDEF";
|
232
|
+
unsigned char tmp[sizeof(struct in6_addr)], *tp, *endp, *colonp;
|
233
|
+
const char *xdigits, *curtok;
|
234
|
+
int ch, seen_xdigits;
|
235
|
+
unsigned int val;
|
236
|
+
|
237
|
+
memset((tp = tmp), '\0', sizeof tmp);
|
238
|
+
endp = tp + sizeof tmp;
|
239
|
+
colonp = NULL;
|
240
|
+
/* Leading :: requires some special handling. */
|
241
|
+
if (*src == ':')
|
242
|
+
if (*++src != ':')
|
243
|
+
return UV_EINVAL;
|
244
|
+
curtok = src;
|
245
|
+
seen_xdigits = 0;
|
246
|
+
val = 0;
|
247
|
+
while ((ch = *src++) != '\0') {
|
248
|
+
const char *pch;
|
249
|
+
|
250
|
+
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
|
251
|
+
pch = strchr((xdigits = xdigits_u), ch);
|
252
|
+
if (pch != NULL) {
|
253
|
+
val <<= 4;
|
254
|
+
val |= (pch - xdigits);
|
255
|
+
if (++seen_xdigits > 4)
|
256
|
+
return UV_EINVAL;
|
257
|
+
continue;
|
258
|
+
}
|
259
|
+
if (ch == ':') {
|
260
|
+
curtok = src;
|
261
|
+
if (!seen_xdigits) {
|
262
|
+
if (colonp)
|
263
|
+
return UV_EINVAL;
|
264
|
+
colonp = tp;
|
265
|
+
continue;
|
266
|
+
} else if (*src == '\0') {
|
267
|
+
return UV_EINVAL;
|
268
|
+
}
|
269
|
+
if (tp + sizeof(uint16_t) > endp)
|
270
|
+
return UV_EINVAL;
|
271
|
+
*tp++ = (unsigned char) (val >> 8) & 0xff;
|
272
|
+
*tp++ = (unsigned char) val & 0xff;
|
273
|
+
seen_xdigits = 0;
|
274
|
+
val = 0;
|
275
|
+
continue;
|
276
|
+
}
|
277
|
+
if (ch == '.' && ((tp + sizeof(struct in_addr)) <= endp)) {
|
278
|
+
int err = inet_pton4(curtok, tp);
|
279
|
+
if (err == 0) {
|
280
|
+
tp += sizeof(struct in_addr);
|
281
|
+
seen_xdigits = 0;
|
282
|
+
break; /*%< '\\0' was seen by inet_pton4(). */
|
283
|
+
}
|
284
|
+
}
|
285
|
+
return UV_EINVAL;
|
286
|
+
}
|
287
|
+
if (seen_xdigits) {
|
288
|
+
if (tp + sizeof(uint16_t) > endp)
|
289
|
+
return UV_EINVAL;
|
290
|
+
*tp++ = (unsigned char) (val >> 8) & 0xff;
|
291
|
+
*tp++ = (unsigned char) val & 0xff;
|
292
|
+
}
|
293
|
+
if (colonp != NULL) {
|
294
|
+
/*
|
295
|
+
* Since some memmove()'s erroneously fail to handle
|
296
|
+
* overlapping regions, we'll do the shift by hand.
|
297
|
+
*/
|
298
|
+
const int n = tp - colonp;
|
299
|
+
int i;
|
300
|
+
|
301
|
+
if (tp == endp)
|
302
|
+
return UV_EINVAL;
|
303
|
+
for (i = 1; i <= n; i++) {
|
304
|
+
endp[- i] = colonp[n - i];
|
305
|
+
colonp[n - i] = 0;
|
306
|
+
}
|
307
|
+
tp = endp;
|
308
|
+
}
|
309
|
+
if (tp != endp)
|
310
|
+
return UV_EINVAL;
|
311
|
+
memcpy(dst, tmp, sizeof tmp);
|
312
|
+
return 0;
|
313
|
+
}
|