Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9379 was 9360, checked in by patrick, 18 years ago

network ip synchronization work

File size: 5.4 KB
Line 
1/**
2 * @file ip.cc
3 * @brief A IP class, that handles IP names and resolution.
4 *
5 * code taken from audiere.
6 *
7 *
8   orxonox - the future of 3D-vertical-scrollers
9
10   Copyright (C) 2004 orx
11
12   This program is free software; you can redistribute it and/or modify
13   it under the terms of the GNU General Public License as published by
14   the Free Software Foundation; either version 2, or (at your option)
15   any later version.
16
17### File Specific:
18   main-programmer: Benjamin Grauer
19 * co-programmer: Patrick Boenzli
20 */
21
22#include "ip.h"
23
24#include <iostream>
25
26#include "multi_type.h"
27#include "substring.h"
28
29/**
30 * @brief default constructor
31 */
32IP::IP()
33{
34  this->_ip = 0;
35  this->_port = IP::_defaultPort;
36}
37
38
39/**
40 * @brief constructor from ip and port
41 * @param ip the IP
42 * @param port the Port
43 * @return self
44 */
45IP::IP(int ip, int port)
46{
47  this->_ip = ip;
48  if (port != -1)
49    this->_port = port;
50  else
51    this->_port = IP::_defaultPort;
52}
53
54
55/**
56 * @brief constructor from a String
57 * @param ip the IP as a String.
58 * @param resolve if true, the IP is resolved via DNS,
59 * @return self
60 */
61IP::IP(const std::string& ip, bool resolve)
62{
63  *this = IP::stringToIP(ip, IP::_defaultPort, resolve);
64}
65
66/**
67 * @brief constructor from a String
68 * @param ip the IP as a String.
69 * @param port The port to be resolved.
70 * @param resolve if true, the IP is resolved via DNS,
71 * @return self
72 */
73IP::IP(const std::string& ip, int port, bool resolve)
74{
75  *this = IP::stringToIP(ip, port, resolve);
76}
77
78
79/**
80 * @brief constructor from an SDL net structure IPaddress
81 * @param ip the ip.
82 * @return self
83 */
84IP::IP(const IPaddress& ip)
85{
86  this->_ip = ip.host;
87  this->_port = ip.port;
88}
89
90
91/**
92 * @brief copy constructor.
93 * @param ip the IP to copy.
94 * @return self
95 */
96IP::IP(const IP& ip)
97{
98  *this = ip;
99}
100
101/**
102 * @brief copy operator
103 * @param ip the IP to copy.
104 * @return self.
105 */
106const IP& IP::operator=(const IP& ip)
107{
108  this->_ip = ip.host();
109  this->_port = ip.port();
110  return *this;
111}
112
113
114/**
115 * @brief copy operator
116 * @param ip the IP to copy.
117 * @return self.
118 */
119const IP& IP::operator=(const IPaddress& ip)
120{
121  this->_ip = ip.host;
122  this->_port = ip.port;
123  return *this;
124}
125
126
127/**
128 * @brief comparison operator
129 * @param ip the IP to compare
130 * @return true if ip _and_ port match.
131 */
132bool IP::operator==(const IP& ip)
133{
134  return (this->_ip == ip.host() &&
135          this->_port == ip.port());
136}
137
138
139/**
140 * @brief comparison operator
141 * @param ip the IP to compare
142 * @return true if ip _and_ port do not match.
143 */
144bool IP::operator!=(const IP& ip)
145{
146  return (this->_ip != ip.host() ||
147      this->_port != ip.port());
148}
149
150
151/**
152 * @brief converts a String into an IP Object.
153 * @param ip The string holding an IP.
154 * @param port The port to be resolved.
155 * @param resolve if true, the IP is resolved via DNS,
156 * otherwise (resolve == false) the IP is being transformed
157 * from a String (xxx.xxx.xxx.xxx) to an integer.
158 *
159 * @return A newly constructed IP.
160 */
161IP IP::stringToIP(const std::string& ip, int port, bool resolve)
162{
163  if (resolve)
164  {
165    IPaddress ipaddr;
166
167    SDLNet_ResolveHost(&ipaddr, NULL, 2000);
168
169    return IP(ipaddr.host, port);
170  }
171  else
172  {
173    SubString ipaddr(ip, '.');
174    if(ip.size() != 4 )
175      return IP();
176
177    MultiType part0(ipaddr[0]);
178    MultiType part1(ipaddr[1]);
179    MultiType part2(ipaddr[2]);
180    MultiType part3(ipaddr[3]);
181
182    int addr = (part0.getInt() << 24) +
183               (part1.getInt() << 16) +
184               (part2.getInt() <<  8) +
185               part3.getInt();
186    return IP(addr, port);
187  }
188}
189
190
191/**
192 * @brief if you want to have a specific part of an IP
193 * @param part the n'th part of the IP addr (splitted by '.').
194 * @return the amount held in the designated part.
195 */
196int IP::ipPart(unsigned int part) const
197{
198  switch (part)
199  {
200    case 0:
201      return  (_ip & 0xFF000000) >> 24;
202    case 1:
203      return  (_ip & 0x00FF0000) >> 16;
204    case 2:
205      return  (_ip & 0x0000FF00) >> 8;
206    case 3:
207      return  (_ip & 0x000000FF);
208    default:
209      return -1;
210  }
211}
212
213
214/**
215 * @return the Ip as a string.
216 */
217std::string IP::ipString() const
218{
219  return IP::ipToString(this->_ip, this->_port);
220}
221
222
223/**
224 * @brief converts an IPaddress struct into a String.
225 * @param ipaddr the IP address as a SDL_net struct.
226 * @return the string retrieved from the IP.
227 */
228std::string IP::ipToString(const IPaddress& ipaddr)
229{
230  int ip = SDLNet_Read32 (ipaddr.host);
231  return IP::ipToString(ip, ipaddr.port);
232}
233
234
235/**
236 * @brief converts a IP into a String (without port).
237 * @param ip the IP to put into the string.
238 * @param port -1 if not wanted
239 * @return the string of the ip.
240 */
241std::string IP::ipToString(int ip, int port)
242{
243  MultiType part0((int) (ip & 0xFF000000) >> 24);
244  MultiType part1((int) (ip & 0x00FF0000) >> 16);
245  MultiType part2((int) (ip & 0x0000FF00) >>  8);
246  MultiType part3((int) (ip & 0x000000FF) );
247
248  std::string addr = part3.getString() + "." + part2.getString() + "." +
249                     part1.getString() + "." + part0.getString();
250
251  if (port != -1)
252    addr += ":" + MultiType(port).getString();
253  return addr;
254}
255
256
257/// default port definition.
258int IP::_defaultPort = 9999;
259
260/**
261 * @brief if no IP is supplied this port is used, so that IP can be resolved usefully.
262 * @param defaultPort The default port.
263 */
264void IP::setDefaultPort(int defaultPort)
265{
266  IP::_defaultPort = defaultPort;
267}
268
269
270/**
271 * @brief print out the IP in a nice fashion
272 */
273void IP::debug() const
274{
275  std::cout << "IP is " << this->ipString() << std::endl;
276}
Note: See TracBrowser for help on using the repository browser.