2 persistent netInterfaceCVec inetAddressCArray
8 isMustHaveAddress =
false;
9 isMustHaveHwAddress=
false;
13 mxberry.
core.throwerror(
'wrongInput',
'Invalid regular argument');
19 isIPv4Only = prop{k+1};
21 isNoLoopback = prop{k+1};
22 case 'musthaveaddress' 23 isMustHaveAddress = prop{k+1};
24 case 'musthavehwaddress' 25 isMustHaveHwAddress=prop{k+1};
28 'unidentified property name: %s ',prop{k});
34 if isempty(netInterfaceCVec) || isempty(inetAddressCArray)
35 % Get all network interfaces
for this machine
36 netInterfaceList = java.util.Collections.list(...
37 java.net.NetworkInterface.getNetworkInterfaces() );
38 netInterfaceCVec =
listToCell(netInterfaceList);
39 % Get IP addresses
for each interface
40 inetAddressCArray = cellfun(@(x)
listToCell(x.getInterfaceAddresses()),...
41 netInterfaceCVec,
'UniformOutput',
false);
44 %% Init the
struct and fill out network interface fields
46 interfaceFieldsCVec = {
'interfaceName',
'interfaceDisplayName',
'hardwareAddress',
'ipAddresses'};
47 nInterfaces = length(netInterfaceCVec);
48 SNetInfoVec = cell2struct(cell(length(interfaceFieldsCVec), nInterfaces),...
49 interfaceFieldsCVec, 1);
51 nameCVec = cellfun(@(x)
char(x.getName()), netInterfaceCVec,...
52 'UniformOutput',
false);
53 displayNameCVec = cellfun(@(x)
char(x.getDisplayName()), netInterfaceCVec,...
54 'UniformOutput',
false);
56 netInterfaceCVec,
'UniformOutput',
false);
57 [SNetInfoVec.interfaceName] = deal( nameCVec{:} );
58 [SNetInfoVec.interfaceDisplayName] = deal( displayNameCVec{:} );
59 [SNetInfoVec.hardwareAddress] = deal( hardwareCVec{:} );
61 %% Fill out IP address fields
63 ipFieldsCVec = {
'hostName',
'ipVersion',
'ipAddress',
'networkPrefixLength',...
64 'netMask',
'broadcastAddress',
'isLoopbackAddress'};
65 for iInt = 1:nInterfaces
66 interfaceAddressCVec = inetAddressCArray{iInt};
67 % IPv4 addresses are 32-bit, IPv6 addresses are 128-bit
68 isIPv4Vec = cellfun(@(x)length(x.getAddress().getAddress())==4,...
69 interfaceAddressCVec);
70 isIPv6Vec = cellfun(@(x)length(x.getAddress().getAddress())==16,...
71 interfaceAddressCVec);
72 isLoopbackVec = cellfun(@(x)x.getAddress.isLoopbackAddress(),...
73 interfaceAddressCVec);
74 % Filter by IPv4Only and noLoopback flags
75 isIncludedVec = (isIPv4Vec | ~isIPv4Only) & (~isLoopbackVec | ~isNoLoopback);
76 interfaceAddressCVec(~isIncludedVec) = [];
79 nAddresses = length(interfaceAddressCVec);
80 ipStruct = cell2struct(cell(length(ipFieldsCVec), nAddresses), ipFieldsCVec, 1);
82 [ipStruct(isIPv4Vec(isIncludedVec)).ipVersion] = deal(
'IPv4');
83 [ipStruct(isIPv6Vec(isIncludedVec)).ipVersion] = deal(
'IPv6');
84 [ipStruct(isLoopbackVec(isIncludedVec)).isLoopbackAddress] = deal(
true);
85 [ipStruct(~isLoopbackVec(isIncludedVec)).isLoopbackAddress] = deal(
false);
87 for iAddr = 1:nAddresses
88 interfaceAddress = interfaceAddressCVec{iAddr};
89 inetAddress = interfaceAddress.getAddress();
91 ipStruct(iAddr).hostName = char( inetAddress.getCanonicalHostName() );
93 ipStruct(iAddr).ipAddress = char( inetAddress.getHostAddress() );
95 % Broadcast addresses are only used
for IPv4 addresses
96 if strcmp(ipStruct(iAddr).ipVersion,
'IPv4')
97 broadcastObj=interfaceAddress.getBroadcast();
98 if ~isempty(broadcastObj)
99 ipStruct(iAddr).broadcastAddress=
char(broadcastObj.getHostAddress());
101 ipStruct(iAddr).broadcastAddress='';
105 ipStruct(iAddr).networkPrefixLength = interfaceAddress.getNetworkPrefixLength();
107 % Subnet masks are only used for IPv4 addresses
108 if strcmp(ipStruct(iAddr).ipVersion, 'IPv4')
109 ipStruct(iAddr).netMask =
formatNetMask(ipStruct(iAddr).networkPrefixLength);
113 SNetInfoVec(iInt).ipAddresses = ipStruct;
117 SNetInfoVec( arrayfun(@(x)isempty(x.ipAddresses),SNetInfoVec) ) = [];
119 if isMustHaveHwAddress
120 SNetInfoVec( arrayfun(@(x)isempty(x.hardwareAddress),SNetInfoVec) ) = [];
125 cVec = cellfun(@list.get,
num2cell(0:nElem-1), 'UniformOutput', false);
129 isNeg = bytesVec < 0;
130 uintBytesVec = uint8(bytesVec);
131 uintBytesVec(isNeg) = 256 - uint8(-bytesVec(isNeg));
132 addrStr = sprintf('%02X-',uintBytesVec);
133 addrStr = addrStr(1:end-1);
140 maskVec = [255,255,255,255];
141 nMasked = fix(prefixLen/8);
143 maskVec(nMasked+1) = bitshift(uint8(255),...
147 maskVec(nMasked+2:end) = 0;
150 netMask = sprintf('%d.',maskVec);
151 netMask = netMask(1:end-1);
function getnetinterface(in varargin)
GETNETINTERFACE returns information about all network interfaces connected to this computer and their...
function num2cell(in inpArray, in varargin)
NUM2CELL is an extension of Matlab built-in function "num2cell" designed to work correctly with empty...
function listToCell(in list)
function formatHardwareAddress(in bytesVec)
Convert int8 to uint8.
function formatNetMask(in prefixLen)