aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-transport.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/gnunet-transport.c')
-rw-r--r--src/transport/gnunet-transport.c286
1 files changed, 77 insertions, 209 deletions
diff --git a/src/transport/gnunet-transport.c b/src/transport/gnunet-transport.c
index 4feb77398..f8d3e26d8 100644
--- a/src/transport/gnunet-transport.c
+++ b/src/transport/gnunet-transport.c
@@ -46,7 +46,7 @@
46#define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) 46#define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 47
48/** 48/**
49 * Timeout for an operations 49 * Timeout for an operation
50 */ 50 */
51#define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) 51#define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
52 52
@@ -271,14 +271,9 @@ static struct GNUNET_TRANSPORT_Handle *handle;
271static struct GNUNET_CONFIGURATION_Handle *cfg; 271static struct GNUNET_CONFIGURATION_Handle *cfg;
272 272
273/** 273/**
274 * Try connect handle 274 * Blacklisting handle
275 */ 275 */
276struct GNUNET_TRANSPORT_TryConnectHandle *tc_handle; 276struct GNUNET_TRANSPORT_Blacklist *blacklist;
277
278/**
279 * Try disconnect handle
280 */
281struct GNUNET_TRANSPORT_TryDisconnectHandle *td_handle;
282 277
283/** 278/**
284 * Option -s. 279 * Option -s.
@@ -336,14 +331,9 @@ static int monitor_validation;
336static int monitor_plugins; 331static int monitor_plugins;
337 332
338/** 333/**
339 * Option -C.
340 */
341static int try_connect;
342
343/**
344 * Option -D. 334 * Option -D.
345 */ 335 */
346static int try_disconnect; 336static int do_disconnect;
347 337
348/** 338/**
349 * Option -n. 339 * Option -n.
@@ -519,11 +509,6 @@ shutdown_task (void *cls,
519 GNUNET_SCHEDULER_cancel (op_timeout); 509 GNUNET_SCHEDULER_cancel (op_timeout);
520 op_timeout = NULL; 510 op_timeout = NULL;
521 } 511 }
522 if (NULL != tc_handle)
523 {
524 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
525 tc_handle = NULL;
526 }
527 if (NULL != pic) 512 if (NULL != pic)
528 { 513 {
529 GNUNET_TRANSPORT_monitor_peers_cancel (pic); 514 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
@@ -605,17 +590,27 @@ shutdown_task (void *cls,
605 GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins); 590 GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
606 monitored_plugins = NULL; 591 monitored_plugins = NULL;
607 } 592 }
593 if (NULL != blacklist)
594 {
595 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
596 blacklist = NULL;
597 ret = 0;
598 }
608} 599}
609 600
610 601
602/**
603 * We are done, shut down.
604 */
611static void 605static void
612operation_timeout (void *cls, 606operation_timeout (void *cls,
613 const struct GNUNET_SCHEDULER_TaskContext *tc) 607 const struct GNUNET_SCHEDULER_TaskContext *tc)
614{ 608{
615 struct PeerResolutionContext *cur; 609 struct PeerResolutionContext *cur;
616 struct PeerResolutionContext *next; 610 struct PeerResolutionContext *next;
611
617 op_timeout = NULL; 612 op_timeout = NULL;
618 if ((try_connect) || (benchmark_send) || (benchmark_receive)) 613 if ((benchmark_send) || (benchmark_receive))
619 { 614 {
620 FPRINTF (stdout, 615 FPRINTF (stdout,
621 _("Failed to connect to `%s'\n"), 616 _("Failed to connect to `%s'\n"),
@@ -1108,24 +1103,6 @@ notify_connect (void *cls,
1108 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity))) 1103 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1109 return; 1104 return;
1110 ret = 0; 1105 ret = 0;
1111 if (try_connect)
1112 {
1113 /* all done, terminate instantly */
1114 FPRINTF (stdout, _("Successfully connected to `%s'\n"),
1115 GNUNET_i2s_full (peer));
1116 ret = 0;
1117
1118 if (NULL != op_timeout)
1119 {
1120 GNUNET_SCHEDULER_cancel (op_timeout);
1121 op_timeout = NULL;
1122 }
1123
1124 if (NULL != end)
1125 GNUNET_SCHEDULER_cancel (end);
1126 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1127 return;
1128 }
1129 if (benchmark_send) 1106 if (benchmark_send)
1130 { 1107 {
1131 if (NULL != op_timeout) 1108 if (NULL != op_timeout)
@@ -1165,25 +1142,6 @@ notify_disconnect (void *cls,
1165 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity))) 1142 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1166 return; 1143 return;
1167 1144
1168 if (try_disconnect)
1169 {
1170 /* all done, terminate instantly */
1171 FPRINTF (stdout, _("Successfully disconnected from `%s'\n"),
1172 GNUNET_i2s_full (peer));
1173 ret = 0;
1174
1175 if (NULL != op_timeout)
1176 {
1177 GNUNET_SCHEDULER_cancel (op_timeout);
1178 op_timeout = NULL;
1179 }
1180
1181 if (NULL != end)
1182 GNUNET_SCHEDULER_cancel (end);
1183 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1184 return;
1185 }
1186
1187 if (NULL != th) 1145 if (NULL != th)
1188 { 1146 {
1189 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 1147 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
@@ -1425,7 +1383,7 @@ process_peer_string (void *cls,
1425 op_timeout = NULL; 1383 op_timeout = NULL;
1426 } 1384 }
1427 ret = 0; 1385 ret = 0;
1428 end = GNUNET_SCHEDULER_add_now (&shutdown_task, 1386 end = GNUNET_SCHEDULER_add_now (&shutdown_task,
1429 NULL); 1387 NULL);
1430 } 1388 }
1431} 1389}
@@ -1450,7 +1408,7 @@ resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1450 struct PeerResolutionContext *rc; 1408 struct PeerResolutionContext *rc;
1451 1409
1452 rc = GNUNET_new (struct PeerResolutionContext); 1410 rc = GNUNET_new (struct PeerResolutionContext);
1453 GNUNET_CONTAINER_DLL_insert (rc_head, 1411 GNUNET_CONTAINER_DLL_insert (rc_head,
1454 rc_tail, 1412 rc_tail,
1455 rc); 1413 rc);
1456 address_resolutions++; 1414 address_resolutions++;
@@ -1512,12 +1470,12 @@ process_peer_iteration_cb (void *cls,
1512 address->transport_name); 1470 address->transport_name);
1513 1471
1514 if (NULL != address) 1472 if (NULL != address)
1515 resolve_peer_address (address, 1473 resolve_peer_address (address,
1516 numeric, 1474 numeric,
1517 state, 1475 state,
1518 state_timeout); 1476 state_timeout);
1519 else 1477 else
1520 print_info (peer, 1478 print_info (peer,
1521 NULL, 1479 NULL,
1522 NULL, 1480 NULL,
1523 state, 1481 state,
@@ -1751,7 +1709,7 @@ process_peer_monitoring_cb (void *cls,
1751 &operation_timeout, 1709 &operation_timeout,
1752 NULL); 1710 NULL);
1753 1711
1754 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, 1712 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1755 peer))) 1713 peer)))
1756 { 1714 {
1757 m = GNUNET_new (struct MonitoredPeer); 1715 m = GNUNET_new (struct MonitoredPeer);
@@ -1770,7 +1728,7 @@ process_peer_monitoring_cb (void *cls,
1770 return; /* No real change */ 1728 return; /* No real change */
1771 } 1729 }
1772 if ( (m->state == state) && 1730 if ( (m->state == state) &&
1773 (NULL != address) && 1731 (NULL != address) &&
1774 (NULL != m->address) && 1732 (NULL != m->address) &&
1775 (0 == GNUNET_HELLO_address_cmp(m->address, address)) ) 1733 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1776 return; /* No real change */ 1734 return; /* No real change */
@@ -1801,73 +1759,23 @@ process_peer_monitoring_cb (void *cls,
1801 1759
1802 1760
1803/** 1761/**
1804 * Function called with our result of trying to connect to a peer 1762 * Function called with the transport service checking if we
1805 * using the transport service. Will retry 10 times, and if we still 1763 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1806 * fail to connect terminate with an error message. 1764 * peer that we should disconnect from.
1807 *
1808 * @param cls NULL
1809 * @param result #GNUNET_OK if we connected to the service
1810 */
1811static void
1812try_connect_cb (void *cls,
1813 const int result)
1814{
1815 static int retries = 0;
1816
1817 tc_handle = NULL;
1818 if (GNUNET_OK == result)
1819 return;
1820 retries++;
1821 if (retries < 10)
1822 {
1823 tc_handle = GNUNET_TRANSPORT_try_connect (handle,
1824 &pid,
1825 &try_connect_cb,
1826 NULL);
1827 return;
1828 }
1829 FPRINTF (stderr,
1830 "%s",
1831 _("Failed to send connect request to transport service\n"));
1832 if (NULL != end)
1833 GNUNET_SCHEDULER_cancel (end);
1834 ret = 1;
1835 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1836}
1837
1838
1839/**
1840 * Function called with our result of trying to disconnect a peer
1841 * using the transport service. Will retry 10 times, and if we still
1842 * fail to disconnect, terminate with an error message.
1843 * 1765 *
1844 * @param cls NULL 1766 * @param cls NULL
1845 * @param result #GNUNET_OK if we connected to the service 1767 * @param cpid peer to check blacklisting for
1768 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1846 */ 1769 */
1847static void 1770static int
1848try_disconnect_cb (void *cls, 1771blacklist_cb (void *cls,
1849 const int result) 1772 const struct GNUNET_PeerIdentity *cpid)
1850{ 1773{
1851 static int retries = 0; 1774 if (0 == memcmp (cpid,
1852 1775 &pid,
1853 td_handle = NULL; 1776 sizeof (struct GNUNET_PeerIdentity)))
1854 if (GNUNET_OK == result) 1777 return GNUNET_SYSERR;
1855 return; 1778 return GNUNET_OK;
1856 retries++;
1857 if (retries < 10)
1858 {
1859 td_handle = GNUNET_TRANSPORT_try_disconnect (handle,
1860 &pid,
1861 &try_disconnect_cb,
1862 NULL);
1863 return;
1864 }
1865 FPRINTF (stderr, "%s",
1866 _("Failed to send disconnect request to transport service\n"));
1867 if (NULL != end)
1868 GNUNET_SCHEDULER_cancel (end);
1869 ret = 1;
1870 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1871} 1779}
1872 1780
1873 1781
@@ -1891,24 +1799,27 @@ testservice_task (void *cls,
1891 return; 1799 return;
1892 } 1800 }
1893 1801
1894 if ((NULL != cpid) 1802 if ( (NULL != cpid) &&
1895 && (GNUNET_OK 1803 (GNUNET_OK !=
1896 != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid), 1804 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1897 &pid.public_key))) 1805 strlen (cpid),
1806 &pid.public_key)))
1898 { 1807 {
1899 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid); 1808 FPRINTF (stderr,
1809 _("Failed to parse peer identity `%s'\n"),
1810 cpid);
1900 return; 1811 return;
1901 } 1812 }
1902 1813
1903 counter = benchmark_send + benchmark_receive + iterate_connections 1814 counter = benchmark_send + benchmark_receive + iterate_connections
1904 + monitor_connections + monitor_connects + try_connect + try_disconnect + 1815 + monitor_connections + monitor_connects + do_disconnect +
1905 + iterate_validation + monitor_validation + monitor_plugins; 1816 + iterate_validation + monitor_validation + monitor_plugins;
1906 1817
1907 if (1 < counter) 1818 if (1 < counter)
1908 { 1819 {
1909 FPRINTF (stderr, 1820 FPRINTF (stderr,
1910 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"), 1821 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1911 "connect", "benchmark send", "benchmark receive", "information", 1822 "disconnect", "benchmark send", "benchmark receive", "information",
1912 "monitor", "events", "plugins"); 1823 "monitor", "events", "plugins");
1913 return; 1824 return;
1914 } 1825 }
@@ -1916,71 +1827,35 @@ testservice_task (void *cls,
1916 { 1827 {
1917 FPRINTF (stderr, 1828 FPRINTF (stderr,
1918 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"), 1829 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1919 "connect", "benchmark send", "benchmark receive", "information", 1830 "disconnect", "benchmark send", "benchmark receive", "information",
1920 "monitor", "events", "plugins"); 1831 "monitor", "events", "plugins");
1921 return; 1832 return;
1922 } 1833 }
1923 1834
1924 if (try_connect) /* -C: Connect to peer */ 1835 if (do_disconnect) /* -D: Disconnect from peer */
1925 { 1836 {
1926 if (NULL == cpid) 1837 if (NULL == cpid)
1927 { 1838 {
1928 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"), 1839 FPRINTF (stderr,
1929 "-C", "-p"); 1840 _("Option `%s' makes no sense without option `%s'.\n"),
1930 ret = 1; 1841 "-D", "-p");
1931 return;
1932 }
1933 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
1934 &notify_connect, &notify_disconnect);
1935 if (NULL == handle)
1936 {
1937 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1938 ret = 1;
1939 return;
1940 }
1941 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1942 NULL);
1943 if (NULL == tc_handle)
1944 {
1945 FPRINTF (stderr, "%s",
1946 _("Failed to send request to transport service\n"));
1947 ret = 1;
1948 return;
1949 }
1950 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1951 NULL);
1952
1953 }
1954 else if (try_disconnect) /* -D: Disconnect from peer */
1955 {
1956 if (NULL == cpid)
1957 {
1958 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1959 "-D", "-p");
1960 ret = 1;
1961 return;
1962 }
1963 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive,
1964 &notify_connect, &notify_disconnect);
1965 if (NULL == handle)
1966 {
1967 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1968 ret = 1; 1842 ret = 1;
1969 return; 1843 return;
1970 } 1844 }
1971 td_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, 1845 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1972 &try_disconnect_cb, 1846 &blacklist_cb,
1973 NULL); 1847 NULL);
1974 if (NULL == td_handle) 1848 if (NULL == blacklist)
1975 { 1849 {
1976 FPRINTF (stderr, "%s", 1850 FPRINTF (stderr,
1977 _("Failed to send request to transport service\n")); 1851 "%s",
1852 _("Failed to connect to transport service for disconnection\n"));
1978 ret = 1; 1853 ret = 1;
1979 return; 1854 return;
1980 } 1855 }
1981 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, 1856 FPRINTF (stdout,
1982 NULL); 1857 "%s",
1983 1858 _("Blacklisting request in place, stop with CTRL-C\n"));
1984 } 1859 }
1985 else if (benchmark_send) /* -s: Benchmark sending */ 1860 else if (benchmark_send) /* -s: Benchmark sending */
1986 { 1861 {
@@ -1991,7 +1866,9 @@ testservice_task (void *cls,
1991 ret = 1; 1866 ret = 1;
1992 return; 1867 return;
1993 } 1868 }
1994 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, 1869 handle = GNUNET_TRANSPORT_connect (cfg,
1870 NULL,
1871 NULL,
1995 &notify_receive, 1872 &notify_receive,
1996 &notify_connect, 1873 &notify_connect,
1997 &notify_disconnect); 1874 &notify_disconnect);
@@ -2001,17 +1878,6 @@ testservice_task (void *cls,
2001 ret = 1; 1878 ret = 1;
2002 return; 1879 return;
2003 } 1880 }
2004 tc_handle = GNUNET_TRANSPORT_try_connect (handle,
2005 &pid,
2006 &try_connect_cb,
2007 NULL);
2008 if (NULL == tc_handle)
2009 {
2010 FPRINTF (stderr, "%s",
2011 _("Failed to send request to transport service\n"));
2012 ret = 1;
2013 return;
2014 }
2015 start_time = GNUNET_TIME_absolute_get (); 1881 start_time = GNUNET_TIME_absolute_get ();
2016 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, 1882 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
2017 &operation_timeout, 1883 &operation_timeout,
@@ -2019,8 +1885,12 @@ testservice_task (void *cls,
2019 } 1885 }
2020 else if (benchmark_receive) /* -b: Benchmark receiving */ 1886 else if (benchmark_receive) /* -b: Benchmark receiving */
2021 { 1887 {
2022 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, &notify_receive, NULL, 1888 handle = GNUNET_TRANSPORT_connect (cfg,
2023 NULL); 1889 NULL,
1890 NULL,
1891 &notify_receive,
1892 NULL,
1893 NULL);
2024 if (NULL == handle) 1894 if (NULL == handle)
2025 { 1895 {
2026 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n")); 1896 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
@@ -2037,17 +1907,18 @@ testservice_task (void *cls,
2037 address_resolution_in_progress = GNUNET_YES; 1907 address_resolution_in_progress = GNUNET_YES;
2038 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid, 1908 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
2039 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg); 1909 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
2040 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, 1910 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
2041 NULL); 1911 &operation_timeout,
1912 NULL);
2042 } 1913 }
2043 else if (monitor_connections) /* -m: List information about peers continuously */ 1914 else if (monitor_connections) /* -m: List information about peers continuously */
2044 { 1915 {
2045 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, 1916 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
2046 GNUNET_NO); 1917 GNUNET_NO);
2047 address_resolution_in_progress = GNUNET_YES; 1918 address_resolution_in_progress = GNUNET_YES;
2048 pic = GNUNET_TRANSPORT_monitor_peers (cfg, 1919 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
2049 (NULL == cpid) ? NULL : &pid, 1920 (NULL == cpid) ? NULL : &pid,
2050 GNUNET_NO, 1921 GNUNET_NO,
2051 TIMEOUT, 1922 TIMEOUT,
2052 &process_peer_monitoring_cb, NULL); 1923 &process_peer_monitoring_cb, NULL);
2053 } 1924 }
@@ -2135,12 +2006,9 @@ main (int argc, char * const *argv)
2135 { 'b', "benchmark", NULL, 2006 { 'b', "benchmark", NULL,
2136 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"), 2007 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
2137 0, &GNUNET_GETOPT_set_one, &benchmark_receive }, 2008 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
2138 { 'C', "connect",
2139 NULL, gettext_noop ("connect to a peer"), 0,
2140 &GNUNET_GETOPT_set_one, &try_connect },
2141 { 'D', "disconnect", 2009 { 'D', "disconnect",
2142 NULL, gettext_noop ("disconnect to a peer"), 0, 2010 NULL, gettext_noop ("disconnect to a peer"), 0,
2143 &GNUNET_GETOPT_set_one, &try_disconnect }, 2011 &GNUNET_GETOPT_set_one, &do_disconnect },
2144 { 'd', "validation", NULL, 2012 { 'd', "validation", NULL,
2145 gettext_noop ("print information for all pending validations "), 2013 gettext_noop ("print information for all pending validations "),
2146 0, &GNUNET_GETOPT_set_one, &iterate_validation }, 2014 0, &GNUNET_GETOPT_set_one, &iterate_validation },