Integer type name change.
[silc.git] / apps / silcd / route.c
1 /*
2
3   route.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 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  * Route cache routines. Server uses these to route packets to specific
22  * routes. If route entry doesn't exist for a specific destination, server
23  * uses primary route (default route).
24  */
25 /* XXX Adding two ID's with same IP number replaces the old entry thus
26    gives wrong route. Thus, now disabled until figured out a better way
27    to do this or when removed the whole thing. This could be removed
28    because entry->router->connection gives always the most optimal route
29    for the ID anyway (unless new routes (faster perhaps) are established
30    after receiving this ID, this we don't know however). */
31 /* $Id$ */
32
33 #include "serverincludes.h"
34 #include "server_internal.h"
35 #include "route.h"
36
37 /* Route cache hash table */
38 SilcServerRouteTable silc_route_cache[SILC_SERVER_ROUTE_SIZE];
39
40 /* Adds new route to the route cache. The argument `index' is the
41    index value generated by silc_server_route_hash. */
42
43 void silc_server_route_add(SilcUInt32 index, unsigned int dest,
44                            SilcServerEntry router)
45 {
46   silc_route_cache[index].dest = dest;
47   silc_route_cache[index].router = router;
48 }
49
50 /* Checksk whether destination has a specific router. Returns the
51    router data if found, NULL otherwise. */
52
53 SilcServerEntry silc_server_route_check(SilcUInt32 dest, 
54                                         SilcUInt16 port)
55 {
56   SilcUInt32 index;
57
58   index = silc_server_route_hash(dest, port);
59
60   if (silc_route_cache[index].router != NULL &&
61       silc_route_cache[index].dest == dest)
62     return silc_route_cache[index].router;
63
64   return NULL;
65 }
66
67 /* Returns the connection object for the fastest route for the given ID.
68    If we are normal server then this just returns our primary route. If
69    we are router we will do route lookup. */
70
71 SilcSocketConnection silc_server_route_get(SilcServer server, void *id,
72                                            SilcIdType id_type)
73 {
74   if (server->server_type == SILC_ROUTER) {
75     SilcUInt32 dest = 0;
76     SilcUInt16 port = 0;
77     SilcServerEntry router = NULL;
78 #if 0
79
80     switch(id_type) {
81     case SILC_ID_CLIENT:
82       dest = ((SilcClientID *)id)->ip.s_addr;
83       port = server->id->port;
84       break;
85     case SILC_ID_SERVER:
86       dest = ((SilcServerID *)id)->ip.s_addr;
87       port = ((SilcServerID *)id)->port;
88       break;
89     case SILC_ID_CHANNEL:
90       dest = ((SilcChannelID *)id)->ip.s_addr;
91       port = ((SilcChannelID *)id)->port;
92       break;
93     default:
94       return NULL;
95     }
96
97 #endif
98
99     router = silc_server_route_check(dest, port);
100     if (!router)
101       return (SilcSocketConnection)server->id_entry->router->connection;
102
103     return (SilcSocketConnection)router->connection;
104   }
105
106   return (SilcSocketConnection)server->id_entry->router->connection;
107 }