summaryrefslogtreecommitdiff
path: root/modules/cons/cons.c
blob: 91c86c67aa8d8ddf5a9080fa6472d2f8001e229d (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
 * @file cons.c  Socket-based command-line console
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <re.h>
#include <baresip.h>


/**
 * @defgroup cons cons
 *
 * Console User-Interface (UI) using UDP/TCP sockets
 *
 *
 * This module implements a simple console for connecting to Baresip via
 * UDP or TCP-based sockets. You can use programs like telnet or netcat to
 * connect to the command-line interface.
 *
 * Example, with the cons-module listening on default port 5555:
 *
 \verbatim
  $ netcat -u 127.0.0.1 5555
 \endverbatim
 *
 * The following options can be configured:
 *
 \verbatim
  cons_listen     0.0.0.0:5555         # IP-address and port to listen on
 \endverbatim
 */


enum {CONS_PORT = 5555};

struct ui_st {
	struct udp_sock *us;
	struct tcp_sock *ts;
	struct tcp_conn *tc;
};


static struct ui_st *cons = NULL;  /* allow only one instance */


static int print_handler(const char *p, size_t size, void *arg)
{
	return mbuf_write_mem(arg, (uint8_t *)p, size);
}


static void udp_recv(const struct sa *src, struct mbuf *mb, void *arg)
{
	struct ui_st *st = arg;
	struct mbuf *mbr = mbuf_alloc(64);
	struct re_printf pf;

	pf.vph = print_handler;
	pf.arg = mbr;

	while (mbuf_get_left(mb)) {
		char ch = mbuf_read_u8(mb);

		if (ch == '\r')
			ch = '\n';

		ui_input_key(ch, &pf);
	}

	mbr->pos = 0;
	(void)udp_send(st->us, src, mbr);

	mem_deref(mbr);
}


static void cons_destructor(void *arg)
{
	struct ui_st *st = arg;

	mem_deref(st->us);
	mem_deref(st->tc);
	mem_deref(st->ts);
}


static int tcp_write_handler(const char *p, size_t size, void *arg)
{
	struct mbuf mb;

	mb.buf = (uint8_t *)p;
	mb.pos = 0;
	mb.end = mb.size = size;

	return tcp_send(arg, &mb);
}


static void tcp_recv_handler(struct mbuf *mb, void *arg)
{
	struct ui_st *st = arg;
	struct re_printf pf;

	pf.vph = tcp_write_handler;
	pf.arg = st->tc;

	while (mbuf_get_left(mb) > 0) {

		char ch = mbuf_read_u8(mb);

		if (ch == '\r')
			ch = '\n';

		ui_input_key(ch, &pf);
	}
}


static void tcp_close_handler(int err, void *arg)
{
	struct ui_st *st = arg;

	(void)err;

	st->tc = mem_deref(st->tc);
}


static void tcp_conn_handler(const struct sa *peer, void *arg)
{
	struct ui_st *st = arg;

	(void)peer;

	/* only one connection allowed */
	st->tc = mem_deref(st->tc);
	(void)tcp_accept(&st->tc, st->ts, NULL, tcp_recv_handler,
			 tcp_close_handler, st);
}


static int cons_alloc(struct ui_st **stp, const struct sa *laddr)
{
	struct ui_st *st;
	int err;

	if (!stp)
		return EINVAL;

	st = mem_zalloc(sizeof(*st), cons_destructor);
	if (!st)
		return ENOMEM;

	err = udp_listen(&st->us, laddr, udp_recv, st);
	if (err) {
		warning("cons: failed to listen on UDP %J (%m)\n",
			laddr, err);
		goto out;
	}

	err = tcp_listen(&st->ts, laddr, tcp_conn_handler, st);
	if (err) {
		warning("cons: failed to listen on TCP %J (%m)\n",
			laddr, err);
		goto out;
	}

	debug("cons: UI console listening on %J\n", laddr);

 out:
	if (err)
		mem_deref(st);
	else
		*stp = st;

	return err;
}


static struct ui ui_cons = {
	.name = "cons",
};


static int cons_init(void)
{
	struct sa laddr;
	int err;

	if (conf_get_sa(conf_cur(), "cons_listen", &laddr)) {
		sa_set_str(&laddr, "0.0.0.0", CONS_PORT);
	}

	err = cons_alloc(&cons, &laddr);
	if (err)
		return err;

	ui_register(&ui_cons);

	return 0;
}


static int cons_close(void)
{
	ui_unregister(&ui_cons);
	cons = mem_deref(cons);
	return 0;
}


const struct mod_export DECL_EXPORTS(cons) = {
	"cons",
	"ui",
	cons_init,
	cons_close
};