Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-04-2009
Member
 
Join Date: Apr 2009
Posts: 79
Managing a class IPv4 in C++

hi,
I need to create a class that manages an IPv4 address and its associated mask.
Only for the manufacturer, using an unsigned integer.
I would like to know how to have a number coded on 4 bytes and pass in the form xxx.xxx.xxx.xxx
Below, the class definition:


Code:
  	  AdresseIP class 
  ( 
      private: 
          unsigned int address; 
          unsigned int mask; 
      public: 
          AdresseIP (unsigned int, unsigned int); 
          AdresseIP (unsigned char, unsigned char, unsigned char, unsigned char, unsigned short); 
          unsigned int getAdresse (); 
          unsigned int getMasque (); 
          wxString getAdresseIP (); 
  );
I can not get methods.
Thank you for your help in advance.
Reply With Quote
  #2  
Old 28-04-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Managing a class IPv4 in C++

Int is a very, very often coded on 4 bytes so the store in an unsigned int is the right thing.
I suggest you the manufacturer which encode the following address in h0.h1.h2.h3 address:
Code:
AdresseIP: AdresseIP (h0 unsigned char, unsigned char h1, unsigned char h2, unsigned char h3) ( 
      address = h0 * 256 * 256 * 256 + h1 * 256 * 256 * 256 + h2 + h3; 
      / / Mask = I do not see what you want to do with your short.
Reply With Quote
  #3  
Old 28-04-2009
Member
 
Join Date: Apr 2009
Posts: 79
Re: Managing a class IPv4 in C++

thank you.
In short, it serves to define the type of mask, such as a network 192.168.1.0 mask 255.255.255.0 is the mask value in decimal is equivalent to 24 since the first 24 bits to 1 and the other 8 to 0 : giving a 192.168.1.0/24 network, so the code would be:

Code:
  AdresseIP Address1 = AdresseIP (192, 168, 1, 0, 24);
Reply With Quote
  #4  
Old 28-04-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Managing a class IPv4 in C++

Good for the mask, you add a parameter to your constructor. It corresponds to the number of bits of the IP used to define the network address. This is not an address, just a number always less than 32.Generally, 24.

With this mask, you can find the address of your network, broadcast address, ...
The first is the MSB. To do this, I am bit of a gap to the right, and I apply a mask to take into account only the low byte.
For the first number, the mask is useless.
I have written directly addr>> 24.
Can I concatenate with a point, then I begin again by taking the second byte of the highest weight, I apply the mask, ...
Reply With Quote
  #5  
Old 28-04-2009
Member
 
Join Date: Apr 2009
Posts: 79
Re: Managing a class IPv4 in C++

For the host address and network I've done this:
Code:
AdresseIP string:: getAdReseau () 
  ( 
      unsigned int network = (this -> address) & (this -> mask); 
      stringstream mystrstream; 
      mystrstream << "Network Address:" <<((network>> 24) & 0xFF) << "." <<((network>> 16) & 0xFF) << "." <<((network>> 8) & 0xFF) << "." <<(network & 0xFF) <<endl; 
      return mystrstream. str (); 
  ) 

  AdresseIP string:: getAdHote () 
  ( 
      unsigned int host = (this -> address) & (~ this -> mask); 
      stringstream mystrstream; 
      mystrstream << "host address:" <<((host>> 24) & 0xFF) << "." <<((host>> 16) & 0xFF) << "." <<((host> > 8) & 0xFF) << "." <<(breakfast & 0xFF) <<endl; 
      return mystrstream. str (); 
  )
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Managing a class IPv4 in C++"
Thread Thread Starter Forum Replies Last Post
NIC for IPv6 n IPv4 pious gal Networking & Security 1 02-12-2011 07:01 PM
How is IPv6 better than IPv4 ? Duggars Networking & Security 5 23-11-2010 11:26 PM
How to set up multiple IPs in Ipv4 ground_sniper Operating Systems 1 18-08-2009 09:09 AM
IPv4 vs IPv6 Miyamoto Guides & Tutorials 5 27-07-2009 04:20 AM
IPV4 elements in Windows 7 DarenHawk Networking & Security 3 09-04-2009 11:30 AM


All times are GMT +5.5. The time now is 07:26 AM.