Skip to content

C++ library for parsing IPv4, IPv6 and CIDR addresses.

License

Notifications You must be signed in to change notification settings

divy9881/IPParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IPParser

C++ library for parsing IPv4, IPv6 and CIDR addresses.

Table of contents

Parsing IP addresses

#include <iostream>

#include "../parser/parseIP.h" // function parseIP()
#include "../parser/IP.h" // class IP

using namespace std;

int main() {
    cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
    IP ip1 = parseIP("192.168.2.123");
    cout << "Status : " << ip1.isLegal <<endl;
    cout << ip1.toString() <<endl;
    cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
    return 0;
}

Parsing CIDR addresses

#include <iostream>

#include "../parser/parseCIDR.h" // function parseCIDR()
#include "../parser/CIDR.h" // class CIDR

using namespace std;

int main() {
    CIDR cidr = parseCIDR("192.168.2.123/24");
    cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
    cout << "Host IP : " << cidr.ip.toString() <<endl;
    cout << "Network IP : " << cidr.net.NETIP_toString() <<endl;
    cout << "Network Mask : " << cidr.net.IPMask_toString() <<endl;
    cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
    return 0;
}

Equality of IP addresses

#include <iostream>

#include "../parser/parseIP.h" // function parseIP()
#include "../parser/IP.h" // class IP

using namespace std;

int main() {
    IP ip1 = parseIP("192.168.2.123");
    IP ip2 = parseIP("192.168.2.123");
    IP ip3 = parseIP("192.168.2.12");
    cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
    cout << "IP1 == IP2 => " << ip1.Equal(ip2) <<endl;
    cout << "IP1 == IP3 => " << ip1.Equal(ip3) <<endl;
    cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
    return 0;
}

CIDR contains IP

#include <iostream>

#include "../parser/IP.h" // class IP
#include "../parser/CIDR.h" // class CIDR
#include "../parser/parseIP.h" // function parseIP()
#include "../parser/parseCIDR.h" // function parseCIDR()

using namespace std;

int main() {
    IP ip = parseIP("192.168.2.123");
    CIDR cidr1 = parseCIDR("192.168.2.123/24");
    CIDR cidr2 = parseCIDR("192.168.3.123/24");
    cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
    cout<< "Does given CIDR1 contains IP => " << cidr1.net.contains(ip) <<endl;
    cout<< "Does given CIDR2 contains IP => " << cidr2.net.contains(ip) <<endl;
    cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
    return 0;
}

License

This project is licensed under the Apache 2.0 license.

Contact

If you have any issues or feature requests, please contact me. PR is welcomed.

About

C++ library for parsing IPv4, IPv6 and CIDR addresses.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published