Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9306 in orxonox.OLD for branches/proxy/src/lib/network/ip.cc


Ignore:
Timestamp:
Jul 17, 2006, 2:21:42 PM (18 years ago)
Author:
bensch
Message:

orxonox/proxy: ip class ok now

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/proxy/src/lib/network/ip.cc

    r9305 r9306  
    88#include "ip.h"
    99#include "multi_type.h"
     10#include "substring.h"
    1011
    1112IP::IP()
    12 {}
     13{
     14  this->_ip = 0;
     15  this->_port = 0;
     16}
    1317
    1418
    15 IP::IP(int ip)
     19IP::IP(int ip, int port)
    1620{
    1721  this->_ip = ip;
    18 }
    19 
    20 IP::IP(const std::string& ip)
    21 {
    22   this->_ip = IP::stringToIP(ip);
     22  this->_port = port;
    2323}
    2424
    2525
    2626
     27IP::IP(const std::string& ip)
     28{
     29  *this = IP::stringToIP(ip);
     30}
    2731
    28 int IP::stringToIP(const std::string& ip)
     32
     33IP::IP(const IPaddress& ip)
    2934{
     35  this->_ip = ip.host;
     36  this->_port = ip.port;
     37}
     38
     39
     40IP::IP(const IP& ip)
     41{
     42  this->_ip = ip.ip();
     43  this->_port = ip.port();
     44}
     45
     46
     47
     48const IP& IP::operator=(const IP& ip)
     49{
     50  this->_ip = ip.ip();
     51  this->_port = ip.port();
     52  return *this;
     53}
     54
     55
     56
     57bool IP::operator==(const IP& ip)
     58{
     59  return (this->_ip == ip.ip() && this->_port == ip.port());
     60}
     61
     62
     63/**
     64 * @returns The retrieved IP
     65 *
     66 */
     67IP IP::stringToIP(const std::string& ip)
     68{
     69  IPaddress ipaddr;
     70
     71  SDLNet_ResolveHost(&ipaddr, NULL, 2000);
     72
     73  return IP(ipaddr);
     74
     75  /*
     76  SubString addr(ip, '.');
     77  if(ip.size() != 4 )
     78    return -1;
     79
     80  MultiType part0(ip[0]);
     81  MultiType part1(ip[1]);
     82  MultiType part2(ip[2]);
     83  MultiType part3(ip[3]);
     84  */
     85}
     86
     87int IP::ipPart(unsigned int part) const
     88{
     89  switch (part)
     90  {
     91    case 0:
     92      return  (_ip & 0xFF000000) >> 24;
     93    case 1:
     94      return  (_ip & 0x00FF0000) >> 16;
     95    case 2:
     96      return  (_ip & 0x0000FF00) >> 8;
     97    case 3:
     98      return  (_ip & 0x000000FF);
     99    default:
     100      return -1;
     101  }
    30102
    31103}
     104
     105
     106std::string IP::ipString() const
     107{
     108  return IP::ipToString(this->_ip);
     109}
     110
    32111
    33112std::string IP::ipToString(const IPaddress& ipaddr)
     
    38117
    39118
     119
     120
     121
    40122std::string IP::ipToString(int ip)
    41123{
    42   MultiType part0( (ip & 0xFF000000) >> 24);
    43   MultiType part1( (ip & 0x00FF0000) >> 16);
    44   MultiType part2( (ip & 0x0000FF00) >>  8);
    45   MultiType part3( (ip & 0x000000FF) );
     124  MultiType part0((int) (ip & 0xFF000000) >> 24);
     125  MultiType part1((int) (ip & 0x00FF0000) >> 16);
     126  MultiType part2((int) (ip & 0x0000FF00) >>  8);
     127  MultiType part3((int) (ip & 0x000000FF) );
    46128
    47129
    48130  return part0.getString() + "." + part1.getString() + "." +
    49            part2.getString() + "." + part3.getString();
     131         part2.getString() + "." + part3.getString();
    50132}
    51133
Note: See TracChangeset for help on using the changeset viewer.