Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

work of the day in one commit:D

  • switched whole network framework to ip sturcture
  • extended ip structrue a little bit
  • reimplemented the disconnection/reconnection algorithm
  • synchronizeable ip bug discovered and solved
File size: 5.7 KB
RevLine 
[7899]1/**
[9304]2 * @file ip.cc
[9321]3 * @brief A IP class, that handles IP names and resolution.
[7899]4 *
5 * code taken from audiere.
[9334]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
[7899]20 */
21
[9304]22#include "ip.h"
[9321]23
24#include <iostream>
25
[9305]26#include "multi_type.h"
[9306]27#include "substring.h"
[7899]28
[9307]29/**
30 * @brief default constructor
31 */
[9304]32IP::IP()
[9306]33{
34  this->_ip = 0;
[9321]35  this->_port = IP::_defaultPort;
[9306]36}
[7899]37
38
[9307]39/**
40 * @brief constructor from ip and port
41 * @param ip the IP
42 * @param port the Port
43 * @return self
44 */
[9306]45IP::IP(int ip, int port)
[9304]46{
47  this->_ip = ip;
[9321]48  if (port != -1)
49    this->_port = port;
50  else
51    this->_port = IP::_defaultPort;
[7909]52}
53
[9306]54
[9307]55/**
56 * @brief constructor from a String
57 * @param ip the IP as a String.
[9321]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.
[9310]69 * @param port The port to be resolved.
70 * @param resolve if true, the IP is resolved via DNS,
[9307]71 * @return self
72 */
[9310]73IP::IP(const std::string& ip, int port, bool resolve)
[9304]74{
[9310]75  *this = IP::stringToIP(ip, port, resolve);
[9304]76}
[7899]77
[7909]78
[9307]79/**
80 * @brief constructor from an SDL net structure IPaddress
81 * @param ip the ip.
82 * @return self
83 */
[9306]84IP::IP(const IPaddress& ip)
85{
86  this->_ip = ip.host;
87  this->_port = ip.port;
88}
[9305]89
90
[9307]91/**
92 * @brief copy constructor.
93 * @param ip the IP to copy.
94 * @return self
95 */
[9306]96IP::IP(const IP& ip)
[9304]97{
[9321]98  *this = ip;
[9306]99}
[7899]100
[9307]101/**
102 * @brief copy operator
103 * @param ip the IP to copy.
104 * @return self.
105 */
[9306]106const IP& IP::operator=(const IP& ip)
107{
108  this->_ip = ip.ip();
109  this->_port = ip.port();
110  return *this;
[9305]111}
[9304]112
[9334]113
[9307]114/**
[9334]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/**
[9307]128 * @brief comparison operator
129 * @param ip the IP to compare
130 * @return true if ip _and_ port match.
131 */
[9306]132bool IP::operator==(const IP& ip)
133{
[9321]134  return (this->_ip == ip.ip() &&
135          this->_port == ip.port());
[9306]136}
137
138
139/**
[9334]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.ip() ||
147      this->_port != ip.port());
148}
149
150
151/**
[9307]152 * @brief converts a String into an IP Object.
153 * @param ip The string holding an IP.
[9310]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.
[9306]160 */
[9310]161IP IP::stringToIP(const std::string& ip, int port, bool resolve)
[9306]162{
[9310]163  if (resolve)
164  {
165    IPaddress ipaddr;
[9306]166
[9310]167    SDLNet_ResolveHost(&ipaddr, NULL, 2000);
[9306]168
[9310]169    return IP(ipaddr.host, port);
170  }
171  else
172  {
173    SubString ipaddr(ip, '.');
174    if(ip.size() != 4 )
175      return IP();
[9306]176
[9310]177    MultiType part0(ipaddr[0]);
178    MultiType part1(ipaddr[1]);
179    MultiType part2(ipaddr[2]);
180    MultiType part3(ipaddr[3]);
[9306]181
[9310]182    int addr = (part0.getInt() << 24) +
183               (part1.getInt() << 16) +
184               (part2.getInt() <<  8) +
185               part3.getInt();
186    return IP(addr, port);
187  }
[9306]188}
189
[9307]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 */
[9306]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
[9307]214/**
215 * @return the Ip as a string.
216 */
[9306]217std::string IP::ipString() const
218{
[9321]219  return IP::ipToString(this->_ip, this->_port);
[9306]220}
221
222
[9307]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 */
[9305]228std::string IP::ipToString(const IPaddress& ipaddr)
229{
230  int ip = SDLNet_Read32 (ipaddr.host);
[9321]231  return IP::ipToString(ip, ipaddr.port);
[7899]232}
[9304]233
[9305]234
[9307]235/**
[9310]236 * @brief converts a IP into a String (without port).
[9307]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)
[9304]242{
[9306]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) );
[9304]247
[9307]248  std::string addr = part0.getString() + "." + part1.getString() + "." +
249                     part2.getString() + "." + part3.getString();
[9305]250
[9307]251  if (port != -1)
[9321]252    addr += ":" + MultiType(port).getString();
[9307]253  return addr;
[9304]254}
255
[9321]256
257/// default port definition.
258short IP::_defaultPort = 80;
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(short 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.