Wed, 20 Jul 2016 18:24:13 +0300
Merged with default
9
e7a09ceb4505
- now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
1 | /* |
109 | 2 | Copyright 2014 - 2016 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 "udpsocket.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 <sys/socket.h> |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
35 | # include <netinet/in.h> |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
36 | # include <sys/time.h> |
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
37 | # include <unistd.h> |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
38 | #else |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
39 | # include <winsock2.h> |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
40 | # include <ws2tcpip.h> |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
41 | #endif |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
42 | |
5 | 43 | #include <sys/types.h> |
44 | #include <string.h> | |
45 | #include <fcntl.h> | |
8
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
46 | #include "../huffman/huffman.h" |
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
47 | |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
48 | BEGIN_ZFC_NAMESPACE |
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
49 | |
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
50 | char UDPSocket::HuffmanBuffer[131072]; |
5 | 51 | |
52 | // ----------------------------------------------------------------------------- | |
53 | // | |
54 | UDPSocket::UDPSocket() : | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
55 | m_socket (socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) {} |
5 | 56 | |
57 | // ----------------------------------------------------------------------------- | |
58 | // | |
56
b4caacf567af
- I think closing sockets is a good idea
Teemu Piippo <crimsondusk64@gmail.com>
parents:
31
diff
changeset
|
59 | UDPSocket::~UDPSocket() |
b4caacf567af
- I think closing sockets is a good idea
Teemu Piippo <crimsondusk64@gmail.com>
parents:
31
diff
changeset
|
60 | { |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
61 | #ifdef _WIN32 |
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
62 | closesocket (m_socket); |
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
63 | #else |
56
b4caacf567af
- I think closing sockets is a good idea
Teemu Piippo <crimsondusk64@gmail.com>
parents:
31
diff
changeset
|
64 | close (m_socket); |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
65 | #endif |
56
b4caacf567af
- I think closing sockets is a good idea
Teemu Piippo <crimsondusk64@gmail.com>
parents:
31
diff
changeset
|
66 | } |
5 | 67 | |
68 | // ------------------------------------------------------------------------------------------------- | |
69 | // | |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
70 | bool UDPSocket::set_blocking (bool a) |
5 | 71 | { |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
72 | #ifndef _WIN32 |
5 | 73 | int flags = fcntl (m_socket, F_GETFL, 0); |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
74 | int newflags = a ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); |
5 | 75 | |
76 | if (flags < 0 || fcntl (m_socket, F_SETFL, newflags) != 0) | |
77 | { | |
78 | m_error = "Unable to set socket as non-blocking"; | |
79 | return false; | |
80 | } | |
81 | ||
82 | return true; | |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
83 | #else |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
84 | unsigned long mode = a ? 0 : 1; |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
85 | |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
86 | if (ioctlsocket (m_socket, FIONBIO, &mode) != 0) |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
87 | { |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
88 | m_error = strerror (errno); |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
89 | return false; |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
90 | } |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
91 | |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
92 | return true; |
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
93 | #endif |
5 | 94 | } |
95 | ||
96 | // ------------------------------------------------------------------------------------------------- | |
97 | // | |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
98 | bool UDPSocket::bind (unsigned short port) |
5 | 99 | { |
19
2046a1651c0b
- removed needless 'struct's
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
100 | sockaddr_in svaddr; |
5 | 101 | memset (&svaddr, 0, sizeof svaddr); |
102 | svaddr.sin_family = AF_INET; | |
103 | svaddr.sin_port = htons (port); | |
104 | svaddr.sin_addr.s_addr = htonl (INADDR_ANY); | |
105 | ||
19
2046a1651c0b
- removed needless 'struct's
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
106 | if (::bind (m_socket, reinterpret_cast<sockaddr*> (&svaddr), sizeof svaddr) == -1) |
5 | 107 | { |
145
d0aedc9be448
Renamed String methods, and reformatted mystring.h
Teemu Piippo <teemu@compsta2.com>
parents:
109
diff
changeset
|
108 | m_error = String ("Couldn't bind to port ") + String::fromNumber (port); |
5 | 109 | return false; |
110 | } | |
111 | ||
112 | return true; | |
113 | } | |
114 | ||
115 | // ------------------------------------------------------------------------------------------------- | |
116 | // | |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
117 | bool UDPSocket::read (Datagram& datagram) |
5 | 118 | { |
119 | sockaddr_in claddr; | |
120 | socklen_t socklen = sizeof claddr; | |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
121 | int length = ::recvfrom (m_socket, HuffmanBuffer, sizeof HuffmanBuffer, 0, |
19
2046a1651c0b
- removed needless 'struct's
Teemu Piippo <crimsondusk64@gmail.com>
parents:
10
diff
changeset
|
122 | reinterpret_cast<sockaddr*> (&claddr), &socklen); |
5 | 123 | |
124 | if (length == -1) | |
125 | { | |
126 | if (errno != EWOULDBLOCK) | |
31
b5b5a6a96d91
- added input history (use up/down to navigate)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
19
diff
changeset
|
127 | m_error = String ("recvfrom error: ") + strerror (errno); |
5 | 128 | |
129 | return false; | |
130 | } | |
131 | ||
130
9f54db6f9922
Commit ZFC's end of reliable RCON streams
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
132 | if (length < 4) |
9f54db6f9922
Commit ZFC's end of reliable RCON streams
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
133 | { |
9f54db6f9922
Commit ZFC's end of reliable RCON streams
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
134 | m_error = "The server sent a too short packet"; |
9f54db6f9922
Commit ZFC's end of reliable RCON streams
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
135 | return false; |
9f54db6f9922
Commit ZFC's end of reliable RCON streams
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
136 | } |
9f54db6f9922
Commit ZFC's end of reliable RCON streams
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
137 | |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
138 | unsigned char decodedPacket[MAX_DATAGRAM_LENGTH]; |
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
139 | int decodedLength = sizeof decodedPacket; |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
140 | HUFFMAN_Decode (reinterpret_cast<unsigned char*> (HuffmanBuffer), |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
141 | decodedPacket, length, &decodedLength); |
152
6be21be4bea1
Renamed Datagram members, RCONSession::handle_packet now takes the datagram as the parameter.
Teemu Piippo <teemu@compsta2.com>
parents:
145
diff
changeset
|
142 | datagram.address.host = ntohl (claddr.sin_addr.s_addr); |
6be21be4bea1
Renamed Datagram members, RCONSession::handle_packet now takes the datagram as the parameter.
Teemu Piippo <teemu@compsta2.com>
parents:
145
diff
changeset
|
143 | datagram.address.port = ntohs (claddr.sin_port); |
6be21be4bea1
Renamed Datagram members, RCONSession::handle_packet now takes the datagram as the parameter.
Teemu Piippo <teemu@compsta2.com>
parents:
145
diff
changeset
|
144 | datagram.message = Bytestream (decodedPacket, decodedLength); |
5 | 145 | return true; |
146 | } | |
147 | ||
148 | // ------------------------------------------------------------------------------------------------- | |
149 | // | |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
150 | bool UDPSocket::send (const IPAddress& address, const Bytestream& data) |
5 | 151 | { |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
152 | int encodedlength = sizeof HuffmanBuffer; |
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
153 | HUFFMAN_Encode (data.data(), reinterpret_cast<unsigned char*> (HuffmanBuffer), |
81
a18aaf460648
Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
154 | data.written_length(), &encodedlength); |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
155 | sockaddr_in claddr = address.to_sockaddr_in(); |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
156 | int res = ::sendto (m_socket, HuffmanBuffer, encodedlength, 0, |
10
3874575d924d
- begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
9
diff
changeset
|
157 | reinterpret_cast<sockaddr*> (&claddr), sizeof claddr); |
5 | 158 | |
159 | if (res == -1) | |
160 | { | |
161 | m_error = String ("Unable to launch packet: ") + strerror (errno); | |
162 | return false; | |
163 | } | |
164 | ||
165 | return true; | |
166 | } | |
88
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
167 | |
08ccaf26cffd
Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents:
81
diff
changeset
|
168 | END_ZFC_NAMESPACE |