Skip to content

Example IP‐Address

ANYKS edited this page Mar 2, 2025 · 8 revisions
#include <net/net.hpp>

using namespace awh;

int32_t main(int32_t argc, char * argv[]){
	fmk_t fmk;
	log_t log(&fmk);
	net_t net(&log);

	net = "[2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d]";
	cout << " [2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d] || " << net << " === " << net.get(net_t::format_t::LONG_IPV4) << " === " << net.get(net_t::format_t::MIDDLE_IPV4) << " === " << net.get(net_t::format_t::SHORT_IPV4) << endl;

	net = "2001:0db8:0000:0000:0000:0000:ae21:ad12";
	cout << " 2001:0db8:0000:0000:0000:0000:ae21:ad12 || " << net << endl;

	net = "2001:db8::ae21:ad12";
	cout << " 2001:db8::ae21:ad12 || " << net.get(net_t::format_t::LONG) << " and " << net.get(net_t::format_t::MIDDLE) << endl;

	net = "0000:0000:0000:0000:0000:0000:ae21:ad12";
	cout << " 0000:0000:0000:0000:0000:0000:ae21:ad12 || " << net.get(net_t::format_t::SHORT) << endl;

	net = "::ae21:ad12";
	cout << " ::ae21:ad12 || " << net.get(net_t::format_t::MIDDLE) << endl;

	net = "2001:0db8:11a3:09d7:1f34::";
	cout << " 2001:0db8:11a3:09d7:1f34:: || " << net.get(net_t::format_t::LONG) << endl;

	net = "::ffff:192.0.2.1";
	cout << boolalpha;
	cout << " ::ffff:192.0.2.1 || " << net << " ==== " << net.broadcastIPv6ToIPv4() << endl;

	net = "::1";
	cout << " ::1 || " << net.get(net_t::format_t::LONG) << endl;

	net = "[::]";
	cout << " [::] || " << net.get(net_t::format_t::LONG) << endl;

	net = "46.39.230.51";
	cout << " 46.39.230.51 || " << net.get(net_t::format_t::LONG) << " ==== " << net.broadcastIPv6ToIPv4() << endl;

	net = "192.16.0.1";
	cout << " 192.16.0.1 || " << net.get(net_t::format_t::LONG) << " === " << net.get(net_t::format_t::LONG_IPV6) << " === " << net.get(net_t::format_t::SHORT_IPV6) << " === " << net.get(net_t::format_t::MIDDLE_IPV6) << endl;

	net = "2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d";
	cout << " Part of the address: 2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d || " << net.v6()[0] << " and " << net.v6()[1] << endl;

	net = "46.39.230.51";
	cout << " Part of the address: 46.39.230.51 || " << net.v4() << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	net.impose(53, net_t::addr_t::NETWORK);
	cout << " Prefix set: 2001:1234:abcd:5678:9877:3322:5541:aabb/53 || " << net << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	net.impose("FFFF:FFFF:FFFF:F800::", net_t::addr_t::NETWORK);
	cout << " Mask set: 2001:1234:abcd:5678:9877:3322:5541:aabb/FFFF:FFFF:FFFF:F800:: || " << net << endl;

	net = "192.168.3.192";
	net.impose(9, net_t::addr_t::NETWORK);
	cout << " Prefix set: 192.168.3.192/9 || " << net << endl;

	net = "192.168.3.192";
	net.impose("255.128.0.0", net_t::addr_t::NETWORK);
	cout << " Mask set: 192.168.3.192/255.128.0.0 || " << net << endl;

	net = "192.168.3.192";
	net.impose("255.255.255.0", net_t::addr_t::NETWORK);
	cout << " Mask set: 192.168.3.192/255.255.255.0 || " << net << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	net.impose(53, net_t::addr_t::HOST);
	cout << " Get host from IP-address: 53/2001:1234:abcd:5678:9877:3322:5541:aabb || " << net << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	net.impose("FFFF:FFFF:FFFF:F800::", net_t::addr_t::HOST);
	cout << " Get host from IP-address: FFFF:FFFF:FFFF:F800::/2001:1234:abcd:5678:9877:3322:5541:aabb || " << net << endl;

	net = "192.168.3.192";
	net.impose(9, net_t::addr_t::HOST);
	cout << " Get host from IP-address: 9/192.168.3.192 || " << net << endl;

	net = "192.168.3.192";
	net.impose("255.128.0.0", net_t::addr_t::HOST);
	cout << " Get host from IP-address: 255.128.0.0/192.168.3.192 || " << net << endl;

	net = "192.168.3.192";
	net.impose(24, net_t::addr_t::HOST);
	cout << " Get host from IP-address: 24/192.168.3.192 || " << net << endl;

	net = "192.168.3.192";
	net.impose("255.255.255.0", net_t::addr_t::HOST);
	cout << " Get host from IP-address: 255.255.255.0/192.168.3.192 || " << net << endl;

	net = "192.168.3.192";
	cout << " Get address mask from network prefix = 9 || " << net.prefix2Mask(9) << endl;
	cout << " Get address prefix from network mask = 255.128.0.0 || " << static_cast <uint16_t> (net.mask2Prefix("255.128.0.0")) << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	cout << " Get address mask from network prefix = 53 || " << net.prefix2Mask(53) << endl;
	cout << " Get address prefix from network mask = FFFF:FFFF:FFFF:F800:: || " << static_cast <uint16_t> (net.mask2Prefix("FFFF:FFFF:FFFF:F800::")) << endl;

	net = "192.168.3.192";
	cout << boolalpha;
	cout << " Check the address compliance 192.168.3.192 by network 192.168.0.0 || " << net.mapping("192.168.0.0") << endl;
	
	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	cout << boolalpha;
	cout << " Check the address compliance 2001:1234:abcd:5678:9877:3322:5541:aabb by network 2001:1234:abcd:5678:: || " << net.mapping("2001:1234:abcd:5678::") << endl;
	
	net = "192.168.3.192";
	cout << boolalpha;
	cout << " Check the address compliance 192.168.3.192 by network 192.128.0.0/9 || " << net.mapping("192.128.0.0", 9, net_t::addr_t::NETWORK) << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	cout << boolalpha;
	cout << " Check the address compliance 2001:1234:abcd:5678:9877:3322:5541:aabb by network 2001:1234:abcd:5000::/53 || " << net.mapping("2001:1234:abcd:5000::", 53, net_t::addr_t::NETWORK) << endl;

	net = "192.168.3.192";
	cout << boolalpha;
	cout << " Check the address compliance 192.168.3.192 by network 192.128.0.0/255.128.0.0 || " << net.mapping("192.128.0.0", "255.128.0.0", net_t::addr_t::NETWORK) << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	cout << boolalpha;
	cout << " Check the address compliance 2001:1234:abcd:5678:9877:3322:5541:aabb by network 2001:1234:abcd:5000::/FFFF:FFFF:FFFF:F800:: || " << net.mapping("2001:1234:abcd:5000::", "FFFF:FFFF:FFFF:F800::", net_t::addr_t::NETWORK) << endl;

	net = "192.168.3.192";
	cout << boolalpha;
	cout << " Check the address compliance 192.168.3.192 by host 9/0.40.3.192 || " << net.mapping("0.40.3.192", 9, net_t::addr_t::HOST) << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	cout << boolalpha;
	cout << " Check the address compliance 2001:1234:abcd:5678:9877:3322:5541:aabb by host 53/::678:9877:3322:5541:AABB || " << net.mapping("::678:9877:3322:5541:AABB", 53, net_t::addr_t::HOST) << endl;

	net = "192.168.3.192";
	cout << boolalpha;
	cout << " Check the address compliance 192.168.3.192 by host 255.128.0.0/0.40.3.192 || " << net.mapping("0.40.3.192", "255.128.0.0", net_t::addr_t::HOST) << endl;

	net = "2001:1234:abcd:5678:9877:3322:5541:aabb";
	cout << boolalpha;
	cout << " Check the address compliance 2001:1234:abcd:5678:9877:3322:5541:aabb by host FFFF:FFFF:FFFF:F800::/::678:9877:3322:5541:AABB || " << net.mapping("::678:9877:3322:5541:AABB", "FFFF:FFFF:FFFF:F800::", net_t::addr_t::HOST) << endl;

	net = "192.168.3.192";
	cout << boolalpha;
	cout << " Check whether the address 192.168.3.192 is in range [192.168.3.100 - 192.168.3.200] || " << net.range("192.168.3.100", "192.168.3.200", 24) << endl;

	net = "46.39.230.51";
	cout << boolalpha;
	cout << " Checking whether IP-address is global 46.39.230.51 || " << (net.mode() == net_t::mode_t::WAN) << endl;

	net = "192.168.31.12";
	cout << boolalpha;
	cout << " Checking whether IP-address is local 192.168.31.12 || " << (net.mode() == net_t::mode_t::LAN) << endl;

	net = "0.0.0.0";
	cout << boolalpha;
	cout << " Checking whether IP-address is system 0.0.0.0 || " << (net.mode() == net_t::mode_t::SYS) << endl;

	net = "[2a00:1450:4010:c0a::8b]";
	cout << boolalpha;
	cout << " Checking whether IP-address is global [2a00:1450:4010:c0a::8b] || " << (net.mode() == net_t::mode_t::WAN) << endl;

	net = "::1";
	cout << boolalpha;
	cout << " Checking whether IP-address is local [::1] || " << (net.mode() == net_t::mode_t::LAN) << endl;

	net = "::";
	cout << boolalpha;
	cout << " Checking whether IP-address is system [::] || " << (net.mode() == net_t::mode_t::SYS) << endl;

	string ip = "2001:0db8:0000:0000:0000:0000:ae21:ad12";
	cout << " Long record address || " << ip << endl;
	ip = net = ip;
	cout << " Short record address || " << ip << endl;

	net = "73:0b:04:0d:db:79";
	cout << " MAC: 73:0b:04:0d:db:79 || " << net << endl;

	net.arpa("70.255.255.5.in-addr.arpa");
	cout << " ARPA: 70.255.255.5.in-addr.arpa || " << net << endl;
	cout << " ARPA IPv4: " << net.arpa() << endl;

	net.arpa("b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa");
	cout << " ARPA: b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa || " << net << endl;
	cout << " ARPA IPv6: " << net.arpa() << endl;

	return EXIT_SUCCESS;
}