comparison librtpalloc/rtp_alloc_simple.c @ 184:f8c40090a0a8

librtpalloc: new library for talking to themwi-rtp-mgr
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Mar 2023 23:48:14 -0800
parents
children
comparison
equal deleted inserted replaced
183:3962d9345a09 184:f8c40090a0a8
1 /*
2 * The library function implemented in this C module provides a
3 * simple interface for obtaining a single RTP endpoint from
4 * themwi-rtp-mgr.
5 */
6
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <strings.h>
15 #include "../include/tmgw_const.h"
16 #include "../include/rtp_alloc.h"
17 #include "rtpmgr_resp.h"
18 #include "rtp_alloc_simple.h"
19
20 static char ctrl_socket_pathname[] = "/var/gsm/rtp_alloc_socket";
21
22 static void
23 close_fds(resp)
24 struct rtp_alloc_resp_wrap *resp;
25 {
26 unsigned n;
27
28 for (n = 0; n < resp->num_fd; n++)
29 close(resp->fd_buf[n]);
30 }
31
32 rtp_alloc_simple(ep_type, out)
33 int ep_type;
34 struct rtp_alloc_simple *out;
35 {
36 struct sockaddr_un sa;
37 unsigned sa_len;
38 int ctrl_fd, rc;
39 struct rtp_alloc_req req;
40 struct rtp_alloc_resp_wrap resp;
41 unsigned expect_num_fd;
42
43 switch (ep_type) {
44 case TMGW_EP_TYPE_GSM_ONLY:
45 case TMGW_EP_TYPE_PSTN_ONLY:
46 expect_num_fd = 2;
47 break;
48 case TMGW_EP_TYPE_GATEWAY:
49 expect_num_fd = 4;
50 break;
51 default:
52 fprintf(stderr,
53 "rtp_alloc_simple() error: unknown EP type %d\n",
54 ep_type);
55 return(-1);
56 }
57 ctrl_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
58 if (ctrl_fd < 0) {
59 perror("socket(AF_UNIX, SOCK_SEQPACKET, 0)");
60 return(-1);
61 }
62 fill_sockaddr_un(ctrl_socket_pathname, &sa, &sa_len);
63 rc = connect(ctrl_fd, (struct sockaddr *) &sa, sa_len);
64 if (rc < 0) {
65 perror(ctrl_socket_pathname);
66 close(ctrl_fd);
67 return(-1);
68 }
69 bzero(&req, sizeof req);
70 req.ep_type = ep_type;
71 rc = send(ctrl_fd, &req, sizeof req, 0);
72 if (rc < 0) {
73 perror("send to RTP allocator socket");
74 close(ctrl_fd);
75 return(-1);
76 }
77 rc = collect_rtpmgr_resp(ctrl_fd, 0, &resp);
78 if (rc < 0) {
79 perror("recvmsg from RTP allocator socket");
80 close(ctrl_fd);
81 return(-1);
82 }
83 close(ctrl_fd);
84 if (resp.resp_len != sizeof(struct rtp_alloc_resp)) {
85 fprintf(stderr,
86 "error: response packet from themwi-rtp-mgr has wrong length (%u bytes)\n",
87 resp.resp_len);
88 close_fds(&resp);
89 return(-1);
90 }
91 if (resp.resp.res != RTP_ALLOC_OK) {
92 fprintf(stderr, "themwi-rtp-mgr returned error %u\n",
93 resp.resp.res);
94 close_fds(&resp);
95 return(-1);
96 }
97 if (resp.num_fd != expect_num_fd) {
98 fprintf(stderr,
99 "error: themwi-rtp-mgr returned %u descriptors instead of expected %u\n",
100 resp.num_fd, expect_num_fd);
101 close_fds(&resp);
102 return(-1);
103 }
104 switch (ep_type) {
105 case TMGW_EP_TYPE_GSM_ONLY:
106 out->gsm_rtp_fd = resp.fd_buf[0];
107 out->gsm_rtcp_fd = resp.fd_buf[1];
108 break;
109 case TMGW_EP_TYPE_PSTN_ONLY:
110 out->pstn_rtp_fd = resp.fd_buf[0];
111 out->pstn_rtcp_fd = resp.fd_buf[1];
112 break;
113 case TMGW_EP_TYPE_GATEWAY:
114 out->gsm_rtp_fd = resp.fd_buf[0];
115 out->gsm_rtcp_fd = resp.fd_buf[1];
116 out->pstn_rtp_fd = resp.fd_buf[2];
117 out->pstn_rtcp_fd = resp.fd_buf[3];
118 }
119 bcopy(&resp.resp.gsm_addr, &out->gsm_addr,
120 sizeof(struct sockaddr_storage));
121 bcopy(&resp.resp.pstn_addr, &out->pstn_addr,
122 sizeof(struct sockaddr_storage));
123 return(0);
124 }