Initial import
[umurmur.git] / src / messages.h
1 /* Copyright (C) 2009, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2009, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef MESSAGES_H_89768
32 #define MESSAGES_H_89768
33
34 #include <stdint.h>
35 #include <openssl/aes.h>
36 #include "list.h"
37 #include "types.h"
38
39 #define MAX_TEXT 64
40 #define SPEEX_DATA_SIZE 1024
41 #define MESSAGE_STREAM_VERSION 4
42
43 typedef enum {
44         ServerReject,
45         ServerAuthenticate,
46         Speex,
47         ServerSync,
48         ServerJoin,
49         ServerLeave,
50         ServerBanList,
51         PlayerMute,
52         PlayerDeaf,
53         PlayerKick,
54         PlayerRename, /*10 */
55         PlayerBan,
56         PlayerMove,
57         PlayerSelfMuteDeaf,
58         ChannelAdd,
59         ChannelRemove,
60         ChannelMove,
61         ChannelLink,
62         ChannelRename,
63         PermissionDenied,
64         EditACL, /* 20 */
65         QueryUsers,
66         Ping,
67         TextMessage,
68         PlayerTexture,
69         CryptSetup,
70         CryptSync,
71         PingStats,
72         ContextAction,
73         ContextAddAction,
74         ChannelDescUpdate,
75 } messageType_t;
76
77
78 typedef enum {
79         AltSpeak = 0x01,
80         LoopBack = 0x02,
81         EndSpeech = 0x04,
82         FrameCountMask = 0x30
83 } speexflag_t;
84
85 typedef struct {
86         int speexflag;
87         int seq;
88         uint8_t *data;
89         int size;
90 } speex_t;
91
92 typedef struct {
93         int maxBandwidth;
94         char welcomeText[MAX_TEXT];
95 } serverSync_t;
96
97 typedef struct {
98         char playerName[MAX_TEXT];
99         int id;
100 } serverLeave_t;
101
102 typedef enum {
103         None,
104         WrongVersion,
105         InvalidUsername,
106         WrongUserPW,
107         WrongServerPW,
108         UsernameInUse,
109         ServerFull
110 } rejectType_t;
111
112 typedef struct {
113         char reason[MAX_TEXT];
114         rejectType_t type;
115 } serverReject_t;
116
117 typedef struct {
118         int version;
119         char userName[MAX_TEXT];
120         char password[MAX_TEXT];
121 } serverAuthenticate_t;
122
123 typedef struct {
124         int id;
125         int parentId;
126         char name[MAX_TEXT];
127 } channelAdd_t;
128
129 typedef struct {
130         int id;
131         char desc[MAX_TEXT];
132 } channelDescUpdate_t;
133
134 typedef struct {
135         char playerName[MAX_TEXT];
136         int id;
137 } serverJoin_t;
138
139 typedef struct {
140         int victim;
141         int channel;
142 } playerMove_t;
143
144 typedef struct {
145         uint8_t key[AES_BLOCK_SIZE];
146         uint8_t clientNonce[AES_BLOCK_SIZE];
147         uint8_t serverNonce[AES_BLOCK_SIZE];
148 } cryptSetup_t;
149
150 typedef struct {
151         bool_t empty;
152         uint8_t nonce[AES_BLOCK_SIZE];
153 } cryptSync_t;
154
155 typedef struct {
156         uint64_t timestamp;
157 } ping_t;
158
159 typedef struct {
160         uint64_t timestamp;
161         uint32_t good;
162         uint32_t late;
163         uint32_t lost;
164         uint32_t resync;
165         double dUDPPingAvg;
166         double dUDPPingVar;
167         uint32_t UDPPackets;
168         double dTCPPingAvg;
169         double dTCPPingVar;
170         uint32_t TCPPackets;
171 } pingStats_t;
172
173 typedef struct {
174         char reason[MAX_TEXT];
175 } permissionDenied_t;
176
177 typedef struct {
178         uint32_t victim;
179         bool_t bMute;
180 } playerMute_t;
181
182 typedef struct {
183         uint32_t victim;
184         bool_t bDeaf;
185 } playerDeaf_t;
186
187 typedef struct {
188         bool_t bMute;
189         bool_t bDeaf;
190 } playerSelfMuteDeaf_t;
191
192 typedef struct {
193         int32_t victim;
194         int32_t channel;
195         bool_t bTree;
196         char message[MAX_TEXT];
197 } textMessage_t;
198
199 typedef union payload {
200         speex_t speex;
201         serverSync_t serverSync;
202         serverJoin_t serverJoin;
203         serverLeave_t serverLeave;
204         serverReject_t serverReject;
205         serverAuthenticate_t serverAuthenticate;
206         cryptSetup_t cryptSetup;
207         cryptSync_t cryptSync;
208         pingStats_t pingStats;
209         ping_t ping;
210         channelAdd_t channelAdd;
211         channelDescUpdate_t channelDescUpdate;
212         playerMove_t playerMove;
213         permissionDenied_t permissinDenied;
214         playerMute_t playerMute;
215         playerDeaf_t playerDeaf;
216         playerSelfMuteDeaf_t playerSelfMuteDeaf;
217         permissionDenied_t permissionDenied;
218         textMessage_t textMessage;
219 } payload_t;
220
221 typedef struct message {
222         messageType_t messageType;
223         uint32_t sessionId;
224         int refcount;
225         struct dlist node;
226         payload_t payload;
227 } message_t;
228
229
230
231 int Msg_messageToNetwork(message_t *msg, uint8_t *buffer, int bufsize);
232 message_t *Msg_networkToMessage(uint8_t *data, int size);
233 void Msg_free(message_t *msg);
234 void Msg_inc_ref(message_t *msg);
235
236 message_t *Msg_create(messageType_t messageType);
237
238 #endif