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 * void *silc_id_payload_parse_id(const unsigned char *data,
116 * Return ID directly from the raw ID Payload data buffer. The
117 * caller must free the returned ID.
120 void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
123 /****f* silccore/SilcIDAPI/silc_id_payload_encode
127 * SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
131 * Encodes ID Payload. The `id' is the ID of the type `type' to put
132 * into the payload. Returns the encoded payload buffer.
135 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
137 /****f* silccore/SilcIDAPI/silc_id_payload_encode_data
141 * SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
142 * uin32 id_len, SilcIdType type);
146 * Encodes ID Payload. The `id' is raw ID data of the length of `id_len'
147 * of type of `type'. Returns the encoded payload buffer.
150 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
151 SilcUInt32 id_len, SilcIdType type);
153 /****f* silccore/SilcIDAPI/silc_id_payload_free
157 * void silc_id_payload_free(SilcIDPayload payload);
161 * Frees the ID Payload and all data in it.
164 void silc_id_payload_free(SilcIDPayload payload);
166 /****f* silccore/SilcIDAPI/silc_id_payload_get_type
170 * SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
174 * Returns the ID type from the ID Payload. The type tells the
175 * type of the ID in the payload.
178 SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
180 /****f* silccore/SilcIDAPI/silc_id_payload_get_id
184 * void *silc_id_payload_get_id(SilcIDPayload payload);
188 * Returns the ID in the ID Payload. The caller must free the
192 void *silc_id_payload_get_id(SilcIDPayload payload);
194 /****f* silccore/SilcIDAPI/silc_id_payload_get_data
198 * unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
202 * Returns the raw ID data from the ID Payload. The data is duplicated
203 * and the caller must free it.
206 unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
208 /****f* silccore/SilcIDAPI/silc_id_payload_get_len
212 * SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
216 * Returns the length of the ID in the ID Payload.
219 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
221 /****s* silccore/SilcIDAPI/SilcIDIP
225 * typedef struct { ... } SilcIDIP;
229 * Generic IP address structure to indicate either IPv4 or IPv6 address.
230 * This structure is used inside all SILC ID's. The true length of the
231 * ID depends of the length of the IP address.
236 unsigned char data[16]; /* IP data (in MSB first order) */
237 SilcUInt8 data_len; /* Length of the data (4 or 16) */
241 /****s* silccore/SilcIDAPI/SilcServerID
245 * typedef struct { ... } SilcServerID;
249 * 64 or 160 bit SilcServerID structure:
253 * 16 bit random number
258 SilcIDIP ip; /* n bit IP address */
259 SilcUInt16 port; /* 16 bit port */
260 SilcUInt16 rnd; /* 16 bit random number */
264 /****s* silccore/SilcIDAPI/SilcClientID
268 * typedef struct { ... } SilcClientID;
272 * 128 or 224 bit SilcClientID structure:
274 * n bit ServerID IP address [bits 1-32 or bits 1-128]
275 * 8 bit random number
276 * 88 bit hash value from lowercase nickname
281 SilcIDIP ip; /* n bit IP address */
282 unsigned char rnd; /* 8 bit random number */
283 unsigned char hash[CLIENTID_HASH_LEN]; /* 88 bit MD5 hash */
287 /****s* silccore/SilcIDAPI/SilcChannelID
291 * typedef struct { ... } SilcChannelID;
295 * 64 or 160 bit SilcChannel ID structure:
297 * n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
298 * 16 bit Router's ServerID port [bits 33-48 or bits 129-144]
299 * 16 bit random number
304 SilcIDIP ip; /* n bit IP address */
305 SilcUInt16 port; /* 16 bit port */
306 SilcUInt16 rnd; /* 16 bit random number */
312 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE
316 * #define SILC_ID_COMPARE ...
320 * Compares two ID's. Returns TRUE if they match and FALSE if they do
325 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
328 /****d* silccore/SilcIDAPI/SILC_ID_CLIENT_COMPARE
332 * #define SILC_ID_CLIENT_COMPARE ...
336 * Compares Client ID's. Returns TRUE if they match.
340 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
341 SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
344 /****d* silccore/SilcIDAPI/SILC_ID_SERVER_COMPARE
348 * #define SILC_ID_SERVER_COMPARE ...
352 * Compares Server ID's. Returns TRUE if they match.
356 #define SILC_ID_SERVER_COMPARE(id1, id2) \
357 SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
360 /****d* silccore/SilcIDAPI/SILC_ID_CHANNEL_COMPARE
364 * #define SILC_ID_CHANNEL_COMPARE ...
368 * Compares Channel ID's. Returns TRUE if they match.
372 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
373 SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
376 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_TYPE
380 * #define SILC_ID_COMPARE_TYPE ...
384 * Compares two ID's by type. Returns TRUE if they match.
388 #define SILC_ID_COMPARE_TYPE(id1, id2, type) \
389 (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) : \
390 type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) : \
391 SILC_ID_CHANNEL_COMPARE(id1, id2))
394 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_HASH
398 * #define SILC_ID_COMPARE_HASH ...
402 * Compares the nickname hash of the Client ID. Returns TRUE if
403 * they match. Since the nickname hash is based on the nickname of
404 * the client this can be used to search the ID by nickname (taking
405 * the hash out of it) or using the hash from the ID.
409 #define SILC_ID_COMPARE_HASH(id1, id2) \
410 (!memcmp((id1)->hash, (id2)->hash, CLIENTID_HASH_LEN))
415 /****f* silccore/SilcIDAPI/silc_id_id2str
419 * unsigned char *silc_id_id2str(const void *id, SilcIdType type,
420 * SilcUInt32 *ret_id_len);
424 * Converts an ID of type `type' to data. This can be used to
425 * convert the ID's to data for inclusion in the packets. Use the
426 * silc_id_get_len to get the length of the ID.
429 unsigned char *silc_id_id2str(const void *id, SilcIdType type,
430 SilcUInt32 *ret_id_len);
432 /****f* silccore/SilcIDAPI/silc_id_str2id
436 * bool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
437 * SilcIdType type, void *ret_id);
441 * Converts ID data string to an ID. This can be used to get the
442 * ID out of data that has been taken for example from packet.
445 bool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
446 SilcIdType type, void *ret_id);
448 /****f* silccore/SilcIDAPI/silc_id_get_len
452 * SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
456 * Returns the true length of the ID of the type `type'.
459 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
461 /****f* silccore/SilcIDAPI/silc_id_dup
465 * void *silc_id_dup(const void *id, SilcIdType type);
469 * Duplicates the ID of the type `type'. The caller must free the
473 void *silc_id_dup(const void *id, SilcIdType type);