Commit 8a92ba94 authored by Youness Alaoui's avatar Youness Alaoui

Clean up utils.[ch] and document the exported functions

parent 66f58dd5
...@@ -50,108 +50,23 @@ bool stun_optional (uint16_t t) ...@@ -50,108 +50,23 @@ bool stun_optional (uint16_t t)
return (t >> 15) == 1; return (t >> 15) == 1;
} }
/**
* @return complement to the next multiple of 4.
*/
size_t stun_padding (size_t l) size_t stun_padding (size_t l)
{ {
return (4 - (l & 3)) & 3; return (4 - (l & 3)) & 3;
} }
/**
* Rounds up an integer to the next multiple of 4.
*/
size_t stun_align (size_t l) size_t stun_align (size_t l)
{ {
return (l + 3) & ~3; return (l + 3) & ~3;
} }
/**
* Reads a word from a non-aligned buffer.
* @return host byte order word value.
*/
uint16_t stun_getw (const uint8_t *ptr) uint16_t stun_getw (const uint8_t *ptr)
{ {
return ((ptr)[0] << 8) | ptr[1]; return ((ptr)[0] << 8) | ptr[1];
} }
/* /\** */
/* * @param msg valid STUN message */
/* * @return true if there is at least one unknown mandatory attribute. */
/* *\/ */
/* bool stun_has_unknown (const void *msg) */
/* { */
/* uint16_t dummy; */
/* return stun_find_unknown (msg, &dummy, 1); */
/* } */
static int debug_enabled = 1;
void stun_debug_enable (void) {
debug_enabled = 1;
}
void stun_debug_disable (void) {
debug_enabled = 0;
}
void stun_debug (const char *fmt, ...)
{
va_list ap;
if (debug_enabled) {
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
}
}
void stun_debug_bytes (const void *data, size_t len)
{
size_t i;
stun_debug ("0x");
for (i = 0; i < len; i++)
stun_debug ("%02x", ((const unsigned char *)data)[i]);
}
StunMessageReturn stun_xor_address (const StunMessage *msg,
struct sockaddr *addr, socklen_t addrlen,
uint32_t magic_cookie)
{
switch (addr->sa_family)
{
case AF_INET:
{
struct sockaddr_in *ip4 = (struct sockaddr_in *)addr;
if ((size_t) addrlen < sizeof (*ip4))
return STUN_MESSAGE_RETURN_INVALID;
ip4->sin_port ^= htons (magic_cookie >> 16);
ip4->sin_addr.s_addr ^= htonl (magic_cookie);
return STUN_MESSAGE_RETURN_SUCCESS;
}
case AF_INET6:
{
struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)addr;
unsigned short i;
if ((size_t) addrlen < sizeof (*ip6))
return STUN_MESSAGE_RETURN_INVALID;
ip6->sin6_port ^= htons (magic_cookie >> 16);
for (i = 0; i < 16; i++)
ip6->sin6_addr.s6_addr[i] ^= msg->buffer[4 + i];
return STUN_MESSAGE_RETURN_SUCCESS;
}
}
return STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS;
}
void *stun_setw (uint8_t *ptr, uint16_t value) void *stun_setw (uint8_t *ptr, uint16_t value)
{ {
*ptr++ = value >> 8; *ptr++ = value >> 8;
...@@ -173,11 +88,6 @@ void stun_set_type (uint8_t *h, StunClass c, StunMethod m) ...@@ -173,11 +88,6 @@ void stun_set_type (uint8_t *h, StunClass c, StunMethod m)
/* assert (stun_get_method (h) == m); */ /* assert (stun_get_method (h) == m); */
} }
/**
* @param code host-byte order error code
* @return a static pointer to a nul-terminated error message string.
*/
const char *stun_strerror (StunError code) const char *stun_strerror (StunError code)
{ {
static const struct static const struct
...@@ -221,3 +131,66 @@ const char *stun_strerror (StunError code) ...@@ -221,3 +131,66 @@ const char *stun_strerror (StunError code)
// assert (strlen (str) < 128); // assert (strlen (str) < 128);
return str; return str;
} }
StunMessageReturn stun_xor_address (const StunMessage *msg,
struct sockaddr *addr, socklen_t addrlen,
uint32_t magic_cookie)
{
switch (addr->sa_family)
{
case AF_INET:
{
struct sockaddr_in *ip4 = (struct sockaddr_in *)addr;
if ((size_t) addrlen < sizeof (*ip4))
return STUN_MESSAGE_RETURN_INVALID;
ip4->sin_port ^= htons (magic_cookie >> 16);
ip4->sin_addr.s_addr ^= htonl (magic_cookie);
return STUN_MESSAGE_RETURN_SUCCESS;
}
case AF_INET6:
{
struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)addr;
unsigned short i;
if ((size_t) addrlen < sizeof (*ip6))
return STUN_MESSAGE_RETURN_INVALID;
ip6->sin6_port ^= htons (magic_cookie >> 16);
for (i = 0; i < 16; i++)
ip6->sin6_addr.s6_addr[i] ^= msg->buffer[4 + i];
return STUN_MESSAGE_RETURN_SUCCESS;
}
}
return STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS;
}
static int debug_enabled = 1;
void stun_debug_enable (void) {
debug_enabled = 1;
}
void stun_debug_disable (void) {
debug_enabled = 0;
}
void stun_debug (const char *fmt, ...)
{
va_list ap;
if (debug_enabled) {
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
}
}
void stun_debug_bytes (const void *data, size_t len)
{
size_t i;
stun_debug ("0x");
for (i = 0; i < len; i++)
stun_debug ("%02x", ((const unsigned char *)data)[i]);
}
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
#ifndef STUN_UTILS_H #ifndef STUN_UTILS_H
# define STUN_UTILS_H 1 # define STUN_UTILS_H 1
/** /*
* @file utils.h * @file utils.h
* @brief STUN client generic utility functions * @brief STUN client generic utility functions
*/ */
...@@ -56,28 +56,60 @@ extern "C" { ...@@ -56,28 +56,60 @@ extern "C" {
# endif # endif
/**
* stun_optional:
* @t: An attribute type
*
* Helper function that checks whether a STUN attribute is a mandatory
* or an optional attribute
*
* Returns: %TRUE if the attribute is an optional one
*/
bool stun_optional (uint16_t t); bool stun_optional (uint16_t t);
size_t stun_padding (size_t l); size_t stun_padding (size_t l);
size_t stun_align (size_t l); size_t stun_align (size_t l);
uint16_t stun_getw (const uint8_t *ptr); uint16_t stun_getw (const uint8_t *ptr);
void stun_debug_enable (void); void *stun_setw (uint8_t *ptr, uint16_t value);
void stun_debug_disable (void);
void stun_debug (const char *fmt, ...); void stun_set_type (uint8_t *h, StunClass c, StunMethod m);
void stun_debug_bytes (const void *data, size_t len);
StunMessageReturn stun_xor_address (const StunMessage *msg, StunMessageReturn stun_xor_address (const StunMessage *msg,
struct sockaddr *addr, socklen_t addrlen, struct sockaddr *addr, socklen_t addrlen,
uint32_t magic_cookie); uint32_t magic_cookie);
void *stun_setw (uint8_t *ptr, uint16_t value); /**
* stun_strerror:
* @code: host-byte order error code
*
* Transforms a STUN error-code into a human readable string
*
* Returns: A static pointer to a NULL-terminated error message string.
*/
const char *stun_strerror (StunError code);
void stun_set_type (uint8_t *h, StunClass c, StunMethod m); /**
* 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);
void stun_debug (const char *fmt, ...);
void stun_debug_bytes (const void *data, size_t len);
const char *stun_strerror (StunError code);
# ifdef __cplusplus # ifdef __cplusplus
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment