Line data Source code
1 : /*-
2 : * Copyright (c) 2011-2012 Irene Ruengeler
3 : * Copyright (c) 2011-2012 Michael Tuexen
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions
8 : * are met:
9 : * 1. Redistributions of source code must retain the above copyright
10 : * notice, this list of conditions and the following disclaimer.
11 : * 2. Redistributions in binary form must reproduce the above copyright
12 : * notice, this list of conditions and the following disclaimer in the
13 : * documentation and/or other materials provided with the distribution.
14 : *
15 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 : * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 : * SUCH DAMAGE.
26 : *
27 : */
28 :
29 :
30 : #ifdef _WIN32
31 : #include <netinet/sctp_pcb.h>
32 : #include <sys/timeb.h>
33 : #include <iphlpapi.h>
34 : #pragma comment(lib, "IPHLPAPI.lib")
35 : #endif
36 : #include <netinet/sctp_os_userspace.h>
37 :
38 : #if !defined(_WIN32) && !defined(__Userspace_os_NaCl)
39 : int
40 0 : sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af)
41 : {
42 : struct ifreq ifr;
43 : int fd;
44 :
45 0 : if_indextoname(if_index, ifr.ifr_name);
46 : /* TODO can I use the raw socket here and not have to open a new one with each query? */
47 0 : if ((fd = socket(af, SOCK_DGRAM, 0)) < 0)
48 0 : return (0);
49 0 : if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
50 0 : close(fd);
51 0 : return (0);
52 : }
53 0 : close(fd);
54 0 : return ifr.ifr_mtu;
55 : }
56 : #endif
57 :
58 : #if defined(__Userspace_os_NaCl)
59 : int
60 : sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af)
61 : {
62 : return 1280;
63 : }
64 : #endif
65 :
66 : #ifdef _WIN32
67 : int
68 : sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af)
69 : {
70 : PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt;
71 : DWORD AdapterAddrsSize, Err;
72 :
73 : AdapterAddrsSize = 0;
74 : if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
75 : if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
76 : SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() sizing failed with error code %d, AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
77 : return (-1);
78 : }
79 : }
80 : if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
81 : SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n");
82 : return (-1);
83 : }
84 : if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
85 : SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() failed with error code %d\n", Err);
86 : return (-1);
87 : }
88 : for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
89 : if (pAdapt->IfIndex == if_index)
90 : return (pAdapt->Mtu);
91 : }
92 : return (0);
93 : }
94 :
95 : void
96 : getwintimeofday(struct timeval *tv)
97 : {
98 : struct timeb tb;
99 :
100 : ftime(&tb);
101 : tv->tv_sec = (long)tb.time;
102 : tv->tv_usec = (long)(tb.millitm) * 1000L;
103 : }
104 :
105 : int
106 : Win_getifaddrs(struct ifaddrs** interfaces)
107 : {
108 : #if defined(INET) || defined(INET6)
109 : DWORD Err, AdapterAddrsSize;
110 : int count;
111 : PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt;
112 : struct ifaddrs *ifa;
113 : #endif
114 : #if defined(INET)
115 : struct sockaddr_in *addr;
116 : #endif
117 : #if defined(INET6)
118 : struct sockaddr_in6 *addr6;
119 : #endif
120 : #if defined(INET) || defined(INET6)
121 : count = 0;
122 : #endif
123 : #if defined(INET)
124 : AdapterAddrsSize = 0;
125 : if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
126 : if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
127 : SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV4Addresses() sizing failed with error code %d and AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
128 : return (-1);
129 : }
130 : }
131 : /* Allocate memory from sizing information */
132 : if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
133 : SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n");
134 : return (-1);
135 : }
136 : /* Get actual adapter information */
137 : if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
138 : SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV4Addresses() failed with error code %d\n", Err);
139 : return (-1);
140 : }
141 : /* Enumerate through each returned adapter and save its information */
142 : for (pAdapt = pAdapterAddrs, count; pAdapt; pAdapt = pAdapt->Next, count++) {
143 : addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
144 : ifa = (struct ifaddrs *)malloc(sizeof(struct ifaddrs));
145 : if ((addr == NULL) || (ifa == NULL)) {
146 : SCTPDBG(SCTP_DEBUG_USR, "Can't allocate memory\n");
147 : return (-1);
148 : }
149 : ifa->ifa_name = _strdup(pAdapt->AdapterName);
150 : ifa->ifa_flags = pAdapt->Flags;
151 : ifa->ifa_addr = (struct sockaddr *)addr;
152 : memcpy(addr, &pAdapt->FirstUnicastAddress->Address.lpSockaddr, sizeof(struct sockaddr_in));
153 : interfaces[count] = ifa;
154 : }
155 : #endif
156 : #if defined(INET6)
157 : if (SCTP_BASE_VAR(userspace_rawsctp6) != -1) {
158 : AdapterAddrsSize = 0;
159 : if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
160 : if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
161 : SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV6Addresses() sizing failed with error code %d AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
162 : return (-1);
163 : }
164 : }
165 : /* Allocate memory from sizing information */
166 : if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
167 : SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n");
168 : return (-1);
169 : }
170 : /* Get actual adapter information */
171 : if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
172 : SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV6Addresses() failed with error code %d\n", Err);
173 : return (-1);
174 : }
175 : /* Enumerate through each returned adapter and save its information */
176 : for (pAdapt = pAdapterAddrs, count; pAdapt; pAdapt = pAdapt->Next, count++) {
177 : addr6 = (struct sockaddr_in6 *)malloc(sizeof(struct sockaddr_in6));
178 : ifa = (struct ifaddrs *)malloc(sizeof(struct ifaddrs));
179 : if ((addr6 == NULL) || (ifa == NULL)) {
180 : SCTPDBG(SCTP_DEBUG_USR, "Can't allocate memory\n");
181 : return (-1);
182 : }
183 : ifa->ifa_name = _strdup(pAdapt->AdapterName);
184 : ifa->ifa_flags = pAdapt->Flags;
185 : ifa->ifa_addr = (struct sockaddr *)addr6;
186 : memcpy(addr6, &pAdapt->FirstUnicastAddress->Address.lpSockaddr, sizeof(struct sockaddr_in6));
187 : interfaces[count] = ifa;
188 : }
189 : }
190 : #endif
191 : return (0);
192 : }
193 :
194 : int
195 : win_if_nametoindex(const char *ifname)
196 : {
197 : IP_ADAPTER_ADDRESSES *addresses, *addr;
198 : ULONG status, size;
199 : int index = 0;
200 :
201 : if (!ifname) {
202 : return 0;
203 : }
204 :
205 : size = 0;
206 : status = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &size);
207 : if (status != ERROR_BUFFER_OVERFLOW) {
208 : return 0;
209 : }
210 : addresses = malloc(size);
211 : status = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, addresses, &size);
212 : if (status == ERROR_SUCCESS) {
213 : for (addr = addresses; addr; addr = addr->Next) {
214 : if (addr->AdapterName && !strcmp(ifname, addr->AdapterName)) {
215 : index = addr->IfIndex;
216 : break;
217 : }
218 : }
219 : }
220 :
221 : free(addresses);
222 : return index;
223 : }
224 :
225 : #if WINVER < 0x0600
226 : /* These functions are written based on the code at
227 : * http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
228 : * Therefore, for the rest of the file the following applies:
229 : *
230 : *
231 : * Copyright and Licensing Information for ACE(TM), TAO(TM), CIAO(TM),
232 : * DAnCE(TM), and CoSMIC(TM)
233 : *
234 : * [1]ACE(TM), [2]TAO(TM), [3]CIAO(TM), DAnCE(TM), and [4]CoSMIC(TM)
235 : * (henceforth referred to as "DOC software") are copyrighted by
236 : * [5]Douglas C. Schmidt and his [6]research group at [7]Washington
237 : * University, [8]University of California, Irvine, and [9]Vanderbilt
238 : * University, Copyright (c) 1993-2012, all rights reserved. Since DOC
239 : * software is open-source, freely available software, you are free to
240 : * use, modify, copy, and distribute--perpetually and irrevocably--the
241 : * DOC software source code and object code produced from the source, as
242 : * well as copy and distribute modified versions of this software. You
243 : * must, however, include this copyright statement along with any code
244 : * built using DOC software that you release. No copyright statement
245 : * needs to be provided if you just ship binary executables of your
246 : * software products.
247 : *
248 : * You can use DOC software in commercial and/or binary software releases
249 : * and are under no obligation to redistribute any of your source code
250 : * that is built using DOC software. Note, however, that you may not
251 : * misappropriate the DOC software code, such as copyrighting it yourself
252 : * or claiming authorship of the DOC software code, in a way that will
253 : * prevent DOC software from being distributed freely using an
254 : * open-source development model. You needn't inform anyone that you're
255 : * using DOC software in your software, though we encourage you to let
256 : * [10]us know so we can promote your project in the [11]DOC software
257 : * success stories.
258 : *
259 : * The [12]ACE, [13]TAO, [14]CIAO, [15]DAnCE, and [16]CoSMIC web sites
260 : * are maintained by the [17]DOC Group at the [18]Institute for Software
261 : * Integrated Systems (ISIS) and the [19]Center for Distributed Object
262 : * Computing of Washington University, St. Louis for the development of
263 : * open-source software as part of the open-source software community.
264 : * Submissions are provided by the submitter ``as is'' with no warranties
265 : * whatsoever, including any warranty of merchantability, noninfringement
266 : * of third party intellectual property, or fitness for any particular
267 : * purpose. In no event shall the submitter be liable for any direct,
268 : * indirect, special, exemplary, punitive, or consequential damages,
269 : * including without limitation, lost profits, even if advised of the
270 : * possibility of such damages. Likewise, DOC software is provided as is
271 : * with no warranties of any kind, including the warranties of design,
272 : * merchantability, and fitness for a particular purpose,
273 : * noninfringement, or arising from a course of dealing, usage or trade
274 : * practice. Washington University, UC Irvine, Vanderbilt University,
275 : * their employees, and students shall have no liability with respect to
276 : * the infringement of copyrights, trade secrets or any patents by DOC
277 : * software or any part thereof. Moreover, in no event will Washington
278 : * University, UC Irvine, or Vanderbilt University, their employees, or
279 : * students be liable for any lost revenue or profits or other special,
280 : * indirect and consequential damages.
281 : *
282 : * DOC software is provided with no support and without any obligation on
283 : * the part of Washington University, UC Irvine, Vanderbilt University,
284 : * their employees, or students to assist in its use, correction,
285 : * modification, or enhancement. A [20]number of companies around the
286 : * world provide commercial support for DOC software, however. DOC
287 : * software is Y2K-compliant, as long as the underlying OS platform is
288 : * Y2K-compliant. Likewise, DOC software is compliant with the new US
289 : * daylight savings rule passed by Congress as "The Energy Policy Act of
290 : * 2005," which established new daylight savings times (DST) rules for
291 : * the United States that expand DST as of March 2007. Since DOC software
292 : * obtains time/date and calendaring information from operating systems
293 : * users will not be affected by the new DST rules as long as they
294 : * upgrade their operating systems accordingly.
295 : *
296 : * The names ACE(TM), TAO(TM), CIAO(TM), DAnCE(TM), CoSMIC(TM),
297 : * Washington University, UC Irvine, and Vanderbilt University, may not
298 : * be used to endorse or promote products or services derived from this
299 : * source without express written permission from Washington University,
300 : * UC Irvine, or Vanderbilt University. This license grants no permission
301 : * to call products or services derived from this source ACE(TM),
302 : * TAO(TM), CIAO(TM), DAnCE(TM), or CoSMIC(TM), nor does it grant
303 : * permission for the name Washington University, UC Irvine, or
304 : * Vanderbilt University to appear in their names.
305 : *
306 : * If you have any suggestions, additions, comments, or questions, please
307 : * let [21]me know.
308 : *
309 : * [22]Douglas C. Schmidt
310 : *
311 : * References
312 : *
313 : * 1. http://www.cs.wustl.edu/~schmidt/ACE.html
314 : * 2. http://www.cs.wustl.edu/~schmidt/TAO.html
315 : * 3. http://www.dre.vanderbilt.edu/CIAO/
316 : * 4. http://www.dre.vanderbilt.edu/cosmic/
317 : * 5. http://www.dre.vanderbilt.edu/~schmidt/
318 : * 6. http://www.cs.wustl.edu/~schmidt/ACE-members.html
319 : * 7. http://www.wustl.edu/
320 : * 8. http://www.uci.edu/
321 : * 9. http://www.vanderbilt.edu/
322 : * 10. mailto:doc_group@cs.wustl.edu
323 : * 11. http://www.cs.wustl.edu/~schmidt/ACE-users.html
324 : * 12. http://www.cs.wustl.edu/~schmidt/ACE.html
325 : * 13. http://www.cs.wustl.edu/~schmidt/TAO.html
326 : * 14. http://www.dre.vanderbilt.edu/CIAO/
327 : * 15. http://www.dre.vanderbilt.edu/~schmidt/DOC_ROOT/DAnCE/
328 : * 16. http://www.dre.vanderbilt.edu/cosmic/
329 : * 17. http://www.dre.vanderbilt.edu/
330 : * 18. http://www.isis.vanderbilt.edu/
331 : * 19. http://www.cs.wustl.edu/~schmidt/doc-center.html
332 : * 20. http://www.cs.wustl.edu/~schmidt/commercial-support.html
333 : * 21. mailto:d.schmidt@vanderbilt.edu
334 : * 22. http://www.dre.vanderbilt.edu/~schmidt/
335 : * 23. http://www.cs.wustl.edu/ACE.html
336 : */
337 :
338 : void
339 : InitializeXPConditionVariable(userland_cond_t *cv)
340 : {
341 : cv->waiters_count = 0;
342 : InitializeCriticalSection(&(cv->waiters_count_lock));
343 : cv->events_[C_SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
344 : cv->events_[C_BROADCAST] = CreateEvent (NULL, TRUE, FALSE, NULL);
345 : }
346 :
347 : void
348 : DeleteXPConditionVariable(userland_cond_t *cv)
349 : {
350 : CloseHandle(cv->events_[C_BROADCAST]);
351 : CloseHandle(cv->events_[C_SIGNAL]);
352 : DeleteCriticalSection(&(cv->waiters_count_lock));
353 : }
354 :
355 : int
356 : SleepXPConditionVariable(userland_cond_t *cv, userland_mutex_t *mtx)
357 : {
358 : int result, last_waiter;
359 :
360 : EnterCriticalSection(&cv->waiters_count_lock);
361 : cv->waiters_count++;
362 : LeaveCriticalSection(&cv->waiters_count_lock);
363 : LeaveCriticalSection (mtx);
364 : result = WaitForMultipleObjects(2, cv->events_, FALSE, INFINITE);
365 : if (result==-1) {
366 : result = GetLastError();
367 : }
368 : EnterCriticalSection(&cv->waiters_count_lock);
369 : cv->waiters_count--;
370 : last_waiter = result == (C_SIGNAL + C_BROADCAST && (cv->waiters_count == 0));
371 : LeaveCriticalSection(&cv->waiters_count_lock);
372 : if (last_waiter)
373 : ResetEvent(cv->events_[C_BROADCAST]);
374 : EnterCriticalSection (mtx);
375 : return result;
376 : }
377 :
378 : void
379 : WakeAllXPConditionVariable(userland_cond_t *cv)
380 : {
381 : int have_waiters;
382 : EnterCriticalSection(&cv->waiters_count_lock);
383 : have_waiters = cv->waiters_count > 0;
384 : LeaveCriticalSection(&cv->waiters_count_lock);
385 : if (have_waiters)
386 : SetEvent (cv->events_[C_BROADCAST]);
387 : }
388 : #endif
389 : #endif
|