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