#include "gnunet_ats_transport_service.h"
#include "transport.h"
#include "gnunet_transport_communication_service.h"
-
+#include "gnunet_resolver_service.h"
/**
* How long do we believe our addresses to remain up (before
* the other peer should revalidate).
}
static struct sockaddr *
-tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int port, socklen_t *sock_len)
+tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port, socklen_t *sock_len)
{
struct sockaddr *in;
/* interpreting value as just a PORT number */
- if (port > UINT16_MAX)
+ if (*port > UINT16_MAX)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"BINDTO specification `%s' invalid: value too large for port\n",
i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
i4->sin_family = AF_INET;
- i4->sin_port = htons ((uint16_t) port);
+ i4->sin_port = htons ((uint16_t) *port);
#if HAVE_SOCKADDR_IN_SIN_LEN
i4->sin_len = sizeof(sizeof(struct sockaddr_in));
#endif
i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
i6->sin6_family = AF_INET6;
- i6->sin6_port = htons ((uint16_t) port);
+ i6->sin6_port = htons ((uint16_t) *port);
#if HAVE_SOCKADDR_IN_SIN_LEN
i6->sin6_len = sizeof(sizeof(struct sockaddr_in6));
#endif
extract_ipv6_address (char *cp)
{
- const char *start;
+ char *start;
start = cp;
if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
{
unsigned int port;
char dummy[2];
+
if (NULL != colon)
- {
+ {
/* interpet value after colon as port */
- *colon = '\0';
+ //colon = '\0';
colon++;
if (1 == sscanf (colon, "%u%1s", &port, dummy))
{
if (port > UINT16_MAX)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Port `%s' invalid: value too large for port\n",
+ "Port `%u' invalid: value too large for port\n",
port);
//GNUNET_free (cp);
- return NULL;
+ return 0;
}
}
else
GNUNET_ERROR_TYPE_ERROR,
"BINDTO specification invalid: last ':' not followed by number\n");
//GNUNET_free (cp);
- return NULL;
+ return 0;
}
}
else
tcp_address_to_sockaddr_numeric (socklen_t *sock_len, struct sockaddr_in v4, struct sockaddr_in6 v6, char *colon)
{
- unsigned int port;
+ unsigned int *port;
struct sockaddr *in;
- port = extract_port(colon);
+ *port = extract_port(colon);
if (NULL == port)
return NULL;
if (NULL != &v4.sin_addr)
{
v4.sin_family = AF_INET;
- v4.sin_port = htons ((uint16_t) port);
+ v4.sin_port = htons ((uint16_t) *port);
#if HAVE_SOCKADDR_IN_SIN_LEN
v4.sin_len = sizeof(struct sockaddr_in);
#endif
in = GNUNET_memdup (&v4, sizeof(v4));
*sock_len = sizeof(v4);
- //GNUNET_free (cp);
return in;
}
}
if (NULL != &v6.sin6_addr)
{
v6.sin6_family = AF_INET6;
- v6.sin6_port = htons ((uint16_t) port);
+ v6.sin6_port = htons ((uint16_t) *port);
#if HAVE_SOCKADDR_IN_SIN_LEN
v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
#endif
in = GNUNET_memdup (&v6, sizeof(v6));
*sock_len = sizeof(v6);
- //GNUNET_free (cp);
return in;
}
}
- //GNUNET_free (cp);
return NULL;
}
tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
{
struct sockaddr *in;
- unsigned int port;
+ unsigned int *port;
char dummy[2];
char *colon;
char *cp;
cp = GNUNET_strdup (bindto);
start = extract_ipv6_address (cp);
- if (1 == sscanf (bindto, "%u%1s", &port, dummy))
+ if (1 == sscanf (bindto, "%u%1s", port, dummy))
{
in = tcp_address_to_sockaddr_port_only (bindto, port, sock_len);
}
colon = strrchr (cp, ':');
in = tcp_address_to_sockaddr_numeric (&in_len, v4, v6, colon);
}
+ return in;
}
* invalid
*/
static int
-mq_init (void *cls, const struct GNUNET_PeerIdentity *peer,
- struct GNUNET_TRANSPORT_CommunicatorHandle *ch,
- const struct GNUNET_TRANSPORT_CreateQueue *cq)
+mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
{
struct Queue *queue;
const char *path;
struct sockaddr *in;
socklen_t in_len;
struct GNUNET_NETWORK_Handle *sock;
- const char *addr = (const char *) &cq[1];
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Connecting to %s\n", addr);
- if (0 != strncmp (addr,
+ "Connecting to %s\n", address);
+ if (0 != strncmp (address,
COMMUNICATOR_ADDRESS_PREFIX "-",
strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
- path = &addr[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
+ path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
in = tcp_address_to_sockaddr (path, &in_len);
+ if (NULL == in)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to setup TCP socket address\n");
+ return GNUNET_SYSERR;
+ }
+
+
sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
if (NULL == sock)
{
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"connect to `%s' failed: %s",
- addr,
+ address,
strerror (errno));
GNUNET_NETWORK_socket_close (sock);
GNUNET_free (in);
}
static void
-init_own_socket (void *cls, struct sockaddr *resolved_in, socklen_t in_len)
+init_own_socket (void *cls,
+ const struct sockaddr *addr,
+ socklen_t in_len)
{
struct sockaddr *in;
socklen_t sto_len;
struct sockaddr_in *v4;
struct sockaddr_in6 *v6;
- char ipstr[INET6_ADDRSTRLEN];
- unsigned int port = cls;
- if (NULL == resolved_in)
+ unsigned int *port = cls;
+
+ char out_string [128];
+ snprintf(out_string, 128, "%u", *port);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Setup TCP socket on port %s\n",
+ out_string);
+
+ if (NULL == addr)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Failed to setup TCP socket address\n");
- //TODO Where do we cancel the request
- //GNUNET_RESOLVER_request_cancel (resolve_request_handle);
- //TODO check where to free after moving code around
- //GNUNET_free (bindto);
+ "Failed to setup TCP socket address %s on port %s\n",
+ GNUNET_a2s (addr, in_len),
+ out_string);
return;
}
- inet_ntop(resolved_in->sa_family, resolved_in, ipstr, sizeof(ipstr));
-
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "address %s\n",
+ GNUNET_a2s (addr, in_len));
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "resolved address %s\n",
- ipstr);
+ "address %s\n",
+ GNUNET_a2s (addr, in_len));
listen_sock =
- GNUNET_NETWORK_socket_create (resolved_in->sa_family, SOCK_STREAM, IPPROTO_TCP);
+ GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
if (NULL == listen_sock)
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
- //GNUNET_RESOLVER_request_cancel (resolve_request_handle);
- GNUNET_free (in);
- //GNUNET_free (bindto);
+ GNUNET_free ((struct sockaddr *) addr);
return;
}
- if (AF_INET == resolved_in->sa_family)
+ if (AF_INET == addr->sa_family)
{
- v4 = GNUNET_new (struct sockaddr_in);
+ v4 = (struct sockaddr_in *)addr;
v4->sin_family = AF_INET;
- v4->sin_port = htons ((uint16_t) port);
+ v4->sin_port = htons ((uint16_t) *port);
#if HAVE_SOCKADDR_IN_SIN_LEN
v4->sin_len = sizeof(struct sockaddr_in);
#endif
in = GNUNET_memdup (v4, sizeof(v4));
- }else if (AF_INET6 == in->sa_family)
+ GNUNET_free (v4);
+ }else if (AF_INET6 == addr->sa_family)
{
- v6 = GNUNET_new (struct sockaddr_in6);
+ v6 = (struct sockaddr_in6 *)addr;
v6->sin6_family = AF_INET6;
- v6->sin6_port = htons ((uint16_t) port);
+ v6->sin6_port = htons ((uint16_t) *port);
#if HAVE_SOCKADDR_IN_SIN_LEN
v6->sin6_len = sizeof(sizeof(struct sockaddr_in6));
#endif
in = GNUNET_memdup (v6, sizeof(v6));
+ GNUNET_free (v6);
}
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "address %s\n",
+ GNUNET_a2s (in, in_len));
+
if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, in, in_len))
{
- //GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "bind", bindto);
- //GNUNET_RESOLVER_request_cancel (resolve_request_handle);
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
GNUNET_NETWORK_socket_close (listen_sock);
listen_sock = NULL;
GNUNET_free (in);
- //GNUNET_free (bindto);
return;
}
if (GNUNET_OK !=
GNUNET_NETWORK_socket_close (listen_sock);
listen_sock = NULL;
GNUNET_free (in);
- //GNUNET_free (bindto);
}
/* We might have bound to port 0, allowing the OS to figure it out;
thus, get the real IN-address from the socket */
GNUNET_SCHEDULER_shutdown ();
return;
}
+ //TODO Shall we register all addresses the resolver sends us. Them we have
+ //to call GNUNET_NAT_register, when the resolver signals resolving ended.
nat = GNUNET_NAT_register (cfg,
COMMUNICATOR_CONFIG_SECTION,
IPPROTO_TCP,
&nat_address_cb,
NULL /* FIXME: support reversal: #5529 */,
NULL /* closure */);
-
+ //TODO Remove this, if we like to handle more then one address.
+ if (NULL != resolve_request_handle)
+ GNUNET_RESOLVER_request_cancel (resolve_request_handle);
}
/**
if (1 == sscanf (bindto, "%u%1s", &port, dummy))
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We think it is port only\n");
- in = tcp_address_to_sockaddr_port_only (bindto, port, &in_len);
- init_own_socket (bindto, &in, &in_len);
+ in = tcp_address_to_sockaddr_port_only (bindto, &port, &in_len);
+ init_own_socket (&port, in, in_len);
}
else if (1 == inet_pton (AF_INET, cp, &v4.sin_addr) || 1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We think it is address and port\n");
colon = strrchr (cp, ':');
- in = tcp_address_to_sockaddr_numeric (&in_len, v4, v6, &colon);
- init_own_socket (bindto, &in, &in_len);
+ port = extract_port(colon);
+ in = tcp_address_to_sockaddr_numeric (&in_len, v4, v6, colon);
+ init_own_socket (&port, in, in_len);
}else
{
- colon = strrchr (cp, ':');
+
+ colon = strrchr (cp, ':');;
port = extract_port(colon);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We try to resolve\n");
+ printf("%u\n", port);
resolve_request_handle = GNUNET_RESOLVER_ip_get (cp,
AF_UNSPEC,
GNUNET_TIME_UNIT_MINUTES,
&init_own_socket,
- port);
+ &port);
}
+ GNUNET_free (bindto);
+ GNUNET_free (cp);
}