Blame view

socket.h 5.53 KB
redmine authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    clsync - file tree sync utility based on fanotify and inotify
    
    Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

redmine authored
20 21 22
#ifndef __CLSYNC_SOCKET_H
#define __CLSYNC_SOCKET_H

redmine authored
23 24 25
#include <stdint.h>
#include <pthread.h>

redmine authored
26 27
#define SOCKET_DEFAULT_PROT	0
#define SOCKET_DEFAULT_SUBPROT	SUBPROT0_TEXT
redmine authored
28 29 30 31

// buffer size
#define SOCKET_BUFSIZ			(1<<12)

redmine authored
32 33 34 35 36 37 38 39 40 41 42 43 44
#if PIC
#	define SOCKET_PROVIDER_LIBCLSYNC
#else
#	define SOCKET_PROVIDER_CLSYNC
#endif

#ifdef SOCKET_PROVIDER_LIBCLSYNC
#	define SOCKET_MAX SOCKET_MAX_LIBCLSYNC
#endif
#ifdef SOCKET_PROVIDER_CLSYNC
#	define SOCKET_MAX SOCKET_MAX_CLSYNC
#endif

redmine authored
45
struct clsyncsock {
redmine authored
46 47 48 49
	int sock;
	uint16_t prot;
	uint16_t subprot;
};
redmine authored
50
typedef struct clsyncsock clsyncsock_t;
redmine authored
51

redmine authored
52
struct clsyncthread {
redmine authored
53
	clsyncsock_t *clsyncsock_p;
redmine authored
54 55 56 57 58
	void *arg;
	void *funct_arg_free;
};
typedef struct clsyncthread clsyncthread_t;

redmine authored
59 60 61 62 63 64
enum subprot0 {
	SUBPROT0_TEXT,
	SUBPROT0_BINARY,
};
typedef enum subprot0 subprot0_t;

redmine authored
65
enum clsyncsock_state {
redmine authored
66 67 68 69 70 71
	CLSTATE_NONE	= 0,
	CLSTATE_AUTH,
	CLSTATE_MAIN,
	CLSTATE_DYING,
	CLSTATE_DIED,
};
redmine authored
72
typedef enum clsyncsock_state clsyncsock_state_t;
redmine authored
73 74

enum sockcmd_id {
redmine authored
75 76 77 78 79 80
	SOCKCMD_REQUEST_NEGOTIATION	= 000,
	SOCKCMD_REPLY_NEGOTIATION	= 001,
	SOCKCMD_REPLY_ACK		= 150,
	SOCKCMD_REPLY_UNKNOWNCMD	= 160,
	SOCKCMD_REPLY_INVALIDCMDID	= 161,
	SOCKCMD_REPLY_EINVAL		= 162,
redmine authored
81 82 83
	SOCKCMD_REPLY_EEXIST		= 163,
	SOCKCMD_REPLY_EPERM		= 164,
	SOCKCMD_REPLY_ECUSTOM		= 199,
redmine authored
84 85 86
	SOCKCMD_REQUEST_LOGIN		= 200,
	SOCKCMD_REQUEST_VERSION		= 201,
	SOCKCMD_REQUEST_INFO		= 202,
redmine authored
87
	SOCKCMD_REQUEST_DUMP		= 203,
redmine authored
88 89 90 91 92
	SOCKCMD_REQUEST_DIE		= 210,
	SOCKCMD_REQUEST_QUIT		= 250,
	SOCKCMD_REPLY_LOGIN		= 300,
	SOCKCMD_REPLY_VERSION		= 301,
	SOCKCMD_REPLY_INFO		= 302,
redmine authored
93
	SOCKCMD_REPLY_DUMP		= 303,
redmine authored
94 95
	SOCKCMD_REPLY_DIE		= 310,
	SOCKCMD_REPLY_BYE		= 350,
redmine authored
96
	SOCKCMD_REPLY_UNEXPECTEDEND	= 351,
redmine authored
97
	SOCKCMD_MAXID
redmine authored
98 99 100
};
typedef enum sockcmd_id sockcmd_id_t;

redmine authored
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
struct sockcmd_dat_negotiation {
	uint16_t prot;
	uint16_t subprot;
};
typedef struct sockcmd_dat_negotiation sockcmd_dat_negotiation_t;

struct sockcmd_dat_ack {
	uint64_t	 cmd_num;
	uint16_t	 cmd_id;
};
typedef struct sockcmd_dat_ack sockcmd_dat_ack_t;
#define sockcmd_dat_einval       sockcmd_dat_ack
#define sockcmd_dat_einval_t     sockcmd_dat_ack_t
#define sockcmd_dat_unknowncmd   sockcmd_dat_ack
#define sockcmd_dat_unknowncmd_t sockcmd_dat_ack_t

struct sockcmd_dat_invalidcmd {
	uint64_t	 cmd_num;
};
typedef struct sockcmd_dat_invalidcmd sockcmd_dat_invalidcmd_t;

struct sockcmd_dat_version {
	int		major;
	int		minor;
	char		revision[1<<8];
};
typedef struct sockcmd_dat_version sockcmd_dat_version_t;

redmine authored
129
#ifdef __CLSYNC_CTX_H
redmine authored
130 131 132 133 134 135 136
struct sockcmd_dat_info {
	char		config_block[1<<8];
	char		label[1<<8];
	char		flags[OPTION_FLAGS];
	char		flags_set[OPTION_FLAGS];
};
typedef struct sockcmd_dat_info sockcmd_dat_info_t;
redmine authored
137
#endif
redmine authored
138

redmine authored
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
struct sockcmd_dat_dump {
	char		dir_path[PATH_MAX];
};
typedef struct sockcmd_dat_dump sockcmd_dat_dump_t;

struct sockcmd_dat_eexist {
	char		file_path[PATH_MAX];
};
typedef struct sockcmd_dat_eexist sockcmd_dat_eexist_t;

struct sockcmd_dat_eperm {
	char		descr[BUFSIZ];
};
typedef struct sockcmd_dat_eperm sockcmd_dat_eperm_t;

redmine authored
154
struct sockcmd {
redmine authored
155 156 157
	uint64_t	 cmd_num;
	uint16_t	 cmd_id;
	size_t		 data_len;
redmine authored
158
	void		*data;
redmine authored
159 160
};
typedef struct sockcmd sockcmd_t;
redmine authored
161

redmine authored
162
enum sockprocflags {
redmine authored
163
	SOCKPROCFLAG_NONE		= 0x00,
redmine authored
164 165 166 167 168 169 170 171 172 173
};
typedef enum sockprocflags sockprocflags_t;

enum sockauth_id {
	SOCKAUTH_UNSET	= 0,
	SOCKAUTH_NULL,
	SOCKAUTH_PAM,
};
typedef enum sockauth_id sockauth_id_t;

redmine authored
174 175
struct socket_sockthreaddata;
typedef int (*clsyncsock_procfunct_t)(struct socket_sockthreaddata *, sockcmd_t *);
redmine authored
176
typedef void (*freefunct_t)(void *);
redmine authored
177
struct socket_sockthreaddata {
redmine authored
178
	int			 id;
redmine authored
179
	clsyncsock_procfunct_t	 procfunct;
redmine authored
180
	freefunct_t		 freefunct_arg;
redmine authored
181
	clsyncsock_t		*clsyncsock_p;
redmine authored
182
	void			*arg;
redmine authored
183
	clsyncsock_state_t	 state;
redmine authored
184 185 186
	sockauth_id_t		 authtype;
	int			*running;		// Pointer to interger with non-zero value to continue running
	sockprocflags_t		 flags;
redmine authored
187
	pthread_t		 thread;
redmine authored
188
};
redmine authored
189
typedef struct socket_sockthreaddata socket_sockthreaddata_t;
redmine authored
190

redmine authored
191 192 193
extern int socket_send(clsyncsock_t *clsyncsock, sockcmd_id_t cmd_id, ...);
extern int socket_sendinvalid(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p);
extern int socket_recv(clsyncsock_t *clsyncsock, sockcmd_t *sockcmd);
redmine authored
194
extern int socket_check_bysock(int sock);
redmine authored
195 196
extern clsyncsock_t *socket_accept(int sock);
extern int socket_cleanup(clsyncsock_t *clsyncsock_p);
redmine authored
197 198
extern int socket_init();
extern int socket_deinit();
redmine authored
199 200 201
extern int socket_procclsyncsock(socket_sockthreaddata_t *arg);
extern clsyncsock_t *socket_connect_unix(const char *const socket_path);
extern clsyncsock_t *socket_listen_unix (const char *const socket_path);
redmine authored
202

redmine authored
203 204
extern socket_sockthreaddata_t *socket_thread_attach(clsyncsock_t *clsyncsock_p);
extern int socket_thread_start(socket_sockthreaddata_t *threaddata_p);
redmine authored
205

redmine authored
206 207 208
extern int clsyncsocks_num;
extern int clsyncsocks_count;
extern int clsyncsocks_last;
redmine authored
209

redmine authored
210 211
extern const char *const textmessage_args[];
extern const char *const textmessage_descr[];
redmine authored
212

redmine authored
213 214
#endif