-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathnetwork_interface.cpp
More file actions
131 lines (112 loc) · 3.96 KB
/
network_interface.cpp
File metadata and controls
131 lines (112 loc) · 3.96 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef NODE_MDNS_NETWORK_INTERFACE_INCLUDED
#define NODE_MDNS_NETWORK_INTERFACE_INCLUDED
#include "mdns.hpp"
#include <v8.h>
#include "mdns_utils.hpp"
#ifdef NODE_MDNS_HAVE_INTERFACE_NAME_CONVERSION
# ifdef WIN32
# include <netioapi.h>
# else
# include <sys/types.h>
# include <sys/socket.h>
# include <net/if.h> // if_nametoindex()
# endif
using namespace v8;
namespace node_mdns {
NAN_METHOD(if_nametoindex) {
if (argumentCountMismatch(info, 1)) {
return throwArgumentCountMismatchException(info, 1);
}
if ( ! info[0]->IsString()) {
return throwTypeError("argument 1 must be a string (interface name)");
}
Nan::Utf8String interfaceName(info[0]);
#ifdef WIN32
DWORD aliasLength = MultiByteToWideChar(CP_UTF8, 0, *interfaceName, -1,
NULL, 0);
if (aliasLength == 0) {
return throwError("failed to determine buffer size");
}
wchar_t * alias = new wchar_t[aliasLength];
if ( ! alias) {
return throwError("failed to allocate alias buffer");
}
if (MultiByteToWideChar(CP_UTF8, 0, *interfaceName, -1, alias,
aliasLength) == 0)
{
delete [] alias;
return throwError("failed to convert utf8 to unicode");
}
NET_LUID luid;
if (ConvertInterfaceAliasToLuid(alias, &luid) != NO_ERROR) {
delete [] alias;
return throwError("failed to convert interface alias to luid");
}
delete [] alias;
NET_IFINDEX index = 0;
if (ConvertInterfaceLuidToIndex(&luid, &index) != NO_ERROR) {
return throwError("failed to convert interface luid to index");
}
#else
unsigned int index = ::if_nametoindex(*interfaceName);
#endif
if (index == 0) {
return throwError((std::string("interface '") + *interfaceName +
"' does not exist").c_str());
}
info.GetReturnValue().Set( Nan::New<Integer>(static_cast<uint32_t>(index)));
}
NAN_METHOD(if_indextoname) {
if (argumentCountMismatch(info, 1)) {
return throwArgumentCountMismatchException(info, 1);
}
if ( ! info[0]->IsUint32()) {
return throwTypeError("argument 1 must be a positive integer "
"(interface index)");
}
#ifdef WIN32
NET_LUID luid;
NET_IFINDEX index = Nan::To<uint32_t>(info[0]).FromJust();
if (ConvertInterfaceIndexToLuid(index, &luid) != NO_ERROR)
{
// IPv6 interface indices are shifted by mDNSResponder - see comment
// near use of kIPv6IfIndexBase in mDNSResponder/mDNSWindows/mDNSWin32.c
// for explanation.
const uint32_t kIPv6IfIndexBase = 10000000L;
if (index <= kIPv6IfIndexBase
|| ConvertInterfaceIndexToLuid(index - kIPv6IfIndexBase, &luid) != NO_ERROR)
{
return throwError("failed to convert interface index to luid");
}
}
enum { size = NDIS_IF_MAX_STRING_SIZE + 1 };
wchar_t alias[size];
if (ConvertInterfaceLuidToAlias(&luid, alias, size) != NO_ERROR) {
return throwError("failed to convert interface luid to alias");
}
int utf8Length = WideCharToMultiByte(CP_UTF8, 0, alias, -1,
NULL, 0, NULL, NULL);
if (utf8Length == 0) {
return throwError("failed to determine buffer size");
}
char * nameBuffer = new char[utf8Length];
if (WideCharToMultiByte(CP_UTF8, 0, alias, -1, nameBuffer, utf8Length,
NULL, NULL) == 0)
{
delete [] nameBuffer;
return throwError("failed to convert unicode to utf8");
}
Local<String> name = Nan::New(nameBuffer).ToLocalChecked();
delete [] nameBuffer;
#else
char nameBuffer[IFNAMSIZ];
if ( ! ::if_indextoname(Nan::To<uint32_t>(info[0]).FromJust(), nameBuffer)) {
return throwError("index has no corresponding interface");
}
Local<String> name = Nan::New(nameBuffer).ToLocalChecked();
#endif
info.GetReturnValue().Set(name);
}
} // end of namespace node_mdns
#endif // NODE_MDNS_HAVE_INTERFACE_NAME_CONVERSION
#endif // NODE_MDNS_NETWORK_INTERFACE_INCLUDED