michael@0: /*- michael@0: * Copyright (c) 2011-2012 Irene Ruengeler michael@0: * Copyright (c) 2011-2012 Michael Tuexen michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: michael@0: #ifdef _WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #pragma comment(lib, "IPHLPAPI.lib") michael@0: #endif michael@0: #include michael@0: michael@0: #ifndef _WIN32 michael@0: int michael@0: sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af) michael@0: { michael@0: struct ifreq ifr; michael@0: int fd; michael@0: michael@0: if_indextoname(if_index, ifr.ifr_name); michael@0: /* TODO can I use the raw socket here and not have to open a new one with each query? */ michael@0: if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) michael@0: return (0); michael@0: if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) { michael@0: close(fd); michael@0: return (0); michael@0: } michael@0: close(fd); michael@0: return ifr.ifr_mtu; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef _WIN32 michael@0: int michael@0: sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af) michael@0: { michael@0: PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt; michael@0: DWORD AdapterAddrsSize, Err; michael@0: michael@0: AdapterAddrsSize = 0; michael@0: if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &AdapterAddrsSize)) != 0) { michael@0: if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() sizing failed with error code %d, AdapterAddrsSize = %d\n", Err, AdapterAddrsSize); michael@0: return (-1); michael@0: } michael@0: } michael@0: if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n"); michael@0: return (-1); michael@0: } michael@0: if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() failed with error code %d\n", Err); michael@0: return (-1); michael@0: } michael@0: for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) { michael@0: if (pAdapt->IfIndex == if_index) michael@0: return (pAdapt->Mtu); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: getwintimeofday(struct timeval *tv) michael@0: { michael@0: struct timeb tb; michael@0: michael@0: ftime(&tb); michael@0: tv->tv_sec = tb.time; michael@0: tv->tv_usec = tb.millitm * 1000; michael@0: } michael@0: michael@0: int michael@0: Win_getifaddrs(struct ifaddrs** interfaces) michael@0: { michael@0: #if defined(INET) || defined(INET6) michael@0: DWORD Err, AdapterAddrsSize; michael@0: int count; michael@0: PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt; michael@0: struct ifaddrs *ifa; michael@0: #endif michael@0: #if defined(INET) michael@0: struct sockaddr_in *addr; michael@0: #endif michael@0: #if defined(INET6) michael@0: struct sockaddr_in6 *addr6; michael@0: #endif michael@0: #if defined(INET) || defined(INET6) michael@0: count = 0; michael@0: #endif michael@0: #if defined(INET) michael@0: AdapterAddrsSize = 0; michael@0: if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, NULL, &AdapterAddrsSize)) != 0) { michael@0: if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV4Addresses() sizing failed with error code %d and AdapterAddrsSize = %d\n", Err, AdapterAddrsSize); michael@0: return (-1); michael@0: } michael@0: } michael@0: /* Allocate memory from sizing information */ michael@0: if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n"); michael@0: return (-1); michael@0: } michael@0: /* Get actual adapter information */ michael@0: if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV4Addresses() failed with error code %d\n", Err); michael@0: return (-1); michael@0: } michael@0: /* Enumerate through each returned adapter and save its information */ michael@0: for (pAdapt = pAdapterAddrs, count; pAdapt; pAdapt = pAdapt->Next, count++) { michael@0: addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in)); michael@0: ifa = (struct ifaddrs *)malloc(sizeof(struct ifaddrs)); michael@0: if ((addr == NULL) || (ifa == NULL)) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "Can't allocate memory\n"); michael@0: return (-1); michael@0: } michael@0: ifa->ifa_name = strdup(pAdapt->AdapterName); michael@0: ifa->ifa_flags = pAdapt->Flags; michael@0: ifa->ifa_addr = (struct sockaddr *)addr; michael@0: memcpy(&addr, &pAdapt->FirstUnicastAddress->Address.lpSockaddr, sizeof(struct sockaddr_in)); michael@0: interfaces[count] = ifa; michael@0: } michael@0: #endif michael@0: #if defined(INET6) michael@0: if (SCTP_BASE_VAR(userspace_rawsctp6) != -1) { michael@0: AdapterAddrsSize = 0; michael@0: if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, NULL, &AdapterAddrsSize)) != 0) { michael@0: if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV6Addresses() sizing failed with error code %d AdapterAddrsSize = %d\n", Err, AdapterAddrsSize); michael@0: return (-1); michael@0: } michael@0: } michael@0: /* Allocate memory from sizing information */ michael@0: if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n"); michael@0: return (-1); michael@0: } michael@0: /* Get actual adapter information */ michael@0: if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersV6Addresses() failed with error code %d\n", Err); michael@0: return (-1); michael@0: } michael@0: /* Enumerate through each returned adapter and save its information */ michael@0: for (pAdapt = pAdapterAddrs, count; pAdapt; pAdapt = pAdapt->Next, count++) { michael@0: addr6 = (struct sockaddr_in6 *)malloc(sizeof(struct sockaddr_in6)); michael@0: ifa = (struct ifaddrs *)malloc(sizeof(struct ifaddrs)); michael@0: if ((addr6 == NULL) || (ifa == NULL)) { michael@0: SCTPDBG(SCTP_DEBUG_USR, "Can't allocate memory\n"); michael@0: return (-1); michael@0: } michael@0: ifa->ifa_name = strdup(pAdapt->AdapterName); michael@0: ifa->ifa_flags = pAdapt->Flags; michael@0: ifa->ifa_addr = (struct sockaddr *)addr6; michael@0: memcpy(&addr6, &pAdapt->FirstUnicastAddress->Address.lpSockaddr, sizeof(struct sockaddr_in6)); michael@0: interfaces[count] = ifa; michael@0: } michael@0: } michael@0: #endif michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: win_if_nametoindex(const char *ifname) michael@0: { michael@0: IP_ADAPTER_ADDRESSES *addresses, *addr; michael@0: ULONG status, size; michael@0: int index = 0; michael@0: michael@0: if (!ifname) { michael@0: return 0; michael@0: } michael@0: michael@0: size = 0; michael@0: status = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &size); michael@0: if (status != ERROR_BUFFER_OVERFLOW) { michael@0: return 0; michael@0: } michael@0: addresses = malloc(size); michael@0: status = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, addresses, &size); michael@0: if (status == ERROR_SUCCESS) { michael@0: for (addr = addresses; addr; addr = addr->Next) { michael@0: if (addr->AdapterName && !strcmp(ifname, addr->AdapterName)) { michael@0: index = addr->IfIndex; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: free(addresses); michael@0: return index; michael@0: } michael@0: michael@0: #if WINVER < 0x0600 michael@0: /* These functions are written based on the code at michael@0: * http://www.cs.wustl.edu/~schmidt/win32-cv-1.html michael@0: * Therefore, for the rest of the file the following applies: michael@0: * michael@0: * michael@0: * Copyright and Licensing Information for ACE(TM), TAO(TM), CIAO(TM), michael@0: * DAnCE(TM), and CoSMIC(TM) michael@0: * michael@0: * [1]ACE(TM), [2]TAO(TM), [3]CIAO(TM), DAnCE(TM), and [4]CoSMIC(TM) michael@0: * (henceforth referred to as "DOC software") are copyrighted by michael@0: * [5]Douglas C. Schmidt and his [6]research group at [7]Washington michael@0: * University, [8]University of California, Irvine, and [9]Vanderbilt michael@0: * University, Copyright (c) 1993-2012, all rights reserved. Since DOC michael@0: * software is open-source, freely available software, you are free to michael@0: * use, modify, copy, and distribute--perpetually and irrevocably--the michael@0: * DOC software source code and object code produced from the source, as michael@0: * well as copy and distribute modified versions of this software. You michael@0: * must, however, include this copyright statement along with any code michael@0: * built using DOC software that you release. No copyright statement michael@0: * needs to be provided if you just ship binary executables of your michael@0: * software products. michael@0: * michael@0: * You can use DOC software in commercial and/or binary software releases michael@0: * and are under no obligation to redistribute any of your source code michael@0: * that is built using DOC software. Note, however, that you may not michael@0: * misappropriate the DOC software code, such as copyrighting it yourself michael@0: * or claiming authorship of the DOC software code, in a way that will michael@0: * prevent DOC software from being distributed freely using an michael@0: * open-source development model. You needn't inform anyone that you're michael@0: * using DOC software in your software, though we encourage you to let michael@0: * [10]us know so we can promote your project in the [11]DOC software michael@0: * success stories. michael@0: * michael@0: * The [12]ACE, [13]TAO, [14]CIAO, [15]DAnCE, and [16]CoSMIC web sites michael@0: * are maintained by the [17]DOC Group at the [18]Institute for Software michael@0: * Integrated Systems (ISIS) and the [19]Center for Distributed Object michael@0: * Computing of Washington University, St. Louis for the development of michael@0: * open-source software as part of the open-source software community. michael@0: * Submissions are provided by the submitter ``as is'' with no warranties michael@0: * whatsoever, including any warranty of merchantability, noninfringement michael@0: * of third party intellectual property, or fitness for any particular michael@0: * purpose. In no event shall the submitter be liable for any direct, michael@0: * indirect, special, exemplary, punitive, or consequential damages, michael@0: * including without limitation, lost profits, even if advised of the michael@0: * possibility of such damages. Likewise, DOC software is provided as is michael@0: * with no warranties of any kind, including the warranties of design, michael@0: * merchantability, and fitness for a particular purpose, michael@0: * noninfringement, or arising from a course of dealing, usage or trade michael@0: * practice. Washington University, UC Irvine, Vanderbilt University, michael@0: * their employees, and students shall have no liability with respect to michael@0: * the infringement of copyrights, trade secrets or any patents by DOC michael@0: * software or any part thereof. Moreover, in no event will Washington michael@0: * University, UC Irvine, or Vanderbilt University, their employees, or michael@0: * students be liable for any lost revenue or profits or other special, michael@0: * indirect and consequential damages. michael@0: * michael@0: * DOC software is provided with no support and without any obligation on michael@0: * the part of Washington University, UC Irvine, Vanderbilt University, michael@0: * their employees, or students to assist in its use, correction, michael@0: * modification, or enhancement. A [20]number of companies around the michael@0: * world provide commercial support for DOC software, however. DOC michael@0: * software is Y2K-compliant, as long as the underlying OS platform is michael@0: * Y2K-compliant. Likewise, DOC software is compliant with the new US michael@0: * daylight savings rule passed by Congress as "The Energy Policy Act of michael@0: * 2005," which established new daylight savings times (DST) rules for michael@0: * the United States that expand DST as of March 2007. Since DOC software michael@0: * obtains time/date and calendaring information from operating systems michael@0: * users will not be affected by the new DST rules as long as they michael@0: * upgrade their operating systems accordingly. michael@0: * michael@0: * The names ACE(TM), TAO(TM), CIAO(TM), DAnCE(TM), CoSMIC(TM), michael@0: * Washington University, UC Irvine, and Vanderbilt University, may not michael@0: * be used to endorse or promote products or services derived from this michael@0: * source without express written permission from Washington University, michael@0: * UC Irvine, or Vanderbilt University. This license grants no permission michael@0: * to call products or services derived from this source ACE(TM), michael@0: * TAO(TM), CIAO(TM), DAnCE(TM), or CoSMIC(TM), nor does it grant michael@0: * permission for the name Washington University, UC Irvine, or michael@0: * Vanderbilt University to appear in their names. michael@0: * michael@0: * If you have any suggestions, additions, comments, or questions, please michael@0: * let [21]me know. michael@0: * michael@0: * [22]Douglas C. Schmidt michael@0: * michael@0: * References michael@0: * michael@0: * 1. http://www.cs.wustl.edu/~schmidt/ACE.html michael@0: * 2. http://www.cs.wustl.edu/~schmidt/TAO.html michael@0: * 3. http://www.dre.vanderbilt.edu/CIAO/ michael@0: * 4. http://www.dre.vanderbilt.edu/cosmic/ michael@0: * 5. http://www.dre.vanderbilt.edu/~schmidt/ michael@0: * 6. http://www.cs.wustl.edu/~schmidt/ACE-members.html michael@0: * 7. http://www.wustl.edu/ michael@0: * 8. http://www.uci.edu/ michael@0: * 9. http://www.vanderbilt.edu/ michael@0: * 10. mailto:doc_group@cs.wustl.edu michael@0: * 11. http://www.cs.wustl.edu/~schmidt/ACE-users.html michael@0: * 12. http://www.cs.wustl.edu/~schmidt/ACE.html michael@0: * 13. http://www.cs.wustl.edu/~schmidt/TAO.html michael@0: * 14. http://www.dre.vanderbilt.edu/CIAO/ michael@0: * 15. http://www.dre.vanderbilt.edu/~schmidt/DOC_ROOT/DAnCE/ michael@0: * 16. http://www.dre.vanderbilt.edu/cosmic/ michael@0: * 17. http://www.dre.vanderbilt.edu/ michael@0: * 18. http://www.isis.vanderbilt.edu/ michael@0: * 19. http://www.cs.wustl.edu/~schmidt/doc-center.html michael@0: * 20. http://www.cs.wustl.edu/~schmidt/commercial-support.html michael@0: * 21. mailto:d.schmidt@vanderbilt.edu michael@0: * 22. http://www.dre.vanderbilt.edu/~schmidt/ michael@0: * 23. http://www.cs.wustl.edu/ACE.html michael@0: */ michael@0: michael@0: void michael@0: InitializeXPConditionVariable(userland_cond_t *cv) michael@0: { michael@0: cv->waiters_count = 0; michael@0: InitializeCriticalSection(&(cv->waiters_count_lock)); michael@0: cv->events_[C_SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL); michael@0: cv->events_[C_BROADCAST] = CreateEvent (NULL, TRUE, FALSE, NULL); michael@0: } michael@0: michael@0: void michael@0: DeleteXPConditionVariable(userland_cond_t *cv) michael@0: { michael@0: CloseHandle(cv->events_[C_BROADCAST]); michael@0: CloseHandle(cv->events_[C_SIGNAL]); michael@0: DeleteCriticalSection(&(cv->waiters_count_lock)); michael@0: } michael@0: michael@0: int michael@0: SleepXPConditionVariable(userland_cond_t *cv, userland_mutex_t *mtx) michael@0: { michael@0: int result, last_waiter; michael@0: michael@0: EnterCriticalSection(&cv->waiters_count_lock); michael@0: cv->waiters_count++; michael@0: LeaveCriticalSection(&cv->waiters_count_lock); michael@0: LeaveCriticalSection (mtx); michael@0: result = WaitForMultipleObjects(2, cv->events_, FALSE, INFINITE); michael@0: if (result==-1) { michael@0: result = GetLastError(); michael@0: } michael@0: EnterCriticalSection(&cv->waiters_count_lock); michael@0: cv->waiters_count--; michael@0: last_waiter = michael@0: result == (C_SIGNAL + C_BROADCAST && (cv->waiters_count == 0)); michael@0: LeaveCriticalSection(&cv->waiters_count_lock); michael@0: if (last_waiter) michael@0: ResetEvent(cv->events_[C_BROADCAST]); michael@0: EnterCriticalSection (mtx); michael@0: return result; michael@0: } michael@0: michael@0: void michael@0: WakeAllXPConditionVariable(userland_cond_t *cv) michael@0: { michael@0: int have_waiters; michael@0: EnterCriticalSection(&cv->waiters_count_lock); michael@0: have_waiters = cv->waiters_count > 0; michael@0: LeaveCriticalSection(&cv->waiters_count_lock); michael@0: if (have_waiters) michael@0: SetEvent (cv->events_[C_BROADCAST]); michael@0: } michael@0: #endif michael@0: #endif