Thu, 23 Jul 2015 00:16:47 +0300
some work on MSVC support (still doesn't work yet...)
9
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
1 | /* |
73
07dda51a7a8e
Update license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
24
diff
changeset
|
2 | Copyright 2014, 2015 Teemu Piippo |
9
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 | |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
31 | #include "ipaddress.h" |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
32 | |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
33 | #ifndef _WIN32 |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
34 | # include <netinet/in.h> |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
35 | # include <netdb.h> |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
36 | #else |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
37 | # include <winsock2.h> |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
38 | # include <ws2tcpip.h> |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
39 | #endif |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
40 | |
5 | 41 | #include <stdio.h> |
42 | #include <stdlib.h> | |
43 | #include <string.h> | |
44 | #include <time.h> | |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
45 | |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
46 | #ifdef _WIN32 |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
47 | using AddrInfo = ADDRINFOA; |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
48 | #else |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
49 | using AddrInfo = struct addrinfo; |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
50 | #endif |
5 | 51 | |
52 | // ----------------------------------------------------------------------------- | |
53 | // | |
54 | IPAddress::IPAddress() : | |
55 | host (0), | |
56 | port (0) {} | |
57 | ||
58 | // ----------------------------------------------------------------------------- | |
59 | // | |
60 | IPAddress::IPAddress (unsigned long host, unsigned short port) : | |
61 | host (host), | |
62 | port (port) {} | |
63 | ||
64 | // ----------------------------------------------------------------------------- | |
65 | // | |
66 | IPAddress::IPAddress (const IPAddress& other) : | |
67 | host (other.host), | |
68 | port (other.port) {} | |
69 | ||
70 | // ----------------------------------------------------------------------------- | |
71 | // | |
72 | METHOD | |
73 | IPAddress::to_string (WithPort withport) const -> String | |
74 | { | |
75 | String val; | |
76 | ||
77 | if (withport == IP_WITH_PORT) | |
78 | val.sprintf ("%u.%u.%u.%u:%u", octet (0), octet (1), octet (2), octet (3), port); | |
79 | else | |
80 | val.sprintf ("%u.%u.%u.%u", octet (0), octet (1), octet (2), octet (3)); | |
81 | ||
82 | return val; | |
83 | } | |
84 | ||
85 | // ----------------------------------------------------------------------------- | |
86 | // | |
87 | METHOD | |
88 | IPAddress::octet (int n) const -> unsigned char | |
89 | { | |
90 | return (host >> ((3 - n) * 8)) & 0xFF; | |
91 | } | |
92 | ||
93 | // ----------------------------------------------------------------------------- | |
94 | // | |
95 | METHOD | |
96 | IPAddress::set_octet (int n, unsigned char oct) -> void | |
97 | { | |
98 | // TODO: make a big-endian version | |
99 | host &= ~(0xFF << (3 - n) * 8); | |
100 | host |= oct << ((3 - n) * 8); | |
101 | } | |
102 | ||
103 | // ----------------------------------------------------------------------------- | |
104 | // | |
105 | METHOD | |
106 | IPAddress::compare (const IPAddress& other) const -> bool | |
107 | { | |
108 | for (int i = 0; i < 4; ++i) | |
109 | { | |
110 | if (octet (i) != other.octet (i)) | |
111 | return false; | |
112 | } | |
113 | ||
114 | if (port != 0 | |
115 | and other.port != 0 | |
116 | and port != other.port) | |
117 | { | |
118 | return false; | |
119 | } | |
120 | ||
121 | return true; | |
122 | } | |
123 | ||
124 | // ----------------------------------------------------------------------------- | |
125 | // | |
126 | METHOD | |
127 | IPAddress::operator< (const IPAddress& other) const -> bool | |
128 | { | |
129 | for (int i = 0; i < 4; ++i) | |
130 | { | |
131 | if (octet (i) != other.octet (i)) | |
132 | return octet (i) < other.octet (i); | |
133 | } | |
134 | ||
135 | return port < other.port; | |
136 | } | |
137 | ||
138 | // ----------------------------------------------------------------------------- | |
139 | // | |
140 | METHOD | |
141 | IPAddress::to_sockaddr_in() const -> sockaddr_in | |
142 | { | |
143 | sockaddr_in claddr; | |
144 | memset (&claddr, 0, sizeof claddr); | |
145 | claddr.sin_addr.s_addr = htonl (host); | |
146 | claddr.sin_port = htons (port); | |
147 | claddr.sin_family = AF_INET; | |
148 | return claddr; | |
149 | } | |
150 | ||
151 | // ----------------------------------------------------------------------------- | |
152 | // | |
153 | STATIC METHOD | |
24
e651d02802c0
- the interface is now able to connect to arbitrary hosts (uses ^N to start the prompt) instead of the hardcoded localhost:10666
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
154 | IPAddress::from_string (String input) -> IPAddress |
5 | 155 | { |
156 | unsigned int parts[4]; | |
157 | int colonpos = input.find (":"); | |
158 | String addressString = colonpos == -1 ? input : input.mid (0, colonpos); | |
159 | IPAddress value; | |
160 | ||
7
01e4e9ae323a
- fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
161 | // Try scanf the IPv4 host first |
5 | 162 | if (sscanf (addressString, "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3])) |
163 | { | |
164 | for (short i = 0; i < 4; ++i) | |
165 | value.set_octet (i, parts[i]); | |
166 | } | |
167 | else | |
168 | { | |
7
01e4e9ae323a
- fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
169 | // Possibly a hostname, try resolve it |
24
e651d02802c0
- the interface is now able to connect to arbitrary hosts (uses ^N to start the prompt) instead of the hardcoded localhost:10666
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
170 | value = IPAddress::resolve (addressString); |
5 | 171 | } |
172 | ||
173 | if (colonpos != -1) | |
7
01e4e9ae323a
- fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents:
5
diff
changeset
|
174 | value.port = input.mid (colonpos + 1, -1).to_int(); |
5 | 175 | |
176 | return value; | |
177 | } | |
178 | ||
179 | // ----------------------------------------------------------------------------- | |
180 | // | |
181 | STATIC METHOD | |
24
e651d02802c0
- the interface is now able to connect to arbitrary hosts (uses ^N to start the prompt) instead of the hardcoded localhost:10666
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
182 | IPAddress::resolve (String node) -> IPAddress |
5 | 183 | { |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
184 | AddrInfo hints; |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
185 | AddrInfo* lookup; |
5 | 186 | memset (&hints, 0, sizeof hints); |
187 | hints.ai_family = AF_INET; | |
188 | ||
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
189 | if (getaddrinfo (node, nullptr, &hints, &lookup) != 0) |
24
e651d02802c0
- the interface is now able to connect to arbitrary hosts (uses ^N to start the prompt) instead of the hardcoded localhost:10666
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
190 | throw StringParseError ("unknown host " + node); |
5 | 191 | |
192 | IPAddress result; | |
193 | assert (lookup != nullptr); | |
19
2046a1651c0b
- removed needless 'struct's
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
194 | sockaddr_in* addr = reinterpret_cast<sockaddr_in*> (lookup[0].ai_addr); |
5 | 195 | result.host = ntohl (addr->sin_addr.s_addr); |
196 | result.port = ntohs (addr->sin_port); | |
197 | freeaddrinfo (lookup); | |
198 | return result; | |
199 | } |