gnunet_chat_util.h (7177B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2021--2024 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author Tobias Frisch 22 * @file gnunet_chat_util.h 23 */ 24 25 #ifndef GNUNET_CHAT_UTIL_H_ 26 #define GNUNET_CHAT_UTIL_H_ 27 28 #include <gnunet/gnunet_messenger_service.h> 29 #include <gnunet/gnunet_util_lib.h> 30 31 #include "gnunet_chat_lib.h" 32 33 /** 34 * Enum for the types of chat contexts. 35 */ 36 enum GNUNET_CHAT_ContextType 37 { 38 /** 39 * Contact context type 40 */ 41 GNUNET_CHAT_CONTEXT_TYPE_CONTACT = 1,/**< GNUNET_CHAT_CONTEXT_TYPE_CONTACT */ 42 43 /** 44 * Group context type 45 */ 46 GNUNET_CHAT_CONTEXT_TYPE_GROUP = 2,/**< GNUNET_CHAT_CONTEXT_TYPE_GROUP */ 47 48 /** 49 * Unknown context type 50 */ 51 GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN = 0 /**< GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN */ 52 }; 53 54 /** 55 * Converts a unique messenger contact, being consistent <i>member</i> 56 * of multiple messenger rooms via memory consistency, into a short 57 * hash variant for map access as key. 58 * 59 * @param[in] member Messenger contact 60 * @param[out] shorthash Short hash 61 */ 62 void 63 util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, 64 struct GNUNET_ShortHashCode *shorthash); 65 66 /** 67 * Converts a discourse id into a short hash variant for map access 68 * as key. 69 * 70 * @param[in] id Discourse id 71 * @param[out] shorthash Short hash 72 */ 73 void 74 util_shorthash_from_discourse_id (const struct GNUNET_CHAT_DiscourseId *id, 75 struct GNUNET_ShortHashCode *shorthash); 76 77 /** 78 * Converts a short hash variant into a discourse id. 79 * 80 * @param[in] shorthash Short hash 81 * @param[out] id Discourse id 82 */ 83 void 84 util_discourse_id_from_shorthash (const struct GNUNET_ShortHashCode *shorthash, 85 struct GNUNET_CHAT_DiscourseId *id); 86 87 /** 88 * Updates the stored content of a <i>field</i> with 89 * a given <i>name</i>. 90 * 91 * @param[in] name Name 92 * @param[out] field String field 93 */ 94 void 95 util_set_name_field (const char *name, 96 char **field); 97 98 /** 99 * Generates the <i>hash</i> of a file under a given 100 * <i>filename</i>. 101 * 102 * @param[in] filename File name 103 * @param[out] hash Hash of file 104 * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR 105 */ 106 enum GNUNET_GenericReturnValue 107 util_hash_file (const char *filename, 108 struct GNUNET_HashCode *hash); 109 110 /** 111 * Encrypts a file inplace under a given <i>filename</i> 112 * with a selected symmetric <i>key</i> and its <i>hash</i> 113 * as initialization vector. 114 * 115 * @param[in] filename File name 116 * @param[in] hash Hash of file 117 * @param[in] key Symmetric key 118 * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR 119 */ 120 enum GNUNET_GenericReturnValue 121 util_encrypt_file (const char *filename, 122 const struct GNUNET_HashCode *hash, 123 const struct GNUNET_CRYPTO_SymmetricSessionKey *key); 124 125 /** 126 * Decrypts a file inplace under a given <i>filename</i> 127 * with a selected symmetric <i>key</i> and its <i>hash</i> 128 * as parameter for the initialization vector and comparison 129 * to verify success. 130 * 131 * @param[in] filename File name 132 * @param[in] hash Hash of file 133 * @param[in] key Symmetric key 134 * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR 135 */ 136 enum GNUNET_GenericReturnValue 137 util_decrypt_file (const char *filename, 138 const struct GNUNET_HashCode *hash, 139 const struct GNUNET_CRYPTO_SymmetricSessionKey *key); 140 141 /** 142 * Append the path of a <i>directory</i> and a custom 143 * subdirectory name to a composed <i>filename</i>. 144 * 145 * @param[in] directory Directory path 146 * @param[in] subdir Subdirectory name 147 * @param[out] filename Filename 148 * @return Number of bytes in filename excluding 0-termination 149 */ 150 int 151 util_get_dirname (const char *directory, 152 const char *subdir, 153 char **filename); 154 155 /** 156 * Append the path of a <i>directory</i>, a custom 157 * subdirectory name and a <i>hash</i> to a composed 158 * <i>filename</i>. 159 * 160 * @param[in] directory Directory path 161 * @param[in] subdir Subdirectory name 162 * @param[in] hash Hash 163 * @param[out] filename Filename 164 * @return Number of bytes in filename excluding 0-termination 165 */ 166 int 167 util_get_filename (const char *directory, 168 const char *subdir, 169 const struct GNUNET_HashCode *hash, 170 char **filename); 171 172 /** 173 * Allocates a new string representing the lower case versionn 174 * of a given <i>name</i> to work properly with the EGO naming 175 * scheme for example. 176 * 177 * @param[in] name Name 178 * @return Lower case name 179 */ 180 char* 181 util_get_lower(const char *name); 182 183 /** 184 * Construct a composed <i>label</i> from a given context 185 * <i>type</i> and the <i>hash</i> of the contexts room. 186 * 187 * @param[in] type Chat context type 188 * @param[in] hash Hash of room 189 * @param[out] label Namestore label 190 * @return Number of bytes in label excluding 0-termination 191 */ 192 int 193 util_get_context_label (enum GNUNET_CHAT_ContextType type, 194 const struct GNUNET_HashCode *hash, 195 char **label); 196 197 /** 198 * Extract the chat context type from a used <i>label</i> by 199 * a given context with a certain <i>hash</i> of its room. 200 * 201 * @param[in] label Namestore label 202 * @param[in] hash Hash of room 203 * @return Chat context type or #GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN 204 */ 205 enum GNUNET_CHAT_ContextType 206 util_get_context_label_type (const char *label, 207 const struct GNUNET_HashCode *hash); 208 209 /** 210 * Provide a standardized <i>name</i> for a lobby using 211 * a given <i>hash</i> of its internal room. 212 * 213 * @param[in] hash Hash of room 214 * @param[out] name Name of lobby 215 * @return Number of bytes in name excluding 0-termination 216 */ 217 int 218 util_lobby_name (const struct GNUNET_HashCode *hash, 219 char **name); 220 221 /** 222 * Check whether an identity <i>name</i> could be a 223 * standardized name for a lobby. 224 * @see util_lobby_name() 225 * 226 * @param[in] name Identity name 227 * @return #GNUNET_YES if it might be a lobby name, 228 * otherwise #GNUNET_NO 229 */ 230 enum GNUNET_GenericReturnValue 231 util_is_lobby_name(const char *name); 232 233 /** 234 * Return the chat related kind for a messages original kind 235 * from the service. It will return #GNUNET_CHAT_KIND_UNKNOWN 236 * in case the message kind is not supported. 237 * 238 * @param[in] kind Messenger service message kind 239 * @return Chat message kind or #GNUNET_CHAT_KIND_UNKNOWN_ 240 */ 241 enum GNUNET_CHAT_MessageKind 242 util_message_kind_from_kind (enum GNUNET_MESSENGER_MessageKind kind); 243 244 #endif /* GNUNET_CHAT_UTIL_H_ */