summaryrefslogtreecommitdiffstats
path: root/src/standard.c
blob: f377bec7b4f1fff0d342f3791aa6840a23249785 (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
/*	Asterisk Manager Proxy
	Copyright (c) 2005-2006 David C. Troy <dave@popvox.com>

	This program is free software, distributed under the terms of
	the GNU General Public License.

	standard.c
	Standard I/O Handler
*/

#include "astmanproxy.h"

extern struct mansession *sessions;

/* Return a fully formed message block to session_do for processing */
int _read(struct mansession *s, struct message *m) {
	int res;

	for (;;) {
		res = get_input(s, m->headers[m->hdrcount]);

		if (strstr(m->headers[m->hdrcount], "--END COMMAND--")) {
				if (debug) debugmsg("Found END COMMAND");
				m->in_command = 0;
		}
		if (strstr(m->headers[m->hdrcount], "Response: Follows")) {
				if (debug) debugmsg("Found Response Follows");
				m->in_command = 1;
		}
		if (res > 0) {
			if (!m->in_command && *(m->headers[m->hdrcount]) == '\0' ) {
				break;
			} else if (m->hdrcount < MAX_HEADERS - 1) {
				m->hdrcount++;
			} else {
				m->in_command = 0; // reset when block full
			}
		} else if (res < 0)
			break;
	}

	return res;
}

int _write(struct mansession *s, struct message *m) {
	int i;

	pthread_mutex_lock(&s->lock);
	for (i=0; i<m->hdrcount; i++) {
	ast_carefulwrite(s->fd, m->headers[i], strlen(m->headers[i]) , s->writetimeout);
		ast_carefulwrite(s->fd, "\r\n", 2, s->writetimeout);
	}
	ast_carefulwrite(s->fd, "\r\n", 2, s->writetimeout);
	pthread_mutex_unlock(&s->lock);

	return 0;
}

int _onconnect(struct mansession *s, struct message *m) {

	char banner[100];

	sprintf(banner, "%s/%s\r\n", PROXY_BANNER, PROXY_VERSION);
	pthread_mutex_lock(&s->lock);
	ast_carefulwrite(s->fd, banner, strlen(banner), s->writetimeout);
	pthread_mutex_unlock(&s->lock);

	return 0;
}