d7aaf3af65ffd244528053cdc1e7c3f05e8079d4
[silc.git] / lib / silccore / id.c
1 /*
2
3   id.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /*
21  * $Id$
22  * $Log$
23  * Revision 1.1  2000/06/27 11:36:55  priikone
24  * Initial revision
25  *
26  *
27  */
28
29 #include "silcincludes.h"
30
31 /* Converts ID to string. */
32
33 unsigned char *silc_id_id2str(void *id, SilcIdType type)
34 {
35   unsigned char *ret_id;
36   SilcServerID *server_id;
37   SilcClientID *client_id;
38   SilcChannelID *channel_id;
39
40   switch(type) {
41   case SILC_ID_SERVER:
42     server_id = (SilcServerID *)id;
43     ret_id = silc_calloc(8, sizeof(unsigned char));
44     SILC_PUT32_MSB(server_id->ip.s_addr, ret_id);
45     SILC_PUT16_MSB(server_id->port, &ret_id[4]);
46     SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
47     return ret_id;
48     break;
49   case SILC_ID_CLIENT:
50     client_id = (SilcClientID *)id;
51     ret_id = silc_calloc(16, sizeof(unsigned char));
52     SILC_PUT32_MSB(client_id->ip.s_addr, ret_id);
53     ret_id[4] = client_id->rnd;
54     memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
55     return ret_id;
56     break;
57   case SILC_ID_CHANNEL:
58     channel_id = (SilcChannelID *)id;
59     ret_id = silc_calloc(8, sizeof(unsigned char));
60     SILC_PUT32_MSB(channel_id->ip.s_addr, ret_id);
61     SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
62     SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
63     return ret_id;
64     break;
65   }
66
67   return NULL;
68 }
69
70 /* Converts string to a ID */
71
72 void *silc_id_str2id(unsigned char *id, SilcIdType type) 
73 {
74
75   switch(type) {
76   case SILC_ID_SERVER:
77     {
78       SilcServerID *server_id = silc_calloc(1, sizeof(*server_id));
79       SILC_GET32_MSB(server_id->ip.s_addr, id);
80       SILC_GET16_MSB(server_id->port, &id[4]);
81       SILC_GET16_MSB(server_id->rnd, &id[6]);
82       return server_id;
83     }
84     break;
85   case SILC_ID_CLIENT:
86     {
87       SilcClientID *client_id = silc_calloc(1, sizeof(*client_id));
88       SILC_GET32_MSB(client_id->ip.s_addr, id);
89       client_id->rnd = id[4];
90       memcpy(client_id->hash, &id[5], CLIENTID_HASH_LEN);
91       return client_id;
92     }
93     break;
94   case SILC_ID_CHANNEL:
95     {
96       SilcChannelID *channel_id = silc_calloc(1, sizeof(*channel_id));
97       SILC_GET32_MSB(channel_id->ip.s_addr, id);
98       SILC_GET16_MSB(channel_id->port, &id[4]);
99       SILC_GET16_MSB(channel_id->rnd, &id[6]);
100       return channel_id;
101     }
102     break;
103   }
104
105   return NULL;
106 }
107
108 /* Returns length of the ID */
109
110 unsigned int silc_id_get_len(SilcIdType type)
111 {
112   switch(type) {
113   case SILC_ID_SERVER:
114     return SILC_ID_SERVER_LEN;
115     break;
116   case SILC_ID_CLIENT:
117     return SILC_ID_CLIENT_LEN;
118     break;
119   case SILC_ID_CHANNEL:
120     return SILC_ID_CHANNEL_LEN;
121     break;
122   }
123
124   return 0;
125 }