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.

Files changed (168) hide show
  1. checksums.yaml +8 -8
  2. checksums.yaml.gz.asc +7 -7
  3. data.tar.gz.asc +7 -7
  4. data/.editorconfig +20 -0
  5. data/CHANGELOG +21 -0
  6. data/bin/passenger-install-apache2-module +3 -1
  7. data/build/agents.rb +7 -5
  8. data/build/basics.rb +3 -3
  9. data/build/common_library.rb +52 -30
  10. data/build/cxx_tests.rb +20 -13
  11. data/build/misc.rb +5 -5
  12. data/doc/Design and Architecture.html +1 -1
  13. data/doc/Design and Architecture.txt +1 -1
  14. data/doc/Packaging.html +4 -4
  15. data/doc/Packaging.txt.md +4 -4
  16. data/doc/Users guide Apache.html +22 -9
  17. data/doc/Users guide Apache.idmap.txt +4 -2
  18. data/doc/Users guide Apache.txt +2 -0
  19. data/doc/Users guide Nginx.html +22 -9
  20. data/doc/Users guide Nginx.idmap.txt +4 -2
  21. data/doc/Users guide Nginx.txt +2 -0
  22. data/doc/Users guide Standalone.html +14 -9
  23. data/doc/Users guide Standalone.idmap.txt +4 -2
  24. data/doc/users_guide_snippets/installation.txt +10 -6
  25. data/ext/apache2/Hooks.cpp +13 -2
  26. data/ext/common/ApplicationPool2/Pool/Inspection.h +8 -3
  27. data/ext/common/BackgroundEventLoop.cpp +249 -67
  28. data/ext/common/BackgroundEventLoop.h +5 -5
  29. data/ext/common/Constants.h +1 -1
  30. data/ext/common/InstanceDirectory.h +8 -6
  31. data/ext/common/ServerKit/Context.h +8 -2
  32. data/ext/common/ServerKit/FileBufferedChannel.h +262 -226
  33. data/ext/common/ServerKit/HeaderTable.h +28 -3
  34. data/ext/common/ServerKit/HttpHeaderParser.h +37 -13
  35. data/ext/common/ServerKit/HttpServer.h +17 -1
  36. data/ext/common/ServerKit/Implementation.cpp +2 -0
  37. data/ext/common/ServerKit/Server.h +25 -28
  38. data/ext/common/Utils/IOUtils.cpp +11 -0
  39. data/ext/common/Utils/ProcessMetricsCollector.h +4 -0
  40. data/ext/common/Utils/StrIntUtils.cpp +11 -7
  41. data/ext/common/Utils/StrIntUtils.h +1 -1
  42. data/ext/common/Utils/StrIntUtilsNoStrictAliasing.cpp +21 -16
  43. data/ext/common/agents/Base.cpp +6 -0
  44. data/ext/common/agents/Base.h +2 -0
  45. data/ext/common/agents/HelperAgent/AdminServer.h +25 -25
  46. data/ext/common/agents/HelperAgent/Main.cpp +37 -12
  47. data/ext/common/agents/HelperAgent/RequestHandler.h +18 -20
  48. data/ext/common/agents/HelperAgent/RequestHandler/AppResponse.h +4 -0
  49. data/ext/common/agents/HelperAgent/RequestHandler/ForwardResponse.cpp +10 -6
  50. data/ext/common/agents/HelperAgent/RequestHandler/Hooks.cpp +2 -0
  51. data/ext/common/agents/HelperAgent/RequestHandler/InitRequest.cpp +1 -1
  52. data/ext/common/agents/HelperAgent/RequestHandler/SendRequest.cpp +1 -1
  53. data/ext/common/agents/HelperAgent/RequestHandler/Utils.cpp +9 -2
  54. data/ext/common/agents/HelperAgent/ResponseCache.h +11 -11
  55. data/ext/common/agents/LoggingAgent/AdminServer.h +8 -8
  56. data/ext/common/agents/LoggingAgent/Main.cpp +6 -5
  57. data/ext/common/agents/Watchdog/AdminServer.h +13 -13
  58. data/ext/common/agents/Watchdog/Main.cpp +8 -3
  59. data/ext/libuv/.gitignore +72 -0
  60. data/ext/libuv/AUTHORS +199 -0
  61. data/ext/libuv/ChangeLog +2023 -0
  62. data/ext/libuv/LICENSE +46 -0
  63. data/ext/libuv/Makefile.am +336 -0
  64. data/ext/libuv/README.md +197 -0
  65. data/ext/libuv/checksparse.sh +233 -0
  66. data/ext/libuv/common.gypi +210 -0
  67. data/ext/libuv/configure.ac +67 -0
  68. data/ext/libuv/gyp_uv.py +96 -0
  69. data/ext/libuv/include/android-ifaddrs.h +54 -0
  70. data/ext/libuv/include/pthread-fixes.h +72 -0
  71. data/ext/libuv/include/tree.h +768 -0
  72. data/ext/libuv/include/uv-aix.h +32 -0
  73. data/ext/libuv/include/uv-bsd.h +34 -0
  74. data/ext/libuv/include/uv-darwin.h +61 -0
  75. data/ext/libuv/include/uv-errno.h +418 -0
  76. data/ext/libuv/include/uv-linux.h +34 -0
  77. data/ext/libuv/include/uv-sunos.h +44 -0
  78. data/ext/libuv/include/uv-threadpool.h +37 -0
  79. data/ext/libuv/include/uv-unix.h +383 -0
  80. data/ext/libuv/include/uv-version.h +39 -0
  81. data/ext/libuv/include/uv.h +1455 -0
  82. data/ext/libuv/libuv.pc.in +11 -0
  83. data/ext/libuv/m4/.gitignore +4 -0
  84. data/ext/libuv/m4/as_case.m4 +21 -0
  85. data/ext/libuv/m4/libuv-check-flags.m4 +319 -0
  86. data/ext/libuv/src/fs-poll.c +255 -0
  87. data/ext/libuv/src/heap-inl.h +245 -0
  88. data/ext/libuv/src/inet.c +313 -0
  89. data/ext/libuv/src/queue.h +92 -0
  90. data/ext/libuv/src/threadpool.c +303 -0
  91. data/ext/libuv/src/unix/aix.c +1240 -0
  92. data/ext/libuv/src/unix/android-ifaddrs.c +703 -0
  93. data/ext/libuv/src/unix/async.c +284 -0
  94. data/ext/libuv/src/unix/atomic-ops.h +60 -0
  95. data/ext/libuv/src/unix/core.c +985 -0
  96. data/ext/libuv/src/unix/darwin-proctitle.c +206 -0
  97. data/ext/libuv/src/unix/darwin.c +331 -0
  98. data/ext/libuv/src/unix/dl.c +83 -0
  99. data/ext/libuv/src/unix/freebsd.c +435 -0
  100. data/ext/libuv/src/unix/fs.c +1189 -0
  101. data/ext/libuv/src/unix/fsevents.c +899 -0
  102. data/ext/libuv/src/unix/getaddrinfo.c +202 -0
  103. data/ext/libuv/src/unix/getnameinfo.c +120 -0
  104. data/ext/libuv/src/unix/internal.h +314 -0
  105. data/ext/libuv/src/unix/kqueue.c +418 -0
  106. data/ext/libuv/src/unix/linux-core.c +876 -0
  107. data/ext/libuv/src/unix/linux-inotify.c +257 -0
  108. data/ext/libuv/src/unix/linux-syscalls.c +471 -0
  109. data/ext/libuv/src/unix/linux-syscalls.h +158 -0
  110. data/ext/libuv/src/unix/loop-watcher.c +63 -0
  111. data/ext/libuv/src/unix/loop.c +135 -0
  112. data/ext/libuv/src/unix/netbsd.c +368 -0
  113. data/ext/libuv/src/unix/openbsd.c +384 -0
  114. data/ext/libuv/src/unix/pipe.c +288 -0
  115. data/ext/libuv/src/unix/poll.c +113 -0
  116. data/ext/libuv/src/unix/process.c +551 -0
  117. data/ext/libuv/src/unix/proctitle.c +102 -0
  118. data/ext/libuv/src/unix/pthread-fixes.c +103 -0
  119. data/ext/libuv/src/unix/signal.c +465 -0
  120. data/ext/libuv/src/unix/spinlock.h +53 -0
  121. data/ext/libuv/src/unix/stream.c +1598 -0
  122. data/ext/libuv/src/unix/sunos.c +763 -0
  123. data/ext/libuv/src/unix/tcp.c +327 -0
  124. data/ext/libuv/src/unix/thread.c +519 -0
  125. data/ext/libuv/src/unix/timer.c +172 -0
  126. data/ext/libuv/src/unix/tty.c +265 -0
  127. data/ext/libuv/src/unix/udp.c +833 -0
  128. data/ext/libuv/src/uv-common.c +544 -0
  129. data/ext/libuv/src/uv-common.h +214 -0
  130. data/ext/libuv/src/version.c +49 -0
  131. data/ext/libuv/uv.gyp +487 -0
  132. data/ext/nginx/ContentHandler.c +21 -10
  133. data/ext/nginx/ngx_http_passenger_module.c +7 -0
  134. data/ext/oxt/implementation.cpp +9 -2
  135. data/ext/oxt/initialize.hpp +5 -1
  136. data/lib/phusion_passenger.rb +3 -3
  137. data/lib/phusion_passenger/admin_tools/instance.rb +10 -6
  138. data/lib/phusion_passenger/admin_tools/instance_registry.rb +6 -2
  139. data/lib/phusion_passenger/packaging.rb +3 -4
  140. data/lib/phusion_passenger/platform_info.rb +13 -1
  141. data/lib/phusion_passenger/platform_info/apache.rb +15 -4
  142. data/lib/phusion_passenger/platform_info/apache_detector.rb +5 -1
  143. data/lib/phusion_passenger/rack/thread_handler_extension.rb +184 -99
  144. data/lib/phusion_passenger/request_handler/thread_handler.rb +13 -6
  145. data/lib/phusion_passenger/standalone/start_command.rb +2 -2
  146. data/resources/templates/apache2/apache_install_broken.txt.erb +2 -1
  147. metadata +99 -22
  148. metadata.gz.asc +7 -7
  149. data/ext/libeio/Changes +0 -76
  150. data/ext/libeio/LICENSE +0 -36
  151. data/ext/libeio/Makefile.am +0 -15
  152. data/ext/libeio/Makefile.in +0 -694
  153. data/ext/libeio/aclocal.m4 +0 -9418
  154. data/ext/libeio/autogen.sh +0 -3
  155. data/ext/libeio/config.guess +0 -1540
  156. data/ext/libeio/config.h.in +0 -136
  157. data/ext/libeio/config.sub +0 -1779
  158. data/ext/libeio/configure +0 -14822
  159. data/ext/libeio/configure.ac +0 -22
  160. data/ext/libeio/demo.c +0 -194
  161. data/ext/libeio/ecb.h +0 -714
  162. data/ext/libeio/eio.c +0 -2818
  163. data/ext/libeio/eio.h +0 -414
  164. data/ext/libeio/install-sh +0 -520
  165. data/ext/libeio/libeio.m4 +0 -195
  166. data/ext/libeio/ltmain.sh +0 -9636
  167. data/ext/libeio/missing +0 -376
  168. data/ext/libeio/xthread.h +0 -166
@@ -0,0 +1,703 @@
1
+ /*
2
+ Copyright (c) 2013, Kenneth MacKay
3
+ Copyright (c) 2014, Emergya (Cloud4all, FP7/2007-2013 grant agreement n° 289016)
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without modification,
7
+ are permitted provided that the following conditions are met:
8
+ * Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
+ */
25
+
26
+ #include "android-ifaddrs.h"
27
+ #include "uv-common.h"
28
+
29
+ #include <string.h>
30
+ #include <stdlib.h>
31
+ #include <errno.h>
32
+ #include <unistd.h>
33
+ #include <sys/socket.h>
34
+ #include <net/if_arp.h>
35
+ #include <netinet/in.h>
36
+ #include <linux/netlink.h>
37
+ #include <linux/rtnetlink.h>
38
+
39
+ typedef struct NetlinkList
40
+ {
41
+ struct NetlinkList *m_next;
42
+ struct nlmsghdr *m_data;
43
+ unsigned int m_size;
44
+ } NetlinkList;
45
+
46
+ static int netlink_socket(void)
47
+ {
48
+ struct sockaddr_nl l_addr;
49
+
50
+ int l_socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
51
+ if(l_socket < 0)
52
+ {
53
+ return -1;
54
+ }
55
+
56
+ memset(&l_addr, 0, sizeof(l_addr));
57
+ l_addr.nl_family = AF_NETLINK;
58
+ if(bind(l_socket, (struct sockaddr *)&l_addr, sizeof(l_addr)) < 0)
59
+ {
60
+ close(l_socket);
61
+ return -1;
62
+ }
63
+
64
+ return l_socket;
65
+ }
66
+
67
+ static int netlink_send(int p_socket, int p_request)
68
+ {
69
+ char l_buffer[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))];
70
+
71
+ struct nlmsghdr *l_hdr;
72
+ struct rtgenmsg *l_msg;
73
+ struct sockaddr_nl l_addr;
74
+
75
+ memset(l_buffer, 0, sizeof(l_buffer));
76
+
77
+ l_hdr = (struct nlmsghdr *)l_buffer;
78
+ l_msg = (struct rtgenmsg *)NLMSG_DATA(l_hdr);
79
+
80
+ l_hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*l_msg));
81
+ l_hdr->nlmsg_type = p_request;
82
+ l_hdr->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
83
+ l_hdr->nlmsg_pid = 0;
84
+ l_hdr->nlmsg_seq = p_socket;
85
+ l_msg->rtgen_family = AF_UNSPEC;
86
+
87
+ memset(&l_addr, 0, sizeof(l_addr));
88
+ l_addr.nl_family = AF_NETLINK;
89
+ return (sendto(p_socket, l_hdr, l_hdr->nlmsg_len, 0, (struct sockaddr *)&l_addr, sizeof(l_addr)));
90
+ }
91
+
92
+ static int netlink_recv(int p_socket, void *p_buffer, size_t p_len)
93
+ {
94
+ struct sockaddr_nl l_addr;
95
+ struct msghdr l_msg;
96
+
97
+ struct iovec l_iov;
98
+ l_iov.iov_base = p_buffer;
99
+ l_iov.iov_len = p_len;
100
+
101
+ for(;;)
102
+ {
103
+ int l_result;
104
+ l_msg.msg_name = (void *)&l_addr;
105
+ l_msg.msg_namelen = sizeof(l_addr);
106
+ l_msg.msg_iov = &l_iov;
107
+ l_msg.msg_iovlen = 1;
108
+ l_msg.msg_control = NULL;
109
+ l_msg.msg_controllen = 0;
110
+ l_msg.msg_flags = 0;
111
+ l_result = recvmsg(p_socket, &l_msg, 0);
112
+
113
+ if(l_result < 0)
114
+ {
115
+ if(errno == EINTR)
116
+ {
117
+ continue;
118
+ }
119
+ return -2;
120
+ }
121
+
122
+ /* Buffer was too small */
123
+ if(l_msg.msg_flags & MSG_TRUNC)
124
+ {
125
+ return -1;
126
+ }
127
+ return l_result;
128
+ }
129
+ }
130
+
131
+ static struct nlmsghdr *getNetlinkResponse(int p_socket, int *p_size, int *p_done)
132
+ {
133
+ size_t l_size = 4096;
134
+ void *l_buffer = NULL;
135
+
136
+ for(;;)
137
+ {
138
+ int l_read;
139
+
140
+ free(l_buffer);
141
+ l_buffer = malloc(l_size);
142
+ if (l_buffer == NULL)
143
+ {
144
+ return NULL;
145
+ }
146
+
147
+ l_read = netlink_recv(p_socket, l_buffer, l_size);
148
+ *p_size = l_read;
149
+ if(l_read == -2)
150
+ {
151
+ free(l_buffer);
152
+ return NULL;
153
+ }
154
+ if(l_read >= 0)
155
+ {
156
+ pid_t l_pid = getpid();
157
+ struct nlmsghdr *l_hdr;
158
+ for(l_hdr = (struct nlmsghdr *)l_buffer; NLMSG_OK(l_hdr, (unsigned int)l_read); l_hdr = (struct nlmsghdr *)NLMSG_NEXT(l_hdr, l_read))
159
+ {
160
+ if((pid_t)l_hdr->nlmsg_pid != l_pid || (int)l_hdr->nlmsg_seq != p_socket)
161
+ {
162
+ continue;
163
+ }
164
+
165
+ if(l_hdr->nlmsg_type == NLMSG_DONE)
166
+ {
167
+ *p_done = 1;
168
+ break;
169
+ }
170
+
171
+ if(l_hdr->nlmsg_type == NLMSG_ERROR)
172
+ {
173
+ free(l_buffer);
174
+ return NULL;
175
+ }
176
+ }
177
+ return l_buffer;
178
+ }
179
+
180
+ l_size *= 2;
181
+ }
182
+ }
183
+
184
+ static NetlinkList *newListItem(struct nlmsghdr *p_data, unsigned int p_size)
185
+ {
186
+ NetlinkList *l_item = malloc(sizeof(NetlinkList));
187
+ if (l_item == NULL)
188
+ {
189
+ return NULL;
190
+ }
191
+
192
+ l_item->m_next = NULL;
193
+ l_item->m_data = p_data;
194
+ l_item->m_size = p_size;
195
+ return l_item;
196
+ }
197
+
198
+ static void freeResultList(NetlinkList *p_list)
199
+ {
200
+ NetlinkList *l_cur;
201
+ while(p_list)
202
+ {
203
+ l_cur = p_list;
204
+ p_list = p_list->m_next;
205
+ free(l_cur->m_data);
206
+ free(l_cur);
207
+ }
208
+ }
209
+
210
+ static NetlinkList *getResultList(int p_socket, int p_request)
211
+ {
212
+ int l_size;
213
+ int l_done;
214
+ NetlinkList *l_list;
215
+ NetlinkList *l_end;
216
+
217
+ if(netlink_send(p_socket, p_request) < 0)
218
+ {
219
+ return NULL;
220
+ }
221
+
222
+ l_list = NULL;
223
+ l_end = NULL;
224
+
225
+ l_done = 0;
226
+ while(!l_done)
227
+ {
228
+ NetlinkList *l_item;
229
+
230
+ struct nlmsghdr *l_hdr = getNetlinkResponse(p_socket, &l_size, &l_done);
231
+ /* Error */
232
+ if(!l_hdr)
233
+ {
234
+ freeResultList(l_list);
235
+ return NULL;
236
+ }
237
+
238
+ l_item = newListItem(l_hdr, l_size);
239
+ if (!l_item)
240
+ {
241
+ freeResultList(l_list);
242
+ return NULL;
243
+ }
244
+ if(!l_list)
245
+ {
246
+ l_list = l_item;
247
+ }
248
+ else
249
+ {
250
+ l_end->m_next = l_item;
251
+ }
252
+ l_end = l_item;
253
+ }
254
+ return l_list;
255
+ }
256
+
257
+ static size_t maxSize(size_t a, size_t b)
258
+ {
259
+ return (a > b ? a : b);
260
+ }
261
+
262
+ static size_t calcAddrLen(sa_family_t p_family, int p_dataSize)
263
+ {
264
+ switch(p_family)
265
+ {
266
+ case AF_INET:
267
+ return sizeof(struct sockaddr_in);
268
+ case AF_INET6:
269
+ return sizeof(struct sockaddr_in6);
270
+ case AF_PACKET:
271
+ return maxSize(sizeof(struct sockaddr_ll), offsetof(struct sockaddr_ll, sll_addr) + p_dataSize);
272
+ default:
273
+ return maxSize(sizeof(struct sockaddr), offsetof(struct sockaddr, sa_data) + p_dataSize);
274
+ }
275
+ }
276
+
277
+ static void makeSockaddr(sa_family_t p_family, struct sockaddr *p_dest, void *p_data, size_t p_size)
278
+ {
279
+ switch(p_family)
280
+ {
281
+ case AF_INET:
282
+ memcpy(&((struct sockaddr_in*)p_dest)->sin_addr, p_data, p_size);
283
+ break;
284
+ case AF_INET6:
285
+ memcpy(&((struct sockaddr_in6*)p_dest)->sin6_addr, p_data, p_size);
286
+ break;
287
+ case AF_PACKET:
288
+ memcpy(((struct sockaddr_ll*)p_dest)->sll_addr, p_data, p_size);
289
+ ((struct sockaddr_ll*)p_dest)->sll_halen = p_size;
290
+ break;
291
+ default:
292
+ memcpy(p_dest->sa_data, p_data, p_size);
293
+ break;
294
+ }
295
+ p_dest->sa_family = p_family;
296
+ }
297
+
298
+ static void addToEnd(struct ifaddrs **p_resultList, struct ifaddrs *p_entry)
299
+ {
300
+ if(!*p_resultList)
301
+ {
302
+ *p_resultList = p_entry;
303
+ }
304
+ else
305
+ {
306
+ struct ifaddrs *l_cur = *p_resultList;
307
+ while(l_cur->ifa_next)
308
+ {
309
+ l_cur = l_cur->ifa_next;
310
+ }
311
+ l_cur->ifa_next = p_entry;
312
+ }
313
+ }
314
+
315
+ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
316
+ {
317
+ struct ifaddrs *l_entry;
318
+
319
+ char *l_index;
320
+ char *l_name;
321
+ char *l_addr;
322
+ char *l_data;
323
+
324
+ struct ifinfomsg *l_info = (struct ifinfomsg *)NLMSG_DATA(p_hdr);
325
+
326
+ size_t l_nameSize = 0;
327
+ size_t l_addrSize = 0;
328
+ size_t l_dataSize = 0;
329
+
330
+ size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
331
+ struct rtattr *l_rta;
332
+ for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
333
+ {
334
+ size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
335
+ switch(l_rta->rta_type)
336
+ {
337
+ case IFLA_ADDRESS:
338
+ case IFLA_BROADCAST:
339
+ l_addrSize += NLMSG_ALIGN(calcAddrLen(AF_PACKET, l_rtaDataSize));
340
+ break;
341
+ case IFLA_IFNAME:
342
+ l_nameSize += NLMSG_ALIGN(l_rtaSize + 1);
343
+ break;
344
+ case IFLA_STATS:
345
+ l_dataSize += NLMSG_ALIGN(l_rtaSize);
346
+ break;
347
+ default:
348
+ break;
349
+ }
350
+ }
351
+
352
+ l_entry = malloc(sizeof(struct ifaddrs) + sizeof(int) + l_nameSize + l_addrSize + l_dataSize);
353
+ if (l_entry == NULL)
354
+ {
355
+ return -1;
356
+ }
357
+ memset(l_entry, 0, sizeof(struct ifaddrs));
358
+ l_entry->ifa_name = "";
359
+
360
+ l_index = ((char *)l_entry) + sizeof(struct ifaddrs);
361
+ l_name = l_index + sizeof(int);
362
+ l_addr = l_name + l_nameSize;
363
+ l_data = l_addr + l_addrSize;
364
+
365
+ /* Save the interface index so we can look it up when handling the
366
+ * addresses.
367
+ */
368
+ memcpy(l_index, &l_info->ifi_index, sizeof(int));
369
+
370
+ l_entry->ifa_flags = l_info->ifi_flags;
371
+
372
+ l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
373
+ for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
374
+ {
375
+ void *l_rtaData = RTA_DATA(l_rta);
376
+ size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
377
+ switch(l_rta->rta_type)
378
+ {
379
+ case IFLA_ADDRESS:
380
+ case IFLA_BROADCAST:
381
+ {
382
+ size_t l_addrLen = calcAddrLen(AF_PACKET, l_rtaDataSize);
383
+ makeSockaddr(AF_PACKET, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
384
+ ((struct sockaddr_ll *)l_addr)->sll_ifindex = l_info->ifi_index;
385
+ ((struct sockaddr_ll *)l_addr)->sll_hatype = l_info->ifi_type;
386
+ if(l_rta->rta_type == IFLA_ADDRESS)
387
+ {
388
+ l_entry->ifa_addr = (struct sockaddr *)l_addr;
389
+ }
390
+ else
391
+ {
392
+ l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
393
+ }
394
+ l_addr += NLMSG_ALIGN(l_addrLen);
395
+ break;
396
+ }
397
+ case IFLA_IFNAME:
398
+ strncpy(l_name, l_rtaData, l_rtaDataSize);
399
+ l_name[l_rtaDataSize] = '\0';
400
+ l_entry->ifa_name = l_name;
401
+ break;
402
+ case IFLA_STATS:
403
+ memcpy(l_data, l_rtaData, l_rtaDataSize);
404
+ l_entry->ifa_data = l_data;
405
+ break;
406
+ default:
407
+ break;
408
+ }
409
+ }
410
+
411
+ addToEnd(p_resultList, l_entry);
412
+ return 0;
413
+ }
414
+
415
+ static struct ifaddrs *findInterface(int p_index, struct ifaddrs **p_links, int p_numLinks)
416
+ {
417
+ int l_num = 0;
418
+ struct ifaddrs *l_cur = *p_links;
419
+ while(l_cur && l_num < p_numLinks)
420
+ {
421
+ char *l_indexPtr = ((char *)l_cur) + sizeof(struct ifaddrs);
422
+ int l_index;
423
+ memcpy(&l_index, l_indexPtr, sizeof(int));
424
+ if(l_index == p_index)
425
+ {
426
+ return l_cur;
427
+ }
428
+
429
+ l_cur = l_cur->ifa_next;
430
+ ++l_num;
431
+ }
432
+ return NULL;
433
+ }
434
+
435
+ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList, int p_numLinks)
436
+ {
437
+ struct ifaddrmsg *l_info = (struct ifaddrmsg *)NLMSG_DATA(p_hdr);
438
+ struct ifaddrs *l_interface = findInterface(l_info->ifa_index, p_resultList, p_numLinks);
439
+
440
+ size_t l_nameSize = 0;
441
+ size_t l_addrSize = 0;
442
+
443
+ int l_addedNetmask = 0;
444
+
445
+ size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
446
+ struct rtattr *l_rta;
447
+ struct ifaddrs *l_entry;
448
+
449
+ char *l_name;
450
+ char *l_addr;
451
+
452
+ for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
453
+ {
454
+ size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
455
+ if(l_info->ifa_family == AF_PACKET)
456
+ {
457
+ continue;
458
+ }
459
+
460
+ switch(l_rta->rta_type)
461
+ {
462
+ case IFA_ADDRESS:
463
+ case IFA_LOCAL:
464
+ if((l_info->ifa_family == AF_INET || l_info->ifa_family == AF_INET6) && !l_addedNetmask)
465
+ {
466
+ /* Make room for netmask */
467
+ l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
468
+ l_addedNetmask = 1;
469
+ }
470
+ case IFA_BROADCAST:
471
+ l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
472
+ break;
473
+ case IFA_LABEL:
474
+ l_nameSize += NLMSG_ALIGN(l_rtaSize + 1);
475
+ break;
476
+ default:
477
+ break;
478
+ }
479
+ }
480
+
481
+ l_entry = malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
482
+ if (l_entry == NULL)
483
+ {
484
+ return -1;
485
+ }
486
+ memset(l_entry, 0, sizeof(struct ifaddrs));
487
+ l_entry->ifa_name = (l_interface ? l_interface->ifa_name : "");
488
+
489
+ l_name = ((char *)l_entry) + sizeof(struct ifaddrs);
490
+ l_addr = l_name + l_nameSize;
491
+
492
+ l_entry->ifa_flags = l_info->ifa_flags;
493
+ if(l_interface)
494
+ {
495
+ l_entry->ifa_flags |= l_interface->ifa_flags;
496
+ }
497
+
498
+ l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
499
+ for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
500
+ {
501
+ void *l_rtaData = RTA_DATA(l_rta);
502
+ size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
503
+ switch(l_rta->rta_type)
504
+ {
505
+ case IFA_ADDRESS:
506
+ case IFA_BROADCAST:
507
+ case IFA_LOCAL:
508
+ {
509
+ size_t l_addrLen = calcAddrLen(l_info->ifa_family, l_rtaDataSize);
510
+ makeSockaddr(l_info->ifa_family, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
511
+ if(l_info->ifa_family == AF_INET6)
512
+ {
513
+ if(IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)l_rtaData) || IN6_IS_ADDR_MC_LINKLOCAL((struct in6_addr *)l_rtaData))
514
+ {
515
+ ((struct sockaddr_in6 *)l_addr)->sin6_scope_id = l_info->ifa_index;
516
+ }
517
+ }
518
+
519
+ /* Apparently in a point-to-point network IFA_ADDRESS contains
520
+ * the dest address and IFA_LOCAL contains the local address
521
+ */
522
+ if(l_rta->rta_type == IFA_ADDRESS)
523
+ {
524
+ if(l_entry->ifa_addr)
525
+ {
526
+ l_entry->ifa_dstaddr = (struct sockaddr *)l_addr;
527
+ }
528
+ else
529
+ {
530
+ l_entry->ifa_addr = (struct sockaddr *)l_addr;
531
+ }
532
+ }
533
+ else if(l_rta->rta_type == IFA_LOCAL)
534
+ {
535
+ if(l_entry->ifa_addr)
536
+ {
537
+ l_entry->ifa_dstaddr = l_entry->ifa_addr;
538
+ }
539
+ l_entry->ifa_addr = (struct sockaddr *)l_addr;
540
+ }
541
+ else
542
+ {
543
+ l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
544
+ }
545
+ l_addr += NLMSG_ALIGN(l_addrLen);
546
+ break;
547
+ }
548
+ case IFA_LABEL:
549
+ strncpy(l_name, l_rtaData, l_rtaDataSize);
550
+ l_name[l_rtaDataSize] = '\0';
551
+ l_entry->ifa_name = l_name;
552
+ break;
553
+ default:
554
+ break;
555
+ }
556
+ }
557
+
558
+ if(l_entry->ifa_addr && (l_entry->ifa_addr->sa_family == AF_INET || l_entry->ifa_addr->sa_family == AF_INET6))
559
+ {
560
+ unsigned l_maxPrefix = (l_entry->ifa_addr->sa_family == AF_INET ? 32 : 128);
561
+ unsigned l_prefix = (l_info->ifa_prefixlen > l_maxPrefix ? l_maxPrefix : l_info->ifa_prefixlen);
562
+ char l_mask[16] = {0};
563
+ unsigned i;
564
+ for(i=0; i<(l_prefix/8); ++i)
565
+ {
566
+ l_mask[i] = 0xff;
567
+ }
568
+ if(l_prefix % 8)
569
+ {
570
+ l_mask[i] = 0xff << (8 - (l_prefix % 8));
571
+ }
572
+
573
+ makeSockaddr(l_entry->ifa_addr->sa_family, (struct sockaddr *)l_addr, l_mask, l_maxPrefix / 8);
574
+ l_entry->ifa_netmask = (struct sockaddr *)l_addr;
575
+ }
576
+
577
+ addToEnd(p_resultList, l_entry);
578
+ return 0;
579
+ }
580
+
581
+ static int interpretLinks(int p_socket, NetlinkList *p_netlinkList, struct ifaddrs **p_resultList)
582
+ {
583
+
584
+ int l_numLinks = 0;
585
+ pid_t l_pid = getpid();
586
+ for(; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
587
+ {
588
+ unsigned int l_nlsize = p_netlinkList->m_size;
589
+ struct nlmsghdr *l_hdr;
590
+ for(l_hdr = p_netlinkList->m_data; NLMSG_OK(l_hdr, l_nlsize); l_hdr = NLMSG_NEXT(l_hdr, l_nlsize))
591
+ {
592
+ if((pid_t)l_hdr->nlmsg_pid != l_pid || (int)l_hdr->nlmsg_seq != p_socket)
593
+ {
594
+ continue;
595
+ }
596
+
597
+ if(l_hdr->nlmsg_type == NLMSG_DONE)
598
+ {
599
+ break;
600
+ }
601
+
602
+ if(l_hdr->nlmsg_type == RTM_NEWLINK)
603
+ {
604
+ if(interpretLink(l_hdr, p_resultList) == -1)
605
+ {
606
+ return -1;
607
+ }
608
+ ++l_numLinks;
609
+ }
610
+ }
611
+ }
612
+ return l_numLinks;
613
+ }
614
+
615
+ static int interpretAddrs(int p_socket, NetlinkList *p_netlinkList, struct ifaddrs **p_resultList, int p_numLinks)
616
+ {
617
+ pid_t l_pid = getpid();
618
+ for(; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
619
+ {
620
+ unsigned int l_nlsize = p_netlinkList->m_size;
621
+ struct nlmsghdr *l_hdr;
622
+ for(l_hdr = p_netlinkList->m_data; NLMSG_OK(l_hdr, l_nlsize); l_hdr = NLMSG_NEXT(l_hdr, l_nlsize))
623
+ {
624
+ if((pid_t)l_hdr->nlmsg_pid != l_pid || (int)l_hdr->nlmsg_seq != p_socket)
625
+ {
626
+ continue;
627
+ }
628
+
629
+ if(l_hdr->nlmsg_type == NLMSG_DONE)
630
+ {
631
+ break;
632
+ }
633
+
634
+ if(l_hdr->nlmsg_type == RTM_NEWADDR)
635
+ {
636
+ if (interpretAddr(l_hdr, p_resultList, p_numLinks) == -1)
637
+ {
638
+ return -1;
639
+ }
640
+ }
641
+ }
642
+ }
643
+ return 0;
644
+ }
645
+
646
+ int getifaddrs(struct ifaddrs **ifap)
647
+ {
648
+ int l_socket;
649
+ int l_result;
650
+ int l_numLinks;
651
+ NetlinkList *l_linkResults;
652
+ NetlinkList *l_addrResults;
653
+
654
+ if(!ifap)
655
+ {
656
+ return -1;
657
+ }
658
+ *ifap = NULL;
659
+
660
+ l_socket = netlink_socket();
661
+ if(l_socket < 0)
662
+ {
663
+ return -1;
664
+ }
665
+
666
+ l_linkResults = getResultList(l_socket, RTM_GETLINK);
667
+ if(!l_linkResults)
668
+ {
669
+ close(l_socket);
670
+ return -1;
671
+ }
672
+
673
+ l_addrResults = getResultList(l_socket, RTM_GETADDR);
674
+ if(!l_addrResults)
675
+ {
676
+ close(l_socket);
677
+ freeResultList(l_linkResults);
678
+ return -1;
679
+ }
680
+
681
+ l_result = 0;
682
+ l_numLinks = interpretLinks(l_socket, l_linkResults, ifap);
683
+ if(l_numLinks == -1 || interpretAddrs(l_socket, l_addrResults, ifap, l_numLinks) == -1)
684
+ {
685
+ l_result = -1;
686
+ }
687
+
688
+ freeResultList(l_linkResults);
689
+ freeResultList(l_addrResults);
690
+ close(l_socket);
691
+ return l_result;
692
+ }
693
+
694
+ void freeifaddrs(struct ifaddrs *ifa)
695
+ {
696
+ struct ifaddrs *l_cur;
697
+ while(ifa)
698
+ {
699
+ l_cur = ifa;
700
+ ifa = ifa->ifa_next;
701
+ free(l_cur);
702
+ }
703
+ }