summaryrefslogtreecommitdiff
path: root/econpacket.c
blob: 0eac748524329d146154ed57fa5683c2fabde170 (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
/*
 * Copyright © 2013 Benjamin Franzke
 * 
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/socket.h>
#include <sys/uio.h>

#include "econpacket.h"
#include "econproto.h"

static void
init_iov(struct econ_packet *pkt)
{
	pkt->iov[0].iov_base = &pkt->hdr;
	pkt->iov[0].iov_len = sizeof pkt->hdr;
	pkt->iov[1].iov_base = &pkt->cmd;
	pkt->iov[1].iov_len = sizeof pkt->cmd;
	pkt->iov[2].iov_base = &pkt->rec;
	pkt->iov[2].iov_len = sizeof pkt->rec;

	/* To receive oversized commands */
	pkt->iov[3].iov_base = pkt->long_data;
	pkt->iov[3].iov_len = sizeof pkt->long_data;
}

static void
init_header(struct econ_header *ehdr, int cmd)
{
	memset(ehdr, 0, sizeof *ehdr);

	strncpy(ehdr->magicnum, ECON_MAGIC_NUMBER,  ECON_MAGICNUM_SIZE);
	strncpy(ehdr->version,  ECON_PROTO_VERSION, ECON_PROTOVER_MAXLEN);

	ehdr->datasize = 0;
	ehdr->commandID = cmd;
}

void
epkt_init(struct econ_packet *pkt, int cmd)
{
	init_iov(pkt);

	memset(&pkt->rec, 0, sizeof pkt->rec);
	memset(&pkt->cmd, 0, sizeof pkt->cmd);
	init_header(&pkt->hdr, cmd);

	/* For irregular commands like 21 */
	pkt->long_data_size = 0;
}

int
epkt_send(int fd, struct econ_packet *pkt)
{
	int i = 1;

	if (pkt->hdr.datasize > 0) {
		i++;
		if (pkt->cmd.recordCount == 1)
			i++;
	}

	return writev(fd, pkt->iov, i);
}

static int
iov_max_read(struct iovec *iov, int *iovcnt, size_t size)
{
	int i;

	for (i = 0; i < *iovcnt; ++i) {
		if (iov[i].iov_len > size) {
			iov[i].iov_len = size;
			*iovcnt = i+1;
			return 0;
		}
		size -= iov[i].iov_len;
	}

	if (size > 0)
		return -1;

	return 0;
}

int
epkt_read(int fd, struct econ_packet *pkt)
{
	union { ssize_t s; size_t u; } len, len2;
	int type;
	socklen_t length = sizeof(int);
	int iovcnt;

	init_iov(pkt);
	pkt->long_data_size = 0;

	if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &length) < 0)
		return -1;

	/* FIXME Do we get the diffs between udp and tcp handled a bit nicer? */
	switch (type) {
	case SOCK_STREAM:
		iovcnt = 1;
		break;
	case SOCK_DGRAM:
		iovcnt = 4;
		break;
	default:
		return -1;
	}

	len.s = readv(fd, pkt->iov, iovcnt);
	if (len.s < 0)
		return -1;

	if (len.u < sizeof(struct econ_header)) {
		fprintf(stderr, "epkt_read: error: incomplete header\n");
		return -1;
	}

	fprintf(stderr, "epkt_read: len.u: %zd, cmd: %d, datasize: %d\n",
		len.u, pkt->hdr.commandID, pkt->hdr.datasize);

	if (pkt->hdr.datasize == 0) {
		if (len.u > sizeof(struct econ_header))
		    return -1;
		return 0;
	}

	if (pkt->hdr.datasize > 1024)
		return -1;

	if (type == SOCK_STREAM) {
		iovcnt = 3;
		if (iov_max_read(&pkt->iov[1], &iovcnt, pkt->hdr.datasize) < 0)
			return -1;

		/* Yes, this may write up to long_data[1024]. */
		len2.s = readv(fd, &pkt->iov[1], iovcnt);
		if (len2.s < 0 || len2.u != pkt->hdr.datasize)
			return -1;

		len.u += len2.u;
	}

	if (len.u != sizeof(struct econ_header) + pkt->hdr.datasize) {
		fprintf(stderr, "packet has invalid datasize\n");
		return -1;
	}

	if (pkt->hdr.datasize < sizeof(struct econ_command)) {
		fprintf(stderr,
			"epkt_read: error: command to short\n");
		return -1;
	}

	/* Keepalive is an irregular command if datasize > 0:
	 *  the regular field recordCount is 1,
	 *  without actually having one. */
	if (pkt->hdr.commandID == E_CMD_KEEPALIVE)
		return 0;

	/* Handle irregular long datasize */
	if (pkt->hdr.datasize > (sizeof(struct econ_command) +
				 sizeof(struct econ_record))) {
		switch (pkt->hdr.commandID) {
		case E_CMD_21:
			break;
		default:
			fprintf(stderr, "epkt_read: received cmd: %d"
				" with oversized datasize\n",
				pkt->hdr.commandID);
			exit(EXIT_FAILURE);
		}
		pkt->long_data_size = (pkt->hdr.datasize -
				       (sizeof (struct econ_command) +
					sizeof (struct econ_record)));
		return 0;
	}

	if (pkt->cmd.recordCount > 0) {
		if (pkt->cmd.recordCount > 1) {
			fprintf(stderr, "epkt_read: did not expect a packet "
				"with more than one record: %d, datasize: %d.\n",
				pkt->cmd.recordCount, pkt->hdr.datasize);
			return -1;
		}
		if (pkt->hdr.datasize != (sizeof (struct econ_command) +
					  sizeof (struct econ_record))) {
			fprintf(stderr, "epkt_read: datasize incorrect, cmd: %d\n",
				pkt->hdr.commandID);
			return -1;
		}
	}

	return 0;
}