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