Thu, 11 Dec 2014 16:17:35 +0200
- now with more license headers
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 | 31 | #include <sys/socket.h> |
32 | #include <sys/types.h> | |
33 | #include <sys/time.h> | |
34 | #include <netinet/in.h> | |
35 | #include <string.h> | |
36 | #include <fcntl.h> | |
6 | 37 | #include "udpsocket.h" |
8
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
38 | #include "../huffman/huffman.h" |
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
39 | |
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
40 | static unsigned char g_huffmanBuffer[131072]; |
5 | 41 | |
42 | // ----------------------------------------------------------------------------- | |
43 | // | |
44 | UDPSocket::UDPSocket() : | |
45 | m_socket (socket (AF_INET, SOCK_DGRAM, 0)) {} | |
46 | ||
47 | // ----------------------------------------------------------------------------- | |
48 | // | |
49 | UDPSocket::~UDPSocket() {} | |
50 | ||
51 | // ------------------------------------------------------------------------------------------------- | |
52 | // | |
53 | METHOD | |
54 | UDPSocket::set_blocking (bool a) -> bool | |
55 | { | |
56 | int flags = fcntl (m_socket, F_GETFL, 0); | |
8
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
57 | int newflags = (a ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK)); |
5 | 58 | |
59 | if (flags < 0 || fcntl (m_socket, F_SETFL, newflags) != 0) | |
60 | { | |
61 | m_error = "Unable to set socket as non-blocking"; | |
62 | return false; | |
63 | } | |
64 | ||
65 | return true; | |
66 | } | |
67 | ||
68 | // ------------------------------------------------------------------------------------------------- | |
69 | // | |
70 | METHOD | |
71 | UDPSocket::bind (unsigned short port) -> bool | |
72 | { | |
73 | struct sockaddr_in svaddr; | |
74 | memset (&svaddr, 0, sizeof svaddr); | |
75 | svaddr.sin_family = AF_INET; | |
76 | svaddr.sin_port = htons (port); | |
77 | svaddr.sin_addr.s_addr = htonl (INADDR_ANY); | |
78 | ||
79 | if (::bind (m_socket, reinterpret_cast<struct sockaddr*> (&svaddr), sizeof svaddr) == -1) | |
80 | { | |
81 | m_error = String ("Couldn't bind to port ") + String::from_number (port); | |
82 | return false; | |
83 | } | |
84 | ||
85 | return true; | |
86 | } | |
87 | ||
88 | // ------------------------------------------------------------------------------------------------- | |
89 | // | |
90 | METHOD | |
91 | UDPSocket::read (Datagram& datagram) -> bool | |
92 | { | |
93 | sockaddr_in claddr; | |
94 | socklen_t socklen = sizeof claddr; | |
95 | static unsigned char packet[MAX_DATAGRAM_LENGTH]; | |
8
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
96 | int length = ::recvfrom (m_socket, g_huffmanBuffer, sizeof packet, 0, |
5 | 97 | reinterpret_cast<struct sockaddr*> (&claddr), &socklen); |
98 | ||
99 | if (length == -1) | |
100 | { | |
101 | // We got an error, though EWOULDBLOCK is silent as it means no packets recieved. | |
102 | if (errno != EWOULDBLOCK) | |
103 | m_error.sprintf ("recvfrom error: %s", strerror (errno));; | |
104 | ||
105 | return false; | |
106 | } | |
107 | ||
8
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
108 | int decodedlength = sizeof g_huffmanBuffer; |
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
109 | HUFFMAN_Decode (g_huffmanBuffer, packet, length, &decodedlength); |
5 | 110 | datagram.from.host = ntohl (claddr.sin_addr.s_addr); |
111 | datagram.from.port = ntohs (claddr.sin_port); | |
8
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
112 | datagram.data = Bytestream (packet, decodedlength); |
5 | 113 | return true; |
114 | } | |
115 | ||
116 | // ------------------------------------------------------------------------------------------------- | |
117 | // | |
118 | METHOD | |
8
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
119 | UDPSocket::send (const IPAddress& address, const Bytestream& data) -> bool |
5 | 120 | { |
8
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
121 | int encodedlength = sizeof g_huffmanBuffer; |
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
122 | HUFFMAN_Encode (data.data(), g_huffmanBuffer, data.written_length(), &encodedlength); |
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
123 | struct sockaddr_in claddr = address.to_sockaddr_in(); |
8b697d30c49f
- added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
124 | int res = ::sendto (m_socket, g_huffmanBuffer, encodedlength, 0, |
5 | 125 | reinterpret_cast<struct sockaddr*> (&claddr), sizeof claddr); |
126 | ||
127 | if (res == -1) | |
128 | { | |
129 | m_error = String ("Unable to launch packet: ") + strerror (errno); | |
130 | return false; | |
131 | } | |
132 | ||
133 | return true; | |
134 | } |