Skip to content

Commit 5151411

Browse files
committed
Extend notification socket mechanism to also accept file
descriptor for the librtpproxy use cases. The format of the socket specifier is "-n fd:X", where X is the socket number in decimal. Multiple sockets might be specified. This socket type does not support the %%CC_SELF%% mechanism and needs to be referenced by the call controller explicitly when requesting notification (e.g. "fd:X").
1 parent 4c99e52 commit 5151411

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

src/rtpp_notify.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,15 @@ reconnect_handler(const struct rtpp_notify_wi *wi)
204204
{
205205

206206
assert (wi->rttp->connected == 0);
207+
assert (wi->rttp->socket_type != RTPP_TNS_FD);
207208

208209
if (wi->rttp->fd == -1) {
209210
RTPP_LOG(wi->glog, RTPP_LOG_DBUG, "connecting %s socket", wi->ntype);
210211
} else {
211212
RTPP_LOG(wi->glog, RTPP_LOG_DBUG, "reconnecting %s socket", wi->ntype);
212213
close(wi->rttp->fd);
213214
}
214-
wi->rttp->fd = socket(wi->rttp->socket_type, SOCK_STREAM, 0);
215+
wi->rttp->fd = socket(RTPP_TNT_STYPE(wi->rttp), SOCK_STREAM, 0);
215216
if (wi->rttp->fd == -1) {
216217
RTPP_ELOG(wi->glog, RTPP_LOG_ERR, "can't create %s socket", wi->ntype);
217218
return;

src/rtpp_tnotify_set.c

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "rtpp_tnotify_set.h"
5454
#include "rtpp_tnotify_tgt.h"
5555
#include "rtpp_mallocs.h"
56+
#include "rtpp_util.h"
5657

5758
#define RTPP_TNOTIFY_TARGETS_MAX 64
5859
#define RTPP_TNOTIFY_WILDCARDS_MAX 2
@@ -61,7 +62,7 @@
6162

6263
struct rtpp_tnotify_wildcard {
6364
char *socket_name;
64-
int socket_type;
65+
enum rtpp_tnotify_stype socket_type;
6566
int port;
6667
};
6768

@@ -177,18 +178,28 @@ parse_timeout_sock(const char *sock_name, union rtpp_tnotify_entry *rtep,
177178
sprefix = NULL;
178179
if (strncmp("unix:", sock_name, 5) == 0) {
179180
usock_name = sock_name + 5;
180-
rtep->rtt.socket_type = AF_LOCAL;
181+
rtep->rtt.socket_type = RTPP_TNS_LOCAL;
181182
} else if (strncmp("tcp:", sock_name, 4) == 0) {
182183
if (parse_hostport(sock_name + 4, host, sizeof(host), port, sizeof(port), 0, e) != 0) {
183184
return (-1);
184185
}
185-
rtep->rtt.socket_type = AF_INET;
186+
rtep->rtt.socket_type = RTPP_TNS_INET;
187+
#if defined(LIBRTPPROXY)
188+
} else if (strncmp("fd:", sock_name, 3) == 0) {
189+
int fd;
190+
if (atoi_safe(sock_name + 3, &fd) != ATOI_OK || fd < 0) {
191+
return (-1);
192+
}
193+
rtep->rtt.socket_type = RTPP_TNS_FD;
194+
rtep->rtt.fd = fd;
195+
#endif
186196
} else {
187197
sprefix = "unix:";
188198
usock_name = sock_name;
189-
rtep->rtt.socket_type = AF_LOCAL;
199+
rtep->rtt.socket_type = RTPP_TNS_LOCAL;
190200
}
191-
if (rtep->rtt.socket_type == AF_UNIX) {
201+
switch (rtep->rtt.socket_type) {
202+
case RTPP_TNS_LOCAL:
192203
if (strlen(usock_name) == 0) {
193204
*e = "Timeout notification socket name too short";
194205
return (-1);
@@ -200,19 +211,26 @@ parse_timeout_sock(const char *sock_name, union rtpp_tnotify_entry *rtep,
200211
ifsun->sun_len = strlen(ifsun->sun_path);
201212
#endif
202213
rtep->rtt.remote_len = sizeof(struct sockaddr_un);
203-
} else if (rtep->rtt.socket_type == AF_INET && strcmp(host, CC_SELF_STR) == 0) {
204-
rtep->rtw.socket_type = AF_INET;
205-
rtep->rtw.port = atoi(port);
206-
snp = &rtep->rtt.socket_name;
207-
rval = 1;
208-
} else {
214+
break;
215+
216+
case RTPP_TNS_INET:
217+
if (strcmp(host, CC_SELF_STR) == 0) {
218+
rtep->rtw.socket_type = RTPP_TNS_INET;
219+
rtep->rtw.port = atoi(port);
220+
snp = &rtep->rtt.socket_name;
221+
rval = 1;
222+
break;
223+
}
209224
ifsa = sstosa(&rtep->rtt.remote);
210225
n = resolve(ifsa, AF_INET, host, port, 0);
211226
if (n != 0) {
212227
*e = gai_strerror(n);
213228
return (-1);
214229
}
215230
rtep->rtt.remote_len = SA_LEN(ifsa);
231+
break;
232+
default:
233+
break;
216234
}
217235
int snlen = (sprefix == NULL) ? strlen(sock_name) :
218236
strlen(sprefix) + strlen(usock_name);
@@ -261,8 +279,12 @@ rtpp_tnotify_set_append(struct rtpp_tnotify_set *pub,
261279
goto e1;
262280
}
263281
memcpy(tntp, &rte.rtt, sizeof(struct rtpp_tnotify_target));
264-
tntp->connected = 0;
265-
tntp->fd = -1;
282+
if (tntp->socket_type != RTPP_TNS_FD) {
283+
tntp->connected = 0;
284+
tntp->fd = -1;
285+
} else {
286+
tntp->connected = 1;
287+
}
266288
pvt->tp[pvt->tp_len] = tntp;
267289
pvt->tp_len += 1;
268290
} else {
@@ -321,6 +343,7 @@ get_tp4wp(struct rtpp_tnotify_set_priv *pvt, struct rtpp_tnotify_wildcard *wp,
321343
continue;
322344
return (tp);
323345
}
346+
assert(wp->socket_type != RTPP_TNS_FD);
324347
/* Nothing found, crank up a new entry */
325348
if (pvt->tp_len == RTPP_TNOTIFY_TARGETS_MAX) {
326349
return (NULL);
@@ -375,19 +398,22 @@ rtpp_tnotify_set_lookup(struct rtpp_tnotify_set *pub, const char *socket_name,
375398
for (i = 0; i < pvt->tp_len; i++) {
376399
if (pvt->tp[i]->socket_name == NULL)
377400
continue;
378-
if (pvt->tp[i]->socket_type != AF_LOCAL ||
401+
if (pvt->tp[i]->socket_type != RTPP_TNS_LOCAL ||
379402
strcmp(pvt->tp[i]->socket_name + 5, socket_name) != 0)
380403
continue;
381404
return (pvt->tp[i]);
382405
}
383406
return (NULL);
407+
} else if (sep - socket_name == 2 && memcmp(socket_name, "fd", 2) == 0) {
408+
/* RTPP_TNS_FD can only match directly */
409+
return (NULL);
384410
}
385411
/* Handle wildcards */
386412
for (i = 0; i < pvt->wp_len; i++) {
387413
wp = pvt->wp[i];
388414
if (strcmp(wp->socket_name, socket_name) != 0)
389415
continue;
390-
if (ccaddr != NULL && wp->socket_type != ccaddr->sa_family)
416+
if (ccaddr != NULL && RTPP_TNT_STYPE(wp) != ccaddr->sa_family)
391417
continue;
392418
return (get_tp4wp(pvt, wp, ccaddr, laddr));
393419
}

src/rtpp_tnotify_tgt.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,27 @@
2525
*
2626
*/
2727

28+
enum rtpp_tnotify_stype {
29+
RTPP_TNS_LOCAL,
30+
RTPP_TNS_INET,
31+
RTPP_TNS_FD
32+
};
33+
34+
#define RTPP_TNT_STYPE(tntp) ({int sst; \
35+
switch ((tntp)->socket_type) { \
36+
case RTPP_TNS_LOCAL: \
37+
case RTPP_TNS_FD: \
38+
sst = AF_LOCAL; \
39+
break; \
40+
case RTPP_TNS_INET: \
41+
sst = AF_INET; \
42+
break; \
43+
} \
44+
sst;})
45+
2846
struct rtpp_tnotify_target {
2947
char *socket_name;
30-
int socket_type;
48+
enum rtpp_tnotify_stype socket_type;
3149
struct sockaddr_storage remote;
3250
socklen_t remote_len;
3351
struct sockaddr *local;

0 commit comments

Comments
 (0)