Home | History | Annotate | Download | only in mDNSWindows
      1 /* -*- Mode: C; tab-width: 4 -*-
      2  *
      3  * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 #include "PosixCompat.h"
     19 #include <DebugServices.h>
     20 
     21 
     22 typedef PCHAR (WINAPI * if_indextoname_funcptr_t)(ULONG index, PCHAR name);
     23 typedef ULONG (WINAPI * if_nametoindex_funcptr_t)(PCSTR name);
     24 
     25 
     26 unsigned
     27 if_nametoindex( const char * ifname )
     28 {
     29 	HMODULE library;
     30 	unsigned index = 0;
     31 
     32 	check( ifname );
     33 
     34 	// Try and load the IP helper library dll
     35 	if ((library = LoadLibrary(TEXT("Iphlpapi")) ) != NULL )
     36 	{
     37 		if_nametoindex_funcptr_t if_nametoindex_funcptr;
     38 
     39 		// On Vista and above there is a Posix like implementation of if_nametoindex
     40 		if ((if_nametoindex_funcptr = (if_nametoindex_funcptr_t) GetProcAddress(library, "if_nametoindex")) != NULL )
     41 		{
     42 			index = if_nametoindex_funcptr(ifname);
     43 		}
     44 
     45 		FreeLibrary(library);
     46 	}
     47 
     48 	return index;
     49 }
     50 
     51 
     52 char*
     53 if_indextoname( unsigned ifindex, char * ifname )
     54 {
     55 	HMODULE library;
     56 	char * name = NULL;
     57 
     58 	check( ifname );
     59 	*ifname = '\0';
     60 
     61 	// Try and load the IP helper library dll
     62 	if ((library = LoadLibrary(TEXT("Iphlpapi")) ) != NULL )
     63 	{
     64 		if_indextoname_funcptr_t if_indextoname_funcptr;
     65 
     66 		// On Vista and above there is a Posix like implementation of if_indextoname
     67 		if ((if_indextoname_funcptr = (if_indextoname_funcptr_t) GetProcAddress(library, "if_indextoname")) != NULL )
     68 		{
     69 			name = if_indextoname_funcptr(ifindex, ifname);
     70 		}
     71 
     72 		FreeLibrary(library);
     73 	}
     74 
     75 	return name;
     76 }
     77 
     78 
     79 int
     80 inet_pton( int family, const char * addr, void * dst )
     81 {
     82 	struct sockaddr_storage ss;
     83 	int sslen = sizeof( ss );
     84 
     85 	ZeroMemory( &ss, sizeof( ss ) );
     86 	ss.ss_family = family;
     87 
     88 	if ( WSAStringToAddressA( ( LPSTR ) addr, family, NULL, ( struct sockaddr* ) &ss, &sslen ) == 0 )
     89 	{
     90 		if ( family == AF_INET ) { memcpy( dst, &( ( struct sockaddr_in* ) &ss)->sin_addr, sizeof( IN_ADDR ) ); return 1; }
     91 		else if ( family == AF_INET6 ) { memcpy( dst, &( ( struct sockaddr_in6* ) &ss)->sin6_addr, sizeof( IN6_ADDR ) ); return 1; }
     92 		else return 0;
     93 	}
     94     else return 0;
     95 }
     96 
     97 
     98 int
     99 gettimeofday( struct timeval * tv, struct timezone * tz )
    100 {
    101 #define EPOCHFILETIME (116444736000000000i64)
    102 
    103 	if ( tv != NULL )
    104 	{
    105 		FILETIME        ft;
    106 		LARGE_INTEGER   li;
    107 		__int64         t;
    108 
    109 		GetSystemTimeAsFileTime(&ft);
    110 		li.LowPart  = ft.dwLowDateTime;
    111 		li.HighPart = ft.dwHighDateTime;
    112 		t  = li.QuadPart;	/* In 100-nanosecond intervals */
    113 		t -= EPOCHFILETIME;	/* Offset to the Epoch time */
    114 		t /= 10;			/* In microseconds */
    115 		tv->tv_sec  = ( long )( t / 1000000 );
    116 		tv->tv_usec = ( long )( t % 1000000 );
    117 	}
    118 
    119 	return 0;
    120 }
    121 
    122 
    123 extern struct tm*
    124 localtime_r( const time_t * clock, struct tm * result )
    125 {
    126 	localtime_s( result, clock );
    127 	return result;
    128 }
    129