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.
18 #include "PosixCompat.h"
19 #include <DebugServices.h>
22 typedef PCHAR (WINAPI
* if_indextoname_funcptr_t
)(ULONG index
, PCHAR name
);
23 typedef ULONG (WINAPI
* if_nametoindex_funcptr_t
)(PCSTR name
);
27 if_nametoindex( const char * ifname
)
34 // Try and load the IP helper library dll
35 if ((library
= LoadLibrary(TEXT("Iphlpapi")) ) != NULL
)
37 if_nametoindex_funcptr_t if_nametoindex_funcptr
;
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
)
42 index
= if_nametoindex_funcptr(ifname
);
53 if_indextoname( unsigned ifindex
, char * ifname
)
61 // Try and load the IP helper library dll
62 if ((library
= LoadLibrary(TEXT("Iphlpapi")) ) != NULL
)
64 if_indextoname_funcptr_t if_indextoname_funcptr
;
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
)
69 name
= if_indextoname_funcptr(ifindex
, ifname
);
80 inet_pton( int family
, const char * addr
, void * dst
)
82 struct sockaddr_storage ss
;
83 int sslen
= sizeof( ss
);
85 ZeroMemory( &ss
, sizeof( ss
) );
86 ss
.ss_family
= family
;
88 if ( WSAStringToAddressA( ( LPSTR
) addr
, family
, NULL
, ( struct sockaddr
* ) &ss
, &sslen
) == 0 )
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; }
99 gettimeofday( struct timeval
* tv
, struct timezone
* tz
)
101 #define EPOCHFILETIME (116444736000000000i64)
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 );
124 localtime_r( const time_t * clock
, struct tm
* result
)
126 localtime_s( result
, clock
);