annotate rtp-mgr/alloc.c @ 5:4a5560ef0807

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