Skip to content

Commit

Permalink
Add support for "cunix" variant of the local unix domain
Browse files Browse the repository at this point in the history
communication socket, which is similar to "unix" but does not
require re-connecting the socket for every request thus
having lower overhead compared to that one. It is supported
by rtpproxy for number of years.
  • Loading branch information
sobomax committed Mar 22, 2023
1 parent e79c76c commit f3fe6c4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
6 changes: 1 addition & 5 deletions modules/rtpproxy/notification_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@

#include "rtpproxy.h"

#if !defined(AF_LOCAL)
#define AF_LOCAL AF_UNIX
#endif

#define BUF_LEN 255

int *rtpp_notify_process_no;
Expand Down Expand Up @@ -186,7 +182,7 @@ static struct rtpp_node *rtpproxy_get_node(union sockaddr_union *rtpp_info)
rtpp_list = rtpp_list->rset_next) {
for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
crt_rtpp = crt_rtpp->rn_next) {
if (crt_rtpp->rn_umode == CM_UNIX)
if (crt_rtpp->rn_umode == CM_UNIX || crt_rtpp->rn_umode == CM_CUNIX)
continue;
if (compare_sa(&crt_rtpp->addr.s, &rtpp_info->s) == 0) {
if (nh_lock)
Expand Down
72 changes: 47 additions & 25 deletions modules/rtpproxy/rtppn_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,51 @@ int connect_rtpp_node(struct rtpp_node *pnode)
int n, s;
char *cp, *hostname;
struct addrinfo hints, *res;
struct sockaddr_un sau;

/*
* This is UDP, TCP, UDP6 or TCP6. Detect host and port; lookup host;
* do connect() in order to specify peer address
*/
hostname = (char*)pkg_malloc(sizeof(char) * (strlen(pnode->rn_address) + 1));
if (hostname == NULL) {
LM_ERR("no more pkg memory\n");
goto e0;
}
strcpy(hostname, pnode->rn_address);
if (pnode->rn_umode != CM_CUNIX) {
hostname = (char*)pkg_malloc(sizeof(char) * (strlen(pnode->rn_address) + 1));
if (hostname == NULL) {
LM_ERR("no more pkg memory\n");
goto e0;
}
strcpy(hostname, pnode->rn_address);

cp = strrchr(hostname, ':');
if (cp != NULL) {
*cp = '\0';
cp++;
}
if (cp == NULL || *cp == '\0')
cp = DEFAULT_CPORT;
cp = strrchr(hostname, ':');
if (cp != NULL) {
*cp = '\0';
cp++;
}
if (cp == NULL || *cp == '\0')
cp = DEFAULT_CPORT;

memset(&hints, 0, sizeof(hints));
hints.ai_flags = 0;
hints.ai_family = (pnode->rn_umode == CM_UDP6 || pnode->rn_umode == CM_TCP6) ? AF_INET6 : AF_INET;
hints.ai_socktype = CM_STREAM(pnode) ? SOCK_STREAM : SOCK_DGRAM;
if ((n = getaddrinfo(hostname, cp, &hints, &res)) != 0) {
LM_ERR("%s\n", gai_strerror(n));
goto e1;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = 0;
hints.ai_family = (pnode->rn_umode == CM_UDP6 || pnode->rn_umode == CM_TCP6) ? AF_INET6 : AF_INET;
hints.ai_socktype = CM_STREAM(pnode) ? SOCK_STREAM : SOCK_DGRAM;
if ((n = getaddrinfo(hostname, cp, &hints, &res)) != 0) {
LM_ERR("%s\n", gai_strerror(n));
goto e1;
}
} else {
memset(&sau, 0, sizeof(sau));
hints = (struct addrinfo) {
.ai_family = AF_LOCAL,
.ai_socktype = SOCK_STREAM,
.ai_protocol = 0,
.ai_addr = (struct sockaddr *)&sau,
.ai_addrlen = sizeof(sau),
};
res = &hints;
sau.sun_family = AF_LOCAL;
strncpy(sau.sun_path, pnode->rn_address, sizeof(sau.sun_path) - 1);
#ifdef HAVE_SOCKADDR_SA_LEN
sau.sun_len = strlen(sau.sun_path);
#endif
}

s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Expand All @@ -107,17 +125,21 @@ int connect_rtpp_node(struct rtpp_node *pnode)
LM_ERR("can't connect to a RTP proxy\n");
goto e3;
}
memcpy(&pnode->addr.s, res->ai_addr, res->ai_addrlen);
pkg_free(hostname);
freeaddrinfo(res);
if (pnode->rn_umode != CM_CUNIX) {
memcpy(&pnode->addr.s, res->ai_addr, res->ai_addrlen);
pkg_free(hostname);
freeaddrinfo(res);
}
LM_DBG("connected %s\n", pnode->rn_address);
return s;
e3:
close(s);
e2:
freeaddrinfo(res);
if (pnode->rn_umode != CM_CUNIX)
freeaddrinfo(res);
e1:
pkg_free(hostname);
if (pnode->rn_umode != CM_CUNIX)
pkg_free(hostname);
e0:
return -1;
}
11 changes: 3 additions & 8 deletions modules/rtpproxy/rtpproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,6 @@

#define NH_TABLE_VERSION 0

#if !defined(AF_LOCAL)
#define AF_LOCAL AF_UNIX
#endif
#if !defined(PF_LOCAL)
#define PF_LOCAL PF_UNIX
#endif


#define DEFAULT_RTPP_SET_ID 0

#define MI_ENABLE_RTP_PROXY "rtpproxy_enable"
Expand Down Expand Up @@ -729,6 +721,9 @@ static int add_rtpproxy_socks(struct rtpp_set * rtpp_list,
} else if (strncasecmp(pnode->rn_address, "unix:", 5) == 0) {
pnode->rn_umode = CM_UNIX;
pnode->rn_address += 5;
} else if (strncasecmp(pnode->rn_address, "cunix:", 6) == 0) {
pnode->rn_umode = CM_CUNIX;
pnode->rn_address += 6;
}

if (rtpp_list->rn_first == NULL) {
Expand Down
8 changes: 6 additions & 2 deletions modules/rtpproxy/rtpproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ struct rtpproxy_vcmd;
#define STR2IOVEC(sx, ix) do {(ix).iov_base = (sx).s; (ix).iov_len = (sx).len;} while(0)
#define SZ2IOVEC(sx, ix) do {(ix).iov_base = (sx); (ix).iov_len = strlen(sx);} while(0)

enum comm_modes {CM_UNIX = 0, CM_UDP, CM_TCP, CM_UDP6, CM_TCP6};
#if !defined(AF_LOCAL)
#define AF_LOCAL AF_UNIX
#endif

enum comm_modes {CM_UNIX = 0, CM_CUNIX, CM_UDP, CM_TCP, CM_UDP6, CM_TCP6};

struct rtpp_node {
unsigned int idx; /* overall index */
Expand All @@ -53,7 +57,7 @@ struct rtpp_node {
struct rtpp_node *rn_next;
};

#define CM_STREAM(ndp) ((ndp)->rn_umode == CM_TCP || (ndp)->rn_umode == CM_TCP6)
#define CM_STREAM(ndp) ((ndp)->rn_umode == CM_TCP || (ndp)->rn_umode == CM_TCP6 || (ndp)->rn_umode == CM_CUNIX)

/* Supported version of the RTP proxy command protocol */
#define SUP_CPROTOVER 20040107
Expand Down

0 comments on commit f3fe6c4

Please sign in to comment.