5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2000 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 /****h* silccore/SILC ID Interface
25 * These are important ID types used in SILC. SILC server creates these
26 * but SILC client has to handle these as well since these are used in
27 * packet sending and reception. However, client never creates these
28 * but it receives the correct ID's from server. Clients, servers and
29 * channels are identified by the these ID's.
31 * The ID's are based on IP addresses. The IP address provides a good
32 * way to distinguish the ID's from other ID's. The ID's supports both
35 * This file also includes the implementation of the SILC ID Payload
36 * parsing and encoding.
43 /****d* silccore/SilcIDAPI/SilcIdType
47 * typedef SilcUInt16 SilcIdType;
51 * SILC ID type definitions and the ID types.
55 typedef SilcUInt16 SilcIdType;
57 /* The SILC ID Types */
58 #define SILC_ID_NONE 0
59 #define SILC_ID_SERVER 1
60 #define SILC_ID_CLIENT 2
61 #define SILC_ID_CHANNEL 3
64 /* The ID Lenghts. These are IPv4 based and should be noted if used directly
65 that these cannot be used with IPv6. */
66 #define SILC_ID_SERVER_LEN (64 / 8)
67 #define SILC_ID_CLIENT_LEN (128 / 8)
68 #define SILC_ID_CHANNEL_LEN (64 / 8)
70 #define CLIENTID_HASH_LEN (88 / 8) /* Client ID's 88 bit MD5 hash */
72 /****s* silccore/SilcIDAPI/SilcIDPayload
76 * typedef struct SilcIDPayloadStruct *SilcIDPayload;
80 * This context is the actual ID Payload and is allocated by
81 * silc_id_payload_parse and given as argument usually to all
82 * silc_id_payload_* functions. It is freed by the function
83 * silc_id_payload_free.
86 typedef struct SilcIDPayloadStruct *SilcIDPayload;
88 /****s* silccore/SilcIDAPI/SilcArgumentPayload
92 * typedef struct SilcArgumentPayloadStruct *SilcArgumentPayload;
96 * This context is the actual Argument Payload and is allocated
97 * by silc_argument_payload_parse and given as argument usually to
98 * all silc_argument_payload_* functions. It is freed by the
99 * silc_argument_payload_free function.
102 typedef struct SilcArgumentPayloadStruct *SilcArgumentPayload;
106 /****f* silccore/SilcIDAPI/silc_id_payload_parse
110 * SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
111 * SilcUInt32 payload_len);
115 * Parses buffer and return ID payload into payload structure. The
116 * `buffer' is raw payload buffer.
119 SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
120 SilcUInt32 payload_len);
122 /****f* silccore/SilcIDAPI/silc_id_payload_parse_id
126 * void *silc_id_payload_parse_id(const unsigned char *data,
132 * Return ID directly from the raw ID Payload data buffer. The
133 * caller must free the returned ID.
136 void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
139 /****f* silccore/SilcIDAPI/silc_id_payload_encode
143 * SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
147 * Encodes ID Payload. The `id' is the ID of the type `type' to put
148 * into the payload. Returns the encoded payload buffer.
151 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
153 /****f* silccore/SilcIDAPI/silc_id_payload_encode_data
157 * SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
158 * uin32 id_len, SilcIdType type);
162 * Encodes ID Payload. The `id' is raw ID data of the length of `id_len'
163 * of type of `type'. Returns the encoded payload buffer.
166 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
167 SilcUInt32 id_len, SilcIdType type);
169 /****f* silccore/SilcIDAPI/silc_id_payload_free
173 * void silc_id_payload_free(SilcIDPayload payload);
177 * Frees the ID Payload and all data in it.
180 void silc_id_payload_free(SilcIDPayload payload);
182 /****f* silccore/SilcIDAPI/silc_id_payload_get_type
186 * SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
190 * Returns the ID type from the ID Payload. The type tells the
191 * type of the ID in the payload.
194 SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
196 /****f* silccore/SilcIDAPI/silc_id_payload_get_id
200 * void *silc_id_payload_get_id(SilcIDPayload payload);
204 * Returns the ID in the ID Payload. The caller must free the
208 void *silc_id_payload_get_id(SilcIDPayload payload);
210 /****f* silccore/SilcIDAPI/silc_id_payload_get_data
214 * unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
218 * Returns the raw ID data from the ID Payload. The data is duplicated
219 * and the caller must free it.
222 unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
224 /****f* silccore/SilcIDAPI/silc_id_payload_get_len
228 * SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
232 * Returns the length of the ID in the ID Payload.
235 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
237 /****s* silccore/SilcIDAPI/SilcIDIP
241 * typedef struct { ... } SilcIDIP;
245 * Generic IP address structure to indicate either IPv4 or IPv6 address.
246 * This structure is used inside all SILC ID's. The true length of the
247 * ID depends of the length of the IP address.
252 unsigned char data[16]; /* IP data (in MSB first order) */
253 SilcUInt8 data_len; /* Length of the data (4 or 16) */
257 /****s* silccore/SilcIDAPI/SilcServerID
261 * typedef struct { ... } SilcServerID;
265 * 64 or 160 bit SilcServerID structure:
269 * 16 bit random number
274 SilcIDIP ip; /* n bit IP address */
275 SilcUInt16 port; /* 16 bit port */
276 SilcUInt16 rnd; /* 16 bit random number */
280 /****s* silccore/SilcIDAPI/SilcClientID
284 * typedef struct { ... } SilcClientID;
288 * 128 or 224 bit SilcClientID structure:
290 * n bit ServerID IP address [bits 1-32 or bits 1-128]
291 * 8 bit random number
292 * 88 bit hash value from nickname
297 SilcIDIP ip; /* n bit IP address */
298 unsigned char rnd; /* 8 bit random number */
299 unsigned char hash[CLIENTID_HASH_LEN]; /* 88 bit MD5 hash */
303 /****s* silccore/SilcIDAPI/SilcChannelID
307 * typedef struct { ... } SilcChannelID;
311 * 64 or 160 bit SilcChannel ID structure:
313 * n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
314 * 16 bit Router's ServerID port [bits 33-48 or bits 129-144]
315 * 16 bit random number
320 SilcIDIP ip; /* n bit IP address */
321 SilcUInt16 port; /* 16 bit port */
322 SilcUInt16 rnd; /* 16 bit random number */
328 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE
332 * #define SILC_ID_COMPARE ...
336 * Compares two ID's. Returns TRUE if they match and FALSE if they do
341 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
344 /****d* silccore/SilcIDAPI/SILC_ID_CLIENT_COMPARE
348 * #define SILC_ID_CLIENT_COMPARE ...
352 * Compares Client ID's. Returns TRUE if they match.
356 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
357 SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
360 /****d* silccore/SilcIDAPI/SILC_ID_SERVER_COMPARE
364 * #define SILC_ID_SERVER_COMPARE ...
368 * Compares Server ID's. Returns TRUE if they match.
372 #define SILC_ID_SERVER_COMPARE(id1, id2) \
373 SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
376 /****d* silccore/SilcIDAPI/SILC_ID_CHANNEL_COMPARE
380 * #define SILC_ID_CHANNEL_COMPARE ...
384 * Compares Channel ID's. Returns TRUE if they match.
388 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
389 SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
392 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_TYPE
396 * #define SILC_ID_COMPARE_TYPE ...
400 * Compares two ID's by type. Returns TRUE if they match.
404 #define SILC_ID_COMPARE_TYPE(id1, id2, type) \
405 (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) : \
406 type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) : \
407 SILC_ID_CHANNEL_COMPARE(id1, id2))
410 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_HASH
414 * #define SILC_ID_COMPARE_HASH ...
418 * Compares the nickname hash of the Client ID. Returns TRUE if
419 * they match. Since the nickname hash is based on the nickname of
420 * the client this can be used to search the ID by nickname (taking
421 * the hash out of it) or using the hash from the ID.
425 #define SILC_ID_COMPARE_HASH(id1, id2) \
426 (!memcmp((id1)->hash, (id2)->hash, CLIENTID_HASH_LEN))
431 /****f* silccore/SilcIDAPI/silc_id_id2str
435 * unsigned char *silc_id_id2str(const void *id, SilcIdType type);
439 * Converts an ID of type `type' to data. This can be used to
440 * convert the ID's to data for inclusion in the packets.
443 unsigned char *silc_id_id2str(const void *id, SilcIdType type);
445 /****f* silccore/SilcIDAPI/silc_id_str2id
449 * void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
454 * Converts ID data string to an ID. This can be used to get the
455 * ID out of data that has been taken for example from packet.
458 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type);
460 /****f* silccore/SilcIDAPI/silc_id_get_len
464 * SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
468 * Returns the true length of the ID of the type `type'.
471 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
473 /****f* silccore/SilcIDAPI/silc_id_dup
477 * void *silc_id_dup(const void *id, SilcIdType type);
481 * Duplicates the ID of the type `type'. The caller must free the
485 void *silc_id_dup(const void *id, SilcIdType type);