sources/network/ipaddress.cpp

Sat, 13 Dec 2014 04:32:15 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 13 Dec 2014 04:32:15 +0200
changeset 12
8d0d1b368de0
parent 10
3874575d924d
child 19
2046a1651c0b
permissions
-rw-r--r--

- rcon session now works! woo!

9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
1 /*
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
2 Copyright 2014 Teemu Piippo
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
3 All rights reserved.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
4
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
5 Redistribution and use in source and binary forms, with or without
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
6 modification, are permitted provided that the following conditions
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
7 are met:
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
8
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
9 1. Redistributions of source code must retain the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
10 notice, this list of conditions and the following disclaimer.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
12 notice, this list of conditions and the following disclaimer in the
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
13 documentation and/or other materials provided with the distribution.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
14 3. Neither the name of the copyright holder nor the names of its
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
15 contributors may be used to endorse or promote products derived from
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
16 this software without specific prior written permission.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
17
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
29 */
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
30
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 #include <stdlib.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include <string.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 #include <time.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 #include <netinet/in.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 #include <netdb.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 #include "ipaddress.h"
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 bool IPAddress::sink;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 IPAddress::IPAddress() :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 host (0),
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 port (0) {}
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 IPAddress::IPAddress (unsigned long host, unsigned short port) :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 host (host),
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 port (port) {}
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 IPAddress::IPAddress (const IPAddress& other) :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 host (other.host),
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 port (other.port) {}
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 IPAddress::to_string (WithPort withport) const -> String
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 String val;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 if (withport == IP_WITH_PORT)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 val.sprintf ("%u.%u.%u.%u:%u", octet (0), octet (1), octet (2), octet (3), port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 else
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 val.sprintf ("%u.%u.%u.%u", octet (0), octet (1), octet (2), octet (3));
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 return val;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 IPAddress::octet (int n) const -> unsigned char
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 return (host >> ((3 - n) * 8)) & 0xFF;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 IPAddress::set_octet (int n, unsigned char oct) -> void
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 // TODO: make a big-endian version
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 host &= ~(0xFF << (3 - n) * 8);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 host |= oct << ((3 - n) * 8);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 IPAddress::compare (const IPAddress& other) const -> bool
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 for (int i = 0; i < 4; ++i)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 if (octet (i) != other.octet (i))
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 if (port != 0
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 and other.port != 0
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 and port != other.port)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 return true;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 IPAddress::operator< (const IPAddress& other) const -> bool
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 for (int i = 0; i < 4; ++i)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 if (octet (i) != other.octet (i))
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 return octet (i) < other.octet (i);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 return port < other.port;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 IPAddress::to_sockaddr_in() const -> sockaddr_in
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 sockaddr_in claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 memset (&claddr, 0, sizeof claddr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 claddr.sin_addr.s_addr = htonl (host);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 claddr.sin_port = htons (port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 claddr.sin_family = AF_INET;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 return claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 STATIC METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 IPAddress::from_string (String input, bool* ok) -> IPAddress
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 unsigned int parts[4];
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 int colonpos = input.find (":");
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 String addressString = colonpos == -1 ? input : input.mid (0, colonpos);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 IPAddress value;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149
7
01e4e9ae323a - fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
150 // Try scanf the IPv4 host first
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 if (sscanf (addressString, "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3]))
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 for (short i = 0; i < 4; ++i)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 value.set_octet (i, parts[i]);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 else
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 {
7
01e4e9ae323a - fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
158 // Possibly a hostname, try resolve it
01e4e9ae323a - fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
159 value = IPAddress::resolve (addressString, ok);
01e4e9ae323a - fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
160
01e4e9ae323a - fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
161 if (*ok == false)
01e4e9ae323a - fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
162 return value;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 if (colonpos != -1)
7
01e4e9ae323a - fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
166 value.port = input.mid (colonpos + 1, -1).to_int();
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 return value;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 STATIC METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 IPAddress::resolve (String node, bool* ok) -> IPAddress
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 struct addrinfo hints;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 struct addrinfo* lookup;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 memset (&hints, 0, sizeof hints);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 hints.ai_family = AF_INET;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
181 if (getaddrinfo (node, nullptr, &hints, &lookup) != 0)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 *ok = false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 return IPAddress();
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 IPAddress result;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 assert (lookup != nullptr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*> (lookup[0].ai_addr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 result.host = ntohl (addr->sin_addr.s_addr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 result.port = ntohs (addr->sin_port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 freeaddrinfo (lookup);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 return result;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 }

mercurial