# HG changeset patch # User Mychaela Falconia # Date 1720310564 0 # Node ID 2a24487453a727d6b50de35b0ea707eae000fb6d # Parent 58e9719d1a842e0be05faa1fecf750b224a65122 implement twrtp_bind_fdpair() diff -r 58e9719d1a84 -r 2a24487453a7 include/fdpair.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/fdpair.h Sun Jul 07 00:02:44 2024 +0000 @@ -0,0 +1,10 @@ +/* + * This header file provides API declaration for twrtp_bind_fdpair() + * standalone function. + */ + +#pragma once + +#include + +int twrtp_bind_fdpair(const char *ip, uint16_t port, int *fd_rtp, int *fd_rtcp); diff -r 58e9719d1a84 -r 2a24487453a7 src/Makefile --- a/src/Makefile Sat Jul 06 22:00:06 2024 +0000 +++ b/src/Makefile Sun Jul 07 00:02:44 2024 +0000 @@ -1,4 +1,4 @@ -OBJS= twjit.o twjit_in.o twjit_out.o twjit_vty.o +OBJS= bind_fdpair.o twjit.o twjit_in.o twjit_out.o twjit_vty.o LIB= libtwrtp.a include ../config.defs diff -r 58e9719d1a84 -r 2a24487453a7 src/bind_fdpair.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bind_fdpair.c Sun Jul 07 00:02:44 2024 +0000 @@ -0,0 +1,63 @@ +/* + * Here we implement the function that creates and binds a pair of + * UDP sockets for RTP & RTCP, given the bind IP address in string form + * and the even port number in integer form. + * + * The current implementation supports only IPv4; however, given that + * the API takes a string for the IP address, it should be possible + * to extend this function to support IPv6 if and when such support + * becomes necessary. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +int twrtp_bind_fdpair(const char *ip, uint16_t port, int *fd_rtp, int *fd_rtcp) +{ + struct sockaddr_in sin; + int rc; + + sin.sin_family = AF_INET; + rc = inet_aton(ip, &sin.sin_addr); + if (!rc) + return -EINVAL; + + /* do RTP socket first */ + rc = socket(AF_INET, SOCK_DGRAM, 0); + if (rc < 0) + return -errno; + *fd_rtp = rc; + sin.sin_port = htons(port); + rc = bind(*fd_rtp, (struct sockaddr *) &sin, sizeof sin); + if (rc < 0) { + rc = -errno; + close(*fd_rtp); + return rc; + } + + /* now do RTCP */ + rc = socket(AF_INET, SOCK_DGRAM, 0); + if (rc < 0) { + rc = -errno; + close(*fd_rtp); + return rc; + } + *fd_rtcp = rc; + sin.sin_port = htons(port + 1); + rc = bind(*fd_rtcp, (struct sockaddr *) &sin, sizeof sin); + if (rc < 0) { + rc = -errno; + close(*fd_rtp); + close(*fd_rtcp); + return rc; + } + + return 0; +}