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));
102 status = listen(fd_server, 16);
109 fd_client_group = epoll_create(1);
110 if (fd_client_group == -1) {
116 strncpy(conn->welcome, p->welcome, CONN_WELCOME_LEN_MAX);
117 strncpy(conn->prompt, p->prompt, CONN_PROMPT_LEN_MAX);
118 conn->buf_size = p->buf_size;
119 conn->msg_in_len_max = p->msg_in_len_max;
120 conn->msg_out_len_max = p->msg_out_len_max;
121 conn->msg_in_len = 0;
122 conn->fd_server = fd_server;
123 conn->fd_client_group = fd_client_group;
124 conn->msg_handle = p->msg_handle;
130 conn_free(struct conn *conn)
135 if (conn->fd_client_group)
136 close(conn->fd_client_group);
139 close(conn->fd_server);
149 conn_poll_for_conn(struct conn *conn)
151 struct sockaddr_in client_address;
152 struct epoll_event event;
153 socklen_t client_address_length;
154 int fd_client, status;
156 /* Check input arguments */
161 client_address_length = sizeof(client_address);
162 fd_client = accept4(conn->fd_server,
163 (struct sockaddr *) &client_address,
164 &client_address_length,
166 if (fd_client == -1) {
167 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
174 event.events = EPOLLIN | EPOLLRDHUP | EPOLLHUP;
175 event.data.fd = fd_client;
177 status = epoll_ctl(conn->fd_client_group,
187 status = write(fd_client,
189 strlen(conn->welcome));
195 status = write(fd_client,
197 strlen(conn->prompt));
207 data_event_handle(struct conn *conn,
210 ssize_t len, i, status;
212 /* Read input message */
214 len = read(fd_client,
218 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
226 /* Handle input messages */
227 for (i = 0; i < len; i++) {
228 if (conn->buf[i] == '\n') {
231 conn->msg_in[conn->msg_in_len] = 0;
232 conn->msg_out[0] = 0;
234 conn->msg_handle(conn->msg_in,
236 conn->msg_out_len_max);
238 n = strlen(conn->msg_out);
240 status = write(fd_client,
247 conn->msg_in_len = 0;
248 } else if (conn->msg_in_len < conn->msg_in_len_max) {
249 conn->msg_in[conn->msg_in_len] = conn->buf[i];
252 status = write(fd_client,
254 strlen(MSG_CMD_TOO_LONG));
258 conn->msg_in_len = 0;
263 status = write(fd_client,
265 strlen(conn->prompt));
273 control_event_handle(struct conn *conn,
278 status = epoll_ctl(conn->fd_client_group,
285 status = close(fd_client);
293 conn_poll_for_msg(struct conn *conn)
295 struct epoll_event event;
296 int fd_client, status, status_data = 0, status_control = 0;
298 /* Check input arguments */
303 status = epoll_wait(conn->fd_client_group,
312 fd_client = event.data.fd;
315 if (event.events & EPOLLIN)
316 status_data = data_event_handle(conn, fd_client);
319 if (event.events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP))
320 status_control = control_event_handle(conn, fd_client);
322 if (status_data || status_control)