aboutsummaryrefslogtreecommitdiff
path: root/src/dht/plugin_dhtlog_mysql.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dht/plugin_dhtlog_mysql.c')
-rw-r--r--src/dht/plugin_dhtlog_mysql.c323
1 files changed, 253 insertions, 70 deletions
diff --git a/src/dht/plugin_dhtlog_mysql.c b/src/dht/plugin_dhtlog_mysql.c
index f93935717..7798263c4 100644
--- a/src/dht/plugin_dhtlog_mysql.c
+++ b/src/dht/plugin_dhtlog_mysql.c
@@ -137,12 +137,26 @@ static struct StatementHandle *update_connection;
137#define GET_TRIAL_STMT "SELECT MAX( trialuid ) FROM trials" 137#define GET_TRIAL_STMT "SELECT MAX( trialuid ) FROM trials"
138static struct StatementHandle *get_trial; 138static struct StatementHandle *get_trial;
139 139
140#define GET_TOPOLOGY_STMT "SELECT MAX( topology_uid ) FROM topology"
141static struct StatementHandle *get_topology;
142
140#define GET_DHTKEYUID_STMT "SELECT dhtkeyuid FROM dhtkeys where dhtkey = ? and trialuid = ?" 143#define GET_DHTKEYUID_STMT "SELECT dhtkeyuid FROM dhtkeys where dhtkey = ? and trialuid = ?"
141static struct StatementHandle *get_dhtkeyuid; 144static struct StatementHandle *get_dhtkeyuid;
142 145
143#define GET_NODEUID_STMT "SELECT nodeuid FROM nodes where trialuid = ? and nodeid = ?" 146#define GET_NODEUID_STMT "SELECT nodeuid FROM nodes where trialuid = ? and nodeid = ?"
144static struct StatementHandle *get_nodeuid; 147static struct StatementHandle *get_nodeuid;
145 148
149#define INSERT_TOPOLOGY_STMT "INSERT INTO topology (trialuid, date, connections) "\
150 "VALUES (?, NOW(), ?)"
151static struct StatementHandle *insert_topology;
152
153#define EXTEND_TOPOLOGY_STMT "INSERT INTO extended_topology (topology_uid, uid_first, uid_second) "\
154 "VALUES (?, ?, ?)"
155static struct StatementHandle *extend_topology;
156
157#define UPDATE_TOPOLOGY_STMT "update topology set connections = ? where topology_uid = ?"
158static struct StatementHandle *update_topology;
159
146/** 160/**
147 * Run a query (not a select statement) 161 * Run a query (not a select statement)
148 * 162 *
@@ -244,6 +258,23 @@ itable ()
244 ") ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1")) 258 ") ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1"))
245 return GNUNET_SYSERR; 259 return GNUNET_SYSERR;
246 260
261 if (MRUNS ("CREATE TABLE IF NOT EXISTS `topology` ("
262 "`topology_uid` int(10) unsigned NOT NULL AUTO_INCREMENT,"
263 "`trialuid` int(10) unsigned NOT NULL,"
264 "`date` datetime NOT NULL,"
265 "`connections` int(10) unsigned NOT NULL,"
266 "PRIMARY KEY (`topology_uid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1"))
267 return GNUNET_SYSERR;
268
269 if (MRUNS ("CREATE TABLE IF NOT EXISTS `extended_topology` ("
270 "`extended_uid` int(10) unsigned NOT NULL AUTO_INCREMENT,"
271 "`topology_uid` int(10) unsigned NOT NULL,"
272 "`uid_first` int(10) unsigned NOT NULL,"
273 "`uid_second` int(10) unsigned NOT NULL,"
274 "PRIMARY KEY (`extended_uid`)"
275 ") ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1"))
276 return GNUNET_SYSERR;
277
247 if (MRUNS ("SET AUTOCOMMIT = 1")) 278 if (MRUNS ("SET AUTOCOMMIT = 1"))
248 return GNUNET_SYSERR; 279 return GNUNET_SYSERR;
249 280
@@ -327,7 +358,11 @@ iopen ()
327 PINIT (get_dhtkeyuid, GET_DHTKEYUID_STMT) || 358 PINIT (get_dhtkeyuid, GET_DHTKEYUID_STMT) ||
328 PINIT (get_nodeuid, GET_NODEUID_STMT) || 359 PINIT (get_nodeuid, GET_NODEUID_STMT) ||
329 PINIT (update_connection, UPDATE_CONNECTIONS_STMT) || 360 PINIT (update_connection, UPDATE_CONNECTIONS_STMT) ||
330 PINIT (get_trial, GET_TRIAL_STMT)) 361 PINIT (get_trial, GET_TRIAL_STMT) ||
362 PINIT (get_topology, GET_TOPOLOGY_STMT) ||
363 PINIT (insert_topology, INSERT_TOPOLOGY_STMT)||
364 PINIT (update_topology, UPDATE_TOPOLOGY_STMT)||
365 PINIT (extend_topology, EXTEND_TOPOLOGY_STMT))
331 { 366 {
332 return GNUNET_SYSERR; 367 return GNUNET_SYSERR;
333 } 368 }
@@ -530,6 +565,44 @@ prepared_statement_run_select (struct StatementHandle
530 return total; 565 return total;
531} 566}
532 567
568
569static int
570get_node_uid (unsigned long long *nodeuid, const GNUNET_HashCode * peerHash)
571{
572 MYSQL_BIND rbind[1];
573 struct GNUNET_CRYPTO_HashAsciiEncoded encPeer;
574 unsigned long long p_len;
575
576 int ret;
577 memset (rbind, 0, sizeof (rbind));
578 rbind[0].buffer_type = MYSQL_TYPE_LONG;
579 rbind[0].buffer = nodeuid;
580 rbind[0].is_unsigned = GNUNET_YES;
581
582 GNUNET_CRYPTO_hash_to_enc (peerHash, &encPeer);
583 p_len = strlen ((char *) &encPeer);
584
585 if (1 != (ret = prepared_statement_run_select (get_nodeuid,
586 1,
587 rbind,
588 return_ok,
589 NULL,
590 MYSQL_TYPE_LONG,
591 &current_trial,
592 GNUNET_YES,
593 MYSQL_TYPE_VAR_STRING,
594 &encPeer,
595 max_varchar_len,
596 &p_len, -1)))
597 {
598#if DEBUG_DHTLOG
599 fprintf (stderr, "FAILED\n");
600#endif
601 return GNUNET_SYSERR;
602 }
603 return GNUNET_OK;
604}
605
533static int 606static int
534get_current_trial (unsigned long long *trialuid) 607get_current_trial (unsigned long long *trialuid)
535{ 608{
@@ -552,6 +625,59 @@ get_current_trial (unsigned long long *trialuid)
552 return GNUNET_OK; 625 return GNUNET_OK;
553} 626}
554 627
628static int
629get_current_topology (unsigned long long *topologyuid)
630{
631 MYSQL_BIND rbind[1];
632
633 memset (rbind, 0, sizeof (rbind));
634 rbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
635 rbind[0].is_unsigned = 1;
636 rbind[0].buffer = topologyuid;
637
638 if ((GNUNET_OK !=
639 prepared_statement_run_select (get_topology,
640 1,
641 rbind,
642 return_ok, NULL, -1)))
643 {
644 return GNUNET_SYSERR;
645 }
646
647 return GNUNET_OK;
648}
649
650static int
651get_dhtkey_uid (unsigned long long *dhtkeyuid, const GNUNET_HashCode * key)
652{
653 MYSQL_BIND rbind[1];
654 struct GNUNET_CRYPTO_HashAsciiEncoded encKey;
655 unsigned long long k_len;
656 memset (rbind, 0, sizeof (rbind));
657 rbind[0].buffer_type = MYSQL_TYPE_LONG;
658 rbind[0].is_unsigned = 1;
659 rbind[0].buffer = dhtkeyuid;
660 GNUNET_CRYPTO_hash_to_enc (key, &encKey);
661 k_len = strlen ((char *) &encKey);
662
663 if ((GNUNET_OK !=
664 prepared_statement_run_select (get_dhtkeyuid,
665 1,
666 rbind,
667 return_ok, NULL,
668 MYSQL_TYPE_VAR_STRING,
669 &encKey,
670 max_varchar_len,
671 &k_len,
672 MYSQL_TYPE_LONGLONG,
673 &current_trial,
674 GNUNET_YES, -1)))
675 {
676 return GNUNET_SYSERR;
677 }
678
679 return GNUNET_OK;
680}
555 681
556/** 682/**
557 * Run a prepared statement that does NOT produce results. 683 * Run a prepared statement that does NOT produce results.
@@ -711,38 +837,6 @@ add_trial (unsigned long long *trialuid, int num_nodes, int topology,
711 return GNUNET_OK; 837 return GNUNET_OK;
712} 838}
713 839
714static int
715get_dhtkey_uid (unsigned long long *dhtkeyuid, const GNUNET_HashCode * key)
716{
717 MYSQL_BIND rbind[1];
718 struct GNUNET_CRYPTO_HashAsciiEncoded encKey;
719 unsigned long long k_len;
720 memset (rbind, 0, sizeof (rbind));
721 rbind[0].buffer_type = MYSQL_TYPE_LONG;
722 rbind[0].is_unsigned = 1;
723 rbind[0].buffer = dhtkeyuid;
724 GNUNET_CRYPTO_hash_to_enc (key, &encKey);
725 k_len = strlen ((char *) &encKey);
726
727 if ((GNUNET_OK !=
728 prepared_statement_run_select (get_dhtkeyuid,
729 1,
730 rbind,
731 return_ok, NULL,
732 MYSQL_TYPE_VAR_STRING,
733 &encKey,
734 max_varchar_len,
735 &k_len,
736 MYSQL_TYPE_LONGLONG,
737 &current_trial,
738 GNUNET_YES, -1)))
739 {
740 return GNUNET_SYSERR;
741 }
742
743 return GNUNET_OK;
744}
745
746/* 840/*
747 * Inserts the specified dhtkey into the dhttests.dhtkeys table, 841 * Inserts the specified dhtkey into the dhttests.dhtkeys table,
748 * stores return value of dhttests.dhtkeys.dhtkeyuid into dhtkeyuid 842 * stores return value of dhttests.dhtkeys.dhtkeyuid into dhtkeyuid
@@ -798,43 +892,6 @@ add_dhtkey (unsigned long long *dhtkeyuid, const GNUNET_HashCode * dhtkey)
798} 892}
799 893
800 894
801static int
802get_node_uid (unsigned long long *nodeuid, const GNUNET_HashCode * peerHash)
803{
804 MYSQL_BIND rbind[1];
805 struct GNUNET_CRYPTO_HashAsciiEncoded encPeer;
806 unsigned long long p_len;
807
808 int ret;
809 memset (rbind, 0, sizeof (rbind));
810 rbind[0].buffer_type = MYSQL_TYPE_LONG;
811 rbind[0].buffer = nodeuid;
812 rbind[0].is_unsigned = GNUNET_YES;
813
814 GNUNET_CRYPTO_hash_to_enc (peerHash, &encPeer);
815 p_len = strlen ((char *) &encPeer);
816
817 if (1 != (ret = prepared_statement_run_select (get_nodeuid,
818 1,
819 rbind,
820 return_ok,
821 NULL,
822 MYSQL_TYPE_LONG,
823 &current_trial,
824 GNUNET_YES,
825 MYSQL_TYPE_VAR_STRING,
826 &encPeer,
827 max_varchar_len,
828 &p_len, -1)))
829 {
830#if DEBUG_DHTLOG
831 fprintf (stderr, "FAILED\n");
832#endif
833 return GNUNET_SYSERR;
834 }
835 return GNUNET_OK;
836}
837
838 895
839/* 896/*
840 * Inserts the specified node into the dhttests.nodes table 897 * Inserts the specified node into the dhttests.nodes table
@@ -1152,6 +1209,125 @@ add_route (unsigned long long *sqlqueryuid, unsigned long long queryid,
1152} 1209}
1153 1210
1154/* 1211/*
1212 * Update dhttests.topology table with total connections information
1213 *
1214 * @param totalConnections the number of connections
1215 *
1216 * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
1217 */
1218int
1219update_current_topology (unsigned int connections)
1220{
1221 int ret;
1222 unsigned long long topologyuid;
1223
1224 get_current_topology(&topologyuid);
1225
1226 if (GNUNET_OK !=
1227 (ret = prepared_statement_run (update_topology,
1228 NULL,
1229 MYSQL_TYPE_LONG,
1230 &connections,
1231 GNUNET_YES,
1232 MYSQL_TYPE_LONGLONG,
1233 &topologyuid, GNUNET_YES, -1)))
1234 {
1235 if (ret == GNUNET_SYSERR)
1236 {
1237 return GNUNET_SYSERR;
1238 }
1239 }
1240 if (ret > 0)
1241 return GNUNET_OK;
1242 else
1243 return GNUNET_SYSERR;
1244 return GNUNET_OK;
1245}
1246
1247/*
1248 * Records the current topology (number of connections, time, trial)
1249 *
1250 * @param num_connections how many connections are in the topology
1251 *
1252 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1253 */
1254int
1255add_topology (int num_connections)
1256{
1257 int ret;
1258
1259 if (GNUNET_OK !=
1260 (ret = prepared_statement_run (insert_topology,
1261 NULL,
1262 MYSQL_TYPE_LONGLONG,
1263 &current_trial,
1264 GNUNET_YES,
1265 MYSQL_TYPE_LONG,
1266 &num_connections,
1267 GNUNET_YES, -1)))
1268 {
1269 if (ret == GNUNET_SYSERR)
1270 {
1271 return GNUNET_SYSERR;
1272 }
1273 }
1274 if (ret > 0)
1275 return GNUNET_OK;
1276 else
1277 return GNUNET_SYSERR;
1278 return GNUNET_OK;
1279}
1280
1281/*
1282 * Records a connection between two peers in the current topology
1283 *
1284 * @param first one side of the connection
1285 * @param second other side of the connection
1286 *
1287 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1288 */
1289int
1290add_extended_topology (struct GNUNET_PeerIdentity *first, struct GNUNET_PeerIdentity *second)
1291{
1292 int ret;
1293 unsigned long long first_uid;
1294 unsigned long long second_uid;
1295 unsigned long long topologyuid;
1296
1297 if (GNUNET_OK != get_current_topology(&topologyuid))
1298 return GNUNET_SYSERR;
1299 if (GNUNET_OK != get_node_uid(&first_uid, &first->hashPubKey))
1300 return GNUNET_SYSERR;
1301 if (GNUNET_OK != get_node_uid(&second_uid, &second->hashPubKey))
1302 return GNUNET_SYSERR;
1303
1304 if (GNUNET_OK !=
1305 (ret = prepared_statement_run (extend_topology,
1306 NULL,
1307 MYSQL_TYPE_LONGLONG,
1308 &topologyuid,
1309 GNUNET_YES,
1310 MYSQL_TYPE_LONGLONG,
1311 &first_uid,
1312 GNUNET_YES,
1313 MYSQL_TYPE_LONGLONG,
1314 &second_uid,
1315 GNUNET_YES,-1)))
1316 {
1317 if (ret == GNUNET_SYSERR)
1318 {
1319 return GNUNET_SYSERR;
1320 }
1321 }
1322 if (ret > 0)
1323 return GNUNET_OK;
1324 else
1325 return GNUNET_SYSERR;
1326 return GNUNET_OK;
1327}
1328
1329
1330/*
1155 * Provides the dhtlog api 1331 * Provides the dhtlog api
1156 * 1332 *
1157 * @param c the configuration to use to connect to a server 1333 * @param c the configuration to use to connect to a server
@@ -1215,6 +1391,9 @@ libgnunet_plugin_dhtlog_mysql_init (void * cls)
1215 plugin->dhtlog_api->insert_node = &add_node; 1391 plugin->dhtlog_api->insert_node = &add_node;
1216 plugin->dhtlog_api->insert_dhtkey = &add_dhtkey; 1392 plugin->dhtlog_api->insert_dhtkey = &add_dhtkey;
1217 plugin->dhtlog_api->update_connections = &add_connections; 1393 plugin->dhtlog_api->update_connections = &add_connections;
1394 plugin->dhtlog_api->insert_topology = &add_topology;
1395 plugin->dhtlog_api->update_topology = &update_current_topology;
1396 plugin->dhtlog_api->insert_extended_topology = &add_extended_topology;
1218 get_current_trial (&current_trial); 1397 get_current_trial (&current_trial);
1219 1398
1220 return NULL; 1399 return NULL;
@@ -1242,6 +1421,10 @@ libgnunet_plugin_dhtlog_mysql_done (void * cls)
1242 prepared_statement_close(get_nodeuid); 1421 prepared_statement_close(get_nodeuid);
1243 prepared_statement_close(update_connection); 1422 prepared_statement_close(update_connection);
1244 prepared_statement_close(get_trial); 1423 prepared_statement_close(get_trial);
1424 prepared_statement_close(get_topology);
1425 prepared_statement_close(insert_topology);
1426 prepared_statement_close(update_topology);
1427 prepared_statement_close(extend_topology);
1245 1428
1246 if (conn != NULL) 1429 if (conn != NULL)
1247 mysql_close (conn); 1430 mysql_close (conn);