Commit f73f9b5c authored by Kai Vehmanen's avatar Kai Vehmanen

Convert indentation from tabs to spaces.

darcs-hash:20070918150713-77cd4-7bdd3f21524c846c1bf0c2c8cb96eb6104c96ad8.gz
parent d3e16af2
......@@ -59,7 +59,7 @@
struct stun_bind_s
{
stun_trans_t trans;
stun_trans_t trans;
};
......@@ -81,37 +81,37 @@ static int
stun_bind_alloc (stun_bind_t **restrict context, int fd,
const struct sockaddr *restrict srv, socklen_t srvlen)
{
int val;
int val;
stun_bind_t *ctx = malloc (sizeof (*ctx));
if (ctx == NULL)
return ENOMEM;
stun_bind_t *ctx = malloc (sizeof (*ctx));
if (ctx == NULL)
return ENOMEM;
memset (ctx, 0, sizeof (*ctx));
*context = ctx;
memset (ctx, 0, sizeof (*ctx));
*context = ctx;
val = (fd != -1)
? stun_trans_init (&ctx->trans, fd, srv, srvlen)
: stun_trans_create (&ctx->trans, SOCK_DGRAM, 0, srv, srvlen);
val = (fd != -1)
? stun_trans_init (&ctx->trans, fd, srv, srvlen)
: stun_trans_create (&ctx->trans, SOCK_DGRAM, 0, srv, srvlen);
if (val)
{
free (ctx);
return val;
}
if (val)
{
free (ctx);
return val;
}
stun_init_request (ctx->trans.msg.buf, STUN_BINDING);
return 0;
stun_init_request (ctx->trans.msg.buf, STUN_BINDING);
return 0;
}
static int
stun_bind_launch (stun_bind_t *ctx)
{
int val = stun_trans_start (&ctx->trans);
if (val)
stun_bind_cancel (ctx);
return val;
int val = stun_trans_start (&ctx->trans);
if (val)
stun_bind_cancel (ctx);
return val;
}
......@@ -119,30 +119,30 @@ int stun_bind_start (stun_bind_t **restrict context, int fd,
const struct sockaddr *restrict srv,
socklen_t srvlen)
{
stun_bind_t *ctx;
stun_bind_t *ctx;
int val = stun_bind_alloc (context, fd, srv, srvlen);
if (val)
return val;
int val = stun_bind_alloc (context, fd, srv, srvlen);
if (val)
return val;
ctx = *context;
ctx = *context;
ctx->trans.msg.length = sizeof (ctx->trans.msg.buf);
val = stun_finish (ctx->trans.msg.buf, &ctx->trans.msg.length);
if (val)
{
stun_bind_cancel (ctx);
return val;
}
ctx->trans.msg.length = sizeof (ctx->trans.msg.buf);
val = stun_finish (ctx->trans.msg.buf, &ctx->trans.msg.length);
if (val)
{
stun_bind_cancel (ctx);
return val;
}
return stun_bind_launch (ctx);
return stun_bind_launch (ctx);
}
void stun_bind_cancel (stun_bind_t *context)
{
stun_trans_deinit (&context->trans);
free (context);
stun_trans_deinit (&context->trans);
free (context);
}
......@@ -150,17 +150,17 @@ void stun_bind_cancel (stun_bind_t *context)
unsigned stun_bind_timeout (const stun_bind_t *context)
{
assert (context != NULL);
return stun_trans_timeout (&context->trans);
assert (context != NULL);
return stun_trans_timeout (&context->trans);
}
int stun_bind_elapse (stun_bind_t *context)
{
int val = stun_trans_tick (&context->trans);
if (val != EAGAIN)
stun_bind_cancel (context);
return val;
int val = stun_trans_tick (&context->trans);
if (val != EAGAIN)
stun_bind_cancel (context);
return val;
}
......@@ -170,40 +170,40 @@ int stun_bind_process (stun_bind_t *restrict ctx,
const void *restrict buf, size_t len,
struct sockaddr *restrict addr, socklen_t *addrlen)
{
int val, code;
assert (ctx != NULL);
val = stun_trans_preprocess (&ctx->trans, &code, buf, len);
switch (val)
{
case EAGAIN:
return EAGAIN;
case 0:
break;
default:
if (code == STUN_ROLE_CONFLICT)
val = ECONNRESET;
stun_bind_cancel (ctx);
return val;
}
val = stun_find_xor_addr (buf, STUN_XOR_MAPPED_ADDRESS, addr, addrlen);
if (val)
{
DBG (" No XOR-MAPPED-ADDRESS: %s\n", strerror (val));
val = stun_find_addr (buf, STUN_MAPPED_ADDRESS, addr, addrlen);
if (val)
{
DBG (" No MAPPED-ADDRESS: %s\n", strerror (val));
stun_bind_cancel (ctx);
return val;
}
}
DBG (" Mapped address found!\n");
stun_bind_cancel (ctx);
return 0;
int val, code;
assert (ctx != NULL);
val = stun_trans_preprocess (&ctx->trans, &code, buf, len);
switch (val)
{
case EAGAIN:
return EAGAIN;
case 0:
break;
default:
if (code == STUN_ROLE_CONFLICT)
val = ECONNRESET;
stun_bind_cancel (ctx);
return val;
}
val = stun_find_xor_addr (buf, STUN_XOR_MAPPED_ADDRESS, addr, addrlen);
if (val)
{
DBG (" No XOR-MAPPED-ADDRESS: %s\n", strerror (val));
val = stun_find_addr (buf, STUN_MAPPED_ADDRESS, addr, addrlen);
if (val)
{
DBG (" No MAPPED-ADDRESS: %s\n", strerror (val));
stun_bind_cancel (ctx);
return val;
}
}
DBG (" Mapped address found!\n");
stun_bind_cancel (ctx);
return 0;
}
......@@ -213,28 +213,28 @@ int stun_bind_run (int fd,
const struct sockaddr *restrict srv, socklen_t srvlen,
struct sockaddr *restrict addr, socklen_t *addrlen)
{
stun_bind_t *ctx;
uint8_t buf[STUN_MAXMSG];
ssize_t val;
val = stun_bind_start (&ctx, fd, srv, srvlen);
if (val)
return val;
do
{
val = stun_trans_recv (&ctx->trans, buf, sizeof (buf));
if (val == -1)
{
val = errno;
continue;
}
val = stun_bind_process (ctx, buf, val, addr, addrlen);
}
while (val == EAGAIN);
return val;
stun_bind_t *ctx;
uint8_t buf[STUN_MAXMSG];
ssize_t val;
val = stun_bind_start (&ctx, fd, srv, srvlen);
if (val)
return val;
do
{
val = stun_trans_recv (&ctx->trans, buf, sizeof (buf));
if (val == -1)
{
val = errno;
continue;
}
val = stun_bind_process (ctx, buf, val, addr, addrlen);
}
while (val == EAGAIN);
return val;
}
......@@ -244,19 +244,19 @@ int
stun_bind_keepalive (int fd, const struct sockaddr *restrict srv,
socklen_t srvlen)
{
uint8_t buf[28];
size_t len = sizeof (buf);
int val;
stun_init_indication (buf, STUN_BINDING);
val = stun_finish (buf, &len);
assert (val == 0);
(void)val;
/* NOTE: hopefully, this is only needed for non-stream sockets */
if (stun_sendto (fd, buf, len, srv, srvlen) == -1)
return errno;
return 0;
uint8_t buf[28];
size_t len = sizeof (buf);
int val;
stun_init_indication (buf, STUN_BINDING);
val = stun_finish (buf, &len);
assert (val == 0);
(void)val;
/* NOTE: hopefully, this is only needed for non-stream sockets */
if (stun_sendto (fd, buf, len, srv, srvlen) == -1)
return errno;
return 0;
}
......@@ -270,58 +270,58 @@ stun_conncheck_start (stun_bind_t **restrict context, int fd,
bool cand_use, bool controlling, uint32_t priority,
uint64_t tie)
{
int val;
stun_bind_t *ctx;
assert (username != NULL);
assert (password != NULL);
val = stun_bind_alloc (&ctx, fd, srv, srvlen);
if (val)
return val;
ctx->trans.key.length = strlen (password);
ctx->trans.key.value = malloc (ctx->trans.key.length);
if (ctx->trans.key.value == NULL)
{
val = ENOMEM;
goto error;
}
*context = ctx;
memcpy (ctx->trans.key.value, password, ctx->trans.key.length);
if (cand_use)
{
val = stun_append_flag (ctx->trans.msg.buf,
sizeof (ctx->trans.msg.buf),
STUN_USE_CANDIDATE);
if (val)
goto error;
}
val = stun_append32 (ctx->trans.msg.buf, sizeof (ctx->trans.msg.buf),
STUN_PRIORITY, priority);
if (val)
goto error;
val = stun_append64 (ctx->trans.msg.buf, sizeof (ctx->trans.msg.buf),
controlling ? STUN_ICE_CONTROLLING
: STUN_ICE_CONTROLLED, tie);
if (val)
goto error;
ctx->trans.msg.length = sizeof (ctx->trans.msg.buf);
val = stun_finish_short (ctx->trans.msg.buf, &ctx->trans.msg.length,
username, password, NULL);
if (val)
goto error;
return stun_bind_launch (ctx);
int val;
stun_bind_t *ctx;
assert (username != NULL);
assert (password != NULL);
val = stun_bind_alloc (&ctx, fd, srv, srvlen);
if (val)
return val;
ctx->trans.key.length = strlen (password);
ctx->trans.key.value = malloc (ctx->trans.key.length);
if (ctx->trans.key.value == NULL)
{
val = ENOMEM;
goto error;
}
*context = ctx;
memcpy (ctx->trans.key.value, password, ctx->trans.key.length);
if (cand_use)
{
val = stun_append_flag (ctx->trans.msg.buf,
sizeof (ctx->trans.msg.buf),
STUN_USE_CANDIDATE);
if (val)
goto error;
}
val = stun_append32 (ctx->trans.msg.buf, sizeof (ctx->trans.msg.buf),
STUN_PRIORITY, priority);
if (val)
goto error;
val = stun_append64 (ctx->trans.msg.buf, sizeof (ctx->trans.msg.buf),
controlling ? STUN_ICE_CONTROLLING
: STUN_ICE_CONTROLLED, tie);
if (val)
goto error;
ctx->trans.msg.length = sizeof (ctx->trans.msg.buf);
val = stun_finish_short (ctx->trans.msg.buf, &ctx->trans.msg.length,
username, password, NULL);
if (val)
goto error;
return stun_bind_launch (ctx);
error:
stun_bind_cancel (*context);
return val;
stun_bind_cancel (*context);
return val;
}
......@@ -52,20 +52,20 @@ static inline uint32_t crc32 (const struct iovec *iov, size_t size);
uint32_t stun_fingerprint (const uint8_t *msg, size_t len)
{
struct iovec iov[3];
uint16_t fakelen = htons (len - 20u);
struct iovec iov[3];
uint16_t fakelen = htons (len - 20u);
assert (len >= 28u);
assert (len >= 28u);
iov[0].iov_base = (void *)msg;
iov[0].iov_len = 2;
iov[1].iov_base = &fakelen;
iov[1].iov_len = 2;
iov[2].iov_base = (void *)(msg + 4);
/* first 4 bytes done, last 8 bytes not summed */
iov[2].iov_len = len - 12u;
iov[0].iov_base = (void *)msg;
iov[0].iov_len = 2;
iov[1].iov_base = &fakelen;
iov[1].iov_len = 2;
iov[2].iov_base = (void *)(msg + 4);
/* first 4 bytes done, last 8 bytes not summed */
iov[2].iov_len = len - 12u;
return crc32 (iov, sizeof (iov) / sizeof (iov[0])) ^ 0x5354554e;
return crc32 (iov, sizeof (iov) / sizeof (iov[0])) ^ 0x5354554e;
}
/*-
......@@ -165,17 +165,17 @@ static const uint32_t crc32_tab[] = {
static inline
uint32_t crc32 (const struct iovec *iov, size_t n)
{
size_t i;
uint32_t crc = 0xffffffff;
size_t i;
uint32_t crc = 0xffffffff;
for (i = 0; i < n; i++)
{
const uint8_t *p = iov[i].iov_base;
size_t size = iov[i].iov_len;
for (i = 0; i < n; i++)
{
const uint8_t *p = iov[i].iov_base;
size_t size = iov[i].iov_len;
while (size--)
crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
}
while (size--)
crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
}
return crc ^ 0xffffffff;
return crc ^ 0xffffffff;
}
......@@ -53,71 +53,71 @@
void stun_sha1 (const uint8_t *msg, size_t len, uint8_t *sha,
const void *restrict key, size_t keylen)
{
HMAC_CTX ctx;
uint16_t fakelen = htons (len - 20u);
assert (len >= 44u);
HMAC_CTX_init (&ctx);
HMAC_Init_ex (&ctx, key, keylen, EVP_sha1 (), NULL);
HMAC_Update (&ctx, msg, 2);
HMAC_Update (&ctx, (const uint8_t *)&fakelen, 2);
/* first 4 bytes done, last 24 bytes not summed */
HMAC_Update (&ctx, msg + 4, len - 28u);
HMAC_Final (&ctx, sha, NULL);
HMAC_CTX_cleanup (&ctx);
HMAC_CTX ctx;
uint16_t fakelen = htons (len - 20u);
assert (len >= 44u);
HMAC_CTX_init (&ctx);
HMAC_Init_ex (&ctx, key, keylen, EVP_sha1 (), NULL);
HMAC_Update (&ctx, msg, 2);
HMAC_Update (&ctx, (const uint8_t *)&fakelen, 2);
/* first 4 bytes done, last 24 bytes not summed */
HMAC_Update (&ctx, msg + 4, len - 28u);
HMAC_Final (&ctx, sha, NULL);
HMAC_CTX_cleanup (&ctx);
}
void stun_hash_creds (const char *realm, const char *login, const char *pw,
unsigned char md5[16])
{
EVP_MD_CTX ctx;
assert (realm && login && pw && md5);
EVP_MD_CTX_init (&ctx);
EVP_DigestInit_ex (&ctx, EVP_md5 (), NULL);
EVP_DigestUpdate (&ctx, realm, strlen (realm));
EVP_DigestUpdate (&ctx, ":", 1);
EVP_DigestUpdate (&ctx, login, strlen (login));
EVP_DigestUpdate (&ctx, ":", 1);
EVP_DigestUpdate (&ctx, pw, strlen (pw));
EVP_DigestFinal (&ctx, md5, NULL);
EVP_MD_CTX ctx;
assert (realm && login && pw && md5);
EVP_MD_CTX_init (&ctx);
EVP_DigestInit_ex (&ctx, EVP_md5 (), NULL);
EVP_DigestUpdate (&ctx, realm, strlen (realm));
EVP_DigestUpdate (&ctx, ":", 1);
EVP_DigestUpdate (&ctx, login, strlen (login));
EVP_DigestUpdate (&ctx, ":", 1);
EVP_DigestUpdate (&ctx, pw, strlen (pw));
EVP_DigestFinal (&ctx, md5, NULL);
}
void stun_make_transid (stun_transid_t id)
{
/*
* transid = (HMAC_SHA1 (secret, counter) >> 64)
* This consumes sizeof (secret) bytes of entropy every 2^64 messages.
*/
static struct
{
pthread_mutex_t lock;
uint64_t counter;
uint8_t secret[16];
} store = { PTHREAD_MUTEX_INITIALIZER, 0, "" };
union
{
uint64_t value;
uint8_t bytes[1];
} counter;
uint8_t key[16], sha[20];
pthread_mutex_lock (&store.lock);
counter.value = store.counter++;
if (counter.value == 0)
RAND_pseudo_bytes (store.secret, sizeof (store.secret));
memcpy (key, store.secret, sizeof (key));
pthread_mutex_unlock (&store.lock);
/* Computes hash out of contentious area */
HMAC (EVP_sha1 (), key, sizeof (key), counter.bytes, sizeof (counter),
sha, NULL);
memcpy (id, sha, 12);
/*
* transid = (HMAC_SHA1 (secret, counter) >> 64)
* This consumes sizeof (secret) bytes of entropy every 2^64 messages.
*/
static struct
{
pthread_mutex_t lock;
uint64_t counter;
uint8_t secret[16];
} store = { PTHREAD_MUTEX_INITIALIZER, 0, "" };
union
{
uint64_t value;
uint8_t bytes[1];
} counter;
uint8_t key[16], sha[20];
pthread_mutex_lock (&store.lock);
counter.value = store.counter++;
if (counter.value == 0)
RAND_pseudo_bytes (store.secret, sizeof (store.secret));
memcpy (key, store.secret, sizeof (key));
pthread_mutex_unlock (&store.lock);
/* Computes hash out of contentious area */
HMAC (EVP_sha1 (), key, sizeof (key), counter.bytes, sizeof (counter),
sha, NULL);
memcpy (id, sha, 12);
}
......@@ -48,7 +48,7 @@ static uint8_t unique_id[UNIQUE_SIZE];
static void generate_unique_id (void)
{
RAND_pseudo_bytes (unique_id, sizeof (unique_id));
RAND_pseudo_bytes (unique_id, sizeof (unique_id));
}
......@@ -56,45 +56,45 @@ static void
stun_generate_nonce (uint8_t *nonce, time_t now,
const struct sockaddr_storage *restrict addr)
{
static pthread_once_t once = PTHREAD_ONCE_INIT;
HMAC_CTX ctx;
uint32_t stamp = now;
static pthread_once_t once = PTHREAD_ONCE_INIT;
HMAC_CTX ctx;
uint32_t stamp = now;
pthread_once (&once, generate_unique_id);
pthread_once (&once, generate_unique_id);
/*
* Nonce are generated from the current time and the client address and
* port number, keyed with a pseudo-random secret.
*/
HMAC_CTX_init (&ctx);
HMAC_Init_ex (&ctx, unique_id, sizeof (unique_id), EVP_sha1 (), NULL);
HMAC_Update (&ctx, &stamp, 4);
HMAC_Update (&ctx, &ss->family, sizeof (ss->family));
switch (addr->ss_family)
{
case AF_INET:
{
const struct sockaddr_in *ip4 = (const struct sockaddr_in *)addr;
HMAC_Update (&ctx, &ip4->sin_addr, 4);
HMAC_Update (&ctx, &ip4->sin_port, 2);
break;
}
/*
* Nonce are generated from the current time and the client address and
* port number, keyed with a pseudo-random secret.
*/
HMAC_CTX_init (&ctx);
HMAC_Init_ex (&ctx, unique_id, sizeof (unique_id), EVP_sha1 (), NULL);
HMAC_Update (&ctx, &stamp, 4);
HMAC_Update (&ctx, &ss->family, sizeof (ss->family));
switch (addr->ss_family)
{
case AF_INET:
{
const struct sockaddr_in *ip4 = (const struct sockaddr_in *)addr;
HMAC_Update (&ctx, &ip4->sin_addr, 4);
HMAC_Update (&ctx, &ip4->sin_port, 2);
break;
}
case AF_INET6:
{
const struct sockaddr_in6*ip6 = (const struct sockaddr_in6*)addr;
HMAC_Update (&ctx, &ip6->sin6_addr, 16);
HMAC_Update (&ctx, &ip6->sin6_port, 2);
if (IN6_IS_ADDR_LINK_LOCAL (&ip6->sin6_addr))
HMAC_Update (&ctx, &ip6->sin6_scope_id
sizeof (ip6->sin6_scope_id));
break;
}
}
case AF_INET6:
{
const struct sockaddr_in6*ip6 = (const struct sockaddr_in6*)addr;
HMAC_Update (&ctx, &ip6->sin6_addr, 16);
HMAC_Update (&ctx, &ip6->sin6_port, 2);
if (IN6_IS_ADDR_LINK_LOCAL (&ip6->sin6_addr))
HMAC_Update (&ctx, &ip6->sin6_scope_id
sizeof (ip6->sin6_scope_id));
break;
}
}
HMAC_Final (&ctx, nonce, NULL);
HMAC_CTX_cleanup (&ctx);
memcpy (nonce + 20, &stamp, 4);
HMAC_Final (&ctx, nonce, NULL);
HMAC_CTX_cleanup (&ctx);
memcpy (nonce + 20, &stamp, 4);
}
......@@ -102,9 +102,9 @@ static int
stun_append_nonce (uint8_t *buf, size_t buflen,