1 /*
2  * Copyright (C) 2022 Intel Corporation.
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef _SOCKET_H_
6 #define _SOCKET_H_
7 #include <sys/queue.h>
8 #include <pthread.h>
9 #include <sys/un.h>
10 
11 #define BUFFER_SIZE 16U
12 #define UNIX_SOCKET_PATH_MAX 1024U
13 #define CLIENT_BUF_LEN 4096U
14 #define SOCKET_MAX_CLIENT		1
15 
16 typedef void data_handler_f(char *cmd_name, int fd);
17 
18 struct socket_client {
19 	struct sockaddr_un addr;
20 	int fd;
21 	socklen_t addr_len;
22 	char buf[CLIENT_BUF_LEN];
23 	int len; /* buf len */
24 	/* When a client is registered as vm_event receiver, we need this per_client_mutex
25 	 * to make sure it is safe to free the client when client disconnects.
26 	 */
27 	pthread_mutex_t *per_client_mutex;
28 	void (*free_client_cb)(struct socket_client *self);
29 	LIST_ENTRY(socket_client) list;
30 };
31 
32 struct socket_dev {
33 	char unix_sock_path[UNIX_SOCKET_PATH_MAX];
34 	int sock_fd;
35 	int logfd;
36 
37 	data_handler_f *data_handler;
38 
39 	bool listening;
40 	bool polling;
41 	pthread_t listen_thread;
42 	pthread_t connect_thread;
43 
44 	LIST_HEAD(client_list, socket_client) client_head;        /* clients for this server */
45 	pthread_mutex_t client_mtx;
46 	int num_client;
47 };
48 
49 /**
50  * @brief Send message through unix domain socket server
51  */
52 int write_socket_char(struct socket_client *client);
53 /**
54  * @brief Find socket client instance according to fd
55  */
56 struct socket_client *find_socket_client(struct socket_dev *sock, int fd);
57 /**
58  * @brief Open one unix domain socket server, initialize a socket,
59  * create one thread to listen to client, another thread to poll message from client.
60  */
61 int open_socket(struct socket_dev *sock, data_handler_f *fn);
62 /**
63  * @brief Close one unix domain socket server
64  */
65 void close_socket(struct socket_dev *sock);
66 /**
67  * @brief Initialize a socket
68  *
69  * @param path the socket path
70  * @return struct socket_dev* the socket instance
71  */
72 struct socket_dev *init_socket(char *path);
73 /**
74  * @brief Deinit a socket
75  *
76  * @param sock The pointer of socket instance
77  */
78 void deinit_socket(struct socket_dev *sock);
79 
80 #endif
81