1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
19 $Log: PosixCompat.c,v $
20 Revision 1.1 2009/07/09 21:40:32 herscher
21 <rdar://problem/3775717> SDK: Port mDNSNetMonitor to Windows. Add a small Posix compatibility layer to the mDNSWindows platform layer. This makes it possible to centralize the implementations to functions such as if_indextoname() and inet_pton() that are made in several projects in B4W.
26 #include "PosixCompat.h"
27 #include <DebugServices.h>
30 typedef PCHAR (WINAPI
* if_indextoname_funcptr_t
)(ULONG index
, PCHAR name
);
31 typedef ULONG (WINAPI
* if_nametoindex_funcptr_t
)(PCSTR name
);
35 if_nametoindex( const char * ifname
)
42 // Try and load the IP helper library dll
43 if ((library
= LoadLibrary(TEXT("Iphlpapi")) ) != NULL
)
45 if_nametoindex_funcptr_t if_nametoindex_funcptr
;
47 // On Vista and above there is a Posix like implementation of if_nametoindex
48 if ((if_nametoindex_funcptr
= (if_nametoindex_funcptr_t
) GetProcAddress(library
, "if_nametoindex")) != NULL
)
50 index
= if_nametoindex_funcptr(ifname
);
61 if_indextoname( unsigned ifindex
, char * ifname
)
69 // Try and load the IP helper library dll
70 if ((library
= LoadLibrary(TEXT("Iphlpapi")) ) != NULL
)
72 if_indextoname_funcptr_t if_indextoname_funcptr
;
74 // On Vista and above there is a Posix like implementation of if_indextoname
75 if ((if_indextoname_funcptr
= (if_indextoname_funcptr_t
) GetProcAddress(library
, "if_indextoname")) != NULL
)
77 name
= if_indextoname_funcptr(ifindex
, ifname
);
88 inet_pton( int family
, const char * addr
, void * dst
)
90 struct sockaddr_storage ss
;
91 int sslen
= sizeof( ss
);
93 ZeroMemory( &ss
, sizeof( ss
) );
94 ss
.ss_family
= family
;
96 if ( WSAStringToAddressA( ( LPSTR
) addr
, family
, NULL
, ( struct sockaddr
* ) &ss
, &sslen
) == 0 )
98 if ( family
== AF_INET
) { memcpy( dst
, &( ( struct sockaddr_in
* ) &ss
)->sin_addr
, sizeof( IN_ADDR
) ); return 1; }
99 else if ( family
== AF_INET6
) { memcpy( dst
, &( ( struct sockaddr_in6
* ) &ss
)->sin6_addr
, sizeof( IN6_ADDR
) ); return 1; }
107 gettimeofday( struct timeval
* tv
, struct timezone
* tz
)
109 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
110 # define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
112 # define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
116 unsigned __int64 tmpres
= 0;
120 GetSystemTimeAsFileTime(&ft
);
122 tmpres
|= ft
.dwHighDateTime
;
124 tmpres
|= ft
.dwLowDateTime
;
126 tmpres
-= DELTA_EPOCH_IN_MICROSECS
;
127 tmpres
/= 10; /*convert into microseconds*/
128 tv
->tv_sec
= (long)(tmpres
/ 1000000UL);
129 tv
->tv_usec
= (long)(tmpres
% 1000000UL);
137 localtime_r( const time_t * clock
, struct tm
* result
)
139 result
= localtime( clock
);