aboutsummaryrefslogtreecommitdiffstats
path: root/rtpsource/ctrl_if.c
blob: 708fb028f88c5bf3c9e19837675eb9e31cf08d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* CTRL interface of rtpsource program
 *
 * (C) 2020 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <osmocom/ctrl/control_cmd.h>

#include "internal.h"

CTRL_CMD_DEFINE_WO_NOVRF(rtp_create, "rtp_create");
static int set_rtp_create(struct ctrl_cmd *cmd, void *data)
{
	struct rtp_connection *conn;
	const char *cname = cmd->value;

	if (find_connection_by_cname(g_rss, cname)) {
		cmd->reply = "Connection already exists for cname";
		return CTRL_CMD_ERROR;
	}

	conn = create_connection(g_rss, cname);
	if (!conn) {
		cmd->reply = "Error creating RTP connection";
		return CTRL_CMD_ERROR;
	}

	/* Respond */
	cmd->reply = talloc_asprintf(cmd, "%s,%s,%d", conn->cname, conn->local_host, conn->local_port);
	return CTRL_CMD_REPLY;
}

CTRL_CMD_DEFINE_WO_NOVRF(rtp_connect, "rtp_connect");
static int set_rtp_connect(struct ctrl_cmd *cmd, void *data)
{
	struct rtp_connection *conn;
	const char *cname, *remote_host, *remote_port, *pt;
	char *tmp, *saveptr;
	int rc;

	tmp = talloc_strdup(cmd, cmd->value);
	OSMO_ASSERT(tmp);

	/* FIXME: parse command */
	cname = strtok_r(tmp, ",", &saveptr);
	remote_host = strtok_r(NULL, ",", &saveptr);
	remote_port = strtok_r(NULL, ",", &saveptr);
	pt = strtok_r(NULL, ",", &saveptr);

	if (!cname || !remote_host || !remote_port || !pt) {
		cmd->reply = "Format is cname,remote_host,remote_port,pt";
		talloc_free(tmp);
		return CTRL_CMD_ERROR;
	}

	conn = find_connection_by_cname(g_rss, cname);
	if (!conn) {
		cmd->reply = "Error finding RTP connection for connect";
		talloc_free(tmp);
		return CTRL_CMD_ERROR;
	}

	rc = connect_connection(conn, remote_host, atoi(remote_port), atoi(pt));
	if (rc < 0) {
		cmd->reply = "Error binding RTP connection";
		talloc_free(tmp);
		return CTRL_CMD_ERROR;
	}

	/* Respond */
	talloc_free(tmp);
	cmd->reply = talloc_asprintf(cmd, "%s,%s,%d,%d", conn->cname, conn->remote_host,
					conn->remote_port, conn->rtp_pt);
	return CTRL_CMD_REPLY;
}

CTRL_CMD_DEFINE_WO_NOVRF(rtp_delete, "rtp_delete");
static int set_rtp_delete(struct ctrl_cmd *cmd, void *data)
{
	struct rtp_connection *conn;
	const char *cname = cmd->value;

	conn = find_connection_by_cname(g_rss, cname);
	if (!conn) {
		cmd->reply = "Error finding RTP connection for delete";
		return CTRL_CMD_ERROR;
	}
	cmd->reply = talloc_asprintf(cmd, "%s", conn->cname);

	delete_connection(conn);

	/* Respond */
	return CTRL_CMD_REPLY;
}





int rtpsource_ctrl_cmds_install(void)
{
	int rc;

	rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rtp_create);
	if (rc)
		goto end;

	rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rtp_connect);
	if (rc)
		goto end;

	rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rtp_delete);
	if (rc)
		goto end;
end:
	return rc;
}