Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

ip is ready for deplyment

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