FreeCalypso > hg > themwi-system-sw
comparison rtp-mgr/alloc.c @ 179:b79d6334f543
themwi-rtp-mgr: RTP port allocation split out of themwi-mgw
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sat, 11 Mar 2023 20:19:14 -0800 |
| parents | mgw/crcx.c@f062c32a5116 |
| children |
comparison
equal
deleted
inserted
replaced
| 178:b259e2722485 | 179:b79d6334f543 |
|---|---|
| 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 "../include/tmgw_const.h" | |
| 18 #include "struct.h" | |
| 19 #include "select.h" | |
| 20 | |
| 21 get_rtp_port_pair(roe, brc) | |
| 22 struct rtp_one_end *roe; | |
| 23 struct bind_range_cfg *brc; | |
| 24 { | |
| 25 struct sockaddr_in sin; | |
| 26 unsigned tries, rtp_port; | |
| 27 int rc; | |
| 28 | |
| 29 sin.sin_family = AF_INET; | |
| 30 sin.sin_addr = brc->bind_ip; | |
| 31 for (tries = brc->port_tries; tries; tries--) { | |
| 32 rtp_port = brc->port_next; | |
| 33 brc->port_next += 2; | |
| 34 if (brc->port_next >= brc->port_range_end) | |
| 35 brc->port_next = brc->port_range_start; | |
| 36 sin.sin_port = htons(rtp_port); | |
| 37 roe->rtp_fd = socket(AF_INET, SOCK_DGRAM, 0); | |
| 38 if (roe->rtp_fd < 0) { | |
| 39 syslog(LOG_CRIT, "socket(AF_INET, SOCK_DGRAM, 0): %m"); | |
| 40 return(-1); | |
| 41 } | |
| 42 rc = bind(roe->rtp_fd, (struct sockaddr *) &sin, sizeof sin); | |
| 43 if (rc < 0) { | |
| 44 close(roe->rtp_fd); | |
| 45 continue; | |
| 46 } | |
| 47 bcopy(&sin, &roe->bound_addr, sizeof(struct sockaddr_in)); | |
| 48 sin.sin_port = htons(rtp_port+1); | |
| 49 roe->rtcp_fd = socket(AF_INET, SOCK_DGRAM, 0); | |
| 50 if (roe->rtcp_fd < 0) { | |
| 51 syslog(LOG_CRIT, "socket(AF_INET, SOCK_DGRAM, 0): %m"); | |
| 52 close(roe->rtp_fd); | |
| 53 return(-1); | |
| 54 } | |
| 55 rc = bind(roe->rtcp_fd, (struct sockaddr *) &sin, sizeof sin); | |
| 56 if (rc < 0) { | |
| 57 close(roe->rtp_fd); | |
| 58 close(roe->rtcp_fd); | |
| 59 continue; | |
| 60 } | |
| 61 /* all good! */ | |
| 62 return(0); | |
| 63 } | |
| 64 /* couldn't find a free port pair */ | |
| 65 return(-1); | |
| 66 } |
