Skip to content

Commit

Permalink
transp.c: add sip_transp_add_sock (#1160)
Browse files Browse the repository at this point in the history
to add SIP transport protocol without opening a listening socket and
adapt sip_transp_send accordingly to make UDP work.
  • Loading branch information
maximilianfridrich authored Jul 26, 2024
1 parent 202d2f2 commit 80baf46
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
2 changes: 2 additions & 0 deletions include/re_sip.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ void sip_set_trace_handler(struct sip *sip, sip_trace_h *traceh);
/* transport */
int sip_transp_add(struct sip *sip, enum sip_transp tp,
const struct sa *laddr, ...);
int sip_transp_add_sock(struct sip *sip, enum sip_transp tp,
bool listen, const struct sa *laddr, ...);
int sip_transp_add_websock(struct sip *sip, enum sip_transp tp,
const struct sa *laddr,
bool server, const char *cert, struct tls *tls);
Expand Down
98 changes: 87 additions & 11 deletions src/sip/transp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1191,19 +1191,19 @@ static void http_req_handler(struct http_conn *hc, const struct http_msg *msg,
/**
* Add a SIP transport
*
* @param sip SIP stack instance
* @param tp SIP Transport
* @param laddr Local network address
* @param ... Optional transport parameters such as TLS context
* @param sip SIP stack instance
* @param tp SIP Transport
* @param listen True to open listening socket (UDP socket always opened)
* @param laddr Local network address
* @param ap Optional transport parameters such as TLS context
*
* @return 0 if success, otherwise errorcode
*/
int sip_transp_add(struct sip *sip, enum sip_transp tp,
const struct sa *laddr, ...)
static int add_transp(struct sip *sip, enum sip_transp tp,
bool listen, const struct sa *laddr, va_list ap)
{
struct sip_transport *transp;
struct tls *tls;
va_list ap;
int err = 0;

if (!sip || !laddr || !sa_isset(laddr, SA_ADDR))
Expand All @@ -1225,8 +1225,6 @@ int sip_transp_add(struct sip *sip, enum sip_transp tp,
transp->sip = sip;
transp->tp = tp;

va_start(ap, laddr);

switch (tp) {

case SIP_TRANSP_UDP:
Expand All @@ -1250,6 +1248,13 @@ int sip_transp_add(struct sip *sip, enum sip_transp tp,
/*@fallthrough@*/

case SIP_TRANSP_TCP:

if (!listen) {
transp->laddr = *laddr;
sa_set_port(&transp->laddr, 0);
return err;
}

err = tcp_listen((struct tcp_sock **)&transp->sock, laddr,
tcp_connect_handler, transp);
if (err)
Expand All @@ -1263,15 +1268,64 @@ int sip_transp_add(struct sip *sip, enum sip_transp tp,
break;
}

va_end(ap);

if (err)
mem_deref(transp);

return err;
}


/**
* Add a SIP transport
*
* @param sip SIP stack instance
* @param tp SIP Transport
* @param laddr Local network address
* @param ... Optional transport parameters such as TLS context
*
* @return 0 if success, otherwise errorcode
*/
int sip_transp_add(struct sip *sip, enum sip_transp tp,
const struct sa *laddr, ...)
{
int err;
va_list ap;

va_start(ap, laddr);
err = add_transp(sip, tp, true, laddr, ap);
va_end(ap);

return err;
}


/**
* Add a SIP transport and open listening socket if requested
*
* UDP socket will always be opened even if listen is false.
*
* @param sip SIP stack instance
* @param tp SIP Transport
* @param listen True to open listening socket
* @param laddr Local network address
* @param ... Optional transport parameters such as TLS context
*
* @return 0 if success, otherwise errorcode
*/
int sip_transp_add_sock(struct sip *sip, enum sip_transp tp,
bool listen, const struct sa *laddr, ...)
{
int err;
va_list ap;

va_start(ap, laddr);
err = add_transp(sip, tp, listen, laddr, ap);
va_end(ap);

return err;
}


/**
* Add a SIP websocket transport
*
Expand Down Expand Up @@ -1764,11 +1818,30 @@ int sip_settos(struct sip *sip, uint8_t tos)
}


static void sip_transports_print(struct re_printf *pf, const struct sip* sip)
{
uint32_t mask = 0;

for (struct le *le = sip->transpl.head; le; le = le->next) {
const struct sip_transport *transp = le->data;
mask |= (1 << transp->tp);
}

for (uint8_t i = 0; i < SIP_TRANSPC; ++i) {
if (mask==0 || (0 != (mask & (1u << i))))
(void)re_hprintf(pf, " %s\n", sip_transp_name(i));
}
}


static bool debug_handler(struct le *le, void *arg)
{
const struct sip_transport *transp = le->data;
struct re_printf *pf = arg;

if (sa_port(&transp->laddr) == 0)
return false;

(void)re_hprintf(pf, " %J (%s)\n",
&transp->laddr,
sip_transp_name(transp->tp));
Expand Down Expand Up @@ -1809,6 +1882,9 @@ int sip_transp_debug(struct re_printf *pf, const struct sip *sip)
int err;

err = re_hprintf(pf, "transports:\n");
sip_transports_print(pf, sip);

err |= re_hprintf(pf, "transport sockets:\n");
list_apply(&sip->transpl, true, debug_handler, pf);

err |= re_hprintf(pf, "connections:\n");
Expand Down

0 comments on commit 80baf46

Please sign in to comment.