1 /*
2     clsync - file tree sync utility based on inotify
3 
4     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
5 
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __CLSYNC_SOCKET_H
21 #define __CLSYNC_SOCKET_H
22 
23 #ifdef __linux__
24 #include <linux/limits.h>
25 #endif
26 
27 #include <pthread.h>
28 #include <stdint.h>
29 
30 #include "clsync.h"
31 #include "ctx.h"
32 
33 #define SOCKET_DEFAULT_PROT	0
34 #define SOCKET_DEFAULT_SUBPROT	SUBPROT0_TEXT
35 
36 // buffer size
37 #define SOCKET_BUFSIZ			(1<<12)
38 
39 #if PIC
40 #	define SOCKET_PROVIDER_LIBCLSYNC
41 #else
42 #	define SOCKET_PROVIDER_CLSYNC
43 #endif
44 
45 #ifdef SOCKET_PROVIDER_LIBCLSYNC
46 #	define SOCKET_MAX SOCKET_MAX_LIBCLSYNC
47 #endif
48 #ifdef SOCKET_PROVIDER_CLSYNC
49 #	define SOCKET_MAX SOCKET_MAX_CLSYNC
50 #endif
51 
52 struct clsyncsock {
53 	int sock;
54 	uint16_t prot;
55 	uint16_t subprot;
56 };
57 typedef struct clsyncsock clsyncsock_t;
58 
59 struct clsyncthread {
60 	clsyncsock_t *clsyncsock_p;
61 	void *arg;
62 	void *funct_arg_free;
63 };
64 typedef struct clsyncthread clsyncthread_t;
65 
66 enum subprot0 {
67 	SUBPROT0_TEXT,
68 	SUBPROT0_BINARY,
69 };
70 typedef enum subprot0 subprot0_t;
71 
72 enum clsyncsock_state {
73 	CLSTATE_NONE	= 0,
74 	CLSTATE_AUTH,
75 	CLSTATE_MAIN,
76 	CLSTATE_DYING,
77 	CLSTATE_DIED,
78 };
79 typedef enum clsyncsock_state clsyncsock_state_t;
80 
81 enum sockcmd_id {
82 	SOCKCMD_REQUEST_NEGOTIATION	= 000,
83 	SOCKCMD_REPLY_NEGOTIATION	= 001,
84 	SOCKCMD_REPLY_ACK		= 150,
85 	SOCKCMD_REPLY_UNKNOWNCMD	= 160,
86 	SOCKCMD_REPLY_INVALIDCMDID	= 161,
87 	SOCKCMD_REPLY_EINVAL		= 162,
88 	SOCKCMD_REPLY_EEXIST		= 163,
89 	SOCKCMD_REPLY_EPERM		= 164,
90 	SOCKCMD_REPLY_ECUSTOM		= 199,
91 	SOCKCMD_REQUEST_LOGIN		= 200,
92 	SOCKCMD_REQUEST_VERSION		= 201,
93 	SOCKCMD_REQUEST_INFO		= 202,
94 	SOCKCMD_REQUEST_DUMP		= 203,
95 	SOCKCMD_REQUEST_DIE		= 210,
96 	SOCKCMD_REQUEST_QUIT		= 250,
97 	SOCKCMD_REPLY_LOGIN		= 300,
98 	SOCKCMD_REPLY_VERSION		= 301,
99 	SOCKCMD_REPLY_INFO		= 302,
100 	SOCKCMD_REPLY_DUMP		= 303,
101 	SOCKCMD_REPLY_DIE		= 310,
102 	SOCKCMD_REPLY_BYE		= 350,
103 	SOCKCMD_REPLY_UNEXPECTEDEND	= 351,
104 	SOCKCMD_MAXID
105 };
106 typedef enum sockcmd_id sockcmd_id_t;
107 
108 struct sockcmd_dat_negotiation {
109 	uint16_t prot;
110 	uint16_t subprot;
111 };
112 typedef struct sockcmd_dat_negotiation sockcmd_dat_negotiation_t;
113 
114 struct sockcmd_dat_ack {
115 	uint64_t	 cmd_num;
116 	uint16_t	 cmd_id;
117 };
118 typedef struct sockcmd_dat_ack sockcmd_dat_ack_t;
119 #define sockcmd_dat_einval       sockcmd_dat_ack
120 #define sockcmd_dat_einval_t     sockcmd_dat_ack_t
121 #define sockcmd_dat_unknowncmd   sockcmd_dat_ack
122 #define sockcmd_dat_unknowncmd_t sockcmd_dat_ack_t
123 
124 struct sockcmd_dat_invalidcmd {
125 	uint64_t	 cmd_num;
126 };
127 typedef struct sockcmd_dat_invalidcmd sockcmd_dat_invalidcmd_t;
128 
129 struct sockcmd_dat_version {
130 	int		major;
131 	int		minor;
132 	char		revision[1<<8];
133 };
134 typedef struct sockcmd_dat_version sockcmd_dat_version_t;
135 
136 struct sockcmd_dat_info {
137 	char		config_block[1<<8];
138 	char		label[1<<8];
139 	char		flags[OPTION_FLAGS];
140 	char		flags_set[OPTION_FLAGS];
141 };
142 typedef struct sockcmd_dat_info sockcmd_dat_info_t;
143 
144 struct sockcmd_dat_dump {
145 	char		dir_path[PATH_MAX];
146 };
147 typedef struct sockcmd_dat_dump sockcmd_dat_dump_t;
148 
149 struct sockcmd_dat_eexist {
150 	char		file_path[PATH_MAX];
151 };
152 typedef struct sockcmd_dat_eexist sockcmd_dat_eexist_t;
153 
154 struct sockcmd_dat_eperm {
155 	char		descr[BUFSIZ];
156 };
157 typedef struct sockcmd_dat_eperm sockcmd_dat_eperm_t;
158 
159 struct sockcmd {
160 	uint64_t	 cmd_num;
161 	uint16_t	 cmd_id;
162 	size_t		 data_len;
163 	void		*data;
164 };
165 typedef struct sockcmd sockcmd_t;
166 
167 enum sockprocflags {
168 	SOCKPROCFLAG_NONE		= 0x00,
169 };
170 typedef enum sockprocflags sockprocflags_t;
171 
172 enum sockauth_id {
173 	SOCKAUTH_UNSET	= 0,
174 	SOCKAUTH_NULL,
175 	SOCKAUTH_PAM,
176 };
177 typedef enum sockauth_id sockauth_id_t;
178 
179 struct socket_sockthreaddata;
180 typedef int (*clsyncsock_procfunct_t)(struct socket_sockthreaddata *, sockcmd_t *);
181 typedef void (*freefunct_t)(void *);
182 struct socket_sockthreaddata {
183 	int			 id;
184 	clsyncsock_procfunct_t	 procfunct;
185 	freefunct_t		 freefunct_arg;
186 	clsyncsock_t		*clsyncsock_p;
187 	void			*arg;
188 	clsyncsock_state_t	 state;
189 	sockauth_id_t		 authtype;
190 	int			*running;		// Pointer to interger with non-zero value to continue running
191 	sockprocflags_t		 flags;
192 	pthread_t		 thread;
193 };
194 typedef struct socket_sockthreaddata socket_sockthreaddata_t;
195 
196 extern int socket_send(clsyncsock_t *clsyncsock, sockcmd_id_t cmd_id, ...);
197 extern int socket_sendinvalid(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p);
198 extern int socket_recv(clsyncsock_t *clsyncsock, sockcmd_t *sockcmd);
199 extern int socket_check_bysock(int sock);
200 extern clsyncsock_t *socket_accept(int sock);
201 extern int socket_cleanup(clsyncsock_t *clsyncsock_p);
202 extern int socket_close(clsyncsock_t *clsyncsock_p);
203 extern int socket_init();
204 extern int socket_deinit();
205 extern int socket_procclsyncsock(socket_sockthreaddata_t *arg);
206 extern clsyncsock_t *socket_connect_unix(const char *const socket_path);
207 extern clsyncsock_t *socket_listen_unix (const char *const socket_path);
208 
209 extern socket_sockthreaddata_t *socket_thread_attach(clsyncsock_t *clsyncsock_p);
210 extern int socket_thread_start(socket_sockthreaddata_t *threaddata_p);
211 
212 extern int clsyncsocks_num;
213 extern int clsyncsocks_count;
214 extern int clsyncsocks_last;
215 
216 extern const char *const textmessage_args[];
217 extern const char *const textmessage_descr[];
218 
219 #endif
220 
221