Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/scriptchanges.new/src/lib/network/ip.cc @ 10514

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

merged the proxy back

File size: 5.9 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->_host = 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->_host = 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->_host = 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/**
103 * @brief constructs an IP out of the four pairs and a Port.
104 * @param first The first octal.
105 * @param second The second octal.
106 * @param third The third octal.
107 * @param fourth The fourth octal.
108 * @param port The Port.
109 * @return self.
110 */
111IP::IP(unsigned int first, unsigned int second, unsigned int third, unsigned int fourth, int port)
112{
113  this->_host = (fourth << 24) +
114                (third << 16) +
115                (second <<  8) +
116                (first);
117  this->_port = port;
118}
119
120
121/**
122 * @brief copy operator
123 * @param ip the IP to copy.
124 * @return self.
125 */
126const IP& IP::operator=(const IP& ip)
127{
128  this->_host = ip.host();
129  this->_port = ip.port();
130  return *this;
131}
132
133
134/**
135 * @brief copy operator
136 * @param ip the IP to copy.
137 * @return self.
138 */
139const IP& IP::operator=(const IPaddress& ip)
140{
141  this->_host = ip.host;
142  this->_port = ip.port;
143  return *this;
144}
145
146
147/**
148 * @brief comparison operator
149 * @param ip the IP to compare
150 * @return true if ip _and_ port match.
151 */
152bool IP::operator==(const IP& ip) const
153{
154  return (this->_host == ip.host() &&
155          this->_port == ip.port());
156}
157
158
159/**
160 * @brief comparison operator
161 * @param ip the IP to compare
162 * @return true if ip _or_ port do not match.
163 */
164bool IP::operator!=(const IP& ip) const
165{
166  return (this->_host != ip.host() ||
167          this->_port != ip.port());
168}
169
170
171/**
172 * @brief converts a String into an IP Object.
173 * @param ip The string holding an IP.
174 * @param port The port to be resolved.
175 * @param resolve if true, the IP is resolved via DNS,
176 * otherwise (resolve == false) the IP is being transformed
177 * from a String (xxx.xxx.xxx.xxx) to an integer.
178 *
179 * @return A newly constructed IP.
180 */
181IP IP::stringToIP(const std::string& ip, int port, bool resolve)
182{
183  if (resolve)
184  {
185    IPaddress ipaddr;
186
187    SDLNet_ResolveHost(&ipaddr, ip.c_str(), port);
188
189    return IP(ipaddr);
190  }
191  else
192  {
193    SubString ipaddr(ip, '.');
194    if(ip.size() != 4 )
195      return IP();
196
197    MultiType part0(ipaddr[0]);
198    MultiType part1(ipaddr[1]);
199    MultiType part2(ipaddr[2]);
200    MultiType part3(ipaddr[3]);
201
202    int addr = (part0.getInt() << 24) +
203               (part1.getInt() << 16) +
204               (part2.getInt() <<  8) +
205               part3.getInt();
206    return IP(addr, port);
207  }
208}
209
210
211/**
212 * @brief if you want to have a specific part of an IP
213 * @param part the n'th part of the IP addr (splitted by '.').
214 * @return the amount held in the designated part.
215 */
216int IP::ipPart(unsigned int part) const
217{
218  switch (part)
219  {
220    case 0:
221      return  (_host & 0xFF000000) >> 24;
222    case 1:
223      return  (_host & 0x00FF0000) >> 16;
224    case 2:
225      return  (_host & 0x0000FF00) >> 8;
226    case 3:
227      return  (_host & 0x000000FF);
228    default:
229      return -1;
230  }
231}
232
233
234/**
235 * @return the Ip as a string.
236 */
237std::string IP::ipString() const
238{
239  return IP::ipToString(this->_host, this->_port);
240}
241
242
243/**
244 * @brief converts an IPaddress struct into a String.
245 * @param ipaddr the IP address as a SDL_net struct.
246 * @return the string retrieved from the IP.
247 */
248std::string IP::ipToString(const IPaddress& ipaddr)
249{
250  int ip = SDLNet_Read32 (ipaddr.host);
251  return IP::ipToString(ip, ipaddr.port);
252}
253
254
255/**
256 * @brief converts a IP into a String (without port).
257 * @param host the IP to put into the string.
258 * @param port -1 if not wanted
259 * @return the string of the ip.
260 */
261std::string IP::ipToString(int host, int port)
262{
263  MultiType part0((int) ((host & 0xFF000000) >> 24));
264  MultiType part1((int) ((host & 0x00FF0000) >> 16));
265  MultiType part2((int) ((host & 0x0000FF00) >>  8));
266  MultiType part3((int) ((host & 0x000000FF) ));
267
268  std::string addr = part3.getString() + "." + part2.getString() + "." +
269                     part1.getString() + "." + part0.getString();
270
271  return addr;
272}
273
274
275/// default port definition.
276int IP::_defaultPort = 9999;
277
278/**
279 * @brief if no IP is supplied this port is used, so that IP can be resolved usefully.
280 * @param defaultPort The default port.
281 */
282void IP::setDefaultPort(int defaultPort)
283{
284  IP::_defaultPort = defaultPort;
285}
286
287
288/**
289 * @brief print out the IP in a nice fashion
290 */
291void IP::debug() const
292{
293  std::cout << "IP is " << this->ipString() << std::endl;
294}
Note: See TracBrowser for help on using the repository browser.