Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

comments

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