comparison rtp-mgr/ctrl_prot.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/ctrl_prot.c@f062c32a5116
children 565477d07418
comparison
equal deleted inserted replaced
178:b259e2722485 179:b79d6334f543
1 /*
2 * In this module we implement our control socket protocol.
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/uio.h>
8 #include <netinet/in.h>
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <syslog.h>
15 #include <unistd.h>
16 #include "../include/tmgw_const.h"
17 #include "../include/rtp_alloc.h"
18 #include "struct.h"
19 #include "select.h"
20
21 extern struct bind_range_cfg bind_range_gsm, bind_range_pstn;
22
23 void
24 free_rtp_end(roe)
25 struct rtp_one_end *roe;
26 {
27 close(roe->rtp_fd);
28 close(roe->rtcp_fd);
29 }
30
31 void
32 ctrl_message_handler(fd)
33 {
34 struct rtp_alloc_req req;
35 struct rtp_alloc_resp resp;
36 struct rtp_one_end rtp_gsm, rtp_pstn;
37 struct iovec iov;
38 struct msghdr msg;
39 int fd_out[4], num_fd, *fd_bufptr;
40 union {
41 char buf[CMSG_SPACE(sizeof fd_out)];
42 struct cmsghdr align;
43 } cmsgu;
44 struct cmsghdr *cmsg;
45 int rc;
46
47 /* receive request */
48 rc = recv(fd, &req, sizeof req, 0);
49 if (rc < sizeof req) {
50 syslog(LOG_DEBUG, "ctrl connection closing");
51 close(fd);
52 FD_CLR(fd, &select_for_read);
53 return;
54 }
55 /* start preparing response */
56 bzero(&resp, sizeof resp);
57 resp.transact_ref = req.transact_ref;
58 switch (req.ep_type) {
59 case TMGW_EP_TYPE_DUMMY_GSM:
60 case TMGW_EP_TYPE_DUMMY_PSTN:
61 case TMGW_EP_TYPE_GATEWAY:
62 break;
63 default:
64 resp.res = RTP_ALLOC_ERR_PARAM;
65 error_resp: send(fd, &resp, sizeof resp, 0);
66 return;
67 }
68 /* allocate resources */
69 if (req.ep_type & TMGW_EP_HAS_GSM_SOCK) {
70 rc = get_rtp_port_pair(&rtp_gsm, &bind_range_gsm);
71 if (rc < 0) {
72 syslog(LOG_ERR,
73 "unable to get local port pair on GSM side");
74 resp.res = RTP_ALLOC_ERR_RSRC;
75 goto error_resp;
76 }
77 }
78 if (req.ep_type & TMGW_EP_HAS_PSTN_SOCK) {
79 rc = get_rtp_port_pair(&rtp_pstn, &bind_range_pstn);
80 if (rc < 0) {
81 syslog(LOG_ERR,
82 "unable to get local port pair on PSTN side");
83 if (req.ep_type & TMGW_EP_HAS_GSM_SOCK)
84 free_rtp_end(&rtp_gsm);
85 resp.res = RTP_ALLOC_ERR_RSRC;
86 goto error_resp;
87 }
88 }
89 /* finish ordinary body of response */
90 resp.res = RTP_ALLOC_OK;
91 if (req.ep_type & TMGW_EP_HAS_GSM_SOCK)
92 bcopy(&rtp_gsm.bound_addr, &resp.gsm_addr,
93 sizeof(struct sockaddr_in));
94 if (req.ep_type & TMGW_EP_HAS_PSTN_SOCK)
95 bcopy(&rtp_pstn.bound_addr, &resp.pstn_addr,
96 sizeof(struct sockaddr_in));
97 iov.iov_base = &resp;
98 iov.iov_len = sizeof resp;
99 /* file descriptor passing voodoo */
100 switch (req.ep_type) {
101 case TMGW_EP_TYPE_DUMMY_GSM:
102 num_fd = 2;
103 fd_out[0] = rtp_gsm.rtp_fd;
104 fd_out[1] = rtp_gsm.rtcp_fd;
105 break;
106 case TMGW_EP_TYPE_DUMMY_PSTN:
107 num_fd = 2;
108 fd_out[0] = rtp_pstn.rtp_fd;
109 fd_out[1] = rtp_pstn.rtcp_fd;
110 break;
111 case TMGW_EP_TYPE_GATEWAY:
112 num_fd = 4;
113 fd_out[0] = rtp_gsm.rtp_fd;
114 fd_out[1] = rtp_gsm.rtcp_fd;
115 fd_out[2] = rtp_pstn.rtp_fd;
116 fd_out[3] = rtp_pstn.rtcp_fd;
117 }
118 bzero(&msg, sizeof msg);
119 msg.msg_iov = &iov;
120 msg.msg_iovlen = 1;
121 msg.msg_control = cmsgu.buf;
122 msg.msg_controllen = CMSG_SPACE(sizeof(int) * num_fd);
123 cmsg = CMSG_FIRSTHDR(&msg);
124 cmsg->cmsg_level = SOL_SOCKET;
125 cmsg->cmsg_type = SCM_RIGHTS;
126 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fd);
127 fd_bufptr = (int *) CMSG_DATA(cmsg);
128 bcopy(fd_out, fd_bufptr, sizeof(int) * num_fd);
129 sendmsg(fd, &msg, 0);
130 }