comparison src/bind_fdpair.c @ 17:2a24487453a7

implement twrtp_bind_fdpair()
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Jul 2024 00:02:44 +0000
parents
children
comparison
equal deleted inserted replaced
16:58e9719d1a84 17:2a24487453a7
1 /*
2 * Here we implement the function that creates and binds a pair of
3 * UDP sockets for RTP & RTCP, given the bind IP address in string form
4 * and the even port number in integer form.
5 *
6 * The current implementation supports only IPv4; however, given that
7 * the API takes a string for the IP address, it should be possible
8 * to extend this function to support IPv6 if and when such support
9 * becomes necessary.
10 */
11
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <stdint.h>
17 #include <unistd.h>
18 #include <errno.h>
19
20 #include <themwi/rtp/fdpair.h>
21
22 int twrtp_bind_fdpair(const char *ip, uint16_t port, int *fd_rtp, int *fd_rtcp)
23 {
24 struct sockaddr_in sin;
25 int rc;
26
27 sin.sin_family = AF_INET;
28 rc = inet_aton(ip, &sin.sin_addr);
29 if (!rc)
30 return -EINVAL;
31
32 /* do RTP socket first */
33 rc = socket(AF_INET, SOCK_DGRAM, 0);
34 if (rc < 0)
35 return -errno;
36 *fd_rtp = rc;
37 sin.sin_port = htons(port);
38 rc = bind(*fd_rtp, (struct sockaddr *) &sin, sizeof sin);
39 if (rc < 0) {
40 rc = -errno;
41 close(*fd_rtp);
42 return rc;
43 }
44
45 /* now do RTCP */
46 rc = socket(AF_INET, SOCK_DGRAM, 0);
47 if (rc < 0) {
48 rc = -errno;
49 close(*fd_rtp);
50 return rc;
51 }
52 *fd_rtcp = rc;
53 sin.sin_port = htons(port + 1);
54 rc = bind(*fd_rtcp, (struct sockaddr *) &sin, sizeof sin);
55 if (rc < 0) {
56 rc = -errno;
57 close(*fd_rtp);
58 close(*fd_rtcp);
59 return rc;
60 }
61
62 return 0;
63 }