clsync
socket.h
Go to the documentation of this file.
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 "port-hacks.h"
31 #include "clsync.h"
32 #include "ctx.h"
33 
34 #define SOCKET_DEFAULT_PROT 0
35 #define SOCKET_DEFAULT_SUBPROT SUBPROT0_TEXT
36 
37 // buffer size
38 #define SOCKET_BUFSIZ (1<<12)
39 
40 #if PIC
41 # define SOCKET_PROVIDER_LIBCLSYNC
42 #else
43 # define SOCKET_PROVIDER_CLSYNC
44 #endif
45 
46 #ifdef SOCKET_PROVIDER_LIBCLSYNC
47 # define SOCKET_MAX SOCKET_MAX_LIBCLSYNC
48 #endif
49 #ifdef SOCKET_PROVIDER_CLSYNC
50 # define SOCKET_MAX SOCKET_MAX_CLSYNC
51 #endif
52 
54 struct sockcmd;
55 
56 typedef int ( *clsyncsock_cb_funct_t ) ( struct socket_sockthreaddata *thread, struct sockcmd *sockcmd_p, void *arg );
57 struct clsynccbqueue {
58  uint64_t cmd_num;
59 
61  void *callback_arg;
62 };
64 
65 struct clsyncsock {
66  int sock;
67  uint16_t prot;
68  uint16_t subprot;
69 
70  uint64_t cmd_num;
71 
72  size_t cbqueue_len;
74  clsynccbqueue_t *cbqueue_cache[4 * CLSYNCSOCK_WINDOW + 1]; // It's a hacky hash-table of size "CLSYNCSOCK_WINDOW*2"
75 };
76 typedef struct clsyncsock clsyncsock_t;
77 
78 struct clsyncthread {
80  void *arg;
82 };
84 
85 enum subprot0 {
88 };
89 typedef enum subprot0 subprot0_t;
90 
97 };
99 
126 };
128 
130  uint16_t prot;
131  uint16_t subprot;
132 };
134 
136  uint64_t cmd_num;
137  uint16_t cmd_id;
138 };
140 #define sockcmd_dat_einval sockcmd_dat_ack
141 #define sockcmd_dat_einval_t sockcmd_dat_ack_t
142 #define sockcmd_dat_unknowncmd sockcmd_dat_ack
143 #define sockcmd_dat_unknowncmd_t sockcmd_dat_ack_t
144 
146  uint64_t cmd_num;
147 };
149 
151  int major;
152  int minor;
153  char revision[1 << 8];
154 };
156 
158  char config_block[1 << 8];
159  char label[1 << 8];
162 };
164 
166  char dir_path[PATH_MAX];
167 };
169 
171  char file_path[PATH_MAX];
172 };
174 
176  char descr[BUFSIZ];
177 };
179 
181  char key[BUFSIZ];
182  char value[BUFSIZ];
183 };
185 
186 struct sockcmd {
187  uint64_t cmd_num;
188  uint16_t cmd_id;
189  size_t data_len;
190  void *data;
191 };
192 typedef struct sockcmd sockcmd_t;
193 
196 };
198 
203 };
205 
206 struct socket_sockthreaddata;
207 typedef int ( *clsyncsock_procfunct_t ) ( struct socket_sockthreaddata *, sockcmd_t * );
208 typedef void ( *freefunct_t ) ( void * );
210  int id;
214  void *arg;
217  int *running; // Pointer to interger with non-zero value to continue running
219  pthread_t thread;
220 };
222 
223 extern int socket_reply ( clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p, sockcmd_id_t cmd_id, ... );
224 extern int socket_send ( clsyncsock_t *clsyncsock, sockcmd_id_t cmd_id, ... );
225 extern int socket_send_cb ( clsyncsock_t *clsyncsock_p, sockcmd_id_t cmd_id, clsyncsock_cb_funct_t cb, void *cb_arg, ... );
226 extern int socket_sendinvalid ( clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p );
228 extern int socket_check_bysock ( int sock );
229 extern clsyncsock_t *socket_accept ( int sock );
231 extern int socket_close ( clsyncsock_t *clsyncsock_p );
232 extern int socket_init();
233 extern int socket_deinit();
235 extern clsyncsock_t *socket_connect_unix ( const char *const socket_path );
236 extern clsyncsock_t *socket_listen_unix ( const char *const socket_path );
237 
239 extern int socket_thread_start ( socket_sockthreaddata_t *threaddata_p );
240 
241 extern int clsyncsocks_num;
242 extern int clsyncsocks_count;
243 extern int clsyncsocks_last;
244 
245 extern const char *const textmessage_args[];
246 extern const char *const textmessage_descr[];
247 
248 #endif
249 
socket_sockthreaddata
Definition: socket.h:209
ctx.h
socket_sockthreaddata::id
int id
Definition: socket.h:210
sockcmd_dat_version::major
int major
Definition: socket.h:151
clsyncsock_procfunct_t
int(* clsyncsock_procfunct_t)(struct socket_sockthreaddata *, sockcmd_t *)
Definition: socket.h:207
SOCKCMD_REQUEST_DIE
@ SOCKCMD_REQUEST_DIE
Definition: socket.h:115
clsyncsock::cbqueue_cache
clsynccbqueue_t * cbqueue_cache[4 *CLSYNCSOCK_WINDOW+1]
Definition: socket.h:74
socket_sockthreaddata::clsyncsock_p
clsyncsock_t * clsyncsock_p
Definition: socket.h:213
SOCKAUTH_UNSET
@ SOCKAUTH_UNSET
Definition: socket.h:200
textmessage_args
const char *const textmessage_args[]
Definition: socket.c:72
clsyncsocks_num
int clsyncsocks_num
socket_sockthreaddata::arg
void * arg
Definition: socket.h:214
clsyncsock_cb_funct_t
int(* clsyncsock_cb_funct_t)(struct socket_sockthreaddata *thread, struct sockcmd *sockcmd_p, void *arg)
Definition: socket.h:56
sockcmd_dat_dump::dir_path
char dir_path[PATH_MAX]
Definition: socket.h:166
sockcmd_dat_set::value
char value[BUFSIZ]
Definition: socket.h:182
SOCKCMD_REQUEST_SET
@ SOCKCMD_REQUEST_SET
Definition: socket.h:114
sockcmd_id_t
enum sockcmd_id sockcmd_id_t
Definition: socket.h:127
sockcmd_dat_info::config_block
char config_block[1<< 8]
Definition: socket.h:158
CLSTATE_DYING
@ CLSTATE_DYING
Definition: socket.h:95
clsyncsock::subprot
uint16_t subprot
Definition: socket.h:68
clsyncthread
Definition: socket.h:78
clsynccbqueue::callback_funct
clsyncsock_cb_funct_t callback_funct
Definition: socket.h:60
socket_send_cb
int socket_send_cb(clsyncsock_t *clsyncsock_p, sockcmd_id_t cmd_id, clsyncsock_cb_funct_t cb, void *cb_arg,...)
Definition: socket.c:352
SOCKCMD_MAXID
@ SOCKCMD_MAXID
Definition: socket.h:125
SOCKCMD_REPLY_VERSION
@ SOCKCMD_REPLY_VERSION
Definition: socket.h:117
SOCKCMD_REPLY_BYE
@ SOCKCMD_REPLY_BYE
Definition: socket.h:123
socket_sockthreaddata::flags
sockprocflags_t flags
Definition: socket.h:218
sockcmd_dat_ack
Definition: socket.h:135
clsynccbqueue
Definition: socket.h:57
subprot0_t
enum subprot0 subprot0_t
Definition: socket.h:89
sockcmd_dat_negotiation::prot
uint16_t prot
Definition: socket.h:130
clsyncthread::funct_arg_free
void * funct_arg_free
Definition: socket.h:81
SOCKCMD_REPLY_DIE
@ SOCKCMD_REPLY_DIE
Definition: socket.h:122
sockcmd_dat_dump
Definition: socket.h:165
CLSTATE_AUTH
@ CLSTATE_AUTH
Definition: socket.h:93
sockcmd_id
sockcmd_id
Definition: socket.h:100
sockcmd_dat_info::flags
char flags[(1<< 10)]
Definition: socket.h:160
socket_sockthreaddata::running
int * running
Definition: socket.h:217
clsyncthread::clsyncsock_p
clsyncsock_t * clsyncsock_p
Definition: socket.h:79
textmessage_descr
const char *const textmessage_descr[]
Definition: socket.c:88
socket_check_bysock
int socket_check_bysock(int sock)
Definition: socket.c:108
clsyncsock_state_t
enum clsyncsock_state clsyncsock_state_t
Definition: socket.h:98
SOCKCMD_REPLY_LOGIN
@ SOCKCMD_REPLY_LOGIN
Definition: socket.h:120
socket_listen_unix
clsyncsock_t * socket_listen_unix(const char *const socket_path)
Definition: socket.c:192
SOCKCMD_REPLY_UNEXPECTEDEND
@ SOCKCMD_REPLY_UNEXPECTEDEND
Definition: socket.h:124
sockprocflags
sockprocflags
Definition: socket.h:194
freefunct_t
void(* freefunct_t)(void *)
Definition: socket.h:208
clsyncsock_state
clsyncsock_state
Definition: socket.h:91
clsyncsock::prot
uint16_t prot
Definition: socket.h:67
sockcmd_dat_invalidcmd::cmd_num
uint64_t cmd_num
Definition: socket.h:146
socket_cleanup
int socket_cleanup(clsyncsock_t *clsyncsock_p)
Definition: socket.c:140
sockcmd_dat_set::key
char key[BUFSIZ]
Definition: socket.h:181
sockauth_id_t
enum sockauth_id sockauth_id_t
Definition: socket.h:204
sockcmd_dat_info::label
char label[1<< 8]
Definition: socket.h:159
SUBPROT0_BINARY
@ SUBPROT0_BINARY
Definition: socket.h:87
socket_thread_start
int socket_thread_start(socket_sockthreaddata_t *threaddata_p)
Definition: socket.c:808
SOCKCMD_REQUEST_LOGIN
@ SOCKCMD_REQUEST_LOGIN
Definition: socket.h:113
SOCKCMD_REQUEST_NEGOTIATION
@ SOCKCMD_REQUEST_NEGOTIATION
Definition: socket.h:101
sockcmd_dat_info::flags_set
char flags_set[(1<< 10)]
Definition: socket.h:161
SOCKAUTH_NULL
@ SOCKAUTH_NULL
Definition: socket.h:201
CLSYNCSOCK_WINDOW
#define CLSYNCSOCK_WINDOW
Definition: configuration.h:159
sockcmd::cmd_id
uint16_t cmd_id
Definition: socket.h:188
sockcmd_dat_invalidcmd
Definition: socket.h:145
socket_init
int socket_init()
Definition: socket.c:818
SOCKCMD_REPLY_INFO
@ SOCKCMD_REPLY_INFO
Definition: socket.h:118
socket_procclsyncsock
int socket_procclsyncsock(socket_sockthreaddata_t *arg)
Definition: socket.c:629
SOCKCMD_REPLY_ECUSTOM
@ SOCKCMD_REPLY_ECUSTOM
Definition: socket.h:109
CLSTATE_MAIN
@ CLSTATE_MAIN
Definition: socket.h:94
sockcmd_dat_negotiation::subprot
uint16_t subprot
Definition: socket.h:131
clsyncsocks_last
int clsyncsocks_last
OPTION_FLAGS
#define OPTION_FLAGS
Definition: ctx.h:37
clsync.h
SOCKCMD_REPLY_EINVAL
@ SOCKCMD_REPLY_EINVAL
Definition: socket.h:106
socket_thread_attach
socket_sockthreaddata_t * socket_thread_attach(clsyncsock_t *clsyncsock_p)
Definition: socket.c:797
socket_sendinvalid
int socket_sendinvalid(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p)
Definition: socket.c:621
CLSTATE_DIED
@ CLSTATE_DIED
Definition: socket.h:96
socket_sockthreaddata::authtype
sockauth_id_t authtype
Definition: socket.h:216
socket_close
int socket_close(clsyncsock_t *clsyncsock_p)
Definition: socket.c:150
sockcmd_dat_eexist::file_path
char file_path[PATH_MAX]
Definition: socket.h:171
CLSTATE_NONE
@ CLSTATE_NONE
Definition: socket.h:92
clsyncsock::sock
int sock
Definition: socket.h:66
sockcmd::cmd_num
uint64_t cmd_num
Definition: socket.h:187
SOCKCMD_REPLY_SET
@ SOCKCMD_REPLY_SET
Definition: socket.h:121
socket_reply
int socket_reply(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p, sockcmd_id_t cmd_id,...)
Definition: socket.c:331
SOCKCMD_REQUEST_INFO
@ SOCKCMD_REQUEST_INFO
Definition: socket.h:111
clsyncthread::arg
void * arg
Definition: socket.h:80
BUFSIZ
#define BUFSIZ
Definition: configuration.h:6
sockcmd_dat_info
Definition: socket.h:157
SOCKCMD_REPLY_UNKNOWNCMD
@ SOCKCMD_REPLY_UNKNOWNCMD
Definition: socket.h:104
sockcmd_dat_ack::cmd_num
uint64_t cmd_num
Definition: socket.h:136
clsyncsocks_count
int clsyncsocks_count
sockcmd_dat_eperm
Definition: socket.h:175
SUBPROT0_TEXT
@ SUBPROT0_TEXT
Definition: socket.h:86
socket_deinit
int socket_deinit()
Definition: socket.c:823
socket_recv
int socket_recv(clsyncsock_t *clsyncsock, sockcmd_t *sockcmd)
Definition: socket.c:501
socket_sockthreaddata::procfunct
clsyncsock_procfunct_t procfunct
Definition: socket.h:211
sockcmd_dat_negotiation
Definition: socket.h:129
SOCKCMD_REPLY_INVALIDCMDID
@ SOCKCMD_REPLY_INVALIDCMDID
Definition: socket.h:105
SOCKCMD_REQUEST_VERSION
@ SOCKCMD_REQUEST_VERSION
Definition: socket.h:110
sockcmd
Definition: socket.h:186
sockcmd_dat_version::revision
char revision[1<< 8]
Definition: socket.h:153
SOCKCMD_REQUEST_QUIT
@ SOCKCMD_REQUEST_QUIT
Definition: socket.h:116
clsynccbqueue::callback_arg
void * callback_arg
Definition: socket.h:61
sockcmd_dat_eperm::descr
char descr[BUFSIZ]
Definition: socket.h:176
SOCKCMD_REPLY_EPERM
@ SOCKCMD_REPLY_EPERM
Definition: socket.h:108
socket_sockthreaddata::thread
pthread_t thread
Definition: socket.h:219
clsyncsock::cmd_num
uint64_t cmd_num
Definition: socket.h:70
sockprocflags_t
enum sockprocflags sockprocflags_t
Definition: socket.h:197
sockcmd_dat_version
Definition: socket.h:150
clsyncsock::cbqueue_len
size_t cbqueue_len
Definition: socket.h:72
socket_sockthreaddata::freefunct_arg
freefunct_t freefunct_arg
Definition: socket.h:212
SOCKAUTH_PAM
@ SOCKAUTH_PAM
Definition: socket.h:202
port-hacks.h
subprot0
subprot0
Definition: socket.h:85
sockcmd_dat_eexist
Definition: socket.h:170
SOCKCMD_REPLY_ACK
@ SOCKCMD_REPLY_ACK
Definition: socket.h:103
clsyncsock::cbqueue
clsynccbqueue_t cbqueue[CLSYNCSOCK_WINDOW+1]
Definition: socket.h:73
SOCKPROCFLAG_NONE
@ SOCKPROCFLAG_NONE
Definition: socket.h:195
clsyncsock
Definition: socket.h:65
SOCKCMD_REPLY_DUMP
@ SOCKCMD_REPLY_DUMP
Definition: socket.h:119
socket_accept
clsyncsock_t * socket_accept(int sock)
Definition: socket.c:177
SOCKCMD_REPLY_NEGOTIATION
@ SOCKCMD_REPLY_NEGOTIATION
Definition: socket.h:102
SOCKCMD_REPLY_EEXIST
@ SOCKCMD_REPLY_EEXIST
Definition: socket.h:107
sockauth_id
sockauth_id
Definition: socket.h:199
clsynccbqueue::cmd_num
uint64_t cmd_num
Definition: socket.h:58
sockcmd::data
void * data
Definition: socket.h:190
SOCKCMD_REQUEST_DUMP
@ SOCKCMD_REQUEST_DUMP
Definition: socket.h:112
sockcmd_dat_set
Definition: socket.h:180
socket_sockthreaddata::state
clsyncsock_state_t state
Definition: socket.h:215
sockcmd::data_len
size_t data_len
Definition: socket.h:189
sockcmd_dat_ack::cmd_id
uint16_t cmd_id
Definition: socket.h:137
sockcmd_dat_version::minor
int minor
Definition: socket.h:152
socket_send
int socket_send(clsyncsock_t *clsyncsock, sockcmd_id_t cmd_id,...)
Definition: socket.c:342
socket_connect_unix
clsyncsock_t * socket_connect_unix(const char *const socket_path)