secmod_common.h (5304B)
1 /* 2 This file is part of GNU Taler 3 Copyright (C) 2021 Taler Systems SA 4 5 GNU Taler is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see 15 <http://www.gnu.org/licenses/> 16 */ 17 /** 18 * @file util/secmod_common.h 19 * @brief Common functions for the exchange security modules 20 * @author Florian Dold <dold@taler.net> 21 */ 22 #ifndef SECMOD_COMMON_H 23 #define SECMOD_COMMON_H 24 25 #include <gnunet/gnunet_util_lib.h> 26 #include <gnunet/gnunet_network_lib.h> 27 #include <pthread.h> 28 29 30 /** 31 * Create the listen socket for a secmod daemon. 32 * 33 * This function is not thread-safe, as it changes and 34 * restores the process umask. 35 * 36 * @param unixpath socket path 37 */ 38 struct GNUNET_NETWORK_Handle * 39 TES_open_socket (const char *unixpath); 40 41 42 /** 43 * Send a message starting with @a hdr to @a sock. 44 * 45 * @param sock where to send the message 46 * @param hdr beginning of the message, length indicated in size field 47 * @return #GNUNET_OK on success 48 */ 49 enum GNUNET_GenericReturnValue 50 TES_transmit (int sock, 51 const struct GNUNET_MessageHeader *hdr); 52 53 54 /** 55 * Transmit @a end bytes from @a pos on @a sock. 56 * 57 * @param sock where to send the data 58 * @param end how many bytes to send 59 * @param pos first address with data 60 * @return #GNUNET_OK on success 61 */ 62 enum GNUNET_GenericReturnValue 63 TES_transmit_raw (int sock, 64 size_t end, 65 const void *pos); 66 67 /** 68 * Information we keep for a client connected to us. 69 */ 70 struct TES_Client; 71 72 /** 73 * Function that handles message @a hdr from @a client. 74 * 75 * @param client sender of the message 76 * @param hdr message we received 77 * @return #GNUNET_OK on success 78 */ 79 typedef enum GNUNET_GenericReturnValue 80 (*TES_MessageDispatch)(struct TES_Client *client, 81 const struct GNUNET_MessageHeader *hdr); 82 83 84 /** 85 * Function that updates the keys for @a client. 86 * 87 * @param client sender of the message 88 * @return #GNUNET_OK on success 89 */ 90 typedef enum GNUNET_GenericReturnValue 91 (*TES_KeyUpdater)(struct TES_Client *client); 92 93 94 /** 95 * Module-specific functions to be used. 96 */ 97 struct TES_Callbacks 98 { 99 /** 100 * Function to handle inbound messages. 101 */ 102 TES_MessageDispatch dispatch; 103 104 /** 105 * Function to update key material initially. 106 */ 107 TES_KeyUpdater init; 108 109 /** 110 * Function to update key material. 111 */ 112 TES_KeyUpdater updater; 113 114 }; 115 116 117 /** 118 * Information we keep for a client connected to us. 119 */ 120 struct TES_Client 121 { 122 123 /** 124 * Kept in a DLL. 125 */ 126 struct TES_Client *next; 127 128 /** 129 * Kept in a DLL. 130 */ 131 struct TES_Client *prev; 132 133 /** 134 * Callbacks to use for work. 135 */ 136 struct TES_Callbacks cb; 137 138 /** 139 * Worker thread for this client. 140 */ 141 pthread_t worker; 142 143 /** 144 * Key generation this client is on. 145 */ 146 uint64_t key_gen; 147 148 /** 149 * IO-buffer used by @a purpose. 150 */ 151 char iobuf[65536]; 152 153 /** 154 * Client socket. 155 */ 156 int csock; 157 158 #ifdef __linux__ 159 /** 160 * Event socket. 161 */ 162 int esock; 163 #else 164 /** 165 * Input end of the event pipe. 166 */ 167 int esock_in; 168 169 /** 170 * Output end of the event pipe. 171 */ 172 int esock_out; 173 #endif 174 }; 175 176 177 /** 178 * Head of DLL of clients connected to us. 179 */ 180 extern struct TES_Client *TES_clients_head; 181 182 /** 183 * Tail of DLL of clients connected to us. 184 */ 185 extern struct TES_Client *TES_clients_tail; 186 187 /** 188 * Lock for the client queue. 189 */ 190 extern pthread_mutex_t TES_clients_lock; 191 192 /** 193 * Private key of this security module. Used to sign denomination key 194 * announcements. 195 */ 196 extern struct TALER_SecurityModulePrivateKeyP TES_smpriv; 197 198 /** 199 * Public key of this security module. 200 */ 201 extern struct TALER_SecurityModulePublicKeyP TES_smpub; 202 203 204 /** 205 * Send a signal to all clients to notify them about a key generation change. 206 */ 207 void 208 TES_wake_clients (void); 209 210 211 /** 212 * Read work request from the client. 213 * 214 * @param cls a `struct TES_Client *` 215 * @param dispatch function to call with work requests received 216 * @return #GNUNET_OK on success 217 */ 218 enum GNUNET_GenericReturnValue 219 TES_read_work (void *cls, 220 TES_MessageDispatch dispatch); 221 222 223 /** 224 * Wait until the socket is ready to read. 225 * 226 * @param client the client to wait for 227 * @return true if we received an event 228 */ 229 bool 230 TES_await_ready (struct TES_Client *client); 231 232 233 /** 234 * Free resources occupied by @a client. 235 * 236 * @param[in] client resources to release 237 */ 238 void 239 TES_free_client (struct TES_Client *client); 240 241 242 /** 243 * Start listen task. 244 * 245 * @param cfg configuration to use 246 * @param section configuration section to use 247 * @param cb callback functions to use 248 * @return 0 on success, otherwise return value to return from main() 249 */ 250 int 251 TES_listen_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 252 const char *section, 253 const struct TES_Callbacks *cb); 254 255 256 /** 257 * Stop listen task. 258 */ 259 void 260 TES_listen_stop (void); 261 262 263 #endif