Skip to content

Commit

Permalink
net,sip: check if destination is reachable when selecting transport
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed Jun 7, 2021
1 parent 1dd8e7f commit af227d6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/re_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ int net_hostaddr(int af, struct sa *ip);
int net_dst_source_addr_get(struct sa *dst, struct sa *ip);
int net_default_source_addr_get(int af, struct sa *ip);
int net_default_gateway_get(int af, struct sa *gw);
int net_check_conn(const struct sa *src, const struct sa *dst);


/* Net sockets */
Expand Down
42 changes: 42 additions & 0 deletions src/net/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,48 @@ int net_dst_source_addr_get(struct sa *dst, struct sa *ip)
}


/**
* Check if connection from source to destination is possible
*
* @param src Source IP address
* @param dst Destination IP address
*
* @return 0 if success, otherwise errorcode
*/
int net_check_conn(const struct sa *src, const struct sa *dst)
{
struct udp_sock *us;
struct sa srctmp;
struct sa dsttmp;
int port;
int err = 0;

if (!sa_isset(src, SA_ADDR) || !sa_isset(dst, SA_ADDR))
return EINVAL;

srctmp = *src;
dsttmp = *dst;
sa_cpy_scopeid(&dsttmp, src);
for (port = sa_port(src) + 3; port < sa_port(src) + 23; port++) {
err = udp_listen(&us, &srctmp, NULL, NULL);
if (!err)
break;

sa_set_port(&srctmp, port);
}

if (err)
return err;

err = udp_connect(us, &dsttmp);
if (err)
goto out;
out:
mem_deref(us);
return err;
}


/**
* Get the default source IP address
*
Expand Down
13 changes: 10 additions & 3 deletions src/sip/transp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <re_http.h>
#include <re_websock.h>
#include <re_sip.h>
#include <re_net.h>
#include "sip.h"


Expand Down Expand Up @@ -150,19 +151,25 @@ static const struct sip_transport *transp_find(struct sip *sip,
int af, const struct sa *dst)
{
struct le *le;
(void)dst;

for (le = sip->transpl.head; le; le = le->next) {

const struct sip_transport *transp = le->data;
const struct sa *src = &transp->laddr;

if (transp->tp != tp)
continue;

if (af != AF_UNSPEC && sa_af(&transp->laddr) != af)
if (af != AF_UNSPEC && sa_af(src) != af)
continue;

if (sa_is_linklocal(&transp->laddr) != sa_is_linklocal(dst))
if (!sa_isset(dst, SA_ADDR))
return transp;

if (sa_is_linklocal(src) != sa_is_linklocal(dst))
continue;

if (net_check_conn(src, dst))
continue;

return transp;
Expand Down

0 comments on commit af227d6

Please sign in to comment.