annotate src/bind_fdpair.c @ 21:2032201bd034

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