}
+
+
+
/**
* Convert the transports address to a nice, human-readable
* format.
return delay;
}
+
+/**
+ * Function called to convert a string address to
+ * a binary address.
+ *
+ * @param cls closure ('struct Plugin*')
+ * @param addr string address
+ * @param addrlen length of the address
+ * @param buf location to store the buffer
+ * If the function returns GNUNET_SYSERR, its contents are undefined.
+ * @param added length of created address
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ */
+int http_string_to_address (void *cls,
+ const char *addr,
+ uint16_t addrlen,
+ void **buf,
+ size_t *added)
+{
+#if !BUILD_HTTPS
+ char *protocol = "http";
+#else
+ char *protocol = "https";
+#endif
+ char *addr_str = NULL;
+ struct sockaddr_in addr_4;
+ struct sockaddr_in6 addr_6;
+ struct IPv4HttpAddress * http_4addr;
+ struct IPv6HttpAddress * http_6addr;
+ if ((addr == NULL) || (addrlen == 0) || (buf == NULL))
+ return GNUNET_SYSERR;
+
+ /* protocoll + "://" + ":" */
+ if (addrlen <= (strlen (protocol) + 4))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Invalid address string `%s' to convert to address\n",
+ addr);
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+
+ if (NULL == (addr_str = strstr(addr, "://")))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Invalid address string `%s' to convert to address\n",
+ addr);
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ addr_str = &addr_str[3];
+
+ if (addr_str[strlen(addr_str)-1] == '/')
+ addr_str[strlen(addr_str)-1] = '\0';
+
+ if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv4(addr_str, strlen(addr_str), &addr_4))
+ {
+ http_4addr = GNUNET_malloc (sizeof (struct IPv4HttpAddress));
+ http_4addr->u4_port = addr_4.sin_port;
+ http_4addr->ipv4_addr = (uint32_t) addr_4.sin_addr.s_addr;
+ (*buf) = http_4addr;
+ (*added) = sizeof (struct IPv4HttpAddress);
+ return GNUNET_OK;
+ }
+ else if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv6(addr_str, strlen(addr_str), &addr_6))
+ {
+ http_6addr = GNUNET_malloc (sizeof (struct IPv6HttpAddress));
+ http_6addr->u6_port = addr_6.sin6_port;
+ http_6addr->ipv6_addr = addr_6.sin6_addr;
+ (*buf) = http_6addr;
+ (*added) = sizeof (struct IPv6HttpAddress);
+ return GNUNET_OK;
+ }
+ else
+ {
+ GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+ "Invalid address string `%s' to convert to address\n",
+ addr);
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+}
+
+
+
/**
* Function called for a quick conversion of the binary address to
* a numeric address. Note that the caller must not free the
}
}
-static void
-nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
- socklen_t addrlen)
+static void *
+find_address (struct Plugin *plugin, const struct sockaddr *addr, socklen_t addrlen)
{
- struct Plugin *plugin = cls;
+ int af;
struct IPv4HttpAddressWrapper *w_t4 = NULL;
struct IPv6HttpAddressWrapper *w_t6 = NULL;
- int af;
af = addr->sa_family;
switch (af)
break;
w_t4 = w_t4->next;
}
- if (w_t4 == NULL)
- {
- w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
- memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
- w_t4->addr.u4_port = a4->sin_port;
-
- GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
- plugin->ipv4_addr_tail, w_t4);
- }
-#if DEBUG_HTTP
- GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
- "Notifying transport to add IPv4 address `%s'\n",
- http_plugin_address_to_string (NULL, &w_t4->addr,
- sizeof (struct
- IPv4HttpAddress)));
-#endif
- plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
- sizeof (struct IPv4HttpAddress));
-
+ return w_t4;
break;
case AF_INET6:
w_t6 = plugin->ipv6_addr_head;
break;
w_t6 = w_t6->next;
}
+ return w_t6;
+ break;
+ default:
+ return NULL;
+ }
+ return NULL;
+}
+
+static void
+nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
+ socklen_t addrlen)
+{
+ struct Plugin *plugin = cls;
+ struct IPv4HttpAddressWrapper *w_t4 = NULL;
+ struct IPv6HttpAddressWrapper *w_t6 = NULL;
+ int af;
+
+ af = addr->sa_family;
+ switch (af)
+ {
+ case AF_INET:
+ w_t4 = find_address (plugin, addr, addrlen);
+ if (w_t4 == NULL)
+ {
+ struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
+ w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
+ memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
+ w_t4->addr.u4_port = a4->sin_port;
+
+ GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
+ plugin->ipv4_addr_tail, w_t4);
+
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Notifying transport to add IPv4 address `%s'\n",
+ http_plugin_address_to_string (NULL, &w_t4->addr,
+ sizeof (struct
+ IPv4HttpAddress)));
+ plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
+ sizeof (struct IPv4HttpAddress));
+ }
+ break;
+ case AF_INET6:
+ w_t6 = find_address (plugin, addr, addrlen);
if (w_t6 == NULL)
{
w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
-
+ struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
w_t6->addr6.u6_port = a6->sin6_port;
GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
plugin->ipv6_addr_tail, w_t6);
- }
-#if DEBUG_HTTP
- GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
"Notifying transport to add IPv6 address `%s'\n",
http_plugin_address_to_string (NULL, &w_t6->addr6,
sizeof (struct
IPv6HttpAddress)));
-#endif
- plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
+ plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
sizeof (struct IPv6HttpAddress));
+ }
break;
default:
return;
switch (af)
{
case AF_INET:
- w_t4 = plugin->ipv4_addr_head;
- struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
-
- while (w_t4 != NULL)
- {
- int res = memcmp (&w_t4->addr.ipv4_addr,
- &a4->sin_addr,
- sizeof (struct in_addr));
-
- if (res == 0)
- {
- if (a4->sin_port != w_t4->addr.u4_port)
- res = -1;
- }
-
- if (0 == res)
- break;
- w_t4 = w_t4->next;
- }
+ w_t4 = find_address (plugin, addr, addrlen);
if (w_t4 == NULL)
return;
-#if DEBUG_HTTP
+
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
"Notifying transport to remove IPv4 address `%s'\n",
http_plugin_address_to_string (NULL, &w_t4->addr,
sizeof (struct
IPv4HttpAddress)));
-#endif
plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
sizeof (struct IPv4HttpAddress));
GNUNET_free (w_t4);
break;
case AF_INET6:
- w_t6 = plugin->ipv6_addr_head;
- struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
-
- while (w_t6)
- {
- int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
- sizeof (struct in6_addr));
-
- if (res == 0)
- {
- if (a6->sin6_port != w_t6->addr6.u6_port)
- res = -1;
- }
- if (0 == res)
- break;
- w_t6 = w_t6->next;
- }
+ w_t6 = find_address (plugin, addr, addrlen);
if (w_t6 == NULL)
return;
-#if DEBUG_HTTP
+
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
"Notifying transport to remove IPv6 address `%s'\n",
http_plugin_address_to_string (NULL, &w_t6->addr6,
sizeof (struct
IPv6HttpAddress)));
-#endif
+
plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
sizeof (struct IPv6HttpAddress));
socklen_t addrlen)
{
GNUNET_assert (cls != NULL);
-#if DEBUG_HTTP
struct Plugin *plugin = cls;
-#endif
-#if DEBUG_HTTP
+
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
"NPMC called %s to address `%s'\n",
(add_remove == GNUNET_NO) ? "remove" : "add",
GNUNET_a2s (addr, addrlen));
-#endif
+
switch (add_remove)
{
case GNUNET_YES:
struct Plugin *plugin;
int res;
+ if (NULL == env->receive)
+ {
+ /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
+ initialze the plugin or the API */
+ api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
+ api->cls = NULL;
+ api->address_pretty_printer = &http_plugin_address_pretty_printer;
+ api->address_to_string = &http_plugin_address_to_string;
+ api->string_to_address = &http_string_to_address;
+ return api;
+ }
+
plugin = GNUNET_malloc (sizeof (struct Plugin));
plugin->env = env;
api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
api->address_pretty_printer = &http_plugin_address_pretty_printer;
api->check_address = &http_plugin_address_suggested;
api->address_to_string = &http_plugin_address_to_string;
+ api->string_to_address = &http_string_to_address;
api->get_session = &http_get_session;
api->send = &http_plugin_send;
{
struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
struct Plugin *plugin = api->cls;
- struct Session *s = NULL;
+ struct Session *s;
+
+ if (NULL == plugin)
+ {
+ GNUNET_free (api);
+ return NULL;
+ }
/* Stop reporting addresses to transport service */
stop_report_addresses (plugin);