[go: up one dir, main page]

Menu

[ebf9b6]: / src / flom_conn.h  Maximize  Restore  History

Download this file

463 lines (353 with data), 11.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright (c) 2013-2024, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLoM, Free Lock Manager
*
* FLoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2.0 as
* published by the Free Software Foundation.
*
* FLoM 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 FLoM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FLOM_CONN_H
# define FLOM_CONN_H
#include <config.h>
#ifdef HAVE_GLIB_H
# include <glib.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_POLL_H
# include <poll.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#endif
#include "flom_config.h"
#include "flom_errors.h"
#include "flom_msg.h"
#include "flom_tcp.h"
#include "flom_tls.h"
/* save old FLOM_TRACE_MODULE and set a new value */
#ifdef FLOM_TRACE_MODULE
# define FLOM_TRACE_MODULE_SAVE FLOM_TRACE_MODULE
# undef FLOM_TRACE_MODULE
#else
# undef FLOM_TRACE_MODULE_SAVE
#endif /* FLOM_TRACE_MODULE */
#define FLOM_TRACE_MODULE FLOM_TRACE_MOD_CONNS
/**
* Possible state of a connection
*/
typedef enum flom_conn_state_e {
/**
* The connection is managed by main daemon thread (the listener thread)
*/
FLOM_CONN_STATE_DAEMON,
/**
* The connection is managed by a locker thread (the resource manager
* threads)
*/
FLOM_CONN_STATE_LOCKER,
/**
* The connection is no more useful and must be eliminated by
* @ref flom_conns_clean
*/
FLOM_CONN_STATE_REMOVE
} flom_conn_state_t;
/**
* Class of objects used to store connection data
*/
typedef struct {
/**
* Unique identifier associated to the connection object
*/
flom_uid_t uid;
/**
* Connection state
*/
flom_conn_state_t state;
/**
* The connection is related to a client waiting for resource creation
* (it's logically parked inside a virtual incubator); this is a boolean
* property
*/
int wait;
/**
* Step of the last sent/received message
*/
int last_step;
/**
* TCP/IP connection data
*/
flom_tcp_t tcp;
/**
* TLS/SSL connection data
*/
flom_tls_t *tls;
/**
* Last received message (allocated by malloc)
*/
struct flom_msg_s *msg;
/**
* GMarkup Parser context (allocated by g_markup_parse_context_new)
*/
GMarkupParseContext *parser;
} flom_conn_t;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* Factory method to create a new flom_conn_t object
* @param config IN configuration object reference or NULL
* @return a new object or NULL if any error occurred
*/
flom_conn_t *flom_conn_new(flom_config_t *config);
/**
* Destroy an object of type flom_conn_t
* @param obj IN object to be destroyed
*/
void flom_conn_delete(flom_conn_t *obj);
/**
* Initialize a connection object with network parameters
* @param obj IN connection object
* @param uid IN unique identifier associated to the connection
* @param domain IN communication domain (AF_UNIX/AF_LOCAL, AF_INET,
* AF_INET6)
* @param sockfd IN TCP socket file descriptor
* @param type IN TCP socket type
* @param addrlen IN address lenght
* @param sa IN socket address
* @param main_thread IN boolean value: TRUE for daemon main thread
* (father/listener), FALSE for locker child thread
* (child/locker)
* @return a reason code
*/
int flom_conn_init(flom_conn_t *obj, flom_uid_t uid, int domain,
int sockfd, int type,
socklen_t addrlen, const struct sockaddr *sa,
int main_thread);
/**
* Getter method for uid property
* @param obj IN connection object
* @return uid
*/
static inline flom_uid_t flom_conn_get_uid(const flom_conn_t *obj) {
return obj->uid;
}
/**
* Setter method for uid property
* @param obj IN/OUT connection object
* @param value IN new value for uid
*/
static inline void flom_conn_set_uid(flom_conn_t *obj, flom_uid_t value) {
obj->uid = value;
}
/**
* Getter method for state property
* @param obj IN connection object
* @return state
*/
static inline int flom_conn_get_state(const flom_conn_t *obj) {
return obj->state;
}
/**
* Setter method for state property
* @param obj IN/OUT connection object
* @param value IN new value for state
*/
static inline void flom_conn_set_state(flom_conn_t *obj, int value) {
obj->state = value;
}
/**
* Getter method for wait property
* @param obj IN connection object
* @return wait
*/
static inline int flom_conn_get_wait(const flom_conn_t *obj) {
return obj->wait;
}
/**
* Setter method for wait property
* @param obj IN/OUT connection object
* @param value IN new value for wait
*/
static inline void flom_conn_set_wait(flom_conn_t *obj, int value) {
obj->wait = value;
}
/**
* Getter method for last_step property
* @param obj IN connection object
* @return last_step
*/
static inline int flom_conn_get_last_step(const flom_conn_t *obj) {
return obj->last_step;
}
/**
* Setter method for last_step property
* @param obj IN/OUT connection object
* @param value IN new value for last_step
*/
static inline void flom_conn_set_last_step(flom_conn_t *obj, int value) {
obj->last_step = value;
}
/**
* Getter method for tcp property
* @param obj IN connection object
* @return tcp
*/
static inline flom_tcp_t *flom_conn_get_tcp(flom_conn_t *obj) {
return &obj->tcp;
}
/**
* Getter method for tls property
* @param obj IN connection object
* @return tls
*/
static inline flom_tls_t *flom_conn_get_tls(flom_conn_t *obj) {
return obj->tls;
}
/**
* Getter method for msg property
* @param obj IN connection object
* @return msg
*/
static inline struct flom_msg_s *flom_conn_get_msg(flom_conn_t *obj) {
return obj->msg;
}
/**
* Setter method for msg property
* @param obj IN/OUT connection object
* @param value IN new value for msg
*/
static inline void flom_conn_set_msg(flom_conn_t *obj,
struct flom_msg_s *value) {
obj->msg = value;
}
/**
* Getter method for parser property
* @param obj IN connection object
* @return msg
*/
static inline GMarkupParseContext *flom_conn_get_parser(flom_conn_t *obj) {
return obj->parser;
}
/**
* Setter method for parser property
* @param obj IN connection object
* @param ref IN reference to the new parser
* @return msg
*/
static inline void flom_conn_set_parser(
flom_conn_t *obj, GMarkupParseContext *ref) {
if (NULL != obj->parser)
g_markup_parse_context_free(obj->parser);
obj->parser = ref;
}
/**
* Release the parser object used by the connection
* @param obj IN/OUT connection object
*/
void flom_conn_free_parser(flom_conn_t *obj);
/**
* Receive a buffer using raw TCP/IP or TLS over TCP/IP
* @param obj IN/OUT TLS object
* @param buf OUT buffer to send
* @param len IN buffer lenght
* @param received OUT number of read bytes
* @param timeout IN maximum wait time to receive the answer
* (milliseconds); use @ref FLOM_NETWORK_WAIT_TIMEOUT for indefinite
* wait
* @param src_addr OUT transparently passed to recvfrom if type is
* SOCK_DGRAM (see recvfrom man page); it applies only to
* UDP/IP
* @param addrlen OUT transparently passed to recvfrom if type is
* SOCK_DGRAM (see recvfrom man page); it applied only to
* UDP/IP
* @return a reason code
*/
int flom_conn_recv(flom_conn_t *obj, void *buf, size_t len,
size_t *received, int timeout,
struct sockaddr *src_addr, socklen_t *addrlen);
/**
* Send a buffer using raw TCP/IP or TLS over TCP/IP
* @param obj IN/OUT connection object
* @param buf IN buffer to send
* @param len IN buffer lenght
* @return a reason code
*/
int flom_conn_send(flom_conn_t *obj, const void *buf, size_t len);
/**
* Close a raw TCP/IP or TLS over TCP/IP connection
* @param obj IN/OUT connection object
* @return a reason code
*/
static inline int flom_conn_close(flom_conn_t *obj) {
return flom_tcp_close(&obj->tcp);
}
/**
* Start connection termination
* @param obj IN/OUT connection object
* @return a reason code
*/
int flom_conn_terminate(flom_conn_t *obj);
/**
* Initialize the TLS object related to a connection
* @param obj IN/OUT connection object
* @param client IN boolean value: TRUE for TLS client connection, FALSE
* for TLS server connection
*/
static inline int flom_conn_init_tls(flom_conn_t *obj, int client) {
if (NULL == (obj->tls = flom_tls_new(client)))
return FLOM_RC_NEW_OBJ;
return FLOM_RC_OK;
}
/**
* Trace the content of a connection data struct
* @param conn IN connection object to trace
*/
void flom_conn_trace(const flom_conn_t *conn);
/**
* Perform connection authentication
* @param conn IN/OUT connection object
* @param peer_id IN unique ID passed by the peer during handshake
* @param tls_check_peer_id IN boolean value
* @return a reason code
*/
int flom_conn_authenticate(flom_conn_t *conn, const gchar *peer_id,
int tls_check_peer_id);
/**
* Set SO_KEEPALIVE and correlated parameters for the socket associated
* to the passed file descriptor.
* This function has no relation with flom_conn_t objects because it's
* used only on accepted socket; it could be refactored when
* flom_conns_add will be add a flom_conn_t object...
* @param config IN configuration object, NULL for global config
* @param fd IN/OUT socket file descriptor
* @return a reason code
*/
int flom_conn_set_keepalive(flom_config_t *config, int fd);
#ifdef __cplusplus
}
#endif /* __cplusplus */
/* restore old value of FLOM_TRACE_MODULE */
#ifdef FLOM_TRACE_MODULE_SAVE
# undef FLOM_TRACE_MODULE
# define FLOM_TRACE_MODULE FLOM_TRACE_MODULE_SAVE
# undef FLOM_TRACE_MODULE_SAVE
#endif /* FLOM_TRACE_MODULE_SAVE */
#endif /* FLOM_CONN_H */