aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ip_pipeline/conn.c
blob: 6b08e9e8e88c83d661f3915951c1d0c4951f00d9 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2018 Intel Corporation
 */

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

#define __USE_GNU
#include <sys/socket.h>

#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#include "conn.h"

#define MSG_CMD_TOO_LONG "Command too long."

struct conn {
	char *welcome;
	char *prompt;
	char *buf;
	char *msg_in;
	char *msg_out;
	size_t buf_size;
	size_t msg_in_len_max;
	size_t msg_out_len_max;
	size_t msg_in_len;
	int fd_server;
	int fd_client_group;
	conn_msg_handle_t msg_handle;
};

struct conn *
conn_init(struct conn_params *p)
{
	struct sockaddr_in server_address;
	struct conn *conn;
	int fd_server, fd_client_group, status;

	memset(&server_address, 0, sizeof(server_address));

	/* Check input arguments */
	if ((p == NULL) ||
		(p->welcome == NULL) ||
		(p->prompt == NULL) ||
		(p->addr == NULL) ||
		(p->buf_size == 0) ||
		(p->msg_in_len_max == 0) ||
		(p->msg_out_len_max == 0) ||
		(p->msg_handle == NULL))
		return NULL;

	status = inet_aton(p->addr, &server_address.sin_addr);
	if (status == 0)
		return NULL;

	/* Memory allocation */
	conn = calloc(1, sizeof(struct conn));
	if (conn == NULL)
		return NULL;

	conn->welcome = calloc(1, CONN_WELCOME_LEN_MAX + 1);
	conn->prompt = calloc(1, CONN_PROMPT_LEN_MAX + 1);
	conn->buf = calloc(1, p->buf_size);
	conn->msg_in = calloc(1, p->msg_in_len_max + 1);
	conn->msg_out = calloc(1, p->msg_out_len_max + 1);

	if ((conn->welcome == NULL) ||
		(conn->prompt == NULL) ||
		(conn->buf == NULL) ||
		(conn->msg_in == NULL) ||
		(conn->msg_out == NULL)) {
		conn_free(conn);
		return NULL;
	}

	/* Server socket */
	server_address.sin_family = AF_INET;
	server_address.sin_port = htons(p->port);

	fd_server = socket(AF_INET,
		SOCK_STREAM | SOCK_NONBLOCK,
		0);
	if (fd_server == -1) {
		conn_free(conn);
		return NULL;
	}

	status = bind(fd_server,
		(struct sockaddr *) &server_address,
		sizeof(server_address));
	if (status == -1) {
		conn_free(conn);
		close(fd_server);
		return NULL;
	}

	status = listen(fd_server, 16);
	if (status == -1) {
		conn_free(conn);
		close(fd_server);
		return NULL;
	}

	/* Client group */
	fd_client_group = epoll_create(1);
	if (fd_client_group == -1) {
		conn_free(conn);
		close(fd_server);
		return NULL;
	}

	/* Fill in */
	strncpy(conn->welcome, p->welcome, CONN_WELCOME_LEN_MAX);
	strncpy(conn->prompt, p->prompt, CONN_PROMPT_LEN_MAX);
	conn->buf_size = p->buf_size;
	conn->msg_in_len_max = p->msg_in_len_max;
	conn->msg_out_len_max = p->msg_out_len_max;
	conn->msg_in_len = 0;
	conn->fd_server = fd_server;
	conn->fd_client_group = fd_client_group;
	conn->msg_handle = p->msg_handle;

	return conn;
}

void
conn_free(struct conn *conn)
{
	if (conn == NULL)
		return;

	if (conn->fd_client_group)
		close(conn->fd_client_group);

	if (conn->fd_server)
		close(conn->fd_server);

	free(conn->msg_out);
	free(conn->msg_in);
	free(conn->prompt);
	free(conn->welcome);
	free(conn);
}

int
conn_poll_for_conn(struct conn *conn)
{
	struct sockaddr_in client_address;
	struct epoll_event event;
	socklen_t client_address_length;
	int fd_client, status;

	/* Check input arguments */
	if (conn == NULL)
		return -1;

	/* Server socket */
	client_address_length = sizeof(client_address);
	fd_client = accept4(conn->fd_server,
		(struct sockaddr *) &client_address,
		&client_address_length,
		SOCK_NONBLOCK);
	if (fd_client == -1) {
		if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
			return 0;

		return -1;
	}

	/* Client group */
	event.events = EPOLLIN | EPOLLRDHUP | EPOLLHUP;
	event.data.fd = fd_client;

	status = epoll_ctl(conn->fd_client_group,
		EPOLL_CTL_ADD,
		fd_client,
		&event);
	if (status == -1) {
		close(fd_client);
		return -1;
	}

	/* Client */
	status = write(fd_client,
		conn->welcome,
		strlen(conn->welcome));
	if (status == -1) {
		close(fd_client);
		return -1;
	}

	status = write(fd_client,
		conn->prompt,
		strlen(conn->prompt));
	if (status == -1) {
		close(fd_client);
		return -1;
	}

	return 0;
}

static int
data_event_handle(struct conn *conn,
	int fd_client)
{
	ssize_t len, i, status;

	/* Read input message */

	len = read(fd_client,
		conn->buf,
		conn->buf_size);
	if (len == -1) {
		if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
			return 0;

		return -1;
	}
	if (len == 0)
		return 0;

	/* Handle input messages */
	for (i = 0; i < len; i++) {
		if (conn->buf[i] == '\n') {
			size_t n;

			conn->msg_in[conn->msg_in_len] = 0;
			conn->msg_out[0] = 0;

			conn->msg_handle(conn->msg_in,
				conn->msg_out,
				conn->msg_out_len_max);

			n = strlen(conn->msg_out);
			if (n) {
				status = write(fd_client,
					conn->msg_out,
					n);
				if (status == -1)
					return status;
			}

			conn->msg_in_len = 0;
		} else if (conn->msg_in_len < conn->msg_in_len_max) {
			conn->msg_in[conn->msg_in_len] = conn->buf[i];
			conn->msg_in_len++;
		} else {
			status = write(fd_client,
				MSG_CMD_TOO_LONG,
				strlen(MSG_CMD_TOO_LONG));
			if (status == -1)
				return status;

			conn->msg_in_len = 0;
		}
	}

	/* Write prompt */
	status = write(fd_client,
		conn->prompt,
		strlen(conn->prompt));
	if (status == -1)
		return status;

	return 0;
}

static int
control_event_handle(struct conn *conn,
	int fd_client)
{
	int status;

	status = epoll_ctl(conn->fd_client_group,
		EPOLL_CTL_DEL,
		fd_client,
		NULL);
	if (status == -1)
		return -1;

	status = close(fd_client);
	if (status == -1)
		return -1;

	return 0;
}

int
conn_poll_for_msg(struct conn *conn)
{
	struct epoll_event event;
	int fd_client, status, status_data = 0, status_control = 0;

	/* Check input arguments */
	if (conn == NULL)
		return -1;

	/* Client group */
	status = epoll_wait(conn->fd_client_group,
		&event,
		1,
		0);
	if (status == -1)
		return -1;
	if (status == 0)
		return 0;

	fd_client = event.data.fd;

	/* Data available */
	if (event.events & EPOLLIN)
		status_data = data_event_handle(conn, fd_client);

	/* Control events */
	if (event.events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP))
		status_control = control_event_handle(conn, fd_client);

	if (status_data || status_control)
		return -1;

	return 0;
}