Libtrap: Internal development docs  1.16.1
ifc_socket_common.h
Go to the documentation of this file.
1 /**
2  * \file ifc_socket_common.h
3  * \brief This file contains common functions and structures used in socket based interfaces (tcp-ip / tls).
4  * \author Matej Barnat <barnama1@fit.cvut.cz>
5  * \date 2019
6  */
7 /*
8  * Copyright (C) 2013-2019 CESNET
9  *
10  * LICENSE TERMS
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  * 3. Neither the name of the Company nor the names of its contributors
22  * may be used to endorse or promote products derived from this
23  * software without specific prior written permission.
24  *
25  * ALTERNATIVELY, provided that this notice is retained in full, this
26  * product may be distributed under the terms of the GNU General Public
27  * License (GPL) version 2 or later, in which case the provisions
28  * of the GPL apply INSTEAD OF those given above.
29  *
30  * This software is provided ``as is'', and any express or implied
31  * warranties, including, but not limited to, the implied warranties of
32  * merchantability and fitness for a particular purpose are disclaimed.
33  * In no event shall the company or contributors be liable for any
34  * direct, indirect, incidental, special, exemplary, or consequential
35  * damages (including, but not limited to, procurement of substitute
36  * goods or services; loss of use, data, or profits; or business
37  * interruption) however caused and on any theory of liability, whether
38  * in contract, strict liability, or tort (including negligence or
39  * otherwise) arising in any way out of the use of this software, even
40  * if advised of the possibility of such damage.
41  *
42  */
43 
44 #ifndef _ifc_socket_common_h_
45 #define _ifc_socket_common_h_
46 
47 #define BUFFER_COUNT_PARAM_LENGTH 13 /**< Used for parsing ifc params */
48 #define BUFFER_SIZE_PARAM_LENGTH 12 /**< Used for parsing ifc params */
49 #define MAX_CLIENTS_PARAM_LENGTH 12 /**< Used for parsing ifc params */
50 
51 #define DEFAULT_MAX_DATA_LENGTH (sizeof(trap_buffer_header_t) + 1024) /**< Obsolete? */
52 
53 #ifndef DEFAULT_BUFFER_COUNT
54 #define DEFAULT_BUFFER_COUNT 50 /**< Default buffer count */
55 #endif
56 
57 #ifndef DEFAULT_BUFFER_SIZE
58 #define DEFAULT_BUFFER_SIZE 100000 /**< Default buffer size [bytes] */
59 #endif
60 
61 #ifndef DEFAULT_MAX_CLIENTS
62 #define DEFAULT_MAX_CLIENTS 64 /**< Default size of client array */
63 #endif
64 
65 #define NO_CLIENTS_SLEEP 100000 /**< Value used in usleep() when waiting for a client to connect */
66 
67 /**
68  * \brief Output buffer structure.
69  */
70 typedef struct buffer_s {
71  uint32_t wr_index; /**< Pointer to first free byte in buffer */
72  uint64_t clients_bit_arr; /**< Bit array of clients that have not yet received the buffer */
73 
74  uint8_t *header; /**< Pointer to first byte in buffer */
75  uint8_t *data; /**< Pointer to first byte of buffer payload */
76 } buffer_t;
77 
78 /**
79  * \brief Array containing constants used for operations with bit arrays.
80  */
81 static uint64_t mask[64] = {
82  1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288,
83  1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824,
84  2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944,
85  549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832,
86  70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248,
87  4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872,
88  288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808ULL
89 };
90 
91 /**
92  * \brief Set i-th element (one bit) of 'bits' to 1.
93  *
94  * \param[in] bits Pointer to the bit array.
95  * \param[in] i Target element's index in the 'bits' array.
96  */
97 static inline void set_index(uint64_t *bits, int i)
98 {
99  *bits = __sync_or_and_fetch(bits, mask[i]);
100 }
101 
102 /**
103  * \brief Set i-th element (one bit) of 'bits' to 0.
104  *
105  * \param[in] bits Pointer to the bit array.
106  * \param[in] i Target element's index in the 'bits' array.
107  */
108 static inline void del_index(uint64_t *bits, int i)
109 {
110  *bits = __sync_and_and_fetch(bits, (0xffffffffffffffff - mask[i]));
111 }
112 
113 /**
114  * \brief Return value of i-th element (one bit) in the 'bits' array.
115  *
116  * \param[in] bits Pointer to the bit array.
117  * \param[in] i Target element's index in the 'bits' array.
118  *
119  * \return Value of i-th element (one bit) in the 'bits' array.
120  */
121 static inline uint64_t check_index(uint64_t bits, int i)
122 {
123  return (bits & mask[i]);
124 }
125 
126 /**
127  * \brief Write data into buffer
128  *
129  * \param[in] buffer Pointer to the buffer.
130  * \param[in] data Pointer to data to write.
131  * \param[in] size Size of the data.
132  */
133 static inline void insert_into_buffer(buffer_t *buffer, const void *data, uint16_t size)
134 {
135  uint16_t *msize = (uint16_t *)(buffer->data + buffer->wr_index);
136  (*msize) = htons(size);
137  memcpy((void *)(msize + 1), data, size);
138  buffer->wr_index += (size + sizeof(size));
139 }
140 
141 #endif
uint8_t * data
uint32_t wr_index
Output buffer structure.
static uint64_t mask[64]
Array containing constants used for operations with bit arrays.
static void set_index(uint64_t *bits, int i)
Set i-th element (one bit) of &#39;bits&#39; to 1.
static void del_index(uint64_t *bits, int i)
Set i-th element (one bit) of &#39;bits&#39; to 0.
static void insert_into_buffer(buffer_t *buffer, const void *data, uint16_t size)
Write data into buffer.
uint8_t data[0]
struct buffer_s buffer_t
Output buffer structure.
static uint64_t check_index(uint64_t bits, int i)
Return value of i-th element (one bit) in the &#39;bits&#39; array.
uint8_t * header
uint64_t clients_bit_arr