command_tcp.c 5.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
	command tcp connection handling

	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014
trac
	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 3 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 <command/command_def.h>
#include <command/command.h>
#include <log/log.h>
24
#include <utils/tcp_connect.h>
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdio.h>

#define COMMAND_BUF_BLOCK	4096

struct command *command_alloc(void)
{
	return calloc(sizeof(struct command), 1);
}

struct command *command_open(char *host, int port)
{
	struct command *command;
	
	command = calloc(sizeof(struct command), 1);
	if (!command)
		return NULL;

	command->host = strdup(host);
	command->port = port;
	
	command->fd = tcp_connect(host, port);
	if (command->fd >= 0) {
		ioctl(command->fd, FIONBIO, &(int){1});
	}
	
	return command;
}

void command_initialize_fd(struct command *command, int fd)
{
	command->fd = fd;
	command->rx_len = 0;
	
	if (command->fd >= 0) {
		ioctl(command->fd, FIONBIO, &(int){1});
	}
}

void command_close(struct command *command)
{
	log_send(LOG_T_DEBUG, "Close command on request");
	if (command->fd >= 0)
		close(command->fd);
	command->fd = -1;	
	command->recover = false;
}

void command_free(struct command *command)
{
	command_close(command);

	free(command->name);
	free(command->rx_buffer);
	free(command->host);
	free(command->unit);
	free(command->id);
	free(command);
}

int command_fd_get(struct command *command)
{
	return command->fd;
}

bool command_fd_read(struct command *command)
{
	ssize_t ret;
	int i;
	bool rx_ready = false;

	if (command->rx_buf_size - command->rx_len < COMMAND_BUF_BLOCK) {
		unsigned char *newbuf;
		
		newbuf = realloc(command->rx_buffer, command->rx_buf_size + COMMAND_BUF_BLOCK);
		if (newbuf) {
			command->rx_buffer = newbuf;
			command->rx_buf_size += COMMAND_BUF_BLOCK;
		} else {
			log_send(LOG_T_WARNING, 
			    "Could not enlarge buffer for receiving commands");
			return false;
		}
	}
	
	ret = read(command->fd, command->rx_buffer + command->rx_len, COMMAND_BUF_BLOCK);

	if (ret > 0) {
		command->rx_len += ret;
	}

	for (i = 0; i < command->rx_len; i++) {
		if (command->rx_buffer[i] == COMMAND_END)
			rx_ready = true;
	}
	
	if (((ret < 0 && errno != EAGAIN) || ret == 0) && !rx_ready) {
		log_send(LOG_T_DEBUG, "error during read, closing");
		close(command->fd);
		command->fd = -1;
		
		if (command->handler_close)
			command->handler_close(command);
	}
	
	return rx_ready;
}

struct command_pkt *command_packet_get(struct command *command)
{
	int i;
	size_t len = 0, buflen;
	struct command_pkt *pkt;
	
	while(command->rx_len && command->rx_buffer[0] == COMMAND_END) {
		memmove(command->rx_buffer, command->rx_buffer+1, command->rx_len-1);
		command->rx_len--;
	}

	for (i = 0; i < command->rx_len; i++) {
		if (command->rx_buffer[i] == COMMAND_END) {
			len = i;
			break;
		}
	}
	
	if (!len) {
		return NULL;
	}
	
	buflen = len;
	
	pkt = command_packet_new();
	if (!pkt)
		goto err_pkt;
	if (command_packet_resize(pkt, len))
		goto err_pkt_data;

	memcpy(pkt->data, command->rx_buffer, len);
	pkt->type = pkt->data[0];

	for (i = 0; i < len - 1; i++) {
		if (pkt->data[i] == COMMAND_ESC) {
			if (pkt->data[i+1] == COMMAND_ESC_END)
				pkt->data[i] = COMMAND_END;
			memmove(&pkt->data[i+1], &pkt->data[i+2], len - i - 2);
			len--;
		}
	}
	pkt->len = len;
	
	command->rx_len -= buflen + 1;
	memmove(command->rx_buffer, command->rx_buffer + buflen + 1, command->rx_len);
	
	return pkt;

err_pkt_data:
	free(pkt);
err_pkt:
	return NULL;
}

int command_packet_write(struct command *command, struct command_pkt *pkt)
{
	int i;
	int start = 0;
	ssize_t r;
	
	if (!pkt->len)
		return 0;
	
	while (start < pkt->len) for (i = start; i < pkt->len; i++) {
		if (pkt->data[i] == COMMAND_ESC || pkt->data[i] == COMMAND_END) {
			unsigned char *seq;
			
			r = write(command->fd, pkt->data + start, i - start);
			if (r != i - start)
				goto err_write;
			
			start = i + 1;
			if (pkt->data[i] == COMMAND_ESC)
				seq = (unsigned char[2]){ COMMAND_ESC, COMMAND_ESC_ESC };
			else
				seq = (unsigned char[2]){ COMMAND_ESC, COMMAND_ESC_END };
			r = write(command->fd, seq, 2);
			if (r != 2)
				goto err_write;
		} else if (i == pkt->len -1) {
			r = write(command->fd, pkt->data + start, i - start + 1);
			if (r != i - start + 1)
				goto err_write;
			start = i + 1;
		}
	}
	r = write(command->fd, &(char[1]){ COMMAND_END }, 1);
	if (r < 0)
		goto err_write;
	
	return 0;

err_write:
	log_send(LOG_T_DEBUG, "Error during write, closing %d", command->fd);
	if (command->fd >= 0)
		close(command->fd);
	command->fd = -1;
		
	if (command->handler_close)
		command->handler_close(command);
	return -1;
}