]> git.saurik.com Git - apple/mdnsresponder.git/blame - mDNSWindows/PosixCompat.c
mDNSResponder-214.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / PosixCompat.c
CommitLineData
1a175162
A
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 Change History (most recent first):
18
19$Log: PosixCompat.c,v $
20Revision 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.
22
23
24*/
25
26#include "PosixCompat.h"
27#include <DebugServices.h>
28
29
30typedef PCHAR (WINAPI * if_indextoname_funcptr_t)(ULONG index, PCHAR name);
31typedef ULONG (WINAPI * if_nametoindex_funcptr_t)(PCSTR name);
32
33
34unsigned
35if_nametoindex( const char * ifname )
36{
37 HMODULE library;
38 unsigned index = 0;
39
40 check( ifname );
41
42 // Try and load the IP helper library dll
43 if ((library = LoadLibrary(TEXT("Iphlpapi")) ) != NULL )
44 {
45 if_nametoindex_funcptr_t if_nametoindex_funcptr;
46
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 )
49 {
50 index = if_nametoindex_funcptr(ifname);
51 }
52
53 FreeLibrary(library);
54 }
55
56 return index;
57}
58
59
60char*
61if_indextoname( unsigned ifindex, char * ifname )
62{
63 HMODULE library;
64 char * name = NULL;
65
66 check( ifname );
67 *ifname = '\0';
68
69 // Try and load the IP helper library dll
70 if ((library = LoadLibrary(TEXT("Iphlpapi")) ) != NULL )
71 {
72 if_indextoname_funcptr_t if_indextoname_funcptr;
73
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 )
76 {
77 name = if_indextoname_funcptr(ifindex, ifname);
78 }
79
80 FreeLibrary(library);
81 }
82
83 return name;
84}
85
86
87int
88inet_pton( int family, const char * addr, void * dst )
89{
90 struct sockaddr_storage ss;
91 int sslen = sizeof( ss );
92
93 ZeroMemory( &ss, sizeof( ss ) );
94 ss.ss_family = family;
95
96 if ( WSAStringToAddressA( ( LPSTR ) addr, family, NULL, ( struct sockaddr* ) &ss, &sslen ) == 0 )
97 {
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; }
100 else return 0;
101 }
102 else return 0;
103}
104
105
106int
107gettimeofday( struct timeval * tv, struct timezone * tz )
108{
109#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
110# define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
111#else
112# define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
113#endif
114
115 FILETIME ft;
116 unsigned __int64 tmpres = 0;
117
118 if ( tv != NULL )
119 {
120 GetSystemTimeAsFileTime(&ft);
121
122 tmpres |= ft.dwHighDateTime;
123 tmpres <<= 32;
124 tmpres |= ft.dwLowDateTime;
125
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);
130 }
131
132 return 0;
133}
134
135
136extern struct tm*
137localtime_r( const time_t * clock, struct tm * result )
138{
139 result = localtime( clock );
140 return result;
141}