aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2013-04-01 22:03:31 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2013-04-01 22:03:31 +0000
commitedbee780876a4b76576714c2b97f9cd6fed7ee71 (patch)
tree1dd8a3b7fdcf4b244c772563111987f234aa547b /src
parent7cba739408a3959da9096dc50cabf51fe0accd41 (diff)
downloadgnunet-edbee780876a4b76576714c2b97f9cd6fed7ee71.tar.gz
gnunet-edbee780876a4b76576714c2b97f9cd6fed7ee71.zip
- release ports while destroying peer
Diffstat (limited to 'src')
-rw-r--r--src/testing/testing.c141
1 files changed, 102 insertions, 39 deletions
diff --git a/src/testing/testing.c b/src/testing/testing.c
index 9d2c69ab7..c8f857a05 100644
--- a/src/testing/testing.c
+++ b/src/testing/testing.c
@@ -212,6 +212,18 @@ struct GNUNET_TESTING_Peer
212 struct GNUNET_PeerIdentity *id; 212 struct GNUNET_PeerIdentity *id;
213 213
214 /** 214 /**
215 * Array of ports currently allocated to this peer. These ports will be
216 * released upon peer destroy and can be used by other peers which are
217 * configured after.
218 */
219 uint16_t *ports;
220
221 /**
222 * The number of ports in the above array
223 */
224 unsigned int nports;
225
226 /**
215 * The keynumber of this peer's hostkey 227 * The keynumber of this peer's hostkey
216 */ 228 */
217 uint32_t key_number; 229 uint32_t key_number;
@@ -595,6 +607,18 @@ struct UpdateContext
595 char *service_home; 607 char *service_home;
596 608
597 /** 609 /**
610 * Array of ports currently allocated to this peer. These ports will be
611 * released upon peer destroy and can be used by other peers which are
612 * configured after.
613 */
614 uint16_t *ports;
615
616 /**
617 * The number of ports in the above array
618 */
619 unsigned int nports;
620
621 /**
598 * build status - to signal error while building a configuration 622 * build status - to signal error while building a configuration
599 */ 623 */
600 int status; 624 int status;
@@ -650,6 +674,7 @@ update_config (void *cls, const char *section, const char *option,
650 } 674 }
651 GNUNET_snprintf (cval, sizeof (cval), "%u", new_port); 675 GNUNET_snprintf (cval, sizeof (cval), "%u", new_port);
652 value = cval; 676 value = cval;
677 GNUNET_array_append (uc->ports, uc->nports, new_port);
653 } 678 }
654 else if ((ival != 0) && 679 else if ((ival != 0) &&
655 (GNUNET_YES == 680 (GNUNET_YES ==
@@ -811,9 +836,11 @@ update_config_sections (void *cls,
811 * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will 836 * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
812 * be incomplete and should not be used there upon 837 * be incomplete and should not be used there upon
813 */ 838 */
814int 839static int
815GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system, 840GNUNET_TESTING_configuration_create_ (struct GNUNET_TESTING_System *system,
816 struct GNUNET_CONFIGURATION_Handle *cfg) 841 struct GNUNET_CONFIGURATION_Handle *cfg,
842 uint16_t **ports,
843 unsigned int *nports)
817{ 844{
818 struct UpdateContext uc; 845 struct UpdateContext uc;
819 char *default_config; 846 char *default_config;
@@ -821,6 +848,8 @@ GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
821 uc.system = system; 848 uc.system = system;
822 uc.cfg = cfg; 849 uc.cfg = cfg;
823 uc.status = GNUNET_OK; 850 uc.status = GNUNET_OK;
851 uc.ports = NULL;
852 uc.nports = 0;
824 GNUNET_asprintf (&uc.service_home, "%s/%u", system->tmppath, 853 GNUNET_asprintf (&uc.service_home, "%s/%u", system->tmppath,
825 system->path_counter++); 854 system->path_counter++);
826 GNUNET_asprintf (&default_config, "%s/config", uc.service_home); 855 GNUNET_asprintf (&default_config, "%s/config", uc.service_home);
@@ -840,11 +869,42 @@ GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
840 "nat", 869 "nat",
841 "USE_LOCALADDR", "YES"); 870 "USE_LOCALADDR", "YES");
842 GNUNET_free (uc.service_home); 871 GNUNET_free (uc.service_home);
872 if ((NULL != ports) && (NULL != nports))
873 {
874 *ports = uc.ports;
875 *nports = uc.nports;
876 }
877 else
878 GNUNET_free_non_null (uc.ports);
843 return uc.status; 879 return uc.status;
844} 880}
845 881
846 882
847/** 883/**
884 * Create a new configuration using the given configuration as a template;
885 * ports and paths will be modified to select available ports on the local
886 * system. The default configuration will be available in PATHS section under
887 * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
888 * section to the temporary directory specific to this configuration. If we run
889 * out of "*port" numbers, return SYSERR.
890 *
891 * This is primarily a helper function used internally
892 * by 'GNUNET_TESTING_peer_configure'.
893 *
894 * @param system system to use to coordinate resource usage
895 * @param cfg template configuration to update
896 * @return GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will
897 * be incomplete and should not be used there upon
898 */
899int
900GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
901 struct GNUNET_CONFIGURATION_Handle *cfg)
902{
903 return GNUNET_TESTING_configuration_create_ (system, cfg, NULL, NULL);
904}
905
906
907/**
848 * Configure a GNUnet peer. GNUnet must be installed on the local 908 * Configure a GNUnet peer. GNUnet must be installed on the local
849 * system and available in the PATH. 909 * system and available in the PATH.
850 * 910 *
@@ -872,31 +932,27 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
872 char *libexec_binary; 932 char *libexec_binary;
873 char *emsg_; 933 char *emsg_;
874 struct GNUNET_CRYPTO_EccPrivateKey *pk; 934 struct GNUNET_CRYPTO_EccPrivateKey *pk;
935 uint16_t *ports;
936 unsigned int nports;
875 937
938 ports = NULL;
939 nports = 0;
876 if (NULL != emsg) 940 if (NULL != emsg)
877 *emsg = NULL; 941 *emsg = NULL;
878 if (GNUNET_OK != GNUNET_TESTING_configuration_create (system, cfg)) 942 if (GNUNET_OK != GNUNET_TESTING_configuration_create_ (system, cfg,
943 &ports, &nports))
879 { 944 {
880 GNUNET_asprintf (&emsg_, 945 GNUNET_asprintf (&emsg_,
881 _("Failed to create configuration for peer (not enough free ports?)\n")); 946 _("Failed to create configuration for peer "
882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_); 947 "(not enough free ports?)\n"));
883 if (NULL != emsg) 948 goto err_ret;
884 *emsg = emsg_; 949 }
885 else
886 GNUNET_free (emsg_);
887 return NULL;
888 }
889 if (key_number >= system->total_hostkeys) 950 if (key_number >= system->total_hostkeys)
890 { 951 {
891 GNUNET_asprintf (&emsg_, 952 GNUNET_asprintf (&emsg_,
892 _("You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"), 953 _("You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"),
893 (unsigned int) system->total_hostkeys); 954 (unsigned int) system->total_hostkeys);
894 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_); 955 goto err_ret;
895 if (NULL != emsg)
896 *emsg = emsg_;
897 else
898 GNUNET_free (emsg_);
899 return NULL;
900 } 956 }
901 pk = NULL; 957 pk = NULL;
902 if ((NULL != id) && 958 if ((NULL != id) &&
@@ -905,12 +961,7 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
905 GNUNET_asprintf (&emsg_, 961 GNUNET_asprintf (&emsg_,
906 _("Failed to initialize hostkey for peer %u\n"), 962 _("Failed to initialize hostkey for peer %u\n"),
907 (unsigned int) key_number); 963 (unsigned int) key_number);
908 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_); 964 goto err_ret;
909 if (NULL != emsg)
910 *emsg = emsg_;
911 else
912 GNUNET_free (emsg_);
913 return NULL;
914 } 965 }
915 if (NULL != pk) 966 if (NULL != pk)
916 GNUNET_CRYPTO_ecc_key_free (pk); 967 GNUNET_CRYPTO_ecc_key_free (pk);
@@ -928,8 +979,9 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
928 | GNUNET_DISK_PERM_USER_WRITE); 979 | GNUNET_DISK_PERM_USER_WRITE);
929 if (NULL == fd) 980 if (NULL == fd)
930 { 981 {
931 GNUNET_break (0); 982 GNUNET_asprintf (&emsg_, _("Cannot open hostkey file: %s\n"),
932 return NULL; 983 STRERROR (errno));
984 goto err_ret;
933 } 985 }
934 if (GNUNET_TESTING_HOSTKEYFILESIZE != 986 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
935 GNUNET_DISK_file_write (fd, system->hostkeys_data 987 GNUNET_DISK_file_write (fd, system->hostkeys_data
@@ -940,13 +992,8 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
940 _("Failed to write hostkey file for peer %u: %s\n"), 992 _("Failed to write hostkey file for peer %u: %s\n"),
941 (unsigned int) key_number, 993 (unsigned int) key_number,
942 STRERROR (errno)); 994 STRERROR (errno));
943 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
944 if (NULL != emsg)
945 *emsg = emsg_;
946 else
947 GNUNET_free (emsg_);
948 GNUNET_DISK_file_close (fd); 995 GNUNET_DISK_file_close (fd);
949 return NULL; 996 goto err_ret;
950 } 997 }
951 GNUNET_DISK_file_close (fd); 998 GNUNET_DISK_file_close (fd);
952 GNUNET_assert (GNUNET_OK == 999 GNUNET_assert (GNUNET_OK ==
@@ -958,14 +1005,9 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
958 _("Failed to write configuration file `%s' for peer %u: %s\n"), 1005 _("Failed to write configuration file `%s' for peer %u: %s\n"),
959 config_filename, 1006 config_filename,
960 (unsigned int) key_number, 1007 (unsigned int) key_number,
961 STRERROR (errno)); 1008 STRERROR (errno));
962 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
963 if (NULL != emsg)
964 *emsg = emsg_;
965 else
966 GNUNET_free (emsg_);
967 GNUNET_free (config_filename); 1009 GNUNET_free (config_filename);
968 return NULL; 1010 goto err_ret;
969 } 1011 }
970 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Peer)); 1012 peer = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Peer));
971 peer->cfgfile = config_filename; /* Free in peer_destroy */ 1013 peer->cfgfile = config_filename; /* Free in peer_destroy */
@@ -982,7 +1024,18 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
982 peer->system = system; 1024 peer->system = system;
983 peer->key_number = key_number; 1025 peer->key_number = key_number;
984 GNUNET_free (libexec_binary); 1026 GNUNET_free (libexec_binary);
1027 peer->ports = ports; /* Free in peer_destroy */
1028 peer->nports = nports;
985 return peer; 1029 return peer;
1030
1031 err_ret:
1032 GNUNET_free_non_null (ports);
1033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
1034 if (NULL != emsg)
1035 *emsg = emsg_;
1036 else
1037 GNUNET_free (emsg_);
1038 return NULL;
986} 1039}
987 1040
988 1041
@@ -1233,6 +1286,8 @@ GNUNET_TESTING_peer_stop_async (struct GNUNET_TESTING_Peer *peer,
1233void 1286void
1234GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer) 1287GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer)
1235{ 1288{
1289 unsigned int cnt;
1290
1236 if (NULL != peer->main_process) 1291 if (NULL != peer->main_process)
1237 GNUNET_TESTING_peer_stop (peer); 1292 GNUNET_TESTING_peer_stop (peer);
1238 if (NULL != peer->ah) 1293 if (NULL != peer->ah)
@@ -1245,6 +1300,14 @@ GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer)
1245 GNUNET_free (peer->main_binary); 1300 GNUNET_free (peer->main_binary);
1246 GNUNET_free (peer->args); 1301 GNUNET_free (peer->args);
1247 GNUNET_free_non_null (peer->id); 1302 GNUNET_free_non_null (peer->id);
1303 if (NULL != peer->ports)
1304 {
1305 for (cnt = 0; cnt < peer->nports; cnt++)
1306 GNUNET_TESTING_release_port (peer->system,
1307 GNUNET_YES,
1308 peer->ports[cnt]);
1309 GNUNET_free (peer->ports);
1310 }
1248 GNUNET_free (peer); 1311 GNUNET_free (peer);
1249} 1312}
1250 1313