From 08e88dc44a48f6aee452b227ddd18678fd89ea05 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 10 Jul 2023 17:34:22 +0200 Subject: [PATCH 01/12] ice: add candidate sdp mdns support --- src/ice/icesdp.c | 112 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 5 deletions(-) diff --git a/src/ice/icesdp.c b/src/ice/icesdp.c index b8ca88571..de8bf41ba 100644 --- a/src/ice/icesdp.c +++ b/src/ice/icesdp.c @@ -3,6 +3,10 @@ * * Copyright (C) 2010 Creytiv.com */ +#ifndef WIN32 +#include +#endif + #include #include #include @@ -14,6 +18,7 @@ #include #include #include +#include #include "ice.h" @@ -34,6 +39,19 @@ static const char rel_addr_str[] = "raddr"; static const char rel_port_str[] = "rport"; +struct rcand { + struct icem *icem; + enum ice_cand_type type; + unsigned cid; + uint32_t prio; + uint32_t port; + struct sa caddr; + struct sa rel_addr; + struct pl foundation; + char domain[128]; +}; + + /* Encode SDP Attributes */ @@ -186,6 +204,67 @@ static int media_pwd_decode(struct icem *icem, const char *value) } +static int getaddr_rcand(void *arg) +{ + struct rcand *rcand = arg; + struct addrinfo *res, *res0 = NULL; + struct addrinfo hints; + int err; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG; + + err = getaddrinfo(rcand->domain, NULL, &hints, &res0); + if (err) + return EADDRNOTAVAIL; + + for (res = res0; res; res = res->ai_next) { + + err = sa_set_sa(&rcand->caddr, res->ai_addr); + if (err) + continue; + + break; + } + + sa_set_port(&rcand->caddr, rcand->port); + + freeaddrinfo(res); + + return 0; +} + + +static void delayed_rcand(int err, void *arg) +{ + struct rcand *rcand = arg; + + if (err) + goto out; + + /* add only if not exist */ + if (icem_cand_find(&rcand->icem->rcandl, rcand->cid, &rcand->caddr)) + goto out; + + icem_rcand_add(rcand->icem, rcand->type, rcand->cid, rcand->prio, + &rcand->caddr, &rcand->rel_addr, &rcand->foundation); + +out: + mem_deref(rcand); +} + + +static void rcand_dealloc(void *arg) +{ + struct rcand *rcand = arg; + + mem_deref(rcand->icem); + mem_deref((char *)rcand->foundation.p); + mem_deref(rcand->domain); +} + + static int cand_decode(struct icem *icem, const char *val) { struct pl foundation, compid, transp, prio, addr, port, cand_type; @@ -233,18 +312,41 @@ static int cand_decode(struct icem *icem, const char *val) } } + (void)pl_strcpy(&cand_type, type, sizeof(type)); + cid = pl_u32(&compid); + err = sa_set(&caddr, &addr, pl_u32(&port)); - if (err) - return err; + if (err) { + if (err != EINVAL) + return err; - cid = pl_u32(&compid); + /* try non blocking getaddr mdns resolution */ + struct rcand *rcand = + mem_zalloc(sizeof(struct rcand), rcand_dealloc); + if (!rcand) + return ENOMEM; + + rcand->icem = mem_ref(icem); + rcand->type = ice_cand_name2type(type); + rcand->cid = cid; + rcand->prio = pl_u32(&prio); + rcand->port = pl_u32(&port); + rcand->rel_addr = rel_addr; + + pl_dup(&rcand->foundation, &foundation); + (void)pl_strcpy(&addr, rcand->domain, sizeof(rcand->domain)); + + err = re_thread_async(getaddr_rcand, delayed_rcand, rcand); + if (err) + mem_deref(rcand); + + return err; + } /* add only if not exist */ if (icem_cand_find(&icem->rcandl, cid, &caddr)) return 0; - (void)pl_strcpy(&cand_type, type, sizeof(type)); - return icem_rcand_add(icem, ice_cand_name2type(type), cid, pl_u32(&prio), &caddr, &rel_addr, &foundation); } From e39e159e7b3929a6f0f324383d454e2b7fb9bdd6 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 27 Aug 2023 14:40:13 +0200 Subject: [PATCH 02/12] use AF_UNSPEC --- src/ice/icesdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ice/icesdp.c b/src/ice/icesdp.c index de8bf41ba..c21b7f566 100644 --- a/src/ice/icesdp.c +++ b/src/ice/icesdp.c @@ -212,7 +212,7 @@ static int getaddr_rcand(void *arg) int err; memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; + hints.ai_family = AF_UNSPEC; hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG; err = getaddrinfo(rcand->domain, NULL, &hints, &res0); From 707792eed963f81a7b9563a5112e92a6afb5992f Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 27 Aug 2023 15:19:30 +0200 Subject: [PATCH 03/12] add icem->rcand_wait (remote candidate wait) --- src/ice/connchk.c | 11 +++++++++++ src/ice/ice.h | 2 ++ src/ice/icem.c | 2 ++ src/ice/icesdp.c | 3 +++ 4 files changed, 18 insertions(+) diff --git a/src/ice/connchk.c b/src/ice/connchk.c index 17a5662fa..c586852ca 100644 --- a/src/ice/connchk.c +++ b/src/ice/connchk.c @@ -400,6 +400,14 @@ static void pace_timeout(void *arg) } +static void rcand_wait_timeout(void *arg) +{ + struct icem *icem = arg; + + icem_conncheck_start(icem); +} + + /** * Scheduling Checks * @@ -414,6 +422,9 @@ int icem_conncheck_start(struct icem *icem) if (!icem) return EINVAL; + if (icem->rcand_wait) + tmr_start(&icem->tmr_rcand, 100, rcand_wait_timeout, icem); + err = icem_checklist_form(icem); if (err) return err; diff --git a/src/ice/ice.h b/src/ice/ice.h index b26b41437..9d2fdf1d8 100644 --- a/src/ice/ice.h +++ b/src/ice/ice.h @@ -59,6 +59,7 @@ struct icem { bool rmode_lite; /**< Remote mode is Lite */ enum ice_role lrole; /**< Local role */ struct tmr tmr_pace; /**< Timer for pacing STUN requests */ + struct tmr tmr_rcand; /**< Timer for remote candidate wait */ int proto; /**< Transport protocol */ int layer; /**< Protocol layer */ enum ice_checkl_state state; /**< State of the checklist */ @@ -70,6 +71,7 @@ struct icem { ice_connchk_h *chkh; /**< Connectivity check handler */ void *arg; /**< Handler argument */ char name[32]; /**< Name of the media stream */ + bool rcand_wait; /**< Waiting for remote candidated */ }; /** Defines a candidate */ diff --git a/src/ice/icem.c b/src/ice/icem.c index 8173e2003..3679a3e35 100644 --- a/src/ice/icem.c +++ b/src/ice/icem.c @@ -51,6 +51,7 @@ static void icem_destructor(void *data) { struct icem *icem = data; + tmr_cancel(&icem->tmr_rcand); tmr_cancel(&icem->tmr_pace); list_flush(&icem->compl); list_flush(&icem->validl); @@ -105,6 +106,7 @@ int icem_alloc(struct icem **icemp, enum ice_role role, int proto, int layer, icem->conf = conf_default; tmr_init(&icem->tmr_pace); + tmr_init(&icem->tmr_rcand); list_init(&icem->lcandl); list_init(&icem->rcandl); list_init(&icem->checkl); diff --git a/src/ice/icesdp.c b/src/ice/icesdp.c index c21b7f566..b7d539bcc 100644 --- a/src/ice/icesdp.c +++ b/src/ice/icesdp.c @@ -251,6 +251,7 @@ static void delayed_rcand(int err, void *arg) &rcand->caddr, &rcand->rel_addr, &rcand->foundation); out: + rcand->icem->rcand_wait = false; mem_deref(rcand); } @@ -336,6 +337,8 @@ static int cand_decode(struct icem *icem, const char *val) pl_dup(&rcand->foundation, &foundation); (void)pl_strcpy(&addr, rcand->domain, sizeof(rcand->domain)); + icem->rcand_wait = true; + err = re_thread_async(getaddr_rcand, delayed_rcand, rcand); if (err) mem_deref(rcand); From b3f344f5fdee421d670496cc986b9381d56c3445 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 27 Aug 2023 15:32:58 +0200 Subject: [PATCH 04/12] fix wording --- src/ice/ice.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ice/ice.h b/src/ice/ice.h index 9d2fdf1d8..ab684428d 100644 --- a/src/ice/ice.h +++ b/src/ice/ice.h @@ -71,7 +71,7 @@ struct icem { ice_connchk_h *chkh; /**< Connectivity check handler */ void *arg; /**< Handler argument */ char name[32]; /**< Name of the media stream */ - bool rcand_wait; /**< Waiting for remote candidated */ + bool rcand_wait; /**< Waiting for remote candidate */ }; /** Defines a candidate */ From 8c5114b16d3cac44621c1dbb579c99725bf70b1a Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 27 Aug 2023 20:57:50 +0200 Subject: [PATCH 05/12] connchk add debug printf --- src/ice/connchk.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ice/connchk.c b/src/ice/connchk.c index c586852ca..2429a2309 100644 --- a/src/ice/connchk.c +++ b/src/ice/connchk.c @@ -422,8 +422,11 @@ int icem_conncheck_start(struct icem *icem) if (!icem) return EINVAL; - if (icem->rcand_wait) + if (icem->rcand_wait) { + icem_printf(icem, "conncheck_start: " + "waiting for remote candidate..."); tmr_start(&icem->tmr_rcand, 100, rcand_wait_timeout, icem); + } err = icem_checklist_form(icem); if (err) From 805f1b69447ce44b4b1865541b893042856937ea Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 27 Aug 2023 20:59:15 +0200 Subject: [PATCH 06/12] fix unneded deref --- src/ice/icesdp.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ice/icesdp.c b/src/ice/icesdp.c index b7d539bcc..f7e2e3c5d 100644 --- a/src/ice/icesdp.c +++ b/src/ice/icesdp.c @@ -262,7 +262,6 @@ static void rcand_dealloc(void *arg) mem_deref(rcand->icem); mem_deref((char *)rcand->foundation.p); - mem_deref(rcand->domain); } From 37071068e14493f7e3ec72fec40eace3fb91b23b Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 27 Aug 2023 21:05:57 +0200 Subject: [PATCH 07/12] remove check --- src/ice/icesdp.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ice/icesdp.c b/src/ice/icesdp.c index f7e2e3c5d..2b34eb339 100644 --- a/src/ice/icesdp.c +++ b/src/ice/icesdp.c @@ -317,9 +317,6 @@ static int cand_decode(struct icem *icem, const char *val) err = sa_set(&caddr, &addr, pl_u32(&port)); if (err) { - if (err != EINVAL) - return err; - /* try non blocking getaddr mdns resolution */ struct rcand *rcand = mem_zalloc(sizeof(struct rcand), rcand_dealloc); From 4c6c4f6939f902e34e987b1f033c41730cb424f2 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 27 Aug 2023 21:38:53 +0200 Subject: [PATCH 08/12] explicit mdns .local check --- src/ice/icesdp.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ice/icesdp.c b/src/ice/icesdp.c index 2b34eb339..555b3da48 100644 --- a/src/ice/icesdp.c +++ b/src/ice/icesdp.c @@ -315,8 +315,8 @@ static int cand_decode(struct icem *icem, const char *val) (void)pl_strcpy(&cand_type, type, sizeof(type)); cid = pl_u32(&compid); - err = sa_set(&caddr, &addr, pl_u32(&port)); - if (err) { + /* check for mdns .local address */ + if (pl_strstr(&addr, ".local") != NULL) { /* try non blocking getaddr mdns resolution */ struct rcand *rcand = mem_zalloc(sizeof(struct rcand), rcand_dealloc); @@ -342,6 +342,10 @@ static int cand_decode(struct icem *icem, const char *val) return err; } + err = sa_set(&caddr, &addr, pl_u32(&port)); + if (err) + return err; + /* add only if not exist */ if (icem_cand_find(&icem->rcandl, cid, &caddr)) return 0; From f7b24d16da4600895dcf0d89862d17e41809f1b6 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 27 Aug 2023 23:38:23 +0200 Subject: [PATCH 09/12] default hints values are enough --- src/ice/icesdp.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/ice/icesdp.c b/src/ice/icesdp.c index 555b3da48..5e5898607 100644 --- a/src/ice/icesdp.c +++ b/src/ice/icesdp.c @@ -208,14 +208,9 @@ static int getaddr_rcand(void *arg) { struct rcand *rcand = arg; struct addrinfo *res, *res0 = NULL; - struct addrinfo hints; int err; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG; - - err = getaddrinfo(rcand->domain, NULL, &hints, &res0); + err = getaddrinfo(rcand->domain, NULL, NULL, &res0); if (err) return EADDRNOTAVAIL; From a63b2a69de2f321f7377d254e937f374e8961dc5 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 28 Aug 2023 11:55:35 +0200 Subject: [PATCH 10/12] improve waiting message --- src/ice/connchk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ice/connchk.c b/src/ice/connchk.c index 2429a2309..fe9ec8305 100644 --- a/src/ice/connchk.c +++ b/src/ice/connchk.c @@ -424,7 +424,7 @@ int icem_conncheck_start(struct icem *icem) if (icem->rcand_wait) { icem_printf(icem, "conncheck_start: " - "waiting for remote candidate..."); + "waiting mDNS for remote candidate...\n"); tmr_start(&icem->tmr_rcand, 100, rcand_wait_timeout, icem); } From 2a9c9e93f79712c24ba1ab45b2f539f8f49ff787 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 28 Aug 2023 12:19:19 +0200 Subject: [PATCH 11/12] do not start --- src/ice/connchk.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ice/connchk.c b/src/ice/connchk.c index fe9ec8305..e9809e688 100644 --- a/src/ice/connchk.c +++ b/src/ice/connchk.c @@ -426,6 +426,7 @@ int icem_conncheck_start(struct icem *icem) icem_printf(icem, "conncheck_start: " "waiting mDNS for remote candidate...\n"); tmr_start(&icem->tmr_rcand, 100, rcand_wait_timeout, icem); + return 0; } err = icem_checklist_form(icem); From 2606627d1c71ac047a5fb93eb282fb8aea610312 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 28 Aug 2023 12:40:48 +0200 Subject: [PATCH 12/12] printf mDNS candidate --- src/ice/icesdp.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ice/icesdp.c b/src/ice/icesdp.c index 5e5898607..3a37ab07c 100644 --- a/src/ice/icesdp.c +++ b/src/ice/icesdp.c @@ -313,6 +313,7 @@ static int cand_decode(struct icem *icem, const char *val) /* check for mdns .local address */ if (pl_strstr(&addr, ".local") != NULL) { /* try non blocking getaddr mdns resolution */ + icem_printf(icem, "mDNS remote cand: %r\n", &addr); struct rcand *rcand = mem_zalloc(sizeof(struct rcand), rcand_dealloc); if (!rcand)