Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

minor cleanup

File size: 4.0 KB
Line 
1/**
2 * @file ip.cc
3 * @brief A IP class, that handles all about time.
4 *
5 * code taken from audiere.
6 */
7
8#include "ip.h"
9#include "multi_type.h"
10#include "substring.h"
11
12/**
13 * @brief default constructor
14 */
15IP::IP()
16{
17  this->_ip = 0;
18  this->_port = 0;
19}
20
21
22/**
23 * @brief constructor from ip and port
24 * @param ip the IP
25 * @param port the Port
26 * @return self
27 */
28IP::IP(int ip, int port)
29{
30  this->_ip = ip;
31  this->_port = port;
32}
33
34
35/**
36 * @brief constructor from a String
37 * @param ip the IP as a String.
38 * @param port The port to be resolved.
39 * @param resolve if true, the IP is resolved via DNS,
40 * @return self
41 */
42IP::IP(const std::string& ip, int port, bool resolve)
43{
44  *this = IP::stringToIP(ip, port, resolve);
45}
46
47
48/**
49 * @brief constructor from an SDL net structure IPaddress
50 * @param ip the ip.
51 * @return self
52 */
53IP::IP(const IPaddress& ip)
54{
55  this->_ip = ip.host;
56  this->_port = ip.port;
57}
58
59
60/**
61 * @brief copy constructor.
62 * @param ip the IP to copy.
63 * @return self
64 */
65IP::IP(const IP& ip)
66{
67  this->_ip = ip.ip();
68  this->_port = ip.port();
69}
70
71/**
72 * @brief copy operator
73 * @param ip the IP to copy.
74 * @return self.
75 */
76const IP& IP::operator=(const IP& ip)
77{
78  this->_ip = ip.ip();
79  this->_port = ip.port();
80  return *this;
81}
82
83/**
84 * @brief comparison operator
85 * @param ip the IP to compare
86 * @return true if ip _and_ port match.
87 */
88bool IP::operator==(const IP& ip)
89{
90  return (this->_ip == ip.ip() && this->_port == ip.port());
91}
92
93
94/**
95 * @brief converts a String into an IP Object.
96 * @param ip The string holding an IP.
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.
103 */
104IP IP::stringToIP(const std::string& ip, int port, bool resolve)
105{
106  if (resolve)
107  {
108    IPaddress ipaddr;
109
110    SDLNet_ResolveHost(&ipaddr, NULL, 2000);
111
112    return IP(ipaddr.host, port);
113  }
114  else
115  {
116    SubString ipaddr(ip, '.');
117    if(ip.size() != 4 )
118      return IP();
119
120    MultiType part0(ipaddr[0]);
121    MultiType part1(ipaddr[1]);
122    MultiType part2(ipaddr[2]);
123    MultiType part3(ipaddr[3]);
124
125    int addr = (part0.getInt() << 24) +
126               (part1.getInt() << 16) +
127               (part2.getInt() <<  8) +
128               part3.getInt();
129    return IP(addr, port);
130  }
131}
132
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 */
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
157/**
158 * @return the Ip as a string.
159 */
160std::string IP::ipString() const
161{
162  return IP::ipToString(this->_ip);
163}
164
165
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 */
171std::string IP::ipToString(const IPaddress& ipaddr)
172{
173  int ip = SDLNet_Read32 (ipaddr.host);
174  return ipToString(ip, ipaddr.port);
175}
176
177
178/**
179 * @brief converts a IP into a String (without port).
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)
185{
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) );
190
191  std::string addr = part0.getString() + "." + part1.getString() + "." +
192                     part2.getString() + "." + part3.getString();
193
194  if (port != -1)
195    addr += MultiType(port).getString();
196  return addr;
197}
198
Note: See TracBrowser for help on using the repository browser.