Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MKConnection.h « MumbleKit « src - github.com/mumble-voip/mumblekit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b202ab2d0640cb76a2c867f44e7b6738a4292be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright 2009-2012 The MumbleKit Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#import <Security/Security.h>

/**
 * @constant The default MKConnection ping interval.
 */
#define MKConnectionPingInterval 5.0f

@class MKConnection;
@class MKPacketDataStream;
@class MKCryptState;
@class MKCertificate;

typedef enum {
    UDPVoiceCELTAlphaMessage = 0,
    UDPPingMessage,
    UDPVoiceSpeexMessage,
    UDPVoiceCELTBetaMessage,
    UDPVoiceOpusMessage,
} MKUDPMessageType;

typedef enum {
    VersionMessage = 0,
    UDPTunnelMessage,
    AuthenticateMessage,
    PingMessage,
    RejectMessage,
    ServerSyncMessage,
    ChannelRemoveMessage,
    ChannelStateMessage,
    UserRemoveMessage,
    UserStateMessage,
    BanListMessage,
    TextMessageMessage,
    PermissionDeniedMessage,
    ACLMessage,
    QueryUsersMessage,
    CryptSetupMessage,
    ContextActionModifyMessage,
    ContextActionMessage,
    UserListMessage,
    VoiceTargetMessage,
    PermissionQueryMessage,
    CodecVersionMessage,
    UserStatsMessage,
    RequestBlobMessage,
    ServerConfigMessage,
} MKMessageType;

/**
 * MKRejectReason is an integer describing the reason for a
 * rejected connection attempt.
 */
typedef enum {
    /**
     * There was no reason.
     */
    MKRejectReasonNone = 0,
    
    /**
     * The client attempted to connect with an unsupported version.
     */
    MKRejectReasonWrongVersion,
    
    /**
     * The specified username is not deemed valid by the remote server.
     */
    MKRejectReasonInvalidUsername,

    /**
     * The given password is an incorrect password for the given username.
     */
    MKRejectReasonWrongUserPassword,

    /**
     * The given password is not the correct server password.
     */
    MKRejectReasonWrongServerPassword,

    /**
     * The username the connection attempted to connect with is already in use
     * on the server.
     */
    MKRejectReasonUsernameInUse,

    /**
     * The server is full and cannot accept any new clients.
     */
    MKRejectReasonServerIsFull,

    /**
     * The client did not present a certificate, but the server is set up to require
     * the presence of a client certificate.
     */
    MKRejectReasonNoCertificate
} MKRejectReason;

/**
 * MKConnectionDelegate implements a set of methods that are called on the delegate
 * object of a MKConnection when important connection-related events happen.
 */
@protocol MKConnectionDelegate

/**
 * This method is called once a connection has been established to the remote host, and
 * the TLS handshake has finished.
 * Once the MKConnection has sent this message to its delegate, it is safe to authenticate
 * with the server.
 *
 * @param conn  The connection that was opened.
 */
- (void) connectionOpened:(MKConnection *)conn;

/**
 * This method is called if a connection cannot be stablished to the given server.
 *
 * @param conn The connection that this occurred in. 
 * @param err  Error describing why the connection could not be established.
 */
- (void) connection:(MKConnection *)conn unableToConnectWithError:(NSError *)err;

/**
 * This method is called whenever the connection is closed, be it by an error, or by
 * disconnection. If the disconnection was caused by an error, the err parameter will
 * be a non-nil value.
 *
 * This method can only be called after the connection has been opened. If an error occurs
 * during the connection phase, the method `connection:unableToConnectWithError:` will be
 * called instead.
 *
 * @param conn  The connection that was closed.
 * @param err   The error that caused the disconnection. (Nil if not caused by an error)
 */
- (void) connection:(MKConnection *)conn closedWithError:(NSError *)err;

/**
 * This method is called if the MKConnection could not verify the TLS certificate chain
 * of the remote server as trusted.
 *
 * To implement support for self-signed certificates, one wold typically save the digest
 * of the leaf certificate of the server's certificate chain somewhere along with host
 * information for the server (hostname:port) that the trust failure happened on.
 * Then, every time a connection attempt is made, the trust failure can then be remedied
 * by setting setIgnoreSSLVerification property on the MKConnection and issuing a
 * reconnect to the MKConnection object.
 *
 * @param conn   The connection that the trust failure occurred on.
 * @param chain  The TLS certificate chain of the remote server.
 *               (An array of MKCertificate objects)
 */
- (void) connection:(MKConnection *)conn trustFailureInCertificateChain:(NSArray *)chain;

/**
 * The connection attempt was rejected. This could, for example, be an authentication failure.
 *
 * @param conn         The MKConnection object whose connection was rejected.
 * @param reason       The reason for the rejected connection attempt. (See MKRejectReason).
 * @param explanation  A textual description of the reason for rejection.
 */
- (void) connection:(MKConnection *)conn rejectedWithReason:(MKRejectReason)reason explanation:(NSString *)explanation;
@end

/**
 * MKMessageHandler implements a set of methods that are called on the messageHandler
 * object of a MKConnection when new control channel messages arrive. Only messages that
 * the MKConnection itself does not know how to handle are delegated to the messageHandler.
 *
 * Typically, a consumer of MKConnection does not directly set a MKMessage handler, but instead
 * wrap the MKConnection in a MKServerModel object which 
 */
@protocol MKMessageHandler

/**
 * Called whenever a ban list message is received. (See MKMessageType's
 * BanListMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a ban list message.
 */
- (void) connection:(MKConnection *)conn handleBanListMessage: /* MPBanList */ (id)msg;

/**
 * Called whenever a server sync message is received. (See MKMessageType's
 * ServerSyncMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a server sync message.
 */
- (void) connection:(MKConnection *)conn handleServerSyncMessage: /* MPServerSync */ (id)msg;

/**
 * Called whenever a permission denied message is received. (See MKMessageType's
 * PermissionDeniedMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a permission denied message.
 */
- (void) connection:(MKConnection *)conn handlePermissionDeniedMessage: /* MPPermissionDenied */ (id)msg;

/**
 * Called whenever a user state message is receieved. (See MKMessageType's
 * UserStateMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a user state message.
 */
- (void) connection:(MKConnection *)conn handleUserStateMessage: /* MPUserState */ (id)msg;

/**
 * Called whenever a user remove message is received. (See MKMessageType's
 * UserRemoveMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a user remove message.
 */
- (void) connection:(MKConnection *)conn handleUserRemoveMessage: /* MPUserRemove */ (id)msg;

/**
 * Called whenever a channel state message is recieved. (See MKMessageType's
 * ChannelStateMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a channel state message.
 */
- (void) connection:(MKConnection *)conn handleChannelStateMessage: /* MPChannelState */ (id)msg;

/**
 * Called whenever a channel remove message is received (See MKMessageType's
 * ChannelRemoveMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a channel remove message.
 */
- (void) connection:(MKConnection *)conn handleChannelRemoveMessage: /* MPChannelRemove */ (id)msg;

/**
 * Called whenever a text message message is recieved. (See MKMessageType's
 * TextMessageMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a text message message.
 */
- (void) connection:(MKConnection *)conn handleTextMessageMessage: /* MPTextMessage */ (id)msg;

/**
 * Called whenever an ACL message is receieved. (See MKMessageType's ACLMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of an ACL message.
 */
- (void) connection:(MKConnection *)conn handleACLMessage: /* MPACL */ (id)msg;

/**
 * Called whenver a query users message is received. (See MKMessageType's
 * QueryUsersMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a query users message.
 */
- (void) connection:(MKConnection *)conn handleQueryUsersMessage: /* MPQueryUsers */ (id)msg;

/**
 * Called whenever a context action message is receieved. (See MKMessageType's
 * ContextActionMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a context action message.
 */
- (void) connection:(MKConnection *)conn handleContextActionMessage: /* MPContextAction */ (id)msg;

/**
 * Called whenever a context action add message is received. (See MKMessageType's
 * ContextActionModify value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a context action add message.
 */
- (void) connection:(MKConnection *)conn handleContextActionModifyMessage: /* MPContextActionModify */ (id)msg;

/**
 * Called whenever a user list message is received. (See MKMessageType's
 * UserListMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a user list message.
 */
- (void) connection:(MKConnection *)conn handleUserListMessage: /* MPUserList */ (id)msg;

/**
 * Called whenever a voice target message is receieved. (See MKMessageType's
 * VoiceTargetMessage value).
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a voice target message.
 */
- (void) connection:(MKConnection *)conn handleVoiceTargetMessage: /* MPVoiceTarget */ (id)msg;

/**
 * Called whenever a permission query message is receieved.
 *
 * @param conn  The connection that received the message.
 * @param msg   An internal representation of a permission query message.
 */
- (void) connection:(MKConnection *)conn handlePermissionQueryMessage: /* MPPermissionQuery */ (id)msg;
@end

/**
 * MKConnection represents a connection to a Mumble server.
 * It is mostly used together with MKServerModel which translates the wire protocol
 * to Objective-C delegate callbacks.
 */
@interface MKConnection : NSThread <NSStreamDelegate>

/**
 * Initialize a new MKConnection object.
 */
- (id) init;

/**
 * Deallocate a MKConnection object.
 */
- (void) dealloc;

#pragma mark -

/**
 * Establish a connection to the given host and port.
 *
 * @param hostName  The hostname to connect to.
 * @param port      The port on hostname to connect to.
 */
- (void) connectToHost:(NSString *)hostName port:(NSUInteger)port;

/**
 * Re-establish the connection. This is often used together with the
 * setIgnoreSSLVerification: method to implement an "Are You Sure?" dialog
 * for self-signed certificates.
 */
- (void) reconnect;

/**
 * Disconnect from the server.
 */
- (void) disconnect;

/**
 * The current status of the connection.
 *
 * @returns Returns YES if the MKConnection is currently connected to a server.
 *          Returns NO otherwise.
 */
- (BOOL) connected;

/**
 * The hostname that the MKConnection is currently connected to.
 */
- (NSString *) hostname;

/**
 * The port number on the host that the MKConnection is currently connected to.
 */
- (NSUInteger) port;

/**
 * Set a certificate chain to be used for the MKConnection. This property is only
 * used during connection establishment, and as such, chaning this value while the
 * MKConnection object is a conncted to a server has no effect.
 *
 * @param chain A NSArray containing a SecIdentityRef as its first item, and SecCertificateRefs subsequently.
 */
- (void) setCertificateChain:(NSArray *)chain;

/**
 * Returns the certificate chain that is to be presented to the server during the next connection attempt.
 */
- (NSArray *) certificateChain;

#pragma mark Server Info

/**
 * A textual description of the version number of the Mumble server that the MKConnection
 * object is currently connected to.
 */
- (NSString *) serverVersion;

/**
 * A textual description of the release name of the Mumble server that the MKConnection
 * object is currently connected to.
 */
- (NSString *) serverRelease;

/**
 * A textual description of the operating system that powers the Mumble server that the
 * MKConnection object is currently connected to.
 */
- (NSString *) serverOSName;

/**
 * A textual description of the version of the operating system that powers the Mumble
 * server that the MKConnection object is currently connected to.
 */
- (NSString *) serverOSVersion;

///-------------------------------------
/// @name Authenticating with the server
///-------------------------------------

/**
 * Once a connection has been established (that is, once the connectionOpened: delegate
 * method has been called), this method must be used to authenticate with the remote
 * Mumble server.
 *
 * @param user The username of the user that the MKConnection should authenticate
 *             itself as. This can be a registered user, or a new user that is currently
 *             not registered.
 *
 * @param pass The password to authenticate with. If the specified username is that
 *             of a registered user, the password will be treated as a user password.
 *             Otherwise, it will be treated as a server password.
 *
 * @param tokens The initial set of access tokens for the user we are connecting as, in
 *               the form of an NSArray of NSStrings.
 *               This parameter may be nil if the user does not have any access tokens.
 */
- (void) authenticateWithUsername:(NSString *)user password:(NSString *)pass accessTokens:(NSArray *)tokens;

///----------------------
/// @name Message Handler
///----------------------

- (void) setMessageHandler: (id<MKMessageHandler>)messageHandler;
- (id) messageHandler;

///----------------------
/// @name Delegate
///----------------------

- (void) setDelegate: (id<MKConnectionDelegate>)delegate;
- (id) delegate;

///---------------------------
/// @name TLS connection state
///---------------------------

/**
 * Signals to the MKConnection that it should ignore most verification
 * errors that happen while verifying the server's certificate chain
 * during the TLS handshake.
 *
 * This is used to implement user trust of servers with self-signed
 * (or perhaps shady) certificates.
 *
 * @param shouldIgnoreVerification Should be YES if the connection should
 *                                 ignore TLS certificate chain verification
 *                                 errors. By default this is set to NO.
 *                                     
 *
 */
- (void) setIgnoreSSLVerification:(BOOL)shouldIgnoreVerification;

/**
 * Once a connection is established, this method returns an array
 * containing the TLS certificate chain of the remote server.
 *
 * Certificates in the chain are represented by MKCertificate objects.
 */
- (NSArray *) peerCertificates;

/**
 * Once a connection is established, this method returns the system's
 * trust status of the server's certificate chain. This trust is based
 * on the system's list of root certificate authorities.
 */
- (BOOL) peerCertificateChainTrusted;

///----------------------
/// @name Forced TCP mode
///----------------------

/**
 * Set whether or not the server should force all UDP trafic to be tunelled
 * through TCP. If at all possible, this should be kept as NO (which is also
 * the default value).
 *
 * @param shouldForceTCP  Should be YES if the connection shall tunnel all UDP
 *                        trafic through TCP.
 *
 */
- (void) setForceTCP:(BOOL)shouldForceTCP;

/**
 * Returns the current Forced-TCP status of the MKConnection object.
 *
 * @returns Returns YES if all UDP trafic to and from the remote server
 *          is being tunelled through a TCP connection. Returns NO otherwise.
 */
- (BOOL) forceTCP;

///----------------------------------------
/// @name Sending data to the remote server
///----------------------------------------

/**
 * Transmits a blob of data (presumed to be a message encoded as expected by
 * the Mumble server) using the given messageType as the token used for identifying
 * how the contents of the message are to be handled by the remote server.
 *
 * @param messageType  A MKMessageType describing the kind of message that is to be
 *                     transmitted.
 *
 * @param data  The raw data to be sent to the remote server. This is presumed to be
 *              a message encoded as expected by the remote server. (That is,
 *              MKConnection will not attempt to serialize the passed-in data).
 */
- (void) sendMessageWithType:(MKMessageType)messageType data:(NSData *)data;

/**
 * Send a voice packet to the remote server.
 * The voice packet will be transported to the server using UDP, unless
 * the forceTCP property has been changed to force all UDP trafic to be
 * tunelled through TCP.
 *
 * @param data  A raw Mumble voice packet.
 */
- (void) sendVoiceData:(NSData *)data;

///------------------------
/// @name Codec Information
///------------------------

/**
 * The current elected alpha codec, as determined by the server through a mojority vote.
 */
- (NSUInteger) alphaCodec;

/**
 * The currently elected beta codec, as determined by the server through a majority vote.
 */
- (NSUInteger) betaCodec;

/**
 * Returns whether or not clients should prefer the alpha codec over the beta codec (if at
 * all possible).
 */
- (BOOL) preferAlphaCodec;

/**
 * Returns whether ot not the connected client should use the Opus codec.
 */
- (BOOL) shouldUseOpus;

@end