00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include "libs/gateway_prot.h"
00022 #include "libs/gateway_rpc.h"
00023 #include "libs/sha1.h"
00024 #include "TcaRegistry.h"
00025
00026 static const char* APP_STRING = "tca";
00027 static const char* CLIB_STRING = "rpcgen";
00028
00029 static const int DHT_KEYLEN = 20;
00030
00031
00032
00033
00034 static void
00035 hash(const std::string& s, uint8 digest[DHT_KEYLEN])
00036 {
00037
00038 sha1_context ctx;
00039 sha1_starts(&ctx);
00040 sha1_update(&ctx, (unsigned char*)(s.c_str()), s.length());
00041 sha1_finish(&ctx, digest);
00042 }
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00056
00057
00058
00059 bool
00060 TcaRegistry::init_nodes()
00061 {
00062
00063
00064
00065
00066
00067
00068
00069 dht_nodes_.push_back(std::string("cloudburst.uwaterloo.ca"));
00070 dht_nodes_.push_back(std::string("blast.uwaterloo.ca"));
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 return true;
00085 }
00086
00087
00088 bool
00089 TcaRegistry::init_addrs()
00090 {
00091
00092
00093
00094
00095
00096
00097 printf("Initializing TcaRegistry...\n");
00098
00099 last_node_ = 0;
00100
00101 sockaddr_in addr;
00102 for (unsigned int i=0; i<dht_nodes_.size(); ++i)
00103 {
00104 if (test_node(dht_nodes_[i].c_str(), &addr))
00105 {
00106
00107 dht_addrs_.push_back(addr);
00108 }
00109 }
00110
00111 if (dht_addrs_.size() == 0) return false;
00112
00113 printf("...dht nodes available = %zu / %zu\n",
00114 dht_addrs_.size(), dht_nodes_.size());
00115 return true;
00116 }
00117
00118
00119
00120
00121
00122 bool
00123 TcaRegistry::write(const RegRecord& rr, int ttl)
00124 {
00125 CLIENT* p_node = get_node();
00126 if (p_node == NULL) return false;
00127
00128
00129
00130 uint8 key[DHT_KEYLEN];
00131 hash(rr.host_, key);
00132
00133
00134 bamboo_put_args args;
00135 memset(&args, 0, sizeof(args));
00136
00137 args.application = const_cast<char*>(APP_STRING);
00138 args.client_library = const_cast<char*>(CLIB_STRING);
00139 memcpy(args.key, key, DHT_KEYLEN);
00140
00141 args.value.bamboo_value_len = rr.link_addr_.length() + 1;
00142 args.value.bamboo_value_val = const_cast<char*>(rr.link_addr_.c_str());
00143
00144 args.ttl_sec = ttl;
00145
00146
00147
00148 bamboo_stat* res = bamboo_dht_proc_put_2(&args, p_node);
00149
00150
00151 return (*res == BAMBOO_OK);
00152 }
00153
00154
00155
00156
00157
00158
00159 bool
00160 TcaRegistry::read(RegRecord& rr)
00161 {
00162 CLIENT* p_node = get_node();
00163 if (p_node == NULL) return false;
00164
00165
00166
00167 uint8 key[DHT_KEYLEN];
00168 hash(rr.host_, key);
00169
00170
00171 bamboo_get_args args;
00172 memset(&args, 0, sizeof(args));
00173
00174 args.application = const_cast<char*>(APP_STRING);
00175 args.client_library = const_cast<char*>(CLIB_STRING);
00176 memcpy(args.key, key, DHT_KEYLEN);
00177
00178
00179
00180 args.maxvals = 1;
00181
00182 bamboo_get_res* res = bamboo_dht_proc_get_2(&args, p_node);
00183 if (res == NULL)
00184 {
00185 printf("TcaRegistry::read: get returned NULL\n");
00186 return false;
00187 }
00188
00189 int n_values = res->values.values_len;
00190
00191 if (n_values != 1)
00192 {
00193
00194 return false;
00195 }
00196
00197 bamboo_value* p_val = &res->values.values_val[0];
00198
00199 rr.link_addr_ = p_val->bamboo_value_val;
00200 printf("TcaRegistry::read: succeeded! value=%s\n", rr.link_addr_.c_str());
00201
00202 return true;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 CLIENT*
00236 TcaRegistry::get_node()
00237 {
00238
00239
00240
00241
00242
00243
00244
00245 CLIENT* p_node = NULL;
00246
00247 for (unsigned int i = last_node_ + 1; i != last_node_; ++i)
00248 {
00249 if (i == dht_addrs_.size()) i = 0;
00250 p_node = get_connection(&dht_addrs_[i]);
00251 if (p_node)
00252 {
00253 last_node_ = i;
00254 break;
00255 }
00256 }
00257
00258 return p_node;
00259 }
00260
00261
00262