trace_tcp.c 4.33 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
24
25
26
27
28
29
30
/*
	trace tcp connection handling

	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2013

	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 <trace_def.h>
#include <trace/trace.h>
#include <log/log.h>
#include <tcp_connect.h>

#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
31
#include <stdio.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#define TRACE_BUF_BLOCK	4096

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

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

54
void trace_initialize_fd(struct trace *trace, int fd)
55
{
56
57
58
	unsigned char *buffer = trace->rx_buffer;
	size_t buflen = trace->rx_buf_size;
	
59
60
	memset(trace, 0, sizeof(struct trace));
	trace->fd = fd;
61
62
63
	trace->rx_buffer = buffer;
	trace->rx_buf_size = buflen;
	
64
65
66
67
68
69
70
71
72
	if (trace->fd >= 0) {
		ioctl(trace->fd, FIONBIO, &(int){1});
	}
}

void trace_close(struct trace *trace)
{
	if (trace->fd >= 0)
		close(trace->fd);
73
74
75
76
77
78
79
	trace->fd = -1;	
}

void trace_free(struct trace *trace)
{
	trace_close(trace);

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	free(trace->rx_buffer);
	free(trace->host);
	free(trace);
}

int trace_fd_get(struct trace *trace)
{
	return trace->fd;
}

bool trace_fd_read(struct trace *trace)
{
	ssize_t ret;
	int i;
	bool rx_ready = false;

	if (trace->rx_buf_size - trace->rx_buf_size < TRACE_BUF_BLOCK) {
97
		unsigned char *newbuf;
98
99
100
101
		
		newbuf = realloc(trace->rx_buffer, trace->rx_buf_size +	TRACE_BUF_BLOCK);
		if (newbuf) {
			trace->rx_buffer = newbuf;
102
			trace->rx_buf_size += TRACE_BUF_BLOCK;
103
104
105
106
107
108
109
110
111
112
113
114
		} else {
			log_send(LOG_T_WARNING, 
			    "Could not enlarge buffer for receiving traces");
			return false;
		}
	}
	
	ret = read(trace->fd, trace->rx_buffer + trace->rx_len, TRACE_BUF_BLOCK);

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

116
117
118
119
120
	for (i = 0; i < trace->rx_len; i++) {
		if (trace->rx_buffer[i] == TRACE_END)
			rx_ready = true;
	}
	
121
122
123
124
125
126
127
	if (ret <= 0 && errno != EAGAIN && !rx_ready) {
		log_send(LOG_T_ERROR, "Trace connection lost");
		close(trace->fd);
		trace->fd = -1;
	}
	
	
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
	return rx_ready;
}

struct trace_pkt *trace_packet_get(struct trace *trace)
{
	int i;
	size_t len = 0;
	struct trace_pkt *pkt;
	
	while(trace->rx_len && trace->rx_buffer[0] == TRACE_END) {
		memmove(trace->rx_buffer, trace->rx_buffer+1, trace->rx_len-1);
		trace->rx_len--;
	}

	for (i = 0; i < trace->rx_len; i++) {
		if (trace->rx_buffer[i] == TRACE_END) {
			len = i;
			break;
		}
	}
	
	if (!len) {
		return NULL;
	}
	
	pkt = malloc(sizeof(struct trace_pkt));
	if (!pkt)
		goto err_pkt;
	pkt->data = malloc(len);
	if (!pkt->data)
		goto err_pkt_data;

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

	for (i = 0; i < len - 1; i++) {
		if (pkt->data[i] == TRACE_ESC) {
			if (pkt->data[i+1] == TRACE_ESC_END)
				pkt->data[i] = TRACE_END;
			memmove(pkt->data + 1, pkt->data + 2, len - i - 2);
			len--;
		}
	}
	pkt->len = len;
	
	trace->rx_len -= len + 1;
	memmove(trace->rx_buffer, trace->rx_buffer + len + 1, trace->rx_len);
	
	return pkt;

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

int trace_packet_write(struct trace *trace, struct trace_pkt *pkt)
{
	int i;
	int start = 0;
	
	while (start < pkt->len) for (i = start; i < pkt->len; i++) {
		if (pkt->data[i] == TRACE_ESC || pkt->data[i] == TRACE_END) {
191
			unsigned char *seq;
192
193
194
195
			
			write(trace->fd, pkt->data + start, i - start);
			start = i + 1;
			if (pkt->data[i] == TRACE_ESC)
196
				seq = (unsigned char[2]){ TRACE_ESC, TRACE_ESC_ESC };
197
			else
198
				seq = (unsigned char[2]){ TRACE_ESC, TRACE_ESC_END };
199
200
			write(trace->fd, seq, 2);
		}
201
202
203
204
		if (i == pkt->len -1) {
			write(trace->fd, pkt->data + start, i - start + 1);
			start = i + 1;
		}
205
	}
206
	write(trace->fd, &(char[1]){ TRACE_END }, 1);
207
208
209
	
	return 0;
}