Commit b11d5a07 authored by Youness Alaoui's avatar Youness Alaoui

Port stun usage trans to use a StunUsageTransReturn enum instead of errno...

Port stun usage trans to use a StunUsageTransReturn enum instead of errno values to communicate errors
parent 809cea41
...@@ -43,16 +43,6 @@ ...@@ -43,16 +43,6 @@
#ifdef _WIN32 #ifdef _WIN32
#define ENOENT -1
#define EINVAL -2
#define ENOBUFS -3
#define EAFNOSUPPORT -4
#define EPROTO -5
#define EACCES -6
#define EINPROGRESS -7
#define EAGAIN -8
#define ENOSYS -9
#define close closesocket #define close closesocket
#else #else
#include <errno.h> #include <errno.h>
...@@ -66,13 +56,13 @@ ...@@ -66,13 +56,13 @@
#include "trans.h" #include "trans.h"
int stun_trans_init (stun_trans_t *tr, int fd, StunUsageTransReturn stun_trans_init (stun_trans_t *tr, int fd,
const struct sockaddr *srv, socklen_t srvlen) const struct sockaddr *srv, socklen_t srvlen)
{ {
assert (fd != -1); assert (fd != -1);
if ((size_t) srvlen > sizeof (tr->dst)) if ((size_t) srvlen > sizeof (tr->dst))
return ENOBUFS; return STUN_USAGE_TRANS_RETURN_INVALID_ADDRESS;
tr->own_fd = -1; tr->own_fd = -1;
tr->fd = fd; tr->fd = fd;
...@@ -80,7 +70,7 @@ int stun_trans_init (stun_trans_t *tr, int fd, ...@@ -80,7 +70,7 @@ int stun_trans_init (stun_trans_t *tr, int fd,
tr->dstlen = srvlen; tr->dstlen = srvlen;
memcpy (&tr->dst, srv, srvlen); memcpy (&tr->dst, srv, srvlen);
return 0; return STUN_USAGE_TRANS_RETURN_SUCCESS;
} }
...@@ -122,25 +112,24 @@ static int stun_socket (int family, int type, int proto) ...@@ -122,25 +112,24 @@ static int stun_socket (int family, int type, int proto)
} }
int stun_trans_create (stun_trans_t *tr, int type, int proto, StunUsageTransReturn stun_trans_create (stun_trans_t *tr, int type, int proto,
const struct sockaddr *srv, socklen_t srvlen) const struct sockaddr *srv, socklen_t srvlen)
{ {
int val, fd; StunUsageTransReturn val = STUN_USAGE_TRANS_RETURN_ERROR;
int fd;
if ((size_t) srvlen < sizeof(*srv)) if ((size_t) srvlen < sizeof(*srv))
return EINVAL; return STUN_USAGE_TRANS_RETURN_INVALID_ADDRESS;
fd = stun_socket (srv->sa_family, type, proto); fd = stun_socket (srv->sa_family, type, proto);
if (fd == -1) if (fd == -1)
return errno; return STUN_USAGE_TRANS_RETURN_ERROR;
if (connect (fd, srv, srvlen) && if (connect (fd, srv, srvlen) &&
#ifdef _WIN32 #ifdef _WIN32
(WSAGetLastError () != WSAEINPROGRESS)) { (WSAGetLastError () != WSAEINPROGRESS)) {
val = WSAGetLastError ();
#else #else
(errno != EINPROGRESS)) { (errno != EINPROGRESS)) {
val = errno;
#endif #endif
goto error; goto error;
} }
...@@ -150,7 +139,7 @@ int stun_trans_create (stun_trans_t *tr, int type, int proto, ...@@ -150,7 +139,7 @@ int stun_trans_create (stun_trans_t *tr, int type, int proto,
goto error; goto error;
tr->own_fd = tr->fd; tr->own_fd = tr->fd;
return 0; return STUN_USAGE_TRANS_RETURN_SUCCESS;
error: error:
close (fd); close (fd);
...@@ -241,8 +230,8 @@ ssize_t stun_trans_recvfrom (stun_trans_t *tr, uint8_t *buf, size_t maxlen, ...@@ -241,8 +230,8 @@ ssize_t stun_trans_recvfrom (stun_trans_t *tr, uint8_t *buf, size_t maxlen,
else else
val = recv (tr->fd, (void *)buf, maxlen, flags); val = recv (tr->fd, (void *)buf, maxlen, flags);
if ((val == -1) && stun_err_dequeue (tr->fd)) if (val == -1)
errno = EAGAIN; stun_err_dequeue (tr->fd);
return val; return val;
} }
...@@ -262,7 +251,7 @@ int stun_trans_fd (const stun_trans_t *tr) ...@@ -262,7 +251,7 @@ int stun_trans_fd (const stun_trans_t *tr)
* @return ETIMEDOUT if the transaction has timed out, or 0 if an incoming * @return ETIMEDOUT if the transaction has timed out, or 0 if an incoming
* message needs to be processed. * message needs to be processed.
*/ */
int stun_trans_poll (stun_trans_t *tr, unsigned int delay) StunUsageTransReturn stun_trans_poll (stun_trans_t *tr, unsigned int delay)
{ {
#ifdef HAVE_POLL #ifdef HAVE_POLL
struct pollfd ufd; struct pollfd ufd;
...@@ -273,12 +262,12 @@ int stun_trans_poll (stun_trans_t *tr, unsigned int delay) ...@@ -273,12 +262,12 @@ int stun_trans_poll (stun_trans_t *tr, unsigned int delay)
ufd.events |= POLLIN; ufd.events |= POLLIN;
if (poll (&ufd, 1, delay) <= 0) { if (poll (&ufd, 1, delay) <= 0) {
return EAGAIN; return STUN_USAGE_TRANS_RETURN_RETRY;
} }
return 0; return STUN_USAGE_TRANS_RETURN_SUCCESS;
#else #else
(void)tr; (void)tr;
return ENOSYS; return STUN_USAGE_TRANS_RETURN_UNSUPPORTED;
#endif #endif
} }
...@@ -65,6 +65,14 @@ typedef struct stun_trans_s ...@@ -65,6 +65,14 @@ typedef struct stun_trans_s
} stun_trans_t; } stun_trans_t;
typedef enum {
STUN_USAGE_TRANS_RETURN_SUCCESS,
STUN_USAGE_TRANS_RETURN_ERROR,
STUN_USAGE_TRANS_RETURN_RETRY,
STUN_USAGE_TRANS_RETURN_INVALID_ADDRESS,
STUN_USAGE_TRANS_RETURN_UNSUPPORTED,
} StunUsageTransReturn;
# ifdef __cplusplus # ifdef __cplusplus
extern "C" { extern "C" {
# endif # endif
...@@ -77,7 +85,7 @@ extern "C" { ...@@ -77,7 +85,7 @@ extern "C" {
* @param srv STUN server socket address (ignored if @a srvlen is 0) * @param srv STUN server socket address (ignored if @a srvlen is 0)
* @param srvlen STUN server socket address length (or 0 @a fd is connected) * @param srvlen STUN server socket address length (or 0 @a fd is connected)
*/ */
int stun_trans_init (stun_trans_t *tr, int fd, StunUsageTransReturn stun_trans_init (stun_trans_t *tr, int fd,
const struct sockaddr *srv, socklen_t srvlen); const struct sockaddr *srv, socklen_t srvlen);
/** /**
...@@ -89,7 +97,7 @@ int stun_trans_init (stun_trans_t *tr, int fd, ...@@ -89,7 +97,7 @@ int stun_trans_init (stun_trans_t *tr, int fd,
* @param srv STUN server socket address (ignored if @a srvlen is 0) * @param srv STUN server socket address (ignored if @a srvlen is 0)
* @param srvlen STUN server socket address length (or 0 @a fd is connected) * @param srvlen STUN server socket address length (or 0 @a fd is connected)
*/ */
int stun_trans_create (stun_trans_t *tr, int sotype, int proto, StunUsageTransReturn stun_trans_create (stun_trans_t *tr, int sotype, int proto,
const struct sockaddr *srv, socklen_t srvlen); const struct sockaddr *srv, socklen_t srvlen);
/** /**
...@@ -105,7 +113,7 @@ void stun_trans_deinit (stun_trans_t *tr); ...@@ -105,7 +113,7 @@ void stun_trans_deinit (stun_trans_t *tr);
* Always succeeds. * Always succeeds.
*/ */
int stun_trans_fd (const stun_trans_t *tr); int stun_trans_fd (const stun_trans_t *tr);
int stun_trans_poll (stun_trans_t *tr, unsigned int delay); StunUsageTransReturn stun_trans_poll (stun_trans_t *tr, unsigned int delay);
/** /**
* Safe wrapper around sendto() * Safe wrapper around sendto()
......
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