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