5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silccore/SILC ID Interface
24 * These are important ID types used in SILC. SILC server creates these
25 * but SILC client has to handle these as well since these are used in
26 * packet sending and reception. However, client never creates these
27 * but it receives the correct ID's from server. Clients, servers and
28 * channels are identified by the these ID's.
30 * The ID's are based on IP addresses. The IP address provides a good
31 * way to distinguish the ID's from other ID's. The ID's supports both
34 * This file also includes the implementation of the SILC ID Payload
35 * parsing and encoding.
42 /****d* silccore/SilcIDAPI/SilcIdType
46 * typedef SilcUInt16 SilcIdType;
50 * SILC ID type definitions and the ID types.
54 typedef SilcUInt16 SilcIdType;
56 /* The SILC ID Types */
57 #define SILC_ID_NONE 0
58 #define SILC_ID_SERVER 1
59 #define SILC_ID_CLIENT 2
60 #define SILC_ID_CHANNEL 3
63 /* The ID Lenghts. These are IPv4 based and should be noted if used directly
64 that these cannot be used with IPv6. */
65 #define SILC_ID_SERVER_LEN (64 / 8)
66 #define SILC_ID_CLIENT_LEN (128 / 8)
67 #define SILC_ID_CHANNEL_LEN (64 / 8)
69 #define CLIENTID_HASH_LEN (88 / 8) /* Client ID's 88 bit MD5 hash */
71 /****s* silccore/SilcIDAPI/SilcIDPayload
75 * typedef struct SilcIDPayloadStruct *SilcIDPayload;
79 * This context is the actual ID Payload and is allocated by
80 * silc_id_payload_parse and given as argument usually to all
81 * silc_id_payload_* functions. It is freed by the function
82 * silc_id_payload_free.
85 typedef struct SilcIDPayloadStruct *SilcIDPayload;
89 /****f* silccore/SilcIDAPI/silc_id_payload_parse
93 * SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
94 * SilcUInt32 payload_len);
98 * Parses buffer and return ID payload into payload structure. The
99 * `buffer' is raw payload buffer. The caller must free the returned
103 SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
104 SilcUInt32 payload_len);
106 /****f* silccore/SilcIDAPI/silc_id_payload_parse_id
110 * SilcBool silc_id_payload_parse_id(const unsigned char *data,
112 * SilcIdType *type, void *ret_id,
113 * SilcUInt32 ret_id_size);
117 * Return ID directly from the raw ID Payload data buffer. This does
118 * not allocate any memory.
121 SilcBool silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
122 SilcIdType *type, void *ret_id,
123 SilcUInt32 ret_id_size);
125 /****f* silccore/SilcIDAPI/silc_id_payload_encode
129 * SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
133 * Encodes ID Payload. The `id' is the ID of the type `type' to put
134 * into the payload. Returns the encoded payload buffer.
137 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
139 /****f* silccore/SilcIDAPI/silc_id_payload_encode_data
143 * SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
144 * uin32 id_len, SilcIdType type);
148 * Encodes ID Payload. The `id' is raw ID data of the length of `id_len'
149 * of type of `type'. Returns the encoded payload buffer.
152 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
153 SilcUInt32 id_len, SilcIdType type);
155 /****f* silccore/SilcIDAPI/silc_id_payload_free
159 * void silc_id_payload_free(SilcIDPayload payload);
163 * Frees the ID Payload and all data in it.
166 void silc_id_payload_free(SilcIDPayload payload);
168 /****f* silccore/SilcIDAPI/silc_id_payload_get_type
172 * SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
176 * Returns the ID type from the ID Payload. The type tells the
177 * type of the ID in the payload.
180 SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
182 /****f* silccore/SilcIDAPI/silc_id_payload_get_id
186 * SilcBool silc_id_payload_get_id(SilcIDPayload payload, void *ret_id,
187 * SilcUInt32 ret_id_len);
191 * Returns the ID in the ID Payload. This does not allocate any memory.
194 SilcBool silc_id_payload_get_id(SilcIDPayload payload, void *ret_id,
195 SilcUInt32 ret_id_len);
197 /****f* silccore/SilcIDAPI/silc_id_payload_get_data
201 * unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
205 * Returns the raw ID data from the ID Payload. The data is duplicated
206 * and the caller must free it.
209 unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
211 /****f* silccore/SilcIDAPI/silc_id_payload_get_len
215 * SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
219 * Returns the length of the ID in the ID Payload.
222 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
224 /****s* silccore/SilcIDAPI/SilcIDIP
228 * typedef struct { ... } SilcIDIP;
232 * Generic IP address structure to indicate either IPv4 or IPv6 address.
233 * This structure is used inside all SILC ID's. The true length of the
234 * ID depends of the length of the IP address.
239 unsigned char data[16]; /* IP data (in MSB first order) */
240 SilcUInt8 data_len; /* Length of the data (4 or 16) */
244 /****s* silccore/SilcIDAPI/SilcServerID
248 * typedef struct { ... } SilcServerID;
252 * 64 or 160 bit SilcServerID structure:
256 * 16 bit random number
261 SilcIDIP ip; /* n bit IP address */
262 SilcUInt16 port; /* 16 bit port */
263 SilcUInt16 rnd; /* 16 bit random number */
267 /****s* silccore/SilcIDAPI/SilcClientID
271 * typedef struct { ... } SilcClientID;
275 * 128 or 224 bit SilcClientID structure:
277 * n bit ServerID IP address [bits 1-32 or bits 1-128]
278 * 8 bit random number
279 * 88 bit hash value from lowercase nickname
284 SilcIDIP ip; /* n bit IP address */
285 unsigned char rnd; /* 8 bit random number */
286 unsigned char hash[CLIENTID_HASH_LEN]; /* 88 bit MD5 hash */
290 /****s* silccore/SilcIDAPI/SilcChannelID
294 * typedef struct { ... } SilcChannelID;
298 * 64 or 160 bit SilcChannel ID structure:
300 * n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
301 * 16 bit Router's ServerID port [bits 33-48 or bits 129-144]
302 * 16 bit random number
307 SilcIDIP ip; /* n bit IP address */
308 SilcUInt16 port; /* 16 bit port */
309 SilcUInt16 rnd; /* 16 bit random number */
315 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE
319 * #define SILC_ID_COMPARE ...
323 * Compares two ID's. Returns TRUE if they match and FALSE if they do
328 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
331 /****d* silccore/SilcIDAPI/SILC_ID_CLIENT_COMPARE
335 * #define SILC_ID_CLIENT_COMPARE ...
339 * Compares Client ID's. Returns TRUE if they match.
343 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
344 SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
347 /****d* silccore/SilcIDAPI/SILC_ID_SERVER_COMPARE
351 * #define SILC_ID_SERVER_COMPARE ...
355 * Compares Server ID's. Returns TRUE if they match.
359 #define SILC_ID_SERVER_COMPARE(id1, id2) \
360 SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
363 /****d* silccore/SilcIDAPI/SILC_ID_CHANNEL_COMPARE
367 * #define SILC_ID_CHANNEL_COMPARE ...
371 * Compares Channel ID's. Returns TRUE if they match.
375 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
376 SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
379 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_TYPE
383 * #define SILC_ID_COMPARE_TYPE ...
387 * Compares two ID's by type. Returns TRUE if they match.
391 #define SILC_ID_COMPARE_TYPE(id1, id2, type) \
392 (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) : \
393 type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) : \
394 SILC_ID_CHANNEL_COMPARE(id1, id2))
397 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_HASH
401 * #define SILC_ID_COMPARE_HASH ...
405 * Compares the nickname hash of the Client ID. Returns TRUE if
406 * they match. Since the nickname hash is based on the nickname of
407 * the client this can be used to search the ID by nickname (taking
408 * the hash out of it) or using the hash from the ID.
412 #define SILC_ID_COMPARE_HASH(id1, id2) \
413 (!memcmp((id1)->hash, (id2)->hash, CLIENTID_HASH_LEN))
418 /****f* silccore/SilcIDAPI/silc_id_id2str
422 * SilcBool silc_id_id2str(const void *id, SilcIdType type,
423 * unsigned char *ret_id, SilcUInt32 ret_id_size,
424 * SilcUInt32 *ret_id_len);
428 * Converts an ID of type `type' to data. This can be used to
429 * convert the ID's to data for inclusion in the packets. This does
430 * not allocate any memory.
433 SilcBool silc_id_id2str(const void *id, SilcIdType type,
434 unsigned char *ret_id, SilcUInt32 ret_id_size,
435 SilcUInt32 *ret_id_len);
437 /****f* silccore/SilcIDAPI/silc_id_str2id
441 * SilcBool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
442 * SilcIdType type, void *ret_id,
443 * SilcUInt32 ret_id_size);
447 * Converts ID data string to an ID. This can be used to get the
448 * ID out of data that has been taken for example from packet. This
449 * does not allocate any memory.
452 SilcBool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
453 SilcIdType type, void *ret_id, SilcUInt32 ret_id_size);
455 /****f* silccore/SilcIDAPI/silc_id_get_len
459 * SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
463 * Returns the true length of the ID of the type `type'.
466 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
468 /****f* silccore/SilcIDAPI/silc_id_dup
472 * void *silc_id_dup(const void *id, SilcIdType type);
476 * Duplicates the ID of the type `type'. The caller must free the
480 void *silc_id_dup(const void *id, SilcIdType type);