5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 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 /* The ID Lenghts. These are IPv4 based and should be noted if used directly
43 that these cannot be used with IPv6. */
44 #define SILC_ID_SERVER_LEN (64 / 8)
45 #define SILC_ID_CLIENT_LEN (128 / 8)
46 #define SILC_ID_CHANNEL_LEN (64 / 8)
48 #define CLIENTID_HASH_LEN (88 / 8) /* Client ID's 88 bit MD5 hash */
50 /****d* silccore/SilcIDAPI/SilcIdType
54 * typedef SilcUInt16 SilcIdType;
58 * SILC ID type definitions and the ID types.
62 typedef SilcUInt16 SilcIdType;
64 /* The SILC ID Types */
65 #define SILC_ID_NONE 0
66 #define SILC_ID_SERVER 1
67 #define SILC_ID_CLIENT 2
68 #define SILC_ID_CHANNEL 3
71 /****s* silccore/SilcIDAPI/SilcIDIP
75 * typedef struct { ... } SilcIDIP;
79 * Generic IP address structure to indicate either IPv4 or IPv6 address.
80 * This structure is used inside all SILC ID's. The true length of the
81 * ID depends of the length of the IP address.
85 typedef struct SilcIDIPStruct {
86 unsigned char data[16]; /* IP data (in MSB first order) */
87 SilcUInt8 data_len; /* Length of the data (4 or 16) */
91 /****s* silccore/SilcIDAPI/SilcServerID
95 * typedef struct { ... } SilcServerID;
99 * 64 or 160 bit SilcServerID structure:
103 * 16 bit random number
107 typedef struct SilcServerIDStruct {
108 SilcIDIP ip; /* n bit IP address */
109 SilcUInt16 port; /* 16 bit port */
110 SilcUInt16 rnd; /* 16 bit random number */
114 /****s* silccore/SilcIDAPI/SilcClientID
118 * typedef struct { ... } SilcClientID;
122 * 128 or 224 bit SilcClientID structure:
124 * n bit ServerID IP address [bits 1-32 or bits 1-128]
125 * 8 bit random number
126 * 88 bit hash value from lowercase nickname
130 typedef struct SilcClientIDStruct {
131 SilcIDIP ip; /* n bit IP address */
132 unsigned char rnd; /* 8 bit random number */
133 unsigned char hash[CLIENTID_HASH_LEN]; /* 88 bit MD5 hash */
137 /****s* silccore/SilcIDAPI/SilcChannelID
141 * typedef struct { ... } SilcChannelID;
145 * 64 or 160 bit SilcChannel ID structure:
147 * n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
148 * 16 bit Router's ServerID port [bits 33-48 or bits 129-144]
149 * 16 bit random number
153 typedef struct SilcChannelIDStruct {
154 SilcIDIP ip; /* n bit IP address */
155 SilcUInt16 port; /* 16 bit port */
156 SilcUInt16 rnd; /* 16 bit random number */
160 /****s* silccore/SilcIDAPI/SilcID
164 * typedef struct { ... } SilcID;
168 * The generic ID structure that can represent SilcClientID, SilcServerID
169 * and SilcChannelID. The silc_id_payload_parse_id returns the ID in the
170 * SilcID structure. Other routines except either SilcClientID,
171 * SilcServerID or SilcChannelID as a void pointer.
175 typedef struct SilcIDStruct {
177 SilcServerID server_id;
178 SilcChannelID channel_id;
179 SilcClientID client_id;
187 /****d* silccore/SilcIDAPI/SILC_ID_GET_ID
191 * #define SILC_ID_GET_ID ...
195 * Returns the ID type specific pointer from the SilcID structure. As
196 * the SilcID is able to house all types of IDs this macro can be used
197 * to get the specific ID from the structure by its type.
201 #define SILC_ID_GET_ID(id) \
202 ((id).type == SILC_ID_CLIENT ? (void *)&(id).u.client_id : \
203 (id).type == SILC_ID_SERVER ? (void *)&(id).u.server_id : \
204 (void *)&(id).u.channel_id)
207 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE
211 * #define SILC_ID_COMPARE ...
215 * Compares two ID's. Returns TRUE if they match and FALSE if they do
220 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
223 /****d* silccore/SilcIDAPI/SILC_ID_CLIENT_COMPARE
227 * #define SILC_ID_CLIENT_COMPARE ...
231 * Compares Client ID's. Returns TRUE if they match.
235 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
236 SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
239 /****d* silccore/SilcIDAPI/SILC_ID_SERVER_COMPARE
243 * #define SILC_ID_SERVER_COMPARE ...
247 * Compares Server ID's. Returns TRUE if they match.
251 #define SILC_ID_SERVER_COMPARE(id1, id2) \
252 SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
255 /****d* silccore/SilcIDAPI/SILC_ID_CHANNEL_COMPARE
259 * #define SILC_ID_CHANNEL_COMPARE ...
263 * Compares Channel ID's. Returns TRUE if they match.
267 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
268 SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
271 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_TYPE
275 * #define SILC_ID_COMPARE_TYPE ...
279 * Compares two ID's by type. Returns TRUE if they match.
283 #define SILC_ID_COMPARE_TYPE(id1, id2, type) \
284 (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) : \
285 type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) : \
286 SILC_ID_CHANNEL_COMPARE(id1, id2))
289 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_HASH
293 * #define SILC_ID_COMPARE_HASH ...
297 * Compares the nickname hash of the Client ID. Returns TRUE if
298 * they match. Since the nickname hash is based on the nickname of
299 * the client this can be used to search the ID by nickname (taking
300 * the hash out of it) or using the hash from the ID.
304 #define SILC_ID_COMPARE_HASH(id1, id2) \
305 (!memcmp((id1)->hash, (id2)->hash, CLIENTID_HASH_LEN))
308 /****s* silccore/SilcIDAPI/SilcIDPayload
312 * typedef struct SilcIDPayloadStruct *SilcIDPayload;
316 * This context is the actual ID Payload and is allocated by
317 * silc_id_payload_parse and given as argument usually to all
318 * silc_id_payload_* functions. It is freed by the function
319 * silc_id_payload_free.
322 typedef struct SilcIDPayloadStruct *SilcIDPayload;
326 /****f* silccore/SilcIDAPI/silc_id_payload_parse
330 * SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
331 * SilcUInt32 payload_len);
335 * Parses buffer and return ID payload into payload structure. The
336 * `buffer' is raw payload buffer. The caller must free the returned
340 SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
341 SilcUInt32 payload_len);
343 /****f* silccore/SilcIDAPI/silc_id_payload_parse_id
347 * SilcBool silc_id_payload_parse_id(const unsigned char *data,
348 * SilcUInt32 len, SilcID *ret_id);
352 * Return ID directly from the raw ID Payload data buffer. This does
353 * not allocate any memory.
356 SilcBool silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
359 /****f* silccore/SilcIDAPI/silc_id_payload_encode
363 * SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
367 * Encodes ID Payload. The `id' is the ID of the type `type' to put
368 * into the payload. Returns the encoded payload buffer.
371 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type);
373 /****f* silccore/SilcIDAPI/silc_id_payload_encode_data
377 * SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
378 * uin32 id_len, SilcIdType type);
382 * Encodes ID Payload. The `id' is raw ID data of the length of `id_len'
383 * of type of `type'. Returns the encoded payload buffer.
386 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
387 SilcUInt32 id_len, SilcIdType type);
389 /****f* silccore/SilcIDAPI/silc_id_payload_free
393 * void silc_id_payload_free(SilcIDPayload payload);
397 * Frees the ID Payload and all data in it.
400 void silc_id_payload_free(SilcIDPayload payload);
402 /****f* silccore/SilcIDAPI/silc_id_payload_get_type
406 * SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
410 * Returns the ID type from the ID Payload. The type tells the
411 * type of the ID in the payload.
414 SilcIdType silc_id_payload_get_type(SilcIDPayload payload);
416 /****f* silccore/SilcIDAPI/silc_id_payload_get_id
420 * SilcBool silc_id_payload_get_id(SilcIDPayload payload, void *ret_id,
421 * SilcUInt32 ret_id_len);
425 * Returns the ID in the ID Payload. This does not allocate any memory.
428 SilcBool silc_id_payload_get_id(SilcIDPayload payload, void *ret_id,
429 SilcUInt32 ret_id_len);
431 /****f* silccore/SilcIDAPI/silc_id_payload_get_data
435 * unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
439 * Returns the raw ID data from the ID Payload. The data is duplicated
440 * and the caller must free it.
443 unsigned char *silc_id_payload_get_data(SilcIDPayload payload);
445 /****f* silccore/SilcIDAPI/silc_id_payload_get_len
449 * SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
453 * Returns the length of the ID in the ID Payload.
456 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload);
458 /****f* silccore/SilcIDAPI/silc_id_id2str
462 * SilcBool silc_id_id2str(const void *id, SilcIdType type,
463 * unsigned char *ret_id, SilcUInt32 ret_id_size,
464 * SilcUInt32 *ret_id_len);
468 * Converts an ID of type `type' to data. This can be used to
469 * convert the ID's to data for inclusion in the packets. This does
470 * not allocate any memory.
473 SilcBool silc_id_id2str(const void *id, SilcIdType type,
474 unsigned char *ret_id, SilcUInt32 ret_id_size,
475 SilcUInt32 *ret_id_len);
477 /****f* silccore/SilcIDAPI/silc_id_str2id
481 * SilcBool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
482 * SilcIdType type, void *ret_id,
483 * SilcUInt32 ret_id_size);
487 * Converts ID data string to an ID. This can be used to get the
488 * ID out of data that has been taken for example from packet. This
489 * does not allocate any memory.
492 SilcBool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
493 SilcIdType type, void *ret_id, SilcUInt32 ret_id_size);
495 /****f* silccore/SilcIDAPI/silc_id_str2id2
499 * SilcBool silc_id_str2id2(const unsigned char *id, SilcUInt32 id_len,
500 * SilcIdType type, SilcID *ret_id);
504 * Same as silc_id_str2id but returns the ID into SilcID structure in
505 * `ret_id' pointer. This does not allocate any memory.
508 SilcBool silc_id_str2id2(const unsigned char *id, SilcUInt32 id_len,
509 SilcIdType type, SilcID *ret_id);
511 /****f* silccore/SilcIDAPI/silc_id_get_len
515 * SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
519 * Returns the true length of the ID of the type `type'.
522 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type);
524 /****f* silccore/SilcIDAPI/silc_id_dup
528 * void *silc_id_dup(const void *id, SilcIdType type);
532 * Duplicates the ID of the type `type'. The caller must free the
536 void *silc_id_dup(const void *id, SilcIdType type);