1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
12 #include <sys/socket.h>
14 #include <sys/epoll.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
21 #define MSG_CMD_TOO_LONG "Command too long."
30 size_t msg_in_len_max;
31 size_t msg_out_len_max;
35 conn_msg_handle_t msg_handle;
39 conn_init(struct conn_params *p)
41 struct sockaddr_in server_address;
43 int fd_server, fd_client_group, status;
45 memset(&server_address, 0, sizeof(server_address));
47 /* Check input arguments */
49 (p->welcome == NULL) ||
50 (p->prompt == NULL) ||
53 (p->msg_in_len_max == 0) ||
54 (p->msg_out_len_max == 0) ||
55 (p->msg_handle == NULL))
58 status = inet_aton(p->addr, &server_address.sin_addr);
62 /* Memory allocation */
63 conn = calloc(1, sizeof(struct conn));
67 conn->welcome = calloc(1, CONN_WELCOME_LEN_MAX + 1);
68 conn->prompt = calloc(1, CONN_PROMPT_LEN_MAX + 1);
69 conn->buf = calloc(1, p->buf_size);
70 conn->msg_in = calloc(1, p->msg_in_len_max + 1);
71 conn->msg_out = calloc(1, p->msg_out_len_max + 1);
73 if ((conn->welcome == NULL) ||
74 (conn->prompt == NULL) ||
75 (conn->buf == NULL) ||
76 (conn->msg_in == NULL) ||
77 (conn->msg_out == NULL)) {
83 server_address.sin_family = AF_INET;
84 server_address.sin_port = htons(p->port);
86 fd_server = socket(AF_INET,
87 SOCK_STREAM | SOCK_NONBLOCK,
89 if (fd_server == -1) {
94 status = bind(fd_server,
95 (struct sockaddr *) &server_address,
96 sizeof(server_address));
103 status = listen(fd_server, 16);
111 fd_client_group = epoll_create(1);
112 if (fd_client_group == -1) {
119 strncpy(conn->welcome, p->welcome, CONN_WELCOME_LEN_MAX);
120 strncpy(conn->prompt, p->prompt, CONN_PROMPT_LEN_MAX);
121 conn->buf_size = p->buf_size;
122 conn->msg_in_len_max = p->msg_in_len_max;
123 conn->msg_out_len_max = p->msg_out_len_max;
124 conn->msg_in_len = 0;
125 conn->fd_server = fd_server;
126 conn->fd_client_group = fd_client_group;
127 conn->msg_handle = p->msg_handle;
133 conn_free(struct conn *conn)
138 if (conn->fd_client_group)
139 close(conn->fd_client_group);
142 close(conn->fd_server);
152 conn_poll_for_conn(struct conn *conn)
154 struct sockaddr_in client_address;
155 struct epoll_event event;
156 socklen_t client_address_length;
157 int fd_client, status;
159 /* Check input arguments */
164 client_address_length = sizeof(client_address);
165 fd_client = accept4(conn->fd_server,
166 (struct sockaddr *) &client_address,
167 &client_address_length,
169 if (fd_client == -1) {
170 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
177 event.events = EPOLLIN | EPOLLRDHUP | EPOLLHUP;
178 event.data.fd = fd_client;
180 status = epoll_ctl(conn->fd_client_group,
190 status = write(fd_client,
192 strlen(conn->welcome));
198 status = write(fd_client,
200 strlen(conn->prompt));
210 data_event_handle(struct conn *conn,
213 ssize_t len, i, status;
215 /* Read input message */
217 len = read(fd_client,
221 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
229 /* Handle input messages */
230 for (i = 0; i < len; i++) {
231 if (conn->buf[i] == '\n') {
234 conn->msg_in[conn->msg_in_len] = 0;
235 conn->msg_out[0] = 0;
237 conn->msg_handle(conn->msg_in,
239 conn->msg_out_len_max);
241 n = strlen(conn->msg_out);
243 status = write(fd_client,
250 conn->msg_in_len = 0;
251 } else if (conn->msg_in_len < conn->msg_in_len_max) {
252 conn->msg_in[conn->msg_in_len] = conn->buf[i];
255 status = write(fd_client,
257 strlen(MSG_CMD_TOO_LONG));
261 conn->msg_in_len = 0;
266 status = write(fd_client,
268 strlen(conn->prompt));
276 control_event_handle(struct conn *conn,
281 status = epoll_ctl(conn->fd_client_group,
288 status = close(fd_client);
296 conn_poll_for_msg(struct conn *conn)
298 struct epoll_event event;
299 int fd_client, status, status_data = 0, status_control = 0;
301 /* Check input arguments */
306 status = epoll_wait(conn->fd_client_group,
315 fd_client = event.data.fd;
318 if (event.events & EPOLLIN)
319 status_data = data_event_handle(conn, fd_client);
322 if (event.events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP))
323 status_control = control_event_handle(conn, fd_client);
325 if (status_data || status_control)