view src/bind_fdpair.c @ 42:334d883b96ba

twrtp_jibuf_create: make config argument const While this config structure is not a constant in the mathematical sense of the term (it is expected that vty config changes may happen while twjit instance is alive), twjit functions never write to it, only read, hence it is 'const' in the not-quite-mathematical C-standard sense.
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 20 Dec 2024 22:47:20 +0000
parents 2a24487453a7
children
line wrap: on
line source

/*
 * 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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>

#include <themwi/rtp/fdpair.h>

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;
}