comparison rtp-mgr/alloc.c @ 2:247f4bbde24c

rtp-mgr: daemon ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 27 May 2024 19:42:19 +0000
parents
children
comparison
equal deleted inserted replaced
1:560a3765ab30 2:247f4bbde24c
1 /*
2 * In this module we implement our RTP port allocation operation.
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/time.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <strings.h>
15 #include <syslog.h>
16 #include <unistd.h>
17 #include "struct.h"
18
19 get_rtp_port_pair(roe, brc)
20 struct rtp_one_end *roe;
21 struct bind_range_cfg *brc;
22 {
23 struct sockaddr_in sin;
24 unsigned tries, rtp_port;
25 int rc;
26
27 sin.sin_family = AF_INET;
28 sin.sin_addr = brc->bind_ip;
29 for (tries = brc->port_tries; tries; tries--) {
30 rtp_port = brc->port_next;
31 brc->port_next += 2;
32 if (brc->port_next >= brc->port_range_end)
33 brc->port_next = brc->port_range_start;
34 sin.sin_port = htons(rtp_port);
35 roe->rtp_fd = socket(AF_INET, SOCK_DGRAM, 0);
36 if (roe->rtp_fd < 0) {
37 syslog(LOG_CRIT, "socket(AF_INET, SOCK_DGRAM, 0): %m");
38 return(-1);
39 }
40 rc = bind(roe->rtp_fd, (struct sockaddr *) &sin, sizeof sin);
41 if (rc < 0) {
42 close(roe->rtp_fd);
43 continue;
44 }
45 bcopy(&sin, &roe->bound_addr, sizeof(struct sockaddr_in));
46 sin.sin_port = htons(rtp_port+1);
47 roe->rtcp_fd = socket(AF_INET, SOCK_DGRAM, 0);
48 if (roe->rtcp_fd < 0) {
49 syslog(LOG_CRIT, "socket(AF_INET, SOCK_DGRAM, 0): %m");
50 close(roe->rtp_fd);
51 return(-1);
52 }
53 rc = bind(roe->rtcp_fd, (struct sockaddr *) &sin, sizeof sin);
54 if (rc < 0) {
55 close(roe->rtp_fd);
56 close(roe->rtcp_fd);
57 continue;
58 }
59 /* all good! */
60 return(0);
61 }
62 /* couldn't find a free port pair */
63 return(-1);
64 }