From f08095272190b77d43cf68971a36c0b943d94f24 Mon Sep 17 00:00:00 2001 From: mojyack Date: Fri, 20 Dec 2024 16:04:39 +0900 Subject: [PATCH 1/3] Return -sockerrno when conn_send() failed Currently, conn_*_send returns -1 on failure. Return more meaningful code to make debugging easier. --- src/conn_mux.c | 1 + src/conn_poll.c | 1 + src/conn_thread.c | 1 + 3 files changed, 3 insertions(+) diff --git a/src/conn_mux.c b/src/conn_mux.c index fb921aea..ca8baca1 100644 --- a/src/conn_mux.c +++ b/src/conn_mux.c @@ -522,6 +522,7 @@ int conn_mux_send(juice_agent_t *agent, const addr_record_t *dst, const char *da int ret = udp_sendto(registry_impl->sock, data, size, dst); if (ret < 0) { + ret = -sockerrno; if (sockerrno == SEAGAIN || sockerrno == SEWOULDBLOCK) JLOG_INFO("Send failed, buffer is full"); else if (sockerrno == SEMSGSIZE) diff --git a/src/conn_poll.c b/src/conn_poll.c index 3410ed2a..a8462c00 100644 --- a/src/conn_poll.c +++ b/src/conn_poll.c @@ -412,6 +412,7 @@ int conn_poll_send(juice_agent_t *agent, const addr_record_t *dst, const char *d int ret = udp_sendto(conn_impl->sock, data, size, dst); if (ret < 0) { + ret = -sockerrno; if (sockerrno == SEAGAIN || sockerrno == SEWOULDBLOCK) JLOG_INFO("Send failed, buffer is full"); else if (sockerrno == SEMSGSIZE) diff --git a/src/conn_thread.c b/src/conn_thread.c index 01074c17..1b054580 100644 --- a/src/conn_thread.c +++ b/src/conn_thread.c @@ -260,6 +260,7 @@ int conn_thread_send(juice_agent_t *agent, const addr_record_t *dst, const char int ret = udp_sendto(conn_impl->sock, data, size, dst); if (ret < 0) { + ret = -sockerrno; if (sockerrno == SEAGAIN || sockerrno == SEWOULDBLOCK) JLOG_INFO("Send failed, buffer is full"); else if (sockerrno == SEMSGSIZE) From ce730a1d00a401da9b411b6b0812b14c7757a155 Mon Sep 17 00:00:00 2001 From: mojyack Date: Fri, 20 Dec 2024 16:10:47 +0900 Subject: [PATCH 2/3] Fix "STUN binding failed" message never printed (ret < 0 && ret == SENETUNREACH) is always false --- src/agent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent.c b/src/agent.c index 099a86eb..13be4f47 100644 --- a/src/agent.c +++ b/src/agent.c @@ -1684,7 +1684,7 @@ int agent_send_stun_binding(juice_agent_t *agent, agent_stun_entry_t *entry, stu // Direct send int ret = agent_direct_send(agent, &entry->record, buffer, size, 0); if (ret < 0) { - if (ret == SENETUNREACH) + if (ret == -SENETUNREACH) JLOG_INFO("STUN binding failed: Network unreachable"); else JLOG_WARN("STUN message send failed"); From f7092668b81702a3af06713db1ff905d96fd18b1 Mon Sep 17 00:00:00 2001 From: mojyack Date: Thu, 9 Jan 2025 10:03:59 +0900 Subject: [PATCH 3/3] Introduce JUICE_ERR_{AGAIN,TOO_LARGE} Add new error codes JUICE_ERR_AGAIN and JUICE_ERR_TOO_LARGE, which can represent more detailed result of juice_send. Then update juice_send(_diffserv) to return them. --- include/juice/juice.h | 2 ++ src/juice.c | 20 +++++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/juice/juice.h b/include/juice/juice.h index b0a8eabf..9e85d800 100644 --- a/include/juice/juice.h +++ b/include/juice/juice.h @@ -41,6 +41,8 @@ extern "C" { #define JUICE_ERR_FAILED -2 // runtime error #define JUICE_ERR_NOT_AVAIL -3 // element not available #define JUICE_ERR_IGNORED -4 // ignored +#define JUICE_ERR_AGAIN -5 // buffer full +#define JUICE_ERR_TOO_LARGE -6 // datagram too large // ICE Agent diff --git a/src/juice.c b/src/juice.c index 8ebb0fd2..70be6446 100644 --- a/src/juice.c +++ b/src/juice.c @@ -85,23 +85,21 @@ JUICE_EXPORT int juice_set_remote_gathering_done(juice_agent_t *agent) { } JUICE_EXPORT int juice_send(juice_agent_t *agent, const char *data, size_t size) { - if (!agent || (!data && size)) - return JUICE_ERR_INVALID; - - if (agent_send(agent, data, size, 0) < 0) - return JUICE_ERR_FAILED; - - return JUICE_ERR_SUCCESS; + return juice_send_diffserv(agent, data, size, 0); } JUICE_EXPORT int juice_send_diffserv(juice_agent_t *agent, const char *data, size_t size, int ds) { if (!agent || (!data && size)) return JUICE_ERR_INVALID; - if (agent_send(agent, data, size, ds) < 0) - return JUICE_ERR_FAILED; - - return JUICE_ERR_SUCCESS; + int ret = agent_send(agent, data, size, ds); + if(ret >= 0) + return JUICE_ERR_SUCCESS; + if(ret == -SEAGAIN || ret == -SEWOULDBLOCK) + return JUICE_ERR_AGAIN; + if(ret == -SEMSGSIZE) + return JUICE_ERR_TOO_LARGE; + return JUICE_ERR_FAILED; } JUICE_EXPORT juice_state_t juice_get_state(juice_agent_t *agent) { return agent_get_state(agent); }