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. The caller must free the returned
120 SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
121 SilcUInt32 payload_len);
123 /****f* silccore/SilcIDAPI/silc_id_payload_parse_id
127 * void *silc_id_payload_parse_id(const unsigned char *data,
133 * Return ID directly from the raw ID Payload data buffer. The
134 * caller must free the returned ID.
137 void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
140 /****f* silccore/SilcIDAPI/silc_id_payload_encode
144 * SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
148 * Encodes ID Payload. The `id' is the ID of the type `type' to put
149 * into the payload. Returns the encoded payload buffer.
152 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
154 /****f* silccore/SilcIDAPI/silc_id_payload_encode_data
158 * SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
159 * uin32 id_len, SilcIdType type);
163 * Encodes ID Payload. The `id' is raw ID data of the length of `id_len'
164 * of type of `type'. Returns the encoded payload buffer.
167 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
168 SilcUInt32 id_len, SilcIdType type);
170 /****f* silccore/SilcIDAPI/silc_id_payload_free
174 * void silc_id_payload_free(SilcIDPayload payload);
178 * Frees the ID Payload and all data in it.
181 void silc_id_payload_free(SilcIDPayload payload);
183 /****f* silccore/SilcIDAPI/silc_id_payload_get_type
187 * SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
191 * Returns the ID type from the ID Payload. The type tells the
192 * type of the ID in the payload.
195 SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
197 /****f* silccore/SilcIDAPI/silc_id_payload_get_id
201 * void *silc_id_payload_get_id(SilcIDPayload payload);
205 * Returns the ID in the ID Payload. The caller must free the
209 void *silc_id_payload_get_id(SilcIDPayload payload);
211 /****f* silccore/SilcIDAPI/silc_id_payload_get_data
215 * unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
219 * Returns the raw ID data from the ID Payload. The data is duplicated
220 * and the caller must free it.
223 unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
225 /****f* silccore/SilcIDAPI/silc_id_payload_get_len
229 * SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
233 * Returns the length of the ID in the ID Payload.
236 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
238 /****s* silccore/SilcIDAPI/SilcIDIP
242 * typedef struct { ... } SilcIDIP;
246 * Generic IP address structure to indicate either IPv4 or IPv6 address.
247 * This structure is used inside all SILC ID's. The true length of the
248 * ID depends of the length of the IP address.
253 unsigned char data[16]; /* IP data (in MSB first order) */
254 SilcUInt8 data_len; /* Length of the data (4 or 16) */
258 /****s* silccore/SilcIDAPI/SilcServerID
262 * typedef struct { ... } SilcServerID;
266 * 64 or 160 bit SilcServerID structure:
270 * 16 bit random number
275 SilcIDIP ip; /* n bit IP address */
276 SilcUInt16 port; /* 16 bit port */
277 SilcUInt16 rnd; /* 16 bit random number */
281 /****s* silccore/SilcIDAPI/SilcClientID
285 * typedef struct { ... } SilcClientID;
289 * 128 or 224 bit SilcClientID structure:
291 * n bit ServerID IP address [bits 1-32 or bits 1-128]
292 * 8 bit random number
293 * 88 bit hash value from lowercase nickname
298 SilcIDIP ip; /* n bit IP address */
299 unsigned char rnd; /* 8 bit random number */
300 unsigned char hash[CLIENTID_HASH_LEN]; /* 88 bit MD5 hash */
304 /****s* silccore/SilcIDAPI/SilcChannelID
308 * typedef struct { ... } SilcChannelID;
312 * 64 or 160 bit SilcChannel ID structure:
314 * n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
315 * 16 bit Router's ServerID port [bits 33-48 or bits 129-144]
316 * 16 bit random number
321 SilcIDIP ip; /* n bit IP address */
322 SilcUInt16 port; /* 16 bit port */
323 SilcUInt16 rnd; /* 16 bit random number */
329 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE
333 * #define SILC_ID_COMPARE ...
337 * Compares two ID's. Returns TRUE if they match and FALSE if they do
342 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
345 /****d* silccore/SilcIDAPI/SILC_ID_CLIENT_COMPARE
349 * #define SILC_ID_CLIENT_COMPARE ...
353 * Compares Client ID's. Returns TRUE if they match.
357 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
358 SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
361 /****d* silccore/SilcIDAPI/SILC_ID_SERVER_COMPARE
365 * #define SILC_ID_SERVER_COMPARE ...
369 * Compares Server ID's. Returns TRUE if they match.
373 #define SILC_ID_SERVER_COMPARE(id1, id2) \
374 SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
377 /****d* silccore/SilcIDAPI/SILC_ID_CHANNEL_COMPARE
381 * #define SILC_ID_CHANNEL_COMPARE ...
385 * Compares Channel ID's. Returns TRUE if they match.
389 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
390 SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
393 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_TYPE
397 * #define SILC_ID_COMPARE_TYPE ...
401 * Compares two ID's by type. Returns TRUE if they match.
405 #define SILC_ID_COMPARE_TYPE(id1, id2, type) \
406 (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) : \
407 type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) : \
408 SILC_ID_CHANNEL_COMPARE(id1, id2))
411 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_HASH
415 * #define SILC_ID_COMPARE_HASH ...
419 * Compares the nickname hash of the Client ID. Returns TRUE if
420 * they match. Since the nickname hash is based on the nickname of
421 * the client this can be used to search the ID by nickname (taking
422 * the hash out of it) or using the hash from the ID.
426 #define SILC_ID_COMPARE_HASH(id1, id2) \
427 (!memcmp((id1)->hash, (id2)->hash, CLIENTID_HASH_LEN))
432 /****f* silccore/SilcIDAPI/silc_id_id2str
436 * unsigned char *silc_id_id2str(const void *id, SilcIdType type);
440 * Converts an ID of type `type' to data. This can be used to
441 * convert the ID's to data for inclusion in the packets.
444 unsigned char *silc_id_id2str(const void *id, SilcIdType type);
446 /****f* silccore/SilcIDAPI/silc_id_str2id
450 * void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
455 * Converts ID data string to an ID. This can be used to get the
456 * ID out of data that has been taken for example from packet.
459 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
462 /****f* silccore/SilcIDAPI/silc_id_get_len
466 * SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
470 * Returns the true length of the ID of the type `type'.
473 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
475 /****f* silccore/SilcIDAPI/silc_id_dup
479 * void *silc_id_dup(const void *id, SilcIdType type);
483 * Duplicates the ID of the type `type'. The caller must free the
487 void *silc_id_dup(const void *id, SilcIdType type);