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