Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/ip.cc @ 9320

Last change on this file since 9320 was 9311, checked in by bensch, 19 years ago

minor cleanup

File size: 4.0 KB
RevLine 
[7899]1/**
[9304]2 * @file ip.cc
3 * @brief A IP class, that handles all about time.
[7899]4 *
5 * code taken from audiere.
6 */
7
[9304]8#include "ip.h"
[9305]9#include "multi_type.h"
[9306]10#include "substring.h"
[7899]11
[9307]12/**
13 * @brief default constructor
14 */
[9304]15IP::IP()
[9306]16{
17  this->_ip = 0;
18  this->_port = 0;
19}
[7899]20
21
[9307]22/**
23 * @brief constructor from ip and port
24 * @param ip the IP
25 * @param port the Port
26 * @return self
27 */
[9306]28IP::IP(int ip, int port)
[9304]29{
30  this->_ip = ip;
[9306]31  this->_port = port;
[7909]32}
33
[9306]34
[9307]35/**
36 * @brief constructor from a String
37 * @param ip the IP as a String.
[9310]38 * @param port The port to be resolved.
39 * @param resolve if true, the IP is resolved via DNS,
[9307]40 * @return self
41 */
[9310]42IP::IP(const std::string& ip, int port, bool resolve)
[9304]43{
[9310]44  *this = IP::stringToIP(ip, port, resolve);
[9304]45}
[7899]46
[7909]47
[9307]48/**
49 * @brief constructor from an SDL net structure IPaddress
50 * @param ip the ip.
51 * @return self
52 */
[9306]53IP::IP(const IPaddress& ip)
54{
55  this->_ip = ip.host;
56  this->_port = ip.port;
57}
[9305]58
59
[9307]60/**
61 * @brief copy constructor.
62 * @param ip the IP to copy.
63 * @return self
64 */
[9306]65IP::IP(const IP& ip)
[9304]66{
[9306]67  this->_ip = ip.ip();
68  this->_port = ip.port();
69}
[7899]70
[9307]71/**
72 * @brief copy operator
73 * @param ip the IP to copy.
74 * @return self.
75 */
[9306]76const IP& IP::operator=(const IP& ip)
77{
78  this->_ip = ip.ip();
79  this->_port = ip.port();
80  return *this;
[9305]81}
[9304]82
[9307]83/**
84 * @brief comparison operator
85 * @param ip the IP to compare
86 * @return true if ip _and_ port match.
87 */
[9306]88bool IP::operator==(const IP& ip)
89{
90  return (this->_ip == ip.ip() && this->_port == ip.port());
91}
92
93
94/**
[9307]95 * @brief converts a String into an IP Object.
96 * @param ip The string holding an IP.
[9310]97 * @param port The port to be resolved.
98 * @param resolve if true, the IP is resolved via DNS,
99 * otherwise (resolve == false) the IP is being transformed
100 * from a String (xxx.xxx.xxx.xxx) to an integer.
101 *
102 * @return A newly constructed IP.
[9306]103 */
[9310]104IP IP::stringToIP(const std::string& ip, int port, bool resolve)
[9306]105{
[9310]106  if (resolve)
107  {
108    IPaddress ipaddr;
[9306]109
[9310]110    SDLNet_ResolveHost(&ipaddr, NULL, 2000);
[9306]111
[9310]112    return IP(ipaddr.host, port);
113  }
114  else
115  {
116    SubString ipaddr(ip, '.');
117    if(ip.size() != 4 )
118      return IP();
[9306]119
[9310]120    MultiType part0(ipaddr[0]);
121    MultiType part1(ipaddr[1]);
122    MultiType part2(ipaddr[2]);
123    MultiType part3(ipaddr[3]);
[9306]124
[9310]125    int addr = (part0.getInt() << 24) +
126               (part1.getInt() << 16) +
127               (part2.getInt() <<  8) +
128               part3.getInt();
129    return IP(addr, port);
130  }
[9306]131}
132
[9307]133
134/**
135 * @brief if you want to have a specific part of an IP
136 * @param part the n'th part of the IP addr (splitted by '.').
137 * @return the amount held in the designated part.
138 */
[9306]139int IP::ipPart(unsigned int part) const
140{
141  switch (part)
142  {
143    case 0:
144      return  (_ip & 0xFF000000) >> 24;
145    case 1:
146      return  (_ip & 0x00FF0000) >> 16;
147    case 2:
148      return  (_ip & 0x0000FF00) >> 8;
149    case 3:
150      return  (_ip & 0x000000FF);
151    default:
152      return -1;
153  }
154}
155
156
[9307]157/**
158 * @return the Ip as a string.
159 */
[9306]160std::string IP::ipString() const
161{
162  return IP::ipToString(this->_ip);
163}
164
165
[9307]166/**
167 * @brief converts an IPaddress struct into a String.
168 * @param ipaddr the IP address as a SDL_net struct.
169 * @return the string retrieved from the IP.
170 */
[9305]171std::string IP::ipToString(const IPaddress& ipaddr)
172{
173  int ip = SDLNet_Read32 (ipaddr.host);
[9307]174  return ipToString(ip, ipaddr.port);
[7899]175}
[9304]176
[9305]177
[9307]178/**
[9310]179 * @brief converts a IP into a String (without port).
[9307]180 * @param ip the IP to put into the string.
181 * @param port -1 if not wanted
182 * @return the string of the ip.
183 */
184std::string IP::ipToString(int ip, int port)
[9304]185{
[9306]186  MultiType part0((int) (ip & 0xFF000000) >> 24);
187  MultiType part1((int) (ip & 0x00FF0000) >> 16);
188  MultiType part2((int) (ip & 0x0000FF00) >>  8);
189  MultiType part3((int) (ip & 0x000000FF) );
[9304]190
[9307]191  std::string addr = part0.getString() + "." + part1.getString() + "." +
192                     part2.getString() + "." + part3.getString();
[9305]193
[9307]194  if (port != -1)
195    addr += MultiType(port).getString();
196  return addr;
[9304]197}
198
Note: See TracBrowser for help on using the repository browser.