-
Notifications
You must be signed in to change notification settings - Fork 158
/
win_list.c
101 lines (83 loc) · 2.98 KB
/
win_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* MIT License
*
* Copyright (c) 2018 Lewis Van Winkle
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#include <winsock2.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
int main() {
WSADATA d;
if (WSAStartup(MAKEWORD(2, 2), &d)) {
printf("Failed to initialize.\n");
return -1;
}
DWORD asize = 20000;
PIP_ADAPTER_ADDRESSES adapters;
do {
adapters = (PIP_ADAPTER_ADDRESSES)malloc(asize);
if (!adapters) {
printf("Couldn't allocate %ld bytes for adapters.\n", asize);
WSACleanup();
return -1;
}
int r = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, 0,
adapters, &asize);
if (r == ERROR_BUFFER_OVERFLOW) {
printf("GetAdaptersAddresses wants %ld bytes.\n", asize);
free(adapters);
} else if (r == ERROR_SUCCESS) {
break;
} else {
printf("Error from GetAdaptersAddresses: %d\n", r);
free(adapters);
WSACleanup();
return -1;
}
} while (!adapters);
PIP_ADAPTER_ADDRESSES adapter = adapters;
while (adapter) {
printf("\nAdapter name: %S\n", adapter->FriendlyName);
PIP_ADAPTER_UNICAST_ADDRESS address = adapter->FirstUnicastAddress;
while (address) {
printf("\t%s",
address->Address.lpSockaddr->sa_family == AF_INET ?
"IPv4" : "IPv6");
char ap[100];
getnameinfo(address->Address.lpSockaddr,
address->Address.iSockaddrLength,
ap, sizeof(ap), 0, 0, NI_NUMERICHOST);
printf("\t%s\n", ap);
address = address->Next;
}
adapter = adapter->Next;
}
free(adapters);
WSACleanup();
return 0;
}