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;
90 /****f* silccore/SilcIDAPI/silc_id_payload_parse
94 * SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
95 * SilcUInt32 payload_len);
99 * Parses buffer and return ID payload into payload structure. The
100 * `buffer' is raw payload buffer. The caller must free the returned
104 SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
105 SilcUInt32 payload_len);
107 /****f* silccore/SilcIDAPI/silc_id_payload_parse_id
111 * void *silc_id_payload_parse_id(const unsigned char *data,
117 * Return ID directly from the raw ID Payload data buffer. The
118 * caller must free the returned ID.
121 void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
124 /****f* silccore/SilcIDAPI/silc_id_payload_encode
128 * SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
132 * Encodes ID Payload. The `id' is the ID of the type `type' to put
133 * into the payload. Returns the encoded payload buffer.
136 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
138 /****f* silccore/SilcIDAPI/silc_id_payload_encode_data
142 * SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
143 * uin32 id_len, SilcIdType type);
147 * Encodes ID Payload. The `id' is raw ID data of the length of `id_len'
148 * of type of `type'. Returns the encoded payload buffer.
151 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
152 SilcUInt32 id_len, SilcIdType type);
154 /****f* silccore/SilcIDAPI/silc_id_payload_free
158 * void silc_id_payload_free(SilcIDPayload payload);
162 * Frees the ID Payload and all data in it.
165 void silc_id_payload_free(SilcIDPayload payload);
167 /****f* silccore/SilcIDAPI/silc_id_payload_get_type
171 * SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
175 * Returns the ID type from the ID Payload. The type tells the
176 * type of the ID in the payload.
179 SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
181 /****f* silccore/SilcIDAPI/silc_id_payload_get_id
185 * void *silc_id_payload_get_id(SilcIDPayload payload);
189 * Returns the ID in the ID Payload. The caller must free the
193 void *silc_id_payload_get_id(SilcIDPayload payload);
195 /****f* silccore/SilcIDAPI/silc_id_payload_get_data
199 * unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
203 * Returns the raw ID data from the ID Payload. The data is duplicated
204 * and the caller must free it.
207 unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
209 /****f* silccore/SilcIDAPI/silc_id_payload_get_len
213 * SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
217 * Returns the length of the ID in the ID Payload.
220 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
222 /****s* silccore/SilcIDAPI/SilcIDIP
226 * typedef struct { ... } SilcIDIP;
230 * Generic IP address structure to indicate either IPv4 or IPv6 address.
231 * This structure is used inside all SILC ID's. The true length of the
232 * ID depends of the length of the IP address.
237 unsigned char data[16]; /* IP data (in MSB first order) */
238 SilcUInt8 data_len; /* Length of the data (4 or 16) */
242 /****s* silccore/SilcIDAPI/SilcServerID
246 * typedef struct { ... } SilcServerID;
250 * 64 or 160 bit SilcServerID structure:
254 * 16 bit random number
259 SilcIDIP ip; /* n bit IP address */
260 SilcUInt16 port; /* 16 bit port */
261 SilcUInt16 rnd; /* 16 bit random number */
265 /****s* silccore/SilcIDAPI/SilcClientID
269 * typedef struct { ... } SilcClientID;
273 * 128 or 224 bit SilcClientID structure:
275 * n bit ServerID IP address [bits 1-32 or bits 1-128]
276 * 8 bit random number
277 * 88 bit hash value from lowercase nickname
282 SilcIDIP ip; /* n bit IP address */
283 unsigned char rnd; /* 8 bit random number */
284 unsigned char hash[CLIENTID_HASH_LEN]; /* 88 bit MD5 hash */
288 /****s* silccore/SilcIDAPI/SilcChannelID
292 * typedef struct { ... } SilcChannelID;
296 * 64 or 160 bit SilcChannel ID structure:
298 * n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
299 * 16 bit Router's ServerID port [bits 33-48 or bits 129-144]
300 * 16 bit random number
305 SilcIDIP ip; /* n bit IP address */
306 SilcUInt16 port; /* 16 bit port */
307 SilcUInt16 rnd; /* 16 bit random number */
313 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE
317 * #define SILC_ID_COMPARE ...
321 * Compares two ID's. Returns TRUE if they match and FALSE if they do
326 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
329 /****d* silccore/SilcIDAPI/SILC_ID_CLIENT_COMPARE
333 * #define SILC_ID_CLIENT_COMPARE ...
337 * Compares Client ID's. Returns TRUE if they match.
341 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
342 SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
345 /****d* silccore/SilcIDAPI/SILC_ID_SERVER_COMPARE
349 * #define SILC_ID_SERVER_COMPARE ...
353 * Compares Server ID's. Returns TRUE if they match.
357 #define SILC_ID_SERVER_COMPARE(id1, id2) \
358 SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
361 /****d* silccore/SilcIDAPI/SILC_ID_CHANNEL_COMPARE
365 * #define SILC_ID_CHANNEL_COMPARE ...
369 * Compares Channel ID's. Returns TRUE if they match.
373 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
374 SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
377 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_TYPE
381 * #define SILC_ID_COMPARE_TYPE ...
385 * Compares two ID's by type. Returns TRUE if they match.
389 #define SILC_ID_COMPARE_TYPE(id1, id2, type) \
390 (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) : \
391 type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) : \
392 SILC_ID_CHANNEL_COMPARE(id1, id2))
395 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_HASH
399 * #define SILC_ID_COMPARE_HASH ...
403 * Compares the nickname hash of the Client ID. Returns TRUE if
404 * they match. Since the nickname hash is based on the nickname of
405 * the client this can be used to search the ID by nickname (taking
406 * the hash out of it) or using the hash from the ID.
410 #define SILC_ID_COMPARE_HASH(id1, id2) \
411 (!memcmp((id1)->hash, (id2)->hash, CLIENTID_HASH_LEN))
416 /****f* silccore/SilcIDAPI/silc_id_id2str
420 * unsigned char *silc_id_id2str(const void *id, SilcIdType type);
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);
431 /****f* silccore/SilcIDAPI/silc_id_str2id
435 * void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
440 * Converts ID data string to an ID. This can be used to get the
441 * ID out of data that has been taken for example from packet.
444 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
447 /****f* silccore/SilcIDAPI/silc_id_get_len
451 * SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
455 * Returns the true length of the ID of the type `type'.
458 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
460 /****f* silccore/SilcIDAPI/silc_id_dup
464 * void *silc_id_dup(const void *id, SilcIdType type);
468 * Duplicates the ID of the type `type'. The caller must free the
472 void *silc_id_dup(const void *id, SilcIdType type);