MatrixBerryCore
getnetinterface.m
Go to the documentation of this file.
1 function SNetInfoVec=getnetinterface(varargin)
2 persistent netInterfaceCVec inetAddressCArray
3 %
4 %% Parse arguments
5 %
6 isIPv4Only = false;
7 isNoLoopback = false;
8 isMustHaveAddress = false;
9 isMustHaveHwAddress=false;
10 %
11 [reg,prop]=mxberry.core.parseparams(varargin,[],0);
12 if ~isempty(reg)
13  mxberry.core.throwerror('wrongInput', 'Invalid regular argument');
14 end
15 nProp=length(prop);
16 for k=1:2:nProp-1
17  switch lower(prop{k})
18  case 'ipv4only'
19  isIPv4Only = prop{k+1};
20  case 'noloopback'
21  isNoLoopback = prop{k+1};
22  case 'musthaveaddress'
23  isMustHaveAddress = prop{k+1};
24  case 'musthavehwaddress'
25  isMustHaveHwAddress=prop{k+1};
26  otherwise
27  mxberry.core.throwerror('wrongInput', ...
28  'unidentified property name: %s ',prop{k});
29  end
30 end
31 %
32 %% Get network info
33 %
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);
42 end
43 %
44 %% Init the struct and fill out network interface fields
45 %
46 interfaceFieldsCVec = {'interfaceName','interfaceDisplayName','hardwareAddress','ipAddresses'};
47 nInterfaces = length(netInterfaceCVec);
48 SNetInfoVec = cell2struct(cell(length(interfaceFieldsCVec), nInterfaces),...
49  interfaceFieldsCVec, 1);
50 %
51 nameCVec = cellfun(@(x)char(x.getName()), netInterfaceCVec,...
52  'UniformOutput', false);
53 displayNameCVec = cellfun(@(x)char(x.getDisplayName()), netInterfaceCVec,...
54  'UniformOutput', false);
55 hardwareCVec = cellfun(@(x)formatHardwareAddress(x.getHardwareAddress()),...
56  netInterfaceCVec, 'UniformOutput', false);
57 [SNetInfoVec.interfaceName] = deal( nameCVec{:} );
58 [SNetInfoVec.interfaceDisplayName] = deal( displayNameCVec{:} );
59 [SNetInfoVec.hardwareAddress] = deal( hardwareCVec{:} );
60 %
61 %% Fill out IP address fields
62 %
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) = [];
77  %
78  % Init IP struct
79  nAddresses = length(interfaceAddressCVec);
80  ipStruct = cell2struct(cell(length(ipFieldsCVec), nAddresses), ipFieldsCVec, 1);
81  %
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);
86  %
87  for iAddr = 1:nAddresses
88  interfaceAddress = interfaceAddressCVec{iAddr};
89  inetAddress = interfaceAddress.getAddress();
90  %
91  ipStruct(iAddr).hostName = char( inetAddress.getCanonicalHostName() );
92  %
93  ipStruct(iAddr).ipAddress = char( inetAddress.getHostAddress() );
94  %
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());
100  else
101  ipStruct(iAddr).broadcastAddress='';
102  end
103  end
104  %
105  ipStruct(iAddr).networkPrefixLength = interfaceAddress.getNetworkPrefixLength();
106  %
107  % Subnet masks are only used for IPv4 addresses
108  if strcmp(ipStruct(iAddr).ipVersion, 'IPv4')
109  ipStruct(iAddr).netMask = formatNetMask(ipStruct(iAddr).networkPrefixLength);
110  end
111  end
112  %
113  SNetInfoVec(iInt).ipAddresses = ipStruct;
114 end
115 %
116 if isMustHaveAddress
117  SNetInfoVec( arrayfun(@(x)isempty(x.ipAddresses),SNetInfoVec) ) = [];
118 end
119 if isMustHaveHwAddress
120  SNetInfoVec( arrayfun(@(x)isempty(x.hardwareAddress),SNetInfoVec) ) = [];
121 end
122 %
123  function cVec = listToCell(list)
124  nElem = list.size();
125  cVec = cellfun(@list.get, num2cell(0:nElem-1), 'UniformOutput', false);
126  end
127 %
128  function addrStr = formatHardwareAddress(bytesVec)
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);
134  end
135 %
136  function netMask = formatNetMask(prefixLen)
137  if prefixLen < 0
138  maskVec = [0,0,0,0];
139  else
140  maskVec = [255,255,255,255];
141  nMasked = fix(prefixLen/8);
142  if nMasked < 4
143  maskVec(nMasked+1) = bitshift(uint8(255),...
144  8-mod(prefixLen,8));
145  end
146  if nMasked < 3
147  maskVec(nMasked+2:end) = 0;
148  end
149  end
150  netMask = sprintf('%d.',maskVec);
151  netMask = netMask(1:end-1);
152  end
153 end
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)