Moved files to correct location

This commit is contained in:
WolverinDEV
2019-11-10 14:05:40 +00:00
parent de9483173e
commit 5fe2da9524
42 changed files with 12072 additions and 0 deletions
+203
View File
@@ -0,0 +1,203 @@
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2008 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2008 Nokia Corporation. All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef _STUN_CONSTANTS_H
#define _STUN_CONSTANTS_H
/**
* SECTION:stunconstants
* @short_description: STUN constants
* @include: stun/constants.h
* @stability: Stable
*
* Various constants defining parts of the STUN and TURN protocols and
* on-the-wire packet formats.
*/
/**
* STUN_ATTRIBUTE_LENGTH_LEN:
*
* Length of the length field of a STUN attribute (in bytes).
*/
/**
* STUN_ATTRIBUTE_LENGTH_POS:
*
* Offset of the length field of a STUN attribute (in bytes).
*/
/**
* STUN_ATTRIBUTE_TYPE_LEN:
*
* Length of the type field of a STUN attribute (in bytes).
*/
/**
* STUN_ATTRIBUTE_TYPE_POS:
*
* Offset of the type field of a STUN attribute (in bytes).
*/
/**
* STUN_ATTRIBUTE_VALUE_POS:
*
* Offset of the value field of a STUN attribute (in bytes).
*/
/**
* STUN_ID_LEN:
*
* Length of the ID field of a STUN message (in bytes).
*/
/**
* STUN_MAGIC_COOKIE:
*
* Magic cookie value used to identify STUN messages.
*/
/**
* TURN_MAGIC_COOKIE:
*
* Magic cookie value used to identify TURN messages.
*/
/**
* STUN_MAX_MESSAGE_SIZE_IPV4:
*
* Maximum size of a STUN message sent over IPv4 (in bytes).
*/
/**
* STUN_MAX_MESSAGE_SIZE_IPV6:
*
* Maximum size of a STUN message sent over IPv6 (in bytes).
*/
/**
* STUN_MESSAGE_ATTRIBUTES_POS:
*
* Offset of the attributes of a STUN message (in bytes).
*/
/**
* STUN_MESSAGE_HEADER_LENGTH:
*
* Total length of a STUN message header (in bytes).
*/
/**
* STUN_MESSAGE_LENGTH_LEN:
*
* Length of the length field of a STUN message (in bytes).
*/
/**
* STUN_MESSAGE_LENGTH_POS:
*
* Offset of the length field of a STUN message (in bytes).
*/
/**
* STUN_MESSAGE_TRANS_ID_LEN:
*
* Length of the transaction ID field of a STUN message (in bytes).
*/
/**
* STUN_MESSAGE_TRANS_ID_POS:
*
* Offset of the transaction ID field of a STUN message (in bytes).
*/
/**
* STUN_MESSAGE_TYPE_LEN:
*
* Length of the type field of a STUN message (in bytes).
*/
/**
* STUN_MESSAGE_TYPE_POS:
*
* Offset of the type field of a STUN message (in bytes).
*/
#define STUN_MESSAGE_TYPE_POS 0
#define STUN_MESSAGE_TYPE_LEN 2
#define STUN_MESSAGE_LENGTH_POS \
(STUN_MESSAGE_TYPE_POS + STUN_MESSAGE_TYPE_LEN)
#define STUN_MESSAGE_LENGTH_LEN 2
#define STUN_MESSAGE_TRANS_ID_POS \
(STUN_MESSAGE_LENGTH_POS + STUN_MESSAGE_LENGTH_LEN)
#define STUN_MESSAGE_TRANS_ID_LEN 16
#define STUN_MESSAGE_ATTRIBUTES_POS \
(STUN_MESSAGE_TRANS_ID_POS + STUN_MESSAGE_TRANS_ID_LEN)
#define STUN_MESSAGE_HEADER_LENGTH STUN_MESSAGE_ATTRIBUTES_POS
#define STUN_ATTRIBUTE_TYPE_POS 0
#define STUN_ATTRIBUTE_TYPE_LEN 2
#define STUN_ATTRIBUTE_LENGTH_POS \
(STUN_ATTRIBUTE_TYPE_POS + STUN_ATTRIBUTE_TYPE_LEN)
#define STUN_ATTRIBUTE_LENGTH_LEN 2
#define STUN_ATTRIBUTE_VALUE_POS \
(STUN_ATTRIBUTE_LENGTH_POS + STUN_ATTRIBUTE_LENGTH_LEN)
/**
* STUN_ATTRIBUTE_HEADER_LENGTH:
*
* Length of a single STUN attribute header (in bytes).
*/
#define STUN_ATTRIBUTE_HEADER_LENGTH STUN_ATTRIBUTE_VALUE_POS
#define STUN_MAX_MESSAGE_SIZE_IPV4 576
#define STUN_MAX_MESSAGE_SIZE_IPV6 1280
/* #define STUN_MAX_MESSAGE_SIZE STUN_MAX_MESSAGE_SIZE_IPV4 */
#define STUN_ID_LEN 16
/**
* STUN_AGENT_MAX_SAVED_IDS:
*
* Maximum number of simultaneously ongoing STUN transactions.
*/
#define STUN_AGENT_MAX_SAVED_IDS 200
/**
* STUN_AGENT_MAX_UNKNOWN_ATTRIBUTES:
*
* Maximum number of unknown attribute which can be handled in a single STUN
* message.
*/
#define STUN_AGENT_MAX_UNKNOWN_ATTRIBUTES 256
#define STUN_MAGIC_COOKIE 0x2112A442
#define TURN_MAGIC_COOKIE 0x72c64bc6
#ifndef TRUE
#define TRUE (1 == 1)
#endif
#ifndef FALSE
#define FALSE (0 == 1)
#endif
#endif /* _STUN_CONSTANTS_H */
+102
View File
@@ -0,0 +1,102 @@
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2008-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2007 Nokia Corporation. All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef STUN_DEBUG_H
#define STUN_DEBUG_H
#include <stddef.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* stun_debug_enable:
*
* Enable debug messages to stderr
*/
void stun_debug_enable (void);
/**
* stun_debug_disable:
*
* Disable debug messages to stderr
*/
void stun_debug_disable (void);
/**
* StunDebugHandler:
* @format: printf()-style debug message format string
* @ap: Parameters to substitute into message placeholders
*
* Callback for a debug message from the STUN code.
*/
#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
typedef void (*StunDebugHandler) (const char *format, va_list ap)
__attribute__((__format__ (__printf__, 1, 0)));
#else
typedef void (*StunDebugHandler) (const char *format, va_list ap);
#endif
/**
* stun_set_debug_handler:
* @handler: (nullable): Handler for STUN debug messages, or %NULL to use the
* default
*
* Set a callback function to be invoked for each debug message from the STUN
* code. The callback will only be invoked if STUN debugging is enabled using
* stun_debug_enable().
*
* The default callback prints the formatted debug message to stderr.
*/
void stun_set_debug_handler (StunDebugHandler handler);
#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
void stun_debug (const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2)));
#else
void stun_debug (const char *fmt, ...);
#endif
void stun_debug_bytes (const char *prefix, const void *data, size_t len);
# ifdef __cplusplus
}
# endif
#endif /* STUN_DEBUG_H */
+521
View File
@@ -0,0 +1,521 @@
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2008-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2008-2009 Nokia Corporation. All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef _STUN_AGENT_H
#define _STUN_AGENT_H
/**
* SECTION:stunagent
* @short_description: STUN agent for building and validating STUN messages
* @include: stun/stunagent.h
* @see_also: #StunMessage
* @stability: Stable
*
* The STUN Agent allows you to create and validate STUN messages easily.
* It's main purpose is to make sure the building and validation methods used
* are compatible with the RFC you create it with. It also tracks the transaction
* ids of the requests you send, so you can validate if a STUN response you
* received should be processed by that agent or not.
*
*/
#ifdef _WIN32
#include "win32_common.h"
#else
#include <stdint.h>
#endif
#include <stdbool.h>
#include <sys/types.h>
/**
* StunAgent:
*
* An opaque structure representing the STUN agent.
*/
typedef struct stun_agent_t StunAgent;
#include "stunmessage.h"
#include "debug.h"
/**
* StunCompatibility:
* @STUN_COMPATIBILITY_RFC3489: Use the STUN specifications compatible with
* RFC 3489
* @STUN_COMPATIBILITY_RFC5389: Use the STUN specifications compatible with
* RFC 5389
* @STUN_COMPATIBILITY_MSICE2: Use the STUN specifications compatible with
* [MS-ICE2] (a mix between RFC3489 and RFC5389)
* @STUN_COMPATIBILITY_OC2007: Use the STUN specifications compatible with
* Microsoft Office Communicator 2007 (basically RFC3489 with swapped
* REALM and NONCE attribute hex IDs, attributes are not aligned)
* @STUN_COMPATIBILITY_WLM2009: An alias for @STUN_COMPATIBILITY_MSICE2
* @STUN_COMPATIBILITY_LAST: Dummy last compatibility mode
*
* Enum that specifies the STUN compatibility mode of the #StunAgent
*
* <warning>@STUN_COMPATIBILITY_WLM2009 is deprecated and should not be used
* in newly-written code. It is kept for compatibility reasons and represents
* the same compatibility as @STUN_COMPATIBILITY_MSICE2.</warning>
*/
typedef enum {
STUN_COMPATIBILITY_RFC3489,
STUN_COMPATIBILITY_RFC5389,
STUN_COMPATIBILITY_MSICE2,
STUN_COMPATIBILITY_OC2007,
STUN_COMPATIBILITY_WLM2009 = STUN_COMPATIBILITY_MSICE2,
STUN_COMPATIBILITY_LAST = STUN_COMPATIBILITY_OC2007
} StunCompatibility;
/**
* StunValidationStatus:
* @STUN_VALIDATION_SUCCESS: The message is validated
* @STUN_VALIDATION_NOT_STUN: This is not a valid STUN message
* @STUN_VALIDATION_INCOMPLETE_STUN: The message seems to be valid but incomplete
* @STUN_VALIDATION_BAD_REQUEST: The message does not have the cookie or the
* fingerprint while the agent needs it with its usage
* @STUN_VALIDATION_UNAUTHORIZED_BAD_REQUEST: The message is valid but
* unauthorized with no username and message-integrity attributes.
* A BAD_REQUEST error must be generated
* @STUN_VALIDATION_UNAUTHORIZED: The message is valid but unauthorized as
* the username/password do not match.
* An UNAUTHORIZED error must be generated
* @STUN_VALIDATION_UNMATCHED_RESPONSE: The message is valid but this is a
* response/error that doesn't match a previously sent request
* @STUN_VALIDATION_UNKNOWN_REQUEST_ATTRIBUTE: The message is valid but
* contains one or more unknown comprehension attributes.
* stun_agent_build_unknown_attributes_error() should be called
* @STUN_VALIDATION_UNKNOWN_ATTRIBUTE: The message is valid but contains one
* or more unknown comprehension attributes. This is a response, or error,
* or indication message and no error response should be sent
*
* This enum is used as the return value of stun_agent_validate() and represents
* the status result of the validation of a STUN message.
*/
typedef enum {
STUN_VALIDATION_SUCCESS,
STUN_VALIDATION_NOT_STUN,
STUN_VALIDATION_INCOMPLETE_STUN,
STUN_VALIDATION_BAD_REQUEST,
STUN_VALIDATION_UNAUTHORIZED_BAD_REQUEST,
STUN_VALIDATION_UNAUTHORIZED,
STUN_VALIDATION_UNMATCHED_RESPONSE,
STUN_VALIDATION_UNKNOWN_REQUEST_ATTRIBUTE,
STUN_VALIDATION_UNKNOWN_ATTRIBUTE,
} StunValidationStatus;
/**
* StunAgentUsageFlags:
* @STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS: The agent should be using the short
* term credentials mechanism for authenticating STUN messages
* @STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS: The agent should be using the long
* term credentials mechanism for authenticating STUN messages
* @STUN_AGENT_USAGE_USE_FINGERPRINT: The agent should add the FINGERPRINT
* attribute to the STUN messages it creates.
* @STUN_AGENT_USAGE_ADD_SOFTWARE: The agent should add the SOFTWARE attribute
* to the STUN messages it creates. Calling nice_agent_set_software() will have
* the same effect as enabling this Usage. STUN Indications do not have the
* SOFTWARE attributes added to them though. The SOFTWARE attribute is only
* added for the RFC5389 and MSICE2 compatibility modes.
* @STUN_AGENT_USAGE_IGNORE_CREDENTIALS: The agent should ignore any credentials
* in the STUN messages it receives (the MESSAGE-INTEGRITY attribute
* will never be validated by stun_agent_validate())
* @STUN_AGENT_USAGE_NO_INDICATION_AUTH: The agent should ignore credentials
* in the STUN messages it receives if the #StunClass of the message is
* #STUN_INDICATION (some implementation require #STUN_INDICATION messages to
* be authenticated, while others never add a MESSAGE-INTEGRITY attribute to a
* #STUN_INDICATION message)
* @STUN_AGENT_USAGE_FORCE_VALIDATER: The agent should always try to validate
* the password of a STUN message, even if it already knows what the password
* should be (a response to a previously created request). This means that the
* #StunMessageIntegrityValidate callback will always be called when there is
* a MESSAGE-INTEGRITY attribute.
* @STUN_AGENT_USAGE_NO_ALIGNED_ATTRIBUTES: The agent should not assume STUN
* attributes are aligned on 32-bit boundaries when parsing messages and also
* do not add padding when creating messages.
*
* This enum defines a bitflag usages for a #StunAgent and they will define how
* the agent should behave, independently of the compatibility mode it uses.
* <para> See also: stun_agent_init() </para>
* <para> See also: stun_agent_validate() </para>
*/
typedef enum {
STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS = (1 << 0),
STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS = (1 << 1),
STUN_AGENT_USAGE_USE_FINGERPRINT = (1 << 2),
STUN_AGENT_USAGE_ADD_SOFTWARE = (1 << 3),
STUN_AGENT_USAGE_IGNORE_CREDENTIALS = (1 << 4),
STUN_AGENT_USAGE_NO_INDICATION_AUTH = (1 << 5),
STUN_AGENT_USAGE_FORCE_VALIDATER = (1 << 6),
STUN_AGENT_USAGE_NO_ALIGNED_ATTRIBUTES = (1 << 7),
} StunAgentUsageFlags;
typedef struct {
StunTransactionId id;
StunMethod method;
uint8_t *key;
size_t key_len;
uint8_t long_term_key[16];
bool long_term_valid;
bool valid;
} StunAgentSavedIds;
struct stun_agent_t {
StunCompatibility compatibility;
StunAgentSavedIds sent_ids[STUN_AGENT_MAX_SAVED_IDS];
uint16_t *known_attributes;
StunAgentUsageFlags usage_flags;
const char *software_attribute;
bool ms_ice2_send_legacy_connchecks;
};
/**
* StunDefaultValidaterData:
* @username: The username
* @username_len: The length of the @username
* @password: The password
* @password_len: The length of the @password
*
* This structure is used as an element of the user_data to the
* stun_agent_default_validater() function for authenticating a STUN
* message during validationg.
* <para> See also: stun_agent_default_validater() </para>
*/
typedef struct {
uint8_t *username;
size_t username_len;
uint8_t *password;
size_t password_len;
} StunDefaultValidaterData;
/**
* StunMessageIntegrityValidate:
* @agent: The #StunAgent
* @message: The #StunMessage being validated
* @username: The username found in the @message
* @username_len: The length of @username
* @password: The password associated with that username. This argument is a
* pointer to a byte array that must be set by the validater function.
* @password_len: The length of @password which must also be set by the
* validater function.
* @user_data: Data to give the function
*
* This is the prototype for the @validater argument of the stun_agent_validate()
* function.
* <para> See also: stun_agent_validate() </para>
* Returns: %TRUE if the authentication was successful,
* %FALSE if the authentication failed
*/
typedef bool (*StunMessageIntegrityValidate) (StunAgent *agent,
StunMessage *message, uint8_t *username, uint16_t username_len,
uint8_t **password, size_t *password_len, void *user_data);
/**
* stun_agent_default_validater:
* @agent: The #StunAgent
* @message: The #StunMessage being validated
* @username: The username found in the @message
* @username_len: The length of @username
* @password: The password associated with that username. This argument is a
* pointer to a byte array that must be set by the validater function.
* @password_len: The length of @password which must also be set by the
* validater function.
* @user_data: This must be an array of #StunDefaultValidaterData structures.
* The last element in the array must have a username set to NULL
*
* This is a helper function to be used with stun_agent_validate(). If no
* complicated processing of the username needs to be done, this function can
* be used with stun_agent_validate() to quickly and easily match the username
* of a STUN message with its password. Its @user_data argument must be an array
* of #StunDefaultValidaterData which will allow us to map a username to a
* password
* <para> See also: stun_agent_validate() </para>
* Returns: %TRUE if the authentication was successful,
* %FALSE if the authentication failed
*/
bool stun_agent_default_validater (StunAgent *agent,
StunMessage *message, uint8_t *username, uint16_t username_len,
uint8_t **password, size_t *password_len, void *user_data);
/**
* stun_agent_init:
* @agent: The #StunAgent to initialize
* @known_attributes: An array of #uint16_t specifying which attributes should
* be known by the agent. Any STUN message received that contains a mandatory
* attribute that is not in this array will yield a
* #STUN_VALIDATION_UNKNOWN_REQUEST_ATTRIBUTE or a
* #STUN_VALIDATION_UNKNOWN_ATTRIBUTE error when calling stun_agent_validate()
* @compatibility: The #StunCompatibility to use for this agent. This will affect
* how the agent builds and validates the STUN messages
* @usage_flags: A bitflag using #StunAgentUsageFlags values to define which
* STUN usages the agent should use.
*
* This function must be called to initialize an agent before it is being used.
*
<note>
<para>
The @known_attributes data must exist in memory as long as the @agent is used
</para>
<para>
If the #STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS and
#STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS usage flags are not set, then the
agent will default in using the short term credentials mechanism
</para>
<para>
The #STUN_AGENT_USAGE_USE_FINGERPRINT and #STUN_AGENT_USAGE_ADD_SOFTWARE
usage flags are only valid if the #STUN_COMPATIBILITY_RFC5389 or
#STUN_COMPATIBILITY_MSICE2 @compatibility is used
</para>
</note>
*/
void stun_agent_init (StunAgent *agent, const uint16_t *known_attributes,
StunCompatibility compatibility, StunAgentUsageFlags usage_flags);
/**
* stun_agent_validate:
* @agent: The #StunAgent
* @msg: The #StunMessage to build
* @buffer: The data buffer of the STUN message
* @buffer_len: The length of @buffer
* @validater: A #StunMessageIntegrityValidate function callback that will
* be called if the agent needs to validate a MESSAGE-INTEGRITY attribute. It
* will only be called if the agent finds a message that needs authentication
* and a USERNAME is present in the STUN message, but no password is known.
* The validater will not be called if the #STUN_AGENT_USAGE_IGNORE_CREDENTIALS
* usage flag is set on the agent, and it will always be called if the
* #STUN_AGENT_USAGE_FORCE_VALIDATER usage flag is set on the agent.
* @validater_data: A user data to give to the @validater callback when it gets
* called.
*
* This function is used to validate an inbound STUN message and transform its
* data buffer into a #StunMessage. It will take care of various validation
* algorithms to make sure that the STUN message is valid and correctly
* authenticated.
* <para> See also: stun_agent_default_validater() </para>
* Returns: A #StunValidationStatus
<note>
<para>
if the return value is different from #STUN_VALIDATION_NOT_STUN or
#STUN_VALIDATION_INCOMPLETE_STUN, then the @msg argument will contain a valid
STUN message that can be used.
This means that you can use the @msg variable as the @request argument to
functions like stun_agent_init_error() or
stun_agent_build_unknown_attributes_error().
If the return value is #STUN_VALIDATION_BAD_REQUEST,
#STUN_VALIDATION_UNAUTHORIZED or #STUN_VALIDATION_UNAUTHORIZED_BAD_REQUEST
then the @key in the #StunMessage will not be set, so that error responses
will not have a MESSAGE-INTEGRITY attribute.
</para>
</note>
*/
StunValidationStatus stun_agent_validate (StunAgent *agent, StunMessage *msg,
const uint8_t *buffer, size_t buffer_len,
StunMessageIntegrityValidate validater, void * validater_data);
/**
* stun_agent_init_request:
* @agent: The #StunAgent
* @msg: The #StunMessage to build
* @buffer: The buffer to use in the #StunMessage
* @buffer_len: The length of the buffer
* @m: The #StunMethod of the request
*
* Creates a new STUN message of class #STUN_REQUEST and with the method @m
* Returns: %TRUE if the message was initialized correctly, %FALSE otherwise
*/
bool stun_agent_init_request (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len, StunMethod m);
/**
* stun_agent_init_indication:
* @agent: The #StunAgent
* @msg: The #StunMessage to build
* @buffer: The buffer to use in the #StunMessage
* @buffer_len: The length of the buffer
* @m: The #StunMethod of the indication
*
* Creates a new STUN message of class #STUN_INDICATION and with the method @m
* Returns: %TRUE if the message was initialized correctly, %FALSE otherwise
*/
bool stun_agent_init_indication (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len, StunMethod m);
/**
* stun_agent_init_response:
* @agent: The #StunAgent
* @msg: The #StunMessage to build
* @buffer: The buffer to use in the #StunMessage
* @buffer_len: The length of the buffer
* @request: The #StunMessage of class #STUN_REQUEST that this response is for
*
* Creates a new STUN message of class #STUN_RESPONSE and with the same method
* and transaction ID as the message @request. This will also copy the pointer
* to the key that was used to authenticate the request, so you won't need to
* specify the key with stun_agent_finish_message()
* Returns: %TRUE if the message was initialized correctly, %FALSE otherwise
*/
bool stun_agent_init_response (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len, const StunMessage *request);
/**
* stun_agent_init_error:
* @agent: The #StunAgent
* @msg: The #StunMessage to build
* @buffer: The buffer to use in the #StunMessage
* @buffer_len: The length of the buffer
* @request: The #StunMessage of class #STUN_REQUEST that this error response
* is for
* @err: The #StunError to put in the ERROR-CODE attribute of the error response
*
* Creates a new STUN message of class #STUN_ERROR and with the same method
* and transaction ID as the message @request. This will also copy the pointer
* to the key that was used to authenticate the request (if authenticated),
* so you won't need to specify the key with stun_agent_finish_message().
* It will then add the ERROR-CODE attribute with code @err and the associated
* string.
* Returns: %TRUE if the message was initialized correctly, %FALSE otherwise
*/
bool stun_agent_init_error (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len, const StunMessage *request,
StunError err);
/**
* stun_agent_build_unknown_attributes_error:
* @agent: The #StunAgent
* @msg: The #StunMessage to build
* @buffer: The buffer to use in the #StunMessage
* @buffer_len: The length of the buffer
* @request: The #StunMessage of class #STUN_REQUEST that this response is for
*
* Creates a new STUN message of class #STUN_ERROR and with the same method
* and transaction ID as the message @request. It will then add the ERROR-CODE
* attribute with code #STUN_ERROR_UNKNOWN_ATTRIBUTE and add all the unknown
* mandatory attributes from the @request STUN message in the
* #STUN_ATTRIBUTE_UNKNOWN_ATTRIBUTES attribute, it will then finish the message
* by calling stun_agent_finish_message()
* Returns: The size of the message built
*/
size_t stun_agent_build_unknown_attributes_error (StunAgent *agent,
StunMessage *msg, uint8_t *buffer, size_t buffer_len,
const StunMessage *request);
/**
* stun_agent_finish_message:
* @agent: The #StunAgent
* @msg: The #StunMessage to finish
* @key: The key to use for the MESSAGE-INTEGRITY attribute
* @key_len: The length of the @key
*
* This function will 'finish' a message and make it ready to be sent. It will
* add the MESSAGE-INTEGRITY and FINGERPRINT attributes if necessary. If the
* STUN message has a #STUN_REQUEST class, it will save the transaction id of
* the message in the agent for future matching of the response.
* <para>See also: stun_agent_forget_transaction()</para>
* Returns: The final size of the message built or 0 if an error occured
* <note>
<para>
The return value must always be checked. a value of 0 means the either
the buffer's size is too small to contain the finishing attributes
(MESSAGE-INTEGRITY, FINGERPRINT), or that there is no more free slots
for saving the sent id in the agent's state.
</para>
<para>
Everytime stun_agent_finish_message() is called for a #STUN_REQUEST
message, you must make sure to call stun_agent_forget_transaction() in
case the response times out and is never received. This is to avoid
filling up the #StunAgent's sent ids state preventing any further
use of the stun_agent_finish_message()
</para>
</note>
*/
size_t stun_agent_finish_message (StunAgent *agent, StunMessage *msg,
const uint8_t *key, size_t key_len);
/**
* stun_agent_forget_transaction:
* @agent: The #StunAgent
* @id: The #StunTransactionId of the transaction to forget
*
* This function is used to make the #StunAgent forget about a previously
* created transaction.
* <para>
* This function should be called when a STUN request was previously
* created with stun_agent_finish_message() and for which no response was ever
* received (timed out). The #StunAgent keeps a list of the sent transactions
* in order to validate the responses received. If the response is never received
* this will allow the #StunAgent to forget about the timed out transaction and
* free its slot for future transactions.
* </para>
* Since: 0.0.6
* Returns: %TRUE if the transaction was found, %FALSE otherwise
*/
bool stun_agent_forget_transaction (StunAgent *agent, StunTransactionId id);
/**
* stun_agent_set_software:
* @agent: The #StunAgent
* @software: The value of the SOFTWARE attribute to add.
*
* This function will set the value of the SOFTWARE attribute to be added to
* STUN requests, responses and error responses.
* <para>
* Calling this function will automatically enable the addition of the SOFTWARE
* attribute for RFC5389 and MSICE2 compatibility modes.
*
* </para>
* <note>
<para>
The @software argument must be in UTF-8 encoding and only the first
128 characters will be sent.
</para>
<para>
The value of the @software argument must stay valid throughout the life of
the StunAgent's life. Do not free its content.
</para>
</note>
*
* Since: 0.0.10
*
*/
void stun_agent_set_software (StunAgent *agent, const char *software);
#endif /* _STUN_AGENT_H */
File diff suppressed because it is too large Load Diff
+165
View File
@@ -0,0 +1,165 @@
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2008-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2007-2009 Nokia Corporation. All rights reserved.
* Contact: Rémi Denis-Courmont
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
* Rémi Denis-Courmont, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef STUN_BIND_H
# define STUN_BIND_H 1
/**
* SECTION:bind
* @short_description: STUN Binding Usage
* @include: stun/usages/bind.h
* @stability: Stable
*
* The STUN Binding usage allows for easily creating and parsing STUN Binding
* requests and responses. It offers both an asynchronous and a synchronous API
* that uses the STUN timer usage.
*/
#ifdef _WIN32
# include "../win32_common.h"
#else
# include <stdbool.h>
# include <stdint.h>
#endif
# include "stun/stunagent.h"
# ifdef __cplusplus
extern "C" {
# endif
/**
* StunUsageBindReturn:
* @STUN_USAGE_BIND_RETURN_SUCCESS: The binding usage succeeded
* @STUN_USAGE_BIND_RETURN_ERROR: There was an unknown error in the bind usage
* @STUN_USAGE_BIND_RETURN_INVALID: The message is invalid and should be ignored
* @STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER: The binding request has an
* ALTERNATE-SERVER attribute
* @STUN_USAGE_BIND_RETURN_TIMEOUT: The binding was unsuccessful because it has
* timed out.
*
* Return value of stun_usage_bind_process() and stun_usage_bind_run() which
* allows you to see what status the function call returned.
*/
typedef enum {
STUN_USAGE_BIND_RETURN_SUCCESS,
STUN_USAGE_BIND_RETURN_ERROR,
STUN_USAGE_BIND_RETURN_INVALID,
STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER,
STUN_USAGE_BIND_RETURN_TIMEOUT,
} StunUsageBindReturn;
/**
* stun_usage_bind_create:
* @agent: The #StunAgent to use to create the binding request
* @msg: The #StunMessage to build
* @buffer: The buffer to use for creating the #StunMessage
* @buffer_len: The size of the @buffer
*
* Create a new STUN binding request to use with a STUN server.
* Returns: The length of the built message.
*/
size_t stun_usage_bind_create (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len);
/**
* stun_usage_bind_process:
* @msg: The #StunMessage to process
* @addr: A pointer to a #sockaddr structure to fill with the mapped address
* that the STUN server gives us
* @addrlen: The length of @add. rMust be set to the size of the @addr socket
* address and will be set to the actual length of the socket address.
* @alternate_server: A pointer to a #sockaddr structure to fill with the
* address of an alternate server to which we should send our new STUN
* binding request, in case the currently used STUN server is requesting the use
* of an alternate server. This argument will only be filled if the return value
* of the function is #STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER
* @alternate_server_len: The length of @alternate_server. Must be set to
* the size of the @alternate_server socket address and will be set to the
* actual length of the socket address.
*
* Process a STUN binding response and extracts the mapped address from the STUN
* message. Also checks for the ALTERNATE-SERVER attribute.
* Returns: A #StunUsageBindReturn value.
* Note that #STUN_USAGE_BIND_RETURN_TIMEOUT cannot be returned by this function
*/
StunUsageBindReturn stun_usage_bind_process (StunMessage *msg,
struct sockaddr *addr, socklen_t *addrlen,
struct sockaddr *alternate_server, socklen_t *alternate_server_len);
/**
* stun_usage_bind_keepalive:
* @agent: The #StunAgent to use to build the message
* @msg: The #StunMessage to build
* @buf: The buffer to use for creating the #StunMessage
* @len: The size of the @buf
*
* Creates a STUN binding indication that can be used for a keepalive.
* Since this is an indication message, no STUN response will be generated
* and it can only be used as a keepalive message.
* Returns: The length of the message to send
*/
size_t stun_usage_bind_keepalive (StunAgent *agent, StunMessage *msg,
uint8_t *buf, size_t len);
/**
* stun_usage_bind_run:
* @srv: A pointer to the #sockaddr structure representing the STUN server's
* address
* @srvlen: The length of @srv
* @addr: A pointer to a #sockaddr structure to fill with the mapped address
* that the STUN server gives us
* @addrlen: The length of @addr
*
* This is a convenience function that will do a synchronous Binding request to
* a server and wait for its answer. It will create the socket transports and
* use the #StunTimer usage to send the request and handle the response.
* Returns: A #StunUsageBindReturn.
* Possible return values are #STUN_USAGE_BIND_RETURN_SUCCESS,
* #STUN_USAGE_BIND_RETURN_ERROR and #STUN_USAGE_BIND_RETURN_TIMEOUT
*/
StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv,
socklen_t srvlen, struct sockaddr_storage *addr, socklen_t *addrlen);
# ifdef __cplusplus
}
# endif
#endif
+240
View File
@@ -0,0 +1,240 @@
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2008-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2007-2009 Nokia Corporation. All rights reserved.
* Contact: Rémi Denis-Courmont
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
* Rémi Denis-Courmont, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef STUN_CONNCHECK_H
# define STUN_CONNCHECK_H 1
/**
* SECTION:ice
* @short_description: STUN ICE Usage
* @include: stun/usages/ice.h
* @stability: Stable
*
* The STUN ICE usage allows for easily creating and parsing STUN Binding
* requests and responses used for ICE connectivity checks. The API allows you
* to create a connectivity check message, parse a response or create a reply
* to an incoming connectivity check request.
*/
# include "stun/stunagent.h"
# ifdef __cplusplus
extern "C" {
# endif
/**
* StunUsageIceCompatibility:
* @STUN_USAGE_ICE_COMPATIBILITY_RFC5245: The ICE compatibility with RFC 5245
* @STUN_USAGE_ICE_COMPATIBILITY_GOOGLE: The ICE compatibility with Google's
* implementation of ICE
* @STUN_USAGE_ICE_COMPATIBILITY_MSN: The ICE compatibility with MSN's
* implementation of ICE
* @STUN_USAGE_ICE_COMPATIBILITY_MSICE2: The ICE compatibility with [MS-ICE2]
* specification
* @STUN_USAGE_ICE_COMPATIBILITY_DRAFT19: The ICE compatibility with draft 19
* @STUN_USAGE_ICE_COMPATIBILITY_WLM2009: An alias
* for @STUN_USAGE_ICE_COMPATIBILITY_MSICE2
*
* This enum defines which compatibility modes this ICE usage can use
*
* <warning>@STUN_USAGE_ICE_COMPATIBILITY_DRAFT19 and
* @STUN_USAGE_ICE_COMPATIBILITY_WLM2009 are deprecated and should not be used
* in newly-written code. They are kept for compatibility reasons and represent
* the same compatibilities as @STUN_USAGE_ICE_COMPATIBILITY_RFC5245 and
* @STUN_USAGE_ICE_COMPATIBILITY_MSICE2 respectively.</warning>
*/
typedef enum {
STUN_USAGE_ICE_COMPATIBILITY_RFC5245,
STUN_USAGE_ICE_COMPATIBILITY_GOOGLE,
STUN_USAGE_ICE_COMPATIBILITY_MSN,
STUN_USAGE_ICE_COMPATIBILITY_MSICE2,
STUN_USAGE_ICE_COMPATIBILITY_DRAFT19 = STUN_USAGE_ICE_COMPATIBILITY_RFC5245,
STUN_USAGE_ICE_COMPATIBILITY_WLM2009 = STUN_USAGE_ICE_COMPATIBILITY_MSICE2,
} StunUsageIceCompatibility;
/**
* StunUsageIceReturn:
* @STUN_USAGE_ICE_RETURN_SUCCESS: The function succeeded
* @STUN_USAGE_ICE_RETURN_ERROR: There was an unspecified error
* @STUN_USAGE_ICE_RETURN_INVALID: The message is invalid for processing
* @STUN_USAGE_ICE_RETURN_ROLE_CONFLICT: A role conflict was detected
* @STUN_USAGE_ICE_RETURN_INVALID_REQUEST: The message is an not a request
* @STUN_USAGE_ICE_RETURN_INVALID_METHOD: The method of the request is invalid
* @STUN_USAGE_ICE_RETURN_MEMORY_ERROR: The buffer size is too small to hold
* the STUN reply
* @STUN_USAGE_ICE_RETURN_INVALID_ADDRESS: The mapped address argument has
* an invalid address family
* @STUN_USAGE_ICE_RETURN_NO_MAPPED_ADDRESS: The response is valid but no
* MAPPED-ADDRESS or XOR-MAPPED-ADDRESS attribute was found
*
* Return value of stun_usage_ice_conncheck_process() and
* stun_usage_ice_conncheck_create_reply() which allows you to see what
* status the function call returned.
*/
typedef enum {
STUN_USAGE_ICE_RETURN_SUCCESS,
STUN_USAGE_ICE_RETURN_ERROR,
STUN_USAGE_ICE_RETURN_INVALID,
STUN_USAGE_ICE_RETURN_ROLE_CONFLICT,
STUN_USAGE_ICE_RETURN_INVALID_REQUEST,
STUN_USAGE_ICE_RETURN_INVALID_METHOD,
STUN_USAGE_ICE_RETURN_MEMORY_ERROR,
STUN_USAGE_ICE_RETURN_INVALID_ADDRESS,
STUN_USAGE_ICE_RETURN_NO_MAPPED_ADDRESS,
} StunUsageIceReturn;
/**
* stun_usage_ice_conncheck_create:
* @agent: The #StunAgent to use to build the request
* @msg: The #StunMessage to build
* @buffer: The buffer to use for creating the #StunMessage
* @buffer_len: The size of the @buffer
* @username: The username to use in the request
* @username_len: The length of @username
* @password: The key to use for building the MESSAGE-INTEGRITY
* @password_len: The length of @password
* @cand_use: Set to %TRUE to append the USE-CANDIDATE flag to the request
* @controlling: Set to %TRUE if you are the controlling agent or set to
* %FALSE if you are the controlled agent.
* @priority: The value of the PRIORITY attribute
* @tie: The value of the tie-breaker to put in the ICE-CONTROLLED or
* ICE-CONTROLLING attribute
* @candidate_identifier: The foundation value to put in the
* CANDIDATE-IDENTIFIER attribute
* @compatibility: The compatibility mode to use for building the conncheck
* request
*
* Builds an ICE connectivity check STUN message.
* If the compatibility is not #STUN_USAGE_ICE_COMPATIBILITY_RFC5245, the
* @cand_use, @controlling, @priority and @tie arguments are not used.
* If the compatibility is not #STUN_USAGE_ICE_COMPATIBILITY_MSICE2, the
* @candidate_identifier argument is not used.
* Returns: The length of the message built.
*/
size_t
stun_usage_ice_conncheck_create (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len,
const uint8_t *username, const size_t username_len,
const uint8_t *password, const size_t password_len,
bool cand_use, bool controlling, uint32_t priority,
uint64_t tie, const char *candidate_identifier,
StunUsageIceCompatibility compatibility);
/**
* stun_usage_ice_conncheck_process:
* @msg: The #StunMessage to process
* @addr: A pointer to a #sockaddr structure to fill with the mapped address
* that the STUN connectivity check response contains
* @addrlen: The length of @addr
* @compatibility: The compatibility mode to use for processing the conncheck
* response
*
* Process an ICE connectivity check STUN message and retrieve the
* mapped address from the message
* <para> See also stun_usage_ice_conncheck_priority() and
* stun_usage_ice_conncheck_use_candidate() </para>
* Returns: A #StunUsageIceReturn value
*/
StunUsageIceReturn stun_usage_ice_conncheck_process (StunMessage *msg,
struct sockaddr_storage *addr, socklen_t *addrlen,
StunUsageIceCompatibility compatibility);
/**
* stun_usage_ice_conncheck_create_reply:
* @agent: The #StunAgent to use to build the response
* @req: The original STUN request to reply to
* @msg: The #StunMessage to build
* @buf: The buffer to use for creating the #StunMessage
* @plen: A pointer containing the size of the @buffer on input.
* Will contain the length of the message built on output.
* @src: A pointer to a #sockaddr structure containing the source address from
* which the request was received. Will be used as the mapped address in the
* response
* @srclen: The length of @addr
* @control: Set to %TRUE if you are the controlling agent or set to
* %FALSE if you are the controlled agent.
* @tie: The value of the tie-breaker to put in the ICE-CONTROLLED or
* ICE-CONTROLLING attribute
* @compatibility: The compatibility mode to use for building the conncheck
* response
*
* Tries to parse a STUN connectivity check request and builds a
* response accordingly.
<note>
<para>
In case of error, the @msg is filled with the appropriate error response
to be sent and the value of @plen is set to the size of that message.
If @plen has a size of 0, then no error response should be sent.
</para>
</note>
* Returns: A #StunUsageIceReturn value
*/
StunUsageIceReturn
stun_usage_ice_conncheck_create_reply (StunAgent *agent, StunMessage *req,
StunMessage *msg, uint8_t *buf, size_t *plen,
const struct sockaddr_storage *src, socklen_t srclen,
bool *control, uint64_t tie,
StunUsageIceCompatibility compatibility);
/**
* stun_usage_ice_conncheck_priority:
* @msg: The #StunMessage to parse
*
* Extracts the priority from a STUN message.
* Returns: host byte order priority, or 0 if not specified.
*/
uint32_t stun_usage_ice_conncheck_priority (const StunMessage *msg);
/**
* stun_usage_ice_conncheck_use_candidate:
* @msg: The #StunMessage to parse
*
* Extracts the USE-CANDIDATE attribute flag from a STUN message.
* Returns: %TRUE if the flag is set, %FALSE if not.
*/
bool stun_usage_ice_conncheck_use_candidate (const StunMessage *msg);
# ifdef __cplusplus
}
# endif
#endif
+240
View File
@@ -0,0 +1,240 @@
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2008-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2007-2009 Nokia Corporation. All rights reserved.
* Contact: Rémi Denis-Courmont
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
* Rémi Denis-Courmont, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef STUN_TIMER_H
# define STUN_TIMER_H 1
/**
* SECTION:timer
* @short_description: STUN timer Usage
* @include: stun/usages/timer.h
* @stability: Stable
*
* The STUN timer usage is a set of helpful utility functions that allows you
* to easily track when a STUN message should be retransmitted or considered
* as timed out.
*
*
<example>
<title>Simple example on how to use the timer usage</title>
<programlisting>
StunTimer timer;
unsigned remainder;
StunUsageTimerReturn ret;
// Build the message, etc..
...
// Send the message and start the timer
send(socket, request, sizeof(request));
stun_timer_start(&timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
// Loop until we get the response
for (;;) {
remainder = stun_timer_remainder(&timer);
// Poll the socket until data is received or the timer expires
if (poll (&pollfd, 1, delay) <= 0) {
// Time out and no response was received
ret = stun_timer_refresh (&timer);
if (ret == STUN_USAGE_TIMER_RETURN_TIMEOUT) {
// Transaction timed out
break;
} else if (ret == STUN_USAGE_TIMER_RETURN_RETRANSMIT) {
// A retransmission is necessary
send(socket, request, sizeof(request));
continue;
} else if (ret == STUN_USAGE_TIMER_RETURN_SUCCESS) {
// The refresh succeeded and nothing has to be done, continue polling
continue;
}
} else {
// We received a response, read it
recv(socket, response, sizeof(response));
break;
}
}
// Check if the transaction timed out or not
if (ret == STUN_USAGE_TIMER_RETURN_TIMEOUT) {
// do whatever needs to be done in that case
} else {
// Parse the response
}
</programlisting>
</example>
*/
#ifdef _WIN32
#include <winsock2.h>
#else
# include <sys/types.h>
# include <sys/time.h>
# include <time.h>
#endif
/**
* StunTimer:
*
* An opaque structure representing a STUN transaction retransmission timer
*/
typedef struct stun_timer_s StunTimer;
struct stun_timer_s {
struct timeval deadline;
unsigned delay;
unsigned retransmissions;
unsigned max_retransmissions;
};
/**
* STUN_TIMER_DEFAULT_TIMEOUT:
*
* The default intial timeout to use for the timer
* RFC recommendds 500, but it's ridiculous, 50ms is known to work in most
* cases as it is also what is used by SIP style VoIP when sending A-Law and
* mu-Law audio, so 200ms should be hyper safe. With an initial timeout
* of 200ms, a default of 7 transmissions, the last timeout will be
* 16 * 200ms, and we expect to receive a response from the stun server
* before (1 + 2 + 4 + 8 + 16 + 32 + 16) * 200ms = 15200 ms after the initial
* stun request has been sent.
*/
#define STUN_TIMER_DEFAULT_TIMEOUT 200
/**
* STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS:
*
* The default maximum retransmissions allowed before a timer decides to timeout
*/
#define STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS 7
/**
* STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT:
*
* The default intial timeout to use for a reliable timer
*/
#define STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT 7900
/**
* StunUsageTimerReturn:
* @STUN_USAGE_TIMER_RETURN_SUCCESS: The timer was refreshed successfully
* and there is nothing to be done
* @STUN_USAGE_TIMER_RETURN_RETRANSMIT: The timer expired and the message
* should be retransmitted now.
* @STUN_USAGE_TIMER_RETURN_TIMEOUT: The timer expired as well as all the
* retransmissions, the transaction timed out
*
* Return value of stun_usage_timer_refresh() which provides you with status
* information on the timer.
*/
typedef enum {
STUN_USAGE_TIMER_RETURN_SUCCESS,
STUN_USAGE_TIMER_RETURN_RETRANSMIT,
STUN_USAGE_TIMER_RETURN_TIMEOUT
} StunUsageTimerReturn;
# ifdef __cplusplus
extern "C" {
# endif
/**
* stun_timer_start:
* @timer: The #StunTimer to start
* @initial_timeout: The initial timeout to use before the first retransmission
* @max_retransmissions: The maximum number of transmissions before the
* #StunTimer times out
*
* Starts a STUN transaction retransmission timer.
* This should be called as soon as you send the message for the first time on
* a UDP socket.
* The timeout before the next retransmission is set to @initial_timeout, then
* each time a packet is retransmited, that timeout will be doubled, until the
* @max_retransmissions retransmissions limit is reached.
* <para>
* To determine the total timeout value, one can use the following equation :
<programlisting>
total_timeout = initial_timeout * (2^(max_retransmissions + 1) - 1);
</programlisting>
* </para>
*
* See also: #STUN_TIMER_DEFAULT_TIMEOUT
*
* See also: #STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS
*/
void stun_timer_start (StunTimer *timer, unsigned int initial_timeout,
unsigned int max_retransmissions);
/**
* stun_timer_start_reliable:
* @timer: The #StunTimer to start
* @initial_timeout: The initial timeout to use before the first retransmission
*
* Starts a STUN transaction retransmission timer for a reliable transport.
* This should be called as soon as you send the message for the first time on
* a TCP socket
*/
void stun_timer_start_reliable (StunTimer *timer, unsigned int initial_timeout);
/**
* stun_timer_refresh:
* @timer: The #StunTimer to refresh
*
* Updates a STUN transaction retransmission timer.
* Returns: A #StunUsageTimerReturn telling you what to do next
*/
StunUsageTimerReturn stun_timer_refresh (StunTimer *timer);
/**
* stun_timer_remainder:
* @timer: The #StunTimer to query
*
* Query the timer on the time left before the next refresh should be done
* Returns: The time remaining for the timer to expire in milliseconds
*/
unsigned stun_timer_remainder (const StunTimer *timer);
# ifdef __cplusplus
}
# endif
#endif /* !STUN_TIMER_H */
+301
View File
@@ -0,0 +1,301 @@
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2008-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2007-2009 Nokia Corporation. All rights reserved.
* Contact: Rémi Denis-Courmont
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
* Rémi Denis-Courmont, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef STUN_TURN_H
# define STUN_TURN_H 1
/**
* SECTION:turn
* @short_description: TURN Allocation Usage
* @include: stun/usages/turn.h
* @stability: Stable
*
* The STUN TURN usage allows for easily creating and parsing STUN Allocate
* requests and responses used for TURN. The API allows you to create a new
* allocation or refresh an existing one as well as to parse a response to
* an allocate or refresh request.
*/
#ifdef _WIN32
# include "../win32_common.h"
#else
# include <stdbool.h>
# include <stdint.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif
# include "stun/stunagent.h"
# ifdef __cplusplus
extern "C" {
# endif
/**
* StunUsageTurnRequestPorts:
* @STUN_USAGE_TURN_REQUEST_PORT_NORMAL: Request a normal port
* @STUN_USAGE_TURN_REQUEST_PORT_EVEN: Request an even port
* @STUN_USAGE_TURN_REQUEST_PORT_EVEN_AND_RESERVE: Request an even port and
* reserve the next higher port
*
* This enum is used to specify which port configuration you want when creating
* a new Allocation
*/
typedef enum {
STUN_USAGE_TURN_REQUEST_PORT_NORMAL = 0,
STUN_USAGE_TURN_REQUEST_PORT_EVEN = 1,
STUN_USAGE_TURN_REQUEST_PORT_EVEN_AND_RESERVE = 2
} StunUsageTurnRequestPorts;
/**
* StunUsageTurnCompatibility:
* @STUN_USAGE_TURN_COMPATIBILITY_DRAFT9: Use the specification compatible with
* TURN Draft 09
* @STUN_USAGE_TURN_COMPATIBILITY_GOOGLE: Use the specification compatible with
* Google Talk's relay server
* @STUN_USAGE_TURN_COMPATIBILITY_MSN: Use the specification compatible with
* MSN TURN servers
* @STUN_USAGE_TURN_COMPATIBILITY_OC2007: Use the specification compatible with
* Microsoft Office Communicator 2007
* @STUN_USAGE_TURN_COMPATIBILITY_RFC5766: Use the specification compatible with
* RFC 5766 (the final, canonical TURN standard)
*
* Specifies which TURN specification compatibility to use
*/
typedef enum {
STUN_USAGE_TURN_COMPATIBILITY_DRAFT9,
STUN_USAGE_TURN_COMPATIBILITY_GOOGLE,
STUN_USAGE_TURN_COMPATIBILITY_MSN,
STUN_USAGE_TURN_COMPATIBILITY_OC2007,
STUN_USAGE_TURN_COMPATIBILITY_RFC5766,
} StunUsageTurnCompatibility;
/**
* StunUsageTurnReturn:
* @STUN_USAGE_TURN_RETURN_RELAY_SUCCESS: The response was successful and a relay
* address is provided
* @STUN_USAGE_TURN_RETURN_MAPPED_SUCCESS: The response was successful and a
* relay address as well as a mapped address are provided
* @STUN_USAGE_TURN_RETURN_ERROR: The response resulted in an error
* @STUN_USAGE_TURN_RETURN_INVALID: The response is not a valid response
* @STUN_USAGE_TURN_RETURN_ALTERNATE_SERVER: The server requests the message
* to be sent to an alternate server
*
* Return value of stun_usage_turn_process() and
* stun_usage_turn_refresh_process() which allows you to see what status the
* function call returned.
*/
typedef enum {
STUN_USAGE_TURN_RETURN_RELAY_SUCCESS,
STUN_USAGE_TURN_RETURN_MAPPED_SUCCESS,
STUN_USAGE_TURN_RETURN_ERROR,
STUN_USAGE_TURN_RETURN_INVALID,
STUN_USAGE_TURN_RETURN_ALTERNATE_SERVER,
} StunUsageTurnReturn;
/**
* stun_usage_turn_create:
* @agent: The #StunAgent to use to build the request
* @msg: The #StunMessage to build
* @buffer: The buffer to use for creating the #StunMessage
* @buffer_len: The size of the @buffer
* @previous_response: If this is the first request you are sending, set this
* argument to NULL, if it's a subsequent request you are building, then set this
* argument to the response you have received. This argument is used for building
* long term credentials (using the REALM and NONCE attributes) as well as for
* getting the RESERVATION-TOKEN attribute when you previously requested an
* allocation which reserved two ports
* @request_ports: Specify how you want to request the allocated port(s).
* This is only used if the compatibility is set to
* #STUN_USAGE_TURN_COMPATIBILITY_DRAFT9
* <para>See #StunUsageTurnRequestPorts </para>
* @bandwidth: The bandwidth to request from the server for the allocation. If
* this value is negative, then no BANDWIDTH attribute is added to the request.
* This is only used if the compatibility is set to
* #STUN_USAGE_TURN_COMPATIBILITY_DRAFT9
* @lifetime: The lifetime of the allocation to request from the server. If
* this value is negative, then no LIFETIME attribute is added to the request.
* This is only used if the compatibility is set to
* #STUN_USAGE_TURN_COMPATIBILITY_DRAFT9
* @username: The username to use in the request
* @username_len: The length of @username
* @password: The key to use for building the MESSAGE-INTEGRITY
* @password_len: The length of @password
* @compatibility: The compatibility mode to use for building the Allocation
* request
*
* Create a new TURN Allocation request
* Returns: The length of the message to send
*/
size_t stun_usage_turn_create (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len,
StunMessage *previous_response,
StunUsageTurnRequestPorts request_ports,
int32_t bandwidth, int32_t lifetime,
uint8_t *username, size_t username_len,
uint8_t *password, size_t password_len,
StunUsageTurnCompatibility compatibility);
/**
* stun_usage_turn_create_refresh:
* @agent: The #StunAgent to use to build the request
* @msg: The #StunMessage to build
* @buffer: The buffer to use for creating the #StunMessage
* @buffer_len: The size of the @buffer
* @previous_response: If this is the first request you are sending, set this
* argument to NULL, if it's a subsequent request you are building, then set this
* argument to the response you have received. This argument is used for building
* long term credentials (using the REALM and NONCE attributes)
* @lifetime: The lifetime of the allocation to request from the server. If
* this value is negative, then no LIFETIME attribute is added to the request.
* This is only used if the compatibility is set to
* #STUN_USAGE_TURN_COMPATIBILITY_DRAFT9
* @username: The username to use in the request
* @username_len: The length of @username
* @password: The key to use for building the MESSAGE-INTEGRITY
* @password_len: The length of @password
* @compatibility: The compatibility mode to use for building the Allocation
* request
*
* Create a new TURN Refresh request
* Returns: The length of the message to send
*/
size_t stun_usage_turn_create_refresh (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len,
StunMessage *previous_response, int32_t lifetime,
uint8_t *username, size_t username_len,
uint8_t *password, size_t password_len,
StunUsageTurnCompatibility compatibility);
/**
* stun_usage_turn_create_permission:
* @agent: The #StunAgent to use to build the request
* @msg: The #StunMessage to build
* @buffer: The buffer to use for creating the #StunMessage
* @buffer_len: The size of the @buffer
* @username: The username to use in the request
* @username_len: The length of @username
* @password: The key to use for building the MESSAGE-INTEGRITY
* @password_len: The length of @password
* @realm: The realm identifier to use in the request
* @realm_len: The length of @realm
* @nonce: Unique and securely random nonce to use in the request
* @nonce_len: The length of @nonce
* @peer: Server-reflexive host address to request permission for
* @compatibility: The compatibility mode to use for building the
* CreatePermission request
*
* Create a new TURN CreatePermission request
*
* Returns: The length of the message to send
*/
size_t stun_usage_turn_create_permission (StunAgent *agent, StunMessage *msg,
uint8_t *buffer, size_t buffer_len,
uint8_t *username, size_t username_len,
uint8_t *password, size_t password_len,
uint8_t *realm, size_t realm_len,
uint8_t *nonce, size_t nonce_len,
struct sockaddr_storage *peer,
StunUsageTurnCompatibility compatibility);
/**
* stun_usage_turn_process:
* @msg: The message containing the response
* @relay_addr: A pointer to a #sockaddr structure to fill with the relay address
* that the TURN server allocated for us
* @relay_addrlen: The length of @relay_addr
* @addr: A pointer to a #sockaddr structure to fill with the mapped address
* that the STUN response contains.
* This argument will only be filled if the return value
* of the function is #STUN_USAGE_TURN_RETURN_MAPPED_SUCCESS
* @addrlen: The length of @addr
* @alternate_server: A pointer to a #sockaddr structure to fill with the
* address of an alternate server to which we should send our new STUN
* Allocate request, in case the currently used TURN server is requesting the use
* of an alternate server. This argument will only be filled if the return value
* of the function is #STUN_USAGE_TURN_RETURN_ALTERNATE_SERVER
* In the case of @STUN_USAGE_TURN_COMPATIBILITY_OC2007 compatibility, the
* @alternate_server could be filled at any time, and should only be considered
* if the request was sent to a different server than the address returned
* in the @alternate_server field
* @alternate_server_len: The length of @alternate_server
* @bandwidth: A pointer to fill with the bandwidth the TURN server allocated us
* @lifetime: A pointer to fill with the lifetime of the allocation
* @compatibility: The compatibility mode to use for processing the Allocation
* response
*
* Process a TURN Allocate response and extract the necessary information from
* the message
* Returns: A #StunUsageTurnReturn value
*/
StunUsageTurnReturn stun_usage_turn_process (StunMessage *msg,
struct sockaddr_storage *relay_addr, socklen_t *relay_addrlen,
struct sockaddr_storage *addr, socklen_t *addrlen,
struct sockaddr_storage *alternate_server, socklen_t *alternate_server_len,
uint32_t *bandwidth, uint32_t *lifetime,
StunUsageTurnCompatibility compatibility);
/**
* stun_usage_turn_refresh_process:
* @msg: The message containing the response
* @lifetime: A pointer to fill with the lifetime of the allocation
* @compatibility: The compatibility mode to use for processing the Refresh
* response
*
* Process a TURN Refresh response and extract the necessary information from
* the message
* Returns: A #StunUsageTurnReturn value. A #STUN_USAGE_TURN_RETURN_RELAY_SUCCESS
* means the Refresh was successful, but no relay address is given (kept the same
* as for the original allocation)
*/
StunUsageTurnReturn stun_usage_turn_refresh_process (StunMessage *msg,
uint32_t *lifetime, StunUsageTurnCompatibility compatibility);
# ifdef __cplusplus
}
# endif
#endif
+74
View File
@@ -0,0 +1,74 @@
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2008-2009 Collabora Ltd.
* Contact: Youness Alaoui
* (C) 2008-2009 Nokia Corporation. All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Youness Alaoui, Collabora Ltd.
* Danny Smith
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
/* ISO C9x 7.18 Integer types <stdint.h>
* Based on ISO/IEC SC22/WG14 9899 Committee draft (SC22 N2794)
*
* THIS SOFTWARE IS NOT COPYRIGHTED
*
* Contributor: Danny Smith <danny_r_smith_2001@yahoo.co.nz>
*
* This source code is offered for use in the public domain. You may
* use, modify or distribute it freely.
*
* This code is distributed in the hope that it will be useful but
* WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
* DISCLAIMED. This includes but is not limited to warranties of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Date: 2000-12-02
*/
#ifndef _WIN32_COMMON_H
#define _WIN32_COMMON_H
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
/* On MSVC, ssize_t is SSIZE_T */
#ifdef _MSC_VER
#include <BaseTsd.h>
#define ssize_t SSIZE_T
#endif
/* Windows v10.0.16232 SDK defines MSG_ERRQUEUE, but doesn't support it with
* recvmsg, and also uses a different msghdr struct */
#undef MSG_ERRQUEUE
#endif /* _WIN32_COMMON_H */