[go: up one dir, main page]

Menu

[e669e2]: / src / flom_msg.h  Maximize  Restore  History

Download this file

808 lines (644 with data), 21.9 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/*
* Copyright (c) 2013-2014, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLOM.
*
* FLOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 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 MSG_H
# define MSG_H
#include <config.h>
#ifdef HAVE_GLIB_H
# include <glib.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
/* 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_MSG
/**
* Default buffer size for XML messages (used for serialization/
* deserialization)
**/
#define FLOM_MSG_BUFFER_SIZE 512
/**
* Message state enum type
*/
typedef enum flom_msg_state_e {
/**
* The message has been initialized, but the values are not ready to be
* read
*/
FLOM_MSG_STATE_INITIALIZED,
/**
* The parser is filling the message fields, but the values are not ready to
* be read
*/
FLOM_MSG_STATE_PARSING,
/**
* The parser completed its job and the values are ready to be read
*/
FLOM_MSG_STATE_READY,
/**
* An error occurred and the content of the messafe is INVALID
*/
FLOM_MSG_STATE_INVALID
} flom_msg_state_t;
/**
* Current protocol level; it's used to recognize incompatible client/server
* configuration at run-time
*/
#define FLOM_MSG_LEVEL 0
/**
* Id reserved for a null message: do NOT change this value because it
* would break the @ref flom_msg_init behavior
*/
#define FLOM_MSG_VERB_NULL 0
/**
* Id assigned to verb "lock"
*/
#define FLOM_MSG_VERB_LOCK 1
/**
* Id assigned to verb "unlock"
*/
#define FLOM_MSG_VERB_UNLOCK 2
/**
* Id assigned to verb "ping"
*/
#define FLOM_MSG_VERB_PING 3
/**
* Id assigned to verb "discover"
*/
#define FLOM_MSG_VERB_DISCOVER 4
/**
* Default increment for message step
*/
#define FLOM_MSG_STEP_INCR 8
/**
* Lock mode that can be asked for a resource
*/
typedef enum flom_lock_mode_e {
/**
* Null lock mode
*/
FLOM_LOCK_MODE_NL,
/**
* Concurrent read lock mode
*/
FLOM_LOCK_MODE_CR,
/**
* Concurrent write lock mode
*/
FLOM_LOCK_MODE_CW,
/**
* Protected read / shared lock mode
*/
FLOM_LOCK_MODE_PR,
/**
* Protectec write / update lock mode
*/
FLOM_LOCK_MODE_PW,
/**
* Exclusive lock mode
*/
FLOM_LOCK_MODE_EX,
/**
* Number of lock modes
*/
FLOM_LOCK_MODE_N,
/**
* Special value used to encode an invalid value
*/
FLOM_LOCK_MODE_INVALID
} flom_lock_mode_t;
/**
* Label used to specify initial XML header
*/
extern const gchar *FLOM_MSG_HEADER;
/**
* Label used to specify "address" property
*/
extern const gchar *FLOM_MSG_PROP_ADDRESS;
/**
* Label used to specify "level" property
*/
extern const gchar *FLOM_MSG_PROP_LEVEL;
/**
* Label used to specify "name" property
*/
extern const gchar *FLOM_MSG_PROP_NAME;
/**
* Label used to specify "rcp" property
*/
extern const gchar *FLOM_MSG_PROP_RC;
/**
* Label used to specify "step" property
*/
extern const gchar *FLOM_MSG_PROP_STEP;
/**
* Label used to specify "mode" property
*/
extern const gchar *FLOM_MSG_PROP_MODE;
/**
* Label used to specify "port" property
*/
extern const gchar *FLOM_MSG_PROP_PORT;
/**
* Label used to specify "verb" property
*/
extern const gchar *FLOM_MSG_PROP_VERB;
/**
* Label used to specify "wait" property
*/
extern const gchar *FLOM_MSG_PROP_WAIT;
/**
* Label used to specify "answer" tag
*/
extern const gchar *FLOM_MSG_TAG_ANSWER;
/**
* Label used to specify "msg" tag
*/
extern const gchar *FLOM_MSG_TAG_MSG;
/**
* Label used to specify "network" tag
*/
extern const gchar *FLOM_MSG_TAG_NETWORK;
/**
* Label used to specify "resource" tag
*/
extern const gchar *FLOM_MSG_TAG_RESOURCE;
/**
* A static object used by g_markup functions
*/
extern GMarkupParser flom_msg_parser;
/**
* The communication protocol is discrete and the values are in the set
* (verb x step)
*/
struct flom_msg_verb_step_s {
/**
* Specifies the verb (open, close, begin, commit, ecc...)
*/
int verb;
/**
* Specifies the step inside the verb (1, 2, 3, ...)
*/
int step;
};
/**
* Mandatory header for every message encoded as @ref flom_msg_s
*/
struct flom_msg_header_s {
/**
* Protocol level must be applied to this message
*/
int level;
/**
* Protocol verb and step of the message
*/
struct flom_msg_verb_step_s pvs;
};
/**
* Generic answer message struct
*/
struct flom_msg_body_answer_s {
/**
* Return code of the invoked operation
*/
int rc;
};
/**
* Convenience struct for @ref flom_msg_body_lock_8_s
*/
struct flom_msg_body_lock_8_resource_s {
/**
* name of the resource to lock
*/
gchar *name;
/**
* asked lock mode
*/
flom_lock_mode_t mode;
/**
* wait if lock is currently not available
*/
int wait;
};
/**
* Message body for verb "lock", step "8"
*/
struct flom_msg_body_lock_8_s {
struct flom_msg_body_lock_8_resource_s resource;
};
/**
* Message body for verb "lock", step "16"
*/
struct flom_msg_body_lock_16_s {
struct flom_msg_body_answer_s answer;
};
/**
* Message body for verb "lock", step "24"
*/
struct flom_msg_body_lock_24_s {
struct flom_msg_body_answer_s answer;
};
/**
* Convenience struct for @ref flom_msg_body_unlock_8_s
*/
struct flom_msg_body_unlock_8_resource_s {
/**
* name of the resource to lock
*/
gchar *name;
};
/**
* Message body for verb "unlock", step "8"
*/
struct flom_msg_body_unlock_8_s {
struct flom_msg_body_unlock_8_resource_s resource;
};
/**
* Message body for verb "ping", step "8"
*/
struct flom_msg_body_ping_8_s {
/**
* ping verb does not need to carry anything
*/
int dummy_field;
};
/**
* Message body for verb "ping", step "16"
*/
struct flom_msg_body_ping_16_s {
/**
* ping verb does not need to carry anything
*/
int dummy_field;
};
/**
* Message body for verb "discover", step "8"
*/
struct flom_msg_body_discover_8_s {
/**
* discover verb does not need to carry anything
*/
int dummy_field;
};
/**
* Convenience struct for @ref flom_msg_body_discover_16_s
*/
struct flom_msg_body_discover_16_network_s {
/**
* unicast TCP/IP address used by daemon. It can * NOT * be desumed by
* packet sender because the daemon might be bound to an interface
* distinct from the interface used to send UDP datagrams
*/
gchar *address;
/**
* unicast TCP/IP port used by daemon
*/
in_port_t port;
};
/**
* Message body for verb "discover", step "16"
*/
struct flom_msg_body_discover_16_s {
struct flom_msg_body_discover_16_network_s network;
};
/**
* This structure maps the messages flowing between FLOM client and
* FLOM server (daemon). The struct is not used for the transmission over the
* network, but only inside the client and the server.
* This is a "fake" object; it's defined and used in the hope of simplicity
*/
struct flom_msg_s {
/**
* Message state
*/
flom_msg_state_t state;
/**
* Message header, common to all messages
*/
struct flom_msg_header_s header;
/**
* Message body, it depends from header
*/
union {
struct flom_msg_body_lock_8_s lock_8;
struct flom_msg_body_lock_16_s lock_16;
struct flom_msg_body_lock_24_s lock_24;
struct flom_msg_body_unlock_8_s unlock_8;
struct flom_msg_body_ping_8_s ping_8;
struct flom_msg_body_ping_16_s ping_16;
struct flom_msg_body_discover_8_s discover_8;
struct flom_msg_body_discover_16_s discover_16;
} body;
};
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* Interpret a string and extract the lock mode
* @param text IN string to interpret
* @return a lock mode
*/
flom_lock_mode_t flom_lock_mode_retrieve(const gchar *text);
/**
* Initialize an empty message
* @param msg IN/OUT message must be initialized
*/
static inline void flom_msg_init(struct flom_msg_s *msg) {
memset(msg, 0, sizeof(struct flom_msg_s));
}
/**
* Retrieve the first XML message from a TCP/IP socket (file descriptor)
* @param fd IN file descriptor associated to the TCP/IP socket
* @param type IN file descriptor associated type (SOCK_STREAM or
* SOCK_DGRAM)
* @param buf OUT buffer will be used to store the XML message
* @param buf_size IN size of buf
* @param read_bytes OUT number of bytes read, XML message length
* @param timeout IN maximum wait time to receive the answer (milliseconds)
* @param src_addr OUT transparently passed to recvfrom if type is
* SOCK_DGRAM (see recvfrom man page)
* @param addrlen OUT transparently passed to recvfrom if type is
* SOCK_DGRAM (see recvfrom man page)
* @return a reason code
*/
int flom_msg_retrieve(int fd, int type,
char *buf, size_t buf_size,
ssize_t *read_bytes,
int timeout,
struct sockaddr *src_addr, socklen_t *addrlen);
/**
* Send a message to a TCP/IP socket (file descriptor)
* @param fd IN file descriptor associated to the TCP/IP socket
* @param buf IN buffer will be used to store the XML message
* @param buf_size IN size of buf
* @return a reason code
*/
int flom_msg_send(int fd, const char *buf, size_t buf_size);
/**
* Free all the dynamically allocated strings previously allocated by
* @ref flom_msg_deserialize
* @param msg IN/OUT the message must be massaged
* @return a reason code
*/
int flom_msg_free(struct flom_msg_s *msg);
/**
* Check if the message is correct from a protocol point of view:
* client can send only some verb/step combinations, daemon can send
* only other verb/step combinations
* @param msg IN message to be inspected
* @param client IN true if client sent the message, false if daemon sent
* the message
* @return a boolean value
*/
int flom_msg_check_protocol(const struct flom_msg_s *msg, int client);
/**
* Serialize a message struct to an XML buffer for external transmission
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param buffer_len IN the space allocated for buffer
* @param msg_len OUT number of chars used in buffer for serializing msg
* @return a reason code
*/
int flom_msg_serialize(const struct flom_msg_s *msg,
char *buffer, size_t buffer_len,
size_t *msg_len);
/**
* Serialize the "lock_8" specific body part of a message
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param offset IN/OUT offset must be used to start serialization inside
* the buffer
* @param free_chars IN/OUT remaing free chars inside the buffer
* @return a reason code
*/
int flom_msg_serialize_lock_8(const struct flom_msg_s *msg,
char *buffer,
size_t *offset, size_t *free_chars);
/**
* Serialize the "lock_16" specific body part of a message
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param offset IN/OUT offset must be used to start serialization inside
* the buffer
* @param free_chars IN/OUT remaing free chars inside the buffer
* @return a reason code
*/
int flom_msg_serialize_lock_16(const struct flom_msg_s *msg,
char *buffer,
size_t *offset, size_t *free_chars);
/**
* Serialize the "lock_24" specific body part of a message
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param offset IN/OUT offset must be used to start serialization inside
* the buffer
* @param free_chars IN/OUT remaing free chars inside the buffer
* @return a reason code
*/
int flom_msg_serialize_lock_24(const struct flom_msg_s *msg,
char *buffer,
size_t *offset, size_t *free_chars);
/**
* Serialize the "unlock_8" specific body part of a message
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param offset IN/OUT offset must be used to start serialization inside
* the buffer
* @param free_chars IN/OUT remaing free chars inside the buffer
* @return a reason code
*/
int flom_msg_serialize_unlock_8(const struct flom_msg_s *msg,
char *buffer,
size_t *offset, size_t *free_chars);
/**
* Serialize the "ping_8" specific body part of a message
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param offset IN/OUT offset must be used to start serialization inside
* the buffer
* @param free_chars IN/OUT remaing free chars inside the buffer
* @return a reason code
*/
int flom_msg_serialize_ping_8(const struct flom_msg_s *msg,
char *buffer,
size_t *offset, size_t *free_chars);
/**
* Serialize the "ping_16" specific body part of a message
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param offset IN/OUT offset must be used to start serialization inside
* the buffer
* @param free_chars IN/OUT remaing free chars inside the buffer
* @return a reason code
*/
int flom_msg_serialize_ping_16(const struct flom_msg_s *msg,
char *buffer,
size_t *offset, size_t *free_chars);
/**
* Serialize the "discover_8" specific body part of a message
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param offset IN/OUT offset must be used to start serialization inside
* the buffer
* @param free_chars IN/OUT remaing free chars inside the buffer
* @return a reason code
*/
int flom_msg_serialize_discover_8(const struct flom_msg_s *msg,
char *buffer,
size_t *offset, size_t *free_chars);
/**
* Serialize the "discover_16" specific body part of a message
* @param msg IN the object must be serialized
* @param buffer OUT the buffer will contain the XML serialized object
* (the size has fixed size of
* @ref FLOM_MSG_BUFFER_SIZE bytes) and will be
* null terminated
* @param offset IN/OUT offset must be used to start serialization inside
* the buffer
* @param free_chars IN/OUT remaing free chars inside the buffer
* @return a reason code
*/
int flom_msg_serialize_discover_16(const struct flom_msg_s *msg,
char *buffer,
size_t *offset, size_t *free_chars);
/**
* Display the content of a message
* @param msg IN the message must be massaged
* @return a reason code
*/
int flom_msg_trace(const struct flom_msg_s *msg);
/**
* Display the content of a lock message
* @param msg IN the message must be massaged
* @return a reason code
*/
int flom_msg_trace_lock(const struct flom_msg_s *msg);
/**
* Display the content of an unlock message
* @param msg IN the message must be massaged
* @return a reason code
*/
int flom_msg_trace_unlock(const struct flom_msg_s *msg);
/**
* Display the content of a ping message
* @param msg IN the message must be massaged
* @return a reason code
*/
int flom_msg_trace_ping(const struct flom_msg_s *msg);
/**
* Display the content of a discover message
* @param msg IN the message must be massaged
* @return a reason code
*/
int flom_msg_trace_discover(const struct flom_msg_s *msg);
/**
* Deserialize a serialized buffer to a message struct
* @param buffer IN/OUT the buffer that's containing the serialized object
* (it does not have to be null terminated)
* @param buffer_len IN number of significative bytes of buffer
* @param msg OUT the object after deserialization
* @param gmpc IN/OUT GMarkup parser context
* @return a reason code
*/
int flom_msg_deserialize(char *buffer, size_t buffer_len,
struct flom_msg_s *msg,
GMarkupParseContext *gmpc);
/**
* GMarkupParser start_element callback function
*/
void flom_msg_deserialize_start_element(
GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error);
/**
* GMarkupParser end_element callback function
*/
void flom_msg_deserialize_end_element(GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error);
/**
* GMarkupParser text callback function
*/
void flom_msg_deserialize_text(GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error);
/**
* Build a standard answer message
* @param msg IN/OUT message allocated and initialized
* @param verb IN answer verb
* @param step IN answer step
* @param rc IN answer rc
* @return a reason code
*/
int flom_msg_build_answer(struct flom_msg_s *msg,
int verb, int step, int rc);
#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 /* MSG_H */