aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_util.h
blob: 55f13de3a93b806dd13b75ac7b0d2cca15207efd (plain) (blame)
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
/*
   This file is part of GNUnet.
   Copyright (C) 2021--2022 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet 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
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */
/*
 * @author Tobias Frisch
 * @file gnunet_chat_util.h
 */

#ifndef GNUNET_CHAT_UTIL_H_
#define GNUNET_CHAT_UTIL_H_

#include <gnunet/platform.h>
#include <gnunet/gnunet_common.h>
#include <gnunet/gnunet_crypto_lib.h>
#include <gnunet/gnunet_disk_lib.h>
#include <gnunet/gnunet_messenger_service.h>
#include <gnunet/gnunet_util_lib.h>

/**
 * Enum for the types of chat contexts.
 */
enum GNUNET_CHAT_ContextType
{
  /**
   * Contact context type
   */
  GNUNET_CHAT_CONTEXT_TYPE_CONTACT = 1,/**< GNUNET_CHAT_CONTEXT_TYPE_CONTACT */

  /**
   * Group context type
   */
  GNUNET_CHAT_CONTEXT_TYPE_GROUP   = 2,/**< GNUNET_CHAT_CONTEXT_TYPE_GROUP */

  /**
   * Unknown context type
   */
  GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN = 0 /**< GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN */
};

/**
 * Converts a unique messenger contact, being consistent <i>member</i>
 * of multiple messenger rooms via memory consistency, into a short
 * hash variant for map access as key.
 *
 * @param[in] member Messenger contact
 * @param[out] shorthash Short hash
 */
void
util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member,
			    struct GNUNET_ShortHashCode *shorthash);

/**
 * Updates the stored content of a <i>field</i> with
 * a given <i>name</i>.
 *
 * @param[in] name Name
 * @param[out] field String field
 */
void
util_set_name_field (const char *name, char **field);

/**
 * Generates the <i>hash</i> of a file under a given
 * <i>filename</i>.
 *
 * @param[in] filename File name
 * @param[out] hash Hash of file
 * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR
 */
int
util_hash_file (const char *filename, struct GNUNET_HashCode *hash);

/**
 * Encrypts a file inplace under a given <i>filename</i>
 * with a selected symmetric <i>key</i> and its <i>hash</i>
 * as initialization vector.
 *
 * @param[in] filename File name
 * @param[in] hash Hash of file
 * @param[in] key Symmetric key
 * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR
 */
int
util_encrypt_file (const char *filename,
		   const struct GNUNET_HashCode *hash,
		   const struct GNUNET_CRYPTO_SymmetricSessionKey *key);

/**
 * Decrypts a file inplace under a given <i>filename</i>
 * with a selected symmetric <i>key</i> and its <i>hash</i>
 * as parameter for the initialization vector and comparison
 * to verify success.
 *
 * @param[in] filename File name
 * @param[in] hash Hash of file
 * @param[in] key Symmetric key
 * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR
 */
int
util_decrypt_file (const char *filename,
		   const struct GNUNET_HashCode *hash,
		   const struct GNUNET_CRYPTO_SymmetricSessionKey *key);

/**
 * Append the path of a <i>directory</i> and a custom
 * subdirectory name to a composed <i>filename</i>.
 *
 * @param[in] directory Directory path
 * @param[in] subdir Subdirectory name
 * @param[out] filename Filename
 * @return Number of bytes in filename excluding 0-termination
 */
int
util_get_dirname (const char *directory,
		  const char *subdir,
		  char **filename);

/**
 * Append the path of a <i>directory</i>, a custom
 * subdirectory name and a <i>hash</i> to a composed
 * <i>filename</i>.
 *
 * @param[in] directory Directory path
 * @param[in] subdir Subdirectory name
 * @param[in] hash Hash
 * @param[out] filename Filename
 * @return Number of bytes in filename excluding 0-termination
 */
int
util_get_filename (const char *directory,
		   const char *subdir,
		   const struct GNUNET_HashCode *hash,
		   char **filename);

/**
 * Construct a composed <i>label</i> from a given context
 * <i>type</i> and the <i>hash</i> of the contexts room.
 *
 * @param[in] type Chat context type
 * @param[in] hash Hash of room
 * @param[out] label Namestore label
 * @return Number of bytes in label excluding 0-termination
 */
int
util_get_context_label (enum GNUNET_CHAT_ContextType type,
		        const struct GNUNET_HashCode *hash,
		        char **label);

/**
 * Provide a standardized <i>name</i> for a lobby using
 * a given <i>hash</i> of its internal room.
 *
 * @param[in] hash Hash of room
 * @param[out] name Name of lobby
 * @return Number of bytes in name excluding 0-termination
 */
int
util_lobby_name (const struct GNUNET_HashCode *hash,
		 char **name);

#endif /* GNUNET_CHAT_UTIL_H_ */