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, SilcUInt32 len);
130 * Return ID directly from the raw ID Payload data buffer. The
131 * caller must free the returned ID.
134 void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len);
136 /****f* silccore/SilcIDAPI/silc_id_payload_encode
140 * SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
144 * Encodes ID Payload. The `id' is the ID of the type `type' to put
145 * into the payload. Returns the encoded payload buffer.
148 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
150 /****f* silccore/SilcIDAPI/silc_id_payload_encode_data
154 * SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
155 * uin32 id_len, SilcIdType type);
159 * Encodes ID Payload. The `id' is raw ID data of the length of `id_len'
160 * of type of `type'. Returns the encoded payload buffer.
163 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
164 SilcUInt32 id_len, SilcIdType type);
166 /****f* silccore/SilcIDAPI/silc_id_payload_free
170 * void silc_id_payload_free(SilcIDPayload payload);
174 * Frees the ID Payload and all data in it.
177 void silc_id_payload_free(SilcIDPayload payload);
179 /****f* silccore/SilcIDAPI/silc_id_payload_get_type
183 * SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
187 * Returns the ID type from the ID Payload. The type tells the
188 * type of the ID in the payload.
191 SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
193 /****f* silccore/SilcIDAPI/silc_id_payload_get_id
197 * void *silc_id_payload_get_id(SilcIDPayload payload);
201 * Returns the ID in the ID Payload. The caller must free the
205 void *silc_id_payload_get_id(SilcIDPayload payload);
207 /****f* silccore/SilcIDAPI/silc_id_payload_get_data
211 * unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
215 * Returns the raw ID data from the ID Payload. The data is duplicated
216 * and the caller must free it.
219 unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
221 /****f* silccore/SilcIDAPI/silc_id_payload_get_len
225 * SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
229 * Returns the length of the ID in the ID Payload.
232 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
234 /****s* silccore/SilcIDAPI/SilcIDIP
238 * typedef struct { ... } SilcIDIP;
242 * Generic IP address structure to indicate either IPv4 or IPv6 address.
243 * This structure is used inside all SILC ID's. The true length of the
244 * ID depends of the length of the IP address.
249 unsigned char data[16]; /* IP data (in MSB first order) */
250 SilcUInt8 data_len; /* Length of the data (4 or 16) */
254 /****s* silccore/SilcIDAPI/SilcServerID
258 * typedef struct { ... } SilcServerID;
262 * 64 or 160 bit SilcServerID structure:
266 * 16 bit random number
271 SilcIDIP ip; /* n bit IP address */
272 SilcUInt16 port; /* 16 bit port */
273 SilcUInt16 rnd; /* 16 bit random number */
277 /****s* silccore/SilcIDAPI/SilcClientID
281 * typedef struct { ... } SilcClientID;
285 * 128 or 224 bit SilcClientID structure:
287 * n bit ServerID IP address [bits 1-32 or bits 1-128]
288 * 8 bit random number
289 * 88 bit hash value from nickname
294 SilcIDIP ip; /* n bit IP address */
295 unsigned char rnd; /* 8 bit random number */
296 unsigned char hash[CLIENTID_HASH_LEN]; /* 88 bit MD5 hash */
300 /****s* silccore/SilcIDAPI/SilcChannelID
304 * typedef struct { ... } SilcChannelID;
308 * 64 or 160 bit SilcChannel ID structure:
310 * n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
311 * 16 bit Router's ServerID port [bits 33-48 or bits 129-144]
312 * 16 bit random number
317 SilcIDIP ip; /* n bit IP address */
318 SilcUInt16 port; /* 16 bit port */
319 SilcUInt16 rnd; /* 16 bit random number */
325 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE
329 * #define SILC_ID_COMPARE ...
333 * Compares two ID's. Returns TRUE if they match and FALSE if they do
338 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
341 /****d* silccore/SilcIDAPI/SILC_ID_CLIENT_COMPARE
345 * #define SILC_ID_CLIENT_COMPARE ...
349 * Compares Client ID's. Returns TRUE if they match.
353 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
354 SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
357 /****d* silccore/SilcIDAPI/SILC_ID_SERVER_COMPARE
361 * #define SILC_ID_SERVER_COMPARE ...
365 * Compares Server ID's. Returns TRUE if they match.
369 #define SILC_ID_SERVER_COMPARE(id1, id2) \
370 SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
373 /****d* silccore/SilcIDAPI/SILC_ID_CHANNEL_COMPARE
377 * #define SILC_ID_CHANNEL_COMPARE ...
381 * Compares Channel ID's. Returns TRUE if they match.
385 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
386 SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
389 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_TYPE
393 * #define SILC_ID_COMPARE_TYPE ...
397 * Compares two ID's by type. Returns TRUE if they match.
401 #define SILC_ID_COMPARE_TYPE(id1, id2, type) \
402 (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) : \
403 type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) : \
404 SILC_ID_CHANNEL_COMPARE(id1, id2))
407 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_HASH
411 * #define SILC_ID_COMPARE_HASH ...
415 * Compares the nickname hash of the Client ID. Returns TRUE if
416 * they match. Since the nickname hash is based on the nickname of
417 * the client this can be used to search the ID by nickname (taking
418 * the hash out of it) or using the hash from the ID.
422 #define SILC_ID_COMPARE_HASH(id1, id2) \
423 (!memcmp((id1)->hash, (id2)->hash, CLIENTID_HASH_LEN))
428 /****f* silccore/SilcIDAPI/silc_id_id2str
432 * unsigned char *silc_id_id2str(const void *id, SilcIdType type);
436 * Converts an ID of type `type' to data. This can be used to
437 * convert the ID's to data for inclusion in the packets.
440 unsigned char *silc_id_id2str(const void *id, SilcIdType type);
442 /****f* silccore/SilcIDAPI/silc_id_str2id
446 * void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
451 * Converts ID data string to an ID. This can be used to get the
452 * ID out of data that has been taken for example from packet.
455 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type);
457 /****f* silccore/SilcIDAPI/silc_id_get_len
461 * SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
465 * Returns the true length of the ID of the type `type'.
468 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
470 /****f* silccore/SilcIDAPI/silc_id_dup
474 * void *silc_id_dup(const void *id, SilcIdType type);
478 * Duplicates the ID of the type `type'. The caller must free the
482 void *silc_id_dup(const void *id, SilcIdType type);