2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
27 #include "plugin_transport_http.h"
30 * After how long do we expire an address that we
31 * learned from another peer if it is not reconfirmed
34 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
37 * Wrapper to manage IPv4 addresses
39 struct IPv4HttpAddressWrapper
44 struct IPv4HttpAddressWrapper *next;
47 * Linked list previous
49 struct IPv4HttpAddressWrapper *prev;
51 struct IPv4HttpAddress addr;
55 * Wrapper for IPv4 addresses.
57 struct IPv6HttpAddressWrapper
62 struct IPv6HttpAddressWrapper *next;
65 * Linked list previous
67 struct IPv6HttpAddressWrapper *prev;
69 struct IPv6HttpAddress addr6;
74 * Context for address to string conversion.
76 struct PrettyPrinterContext
79 * Function to call with the result.
81 GNUNET_TRANSPORT_AddressStringCallback asc;
86 struct Plugin *plugin;
94 * Port to add after the IP address.
101 * Encapsulation of all of the state of the plugin.
108 * Append our port and forward the result.
110 * @param cls the 'struct PrettyPrinterContext*'
111 * @param hostname hostname part of the address
114 append_port (void *cls, const char *hostname)
116 struct PrettyPrinterContext *ppc = cls;
119 if (hostname == NULL)
121 ppc->asc (ppc->asc_cls, NULL);
125 GNUNET_asprintf (&ret, "%s://%s:%d", ppc->plugin->protocol, hostname,
127 ppc->asc (ppc->asc_cls, ret);
133 * Convert the transports address to a nice, human-readable
137 * @param type name of the transport that generated the address
138 * @param addr one of the addresses of the host, NULL for the last address
139 * the specific address format depends on the transport
140 * @param addrlen length of the address
141 * @param numeric should (IP) addresses be displayed in numeric form?
142 * @param timeout after how long should we give up?
143 * @param asc function to call on each string
144 * @param asc_cls closure for asc
147 http_plugin_address_pretty_printer (void *cls, const char *type,
148 const void *addr, size_t addrlen,
150 struct GNUNET_TIME_Relative timeout,
151 GNUNET_TRANSPORT_AddressStringCallback asc,
154 GNUNET_assert (cls != NULL);
155 struct PrettyPrinterContext *ppc;
160 if (addrlen == sizeof (struct IPv6HttpAddress))
162 struct IPv6HttpAddress * a6 = (struct IPv6HttpAddress *) addr;
164 sbs = sizeof (struct in6_addr);
165 port = ntohs (a6->u6_port);
167 else if (addrlen == sizeof (struct IPv4HttpAddress))
169 struct IPv4HttpAddress * a4 = (struct IPv4HttpAddress *) addr;
171 sbs = sizeof (struct in_addr);
172 port = ntohs (a4->u4_port);
176 /* invalid address */
181 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
183 ppc->asc_cls = asc_cls;
186 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
192 * Another peer has suggested an address for this
193 * peer and transport plugin. Check that this could be a valid
194 * address. If so, consider adding it to the list
198 * @param addr pointer to the address
199 * @param addrlen length of addr
200 * @return GNUNET_OK if this is a plausible address for this peer
204 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
207 struct Plugin *plugin = cls;
208 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
209 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
213 GNUNET_assert (cls != NULL);
214 if ((addrlen != sizeof (struct sockaddr_in)) ||
215 (addrlen != sizeof (struct sockaddr_in6)))
216 return GNUNET_SYSERR;
218 if (addrlen == sizeof (struct IPv4HttpAddress))
220 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
221 while (w_tv4 != NULL)
223 if ((0 == memcmp (&w_tv4->addr.ipv4_addr, &a4->ipv4_addr, sizeof (struct in_addr))) &&
224 (w_tv4->addr.u4_port == a4->u4_port))
231 return GNUNET_SYSERR;
233 if (addrlen == sizeof (struct sockaddr_in6))
235 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
236 while (w_tv6 != NULL)
238 if ((0 == memcmp (&w_tv6->addr6.ipv6_addr, &a6->ipv6_addr, sizeof (struct in6_addr))) &&
239 (w_tv6->addr6.u6_port == a6->u6_port))
246 return GNUNET_SYSERR;
251 struct GNUNET_TIME_Relative
252 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity * peer,
253 const struct GNUNET_MessageHeader * message,
254 struct Session * session,
255 const char *sender_address,
256 uint16_t sender_address_len)
258 struct Session *s = cls;
259 struct Plugin *plugin = s->plugin;
260 struct GNUNET_ATS_Information distance;
261 struct GNUNET_TIME_Relative delay;
263 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
264 distance.value = htonl (1);
266 delay = plugin->env->receive (plugin->env->cls, &s->target, message, (const struct GNUNET_ATS_Information*) &distance, 1, s, s->addr, s->addrlen);
271 * Function called for a quick conversion of the binary address to
272 * a numeric address. Note that the caller must not free the
273 * address and that the next call to this function is allowed
274 * to override the address again.
277 * @param addr binary address
278 * @param addrlen length of the address
279 * @return string representing the same address
282 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
285 struct IPv4HttpAddress *a4;
286 struct IPv6HttpAddress *a6;
288 static char rbuf[INET6_ADDRSTRLEN + 13];
292 if (addrlen == sizeof (struct IPv6HttpAddress))
294 a6 = (struct IPv6HttpAddress *) addr;
295 address = GNUNET_malloc (INET6_ADDRSTRLEN);
296 GNUNET_assert(NULL != inet_ntop (AF_INET6, &a6->ipv6_addr, address, INET6_ADDRSTRLEN));
297 port = ntohs (a6->u6_port);
299 else if (addrlen == sizeof (struct IPv4HttpAddress))
301 a4 = (struct IPv4HttpAddress *) addr;
302 address = GNUNET_malloc (INET_ADDRSTRLEN);
303 GNUNET_assert(NULL != inet_ntop (AF_INET, &(a4->ipv4_addr), address, INET_ADDRSTRLEN));
304 port = ntohs (a4->u4_port);
308 /* invalid address */
313 char * protocol = "http";
315 char * protocol = "https";
318 GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
319 if (addrlen == sizeof (struct IPv6HttpAddress))
320 res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, address, port);
321 else if (addrlen == sizeof (struct IPv4HttpAddress))
322 res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address, port);
324 GNUNET_free (address);
325 GNUNET_assert (res != 0);
330 lookup_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
331 struct Session * session,
332 const void *addr, size_t addrlen, int force_address)
334 struct Session *s = NULL;
335 struct Session *t = NULL;
345 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
346 "Comparing peer `%s' address `%s' len %i session %X to \n", GNUNET_i2s(target), GNUNET_a2s(addr,addrlen), addrlen, session);
347 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"peer `%s' address `%s' len %i session %X \n\n", GNUNET_i2s(&t->target), GNUNET_a2s(t->addr,t->addrlen), t->addrlen, t);
348 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"memcmp %i \n", memcmp (addr, t->addr, addrlen));
353 if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
356 if (addrlen == t->addrlen)
358 if (0 == memcmp (addr, t->addr, addrlen))
365 if(t->addrlen == session->addrlen)
367 if (0 == memcmp (session->addr, t->addr, t->addrlen))
375 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
380 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES))
385 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
400 delete_session (struct Session *s)
402 if (s->msg_tk != NULL)
404 GNUNET_SERVER_mst_destroy (s->msg_tk);
407 GNUNET_free (s->addr);
408 GNUNET_free_non_null(s->server_recv);
409 GNUNET_free_non_null(s->server_send);
414 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
415 const void *addr, size_t addrlen,
416 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
418 struct Session *s = NULL;
419 GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) || (addrlen == sizeof (struct IPv4HttpAddress)));
420 s = GNUNET_malloc (sizeof (struct Session));
421 memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
423 s->addr = GNUNET_malloc (addrlen);
424 memcpy (s->addr, addr, addrlen);
425 s->addrlen = addrlen;
427 s->next_receive = GNUNET_TIME_absolute_get_zero();
432 notify_session_end (void *cls,
433 const struct GNUNET_PeerIdentity *
434 peer, struct Session * s)
436 struct Plugin *plugin = cls;
438 plugin->env->session_end (NULL, peer, s);
439 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
445 * Function that can be used by the transport service to transmit
446 * a message using the plugin. Note that in the case of a
447 * peer disconnecting, the continuation MUST be called
448 * prior to the disconnect notification itself. This function
449 * will be called with this peer's HELLO message to initiate
450 * a fresh connection to another peer.
453 * @param target who should receive this message
454 * @param msgbuf the message to transmit
455 * @param msgbuf_size number of bytes in 'msgbuf'
456 * @param priority how important is the message (most plugins will
457 * ignore message priority and just FIFO)
458 * @param to how long to wait at most for the transmission (does not
459 * require plugins to discard the message after the timeout,
460 * just advisory for the desired delay; most plugins will ignore
462 * @param session which session must be used (or NULL for "any")
463 * @param addr the address to use (can be NULL if the plugin
464 * is "on its own" (i.e. re-use existing TCP connection))
465 * @param addrlen length of the address in bytes
466 * @param force_address GNUNET_YES if the plugin MUST use the given address,
467 * GNUNET_NO means the plugin may use any other address and
468 * GNUNET_SYSERR means that only reliable existing
469 * bi-directional connections should be used (regardless
471 * @param cont continuation to call once the message has
472 * been transmitted (or if the transport is ready
473 * for the next transmission call; or if the
474 * peer disconnected...); can be NULL
475 * @param cont_cls closure for cont
476 * @return number of bytes used (on the physical network, with overheads);
477 * -1 on hard errors (i.e. address invalid); 0 is a legal value
478 * and does NOT mean that the message was not transmitted (DV)
481 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
482 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
483 struct GNUNET_TIME_Relative to, struct Session *session,
484 const void *addr, size_t addrlen, int force_address,
485 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
487 struct Plugin *plugin = cls;
488 struct HTTP_Message *msg;
489 GNUNET_assert (plugin != NULL);
491 int res = GNUNET_SYSERR;
494 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
495 "Sending %u bytes to peer `%s' on address `%s' %X %i\n", msgbuf_size,
496 GNUNET_i2s (target), ((addr != NULL) && (addrlen != 0)) ? http_plugin_address_to_string(plugin, addr, addrlen) : "<inbound>", session, force_address);
499 struct Session *s = NULL;
503 GNUNET_assert ((addrlen == sizeof (struct IPv4HttpAddress)) ||
504 (addrlen == sizeof (struct IPv6HttpAddress)));
506 /* look for existing connection */
507 s = lookup_session (plugin, target, session, addr, addrlen, 1);
509 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
510 "%s existing session: %s\n", (s!=NULL) ? "Found" : "NOT Found", ((s != NULL) && (s->inbound == GNUNET_YES)) ? "inbound" : "outbound");
513 /* create new outbound connection */
516 if (plugin->max_connections <= plugin->cur_connections)
518 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
519 "Maximum number of connections reached, "
520 "cannot connect to peer `%s'\n",
521 GNUNET_i2s (target));
526 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
527 "Initiiating new connection to peer `%s'\n",
528 GNUNET_i2s (target));
530 s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
531 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
532 // initiate new connection
533 if (GNUNET_SYSERR == (res = client_connect (s)))
535 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
537 return GNUNET_SYSERR;
541 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
543 msg->size = msgbuf_size;
545 msg->buf = (char *) &msg[1];
546 msg->transmit_cont = cont;
547 msg->transmit_cont_cls = cont_cls;
548 memcpy (msg->buf, msgbuf, msgbuf_size);
550 if (s->inbound == GNUNET_NO)
553 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
554 "Using outbound client session to send to `%s'\n",
555 GNUNET_i2s (target));
557 client_send (s, msg);
560 if (s->inbound == GNUNET_YES)
562 server_send (s, msg);
565 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
566 "Using inbound server session to send to `%s'\n",
567 GNUNET_i2s (target));
576 * Function that can be used to force the plugin to disconnect
577 * from the given peer and cancel all previous transmissions
578 * (and their continuationc).
581 * @param target peer from which to disconnect
584 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
586 struct Plugin *plugin = cls;
587 struct Session *next = NULL;
588 struct Session *s = plugin->head;
590 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
591 "Transport tells me to disconnect `%s'\n",
592 GNUNET_i2s (target));
596 if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
598 if (s->inbound == GNUNET_NO)
599 GNUNET_assert (GNUNET_OK == client_disconnect (s));
601 GNUNET_assert (GNUNET_OK == server_disconnect (s));
602 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
610 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
613 struct Plugin *plugin = cls;
614 struct IPv4HttpAddressWrapper *w_t4 = NULL;
615 struct IPv6HttpAddressWrapper *w_t6 = NULL;
618 af = addr->sa_family;
622 w_t4 = plugin->ipv4_addr_head;
623 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
626 int res = memcmp (&w_t4->addr.ipv4_addr,
628 sizeof (struct in_addr));
631 if (a4->sin_port!= w_t4->addr.u4_port)
641 w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
642 memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr,
643 sizeof (struct in_addr));
644 w_t4->addr.u4_port = a4->sin_port;
646 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
647 plugin->ipv4_addr_tail, w_t4);
650 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
651 "Notifying transport to add IPv4 address `%s'\n",
652 http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct IPv4HttpAddress)));
654 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr, sizeof (struct IPv4HttpAddress));
658 w_t6 = plugin->ipv6_addr_head;
659 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
662 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
663 sizeof (struct in6_addr));
666 if (a6->sin6_port != w_t6->addr6.u6_port)
675 w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
677 memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
678 sizeof (struct in6_addr));
679 w_t6->addr6.u6_port = a6->sin6_port;
681 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
682 plugin->ipv6_addr_tail, w_t6);
685 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
686 "Notifying transport to add IPv6 address `%s'\n",
687 http_plugin_address_to_string(NULL, &w_t6->addr6, sizeof (struct IPv6HttpAddress)));
689 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6, sizeof (struct IPv6HttpAddress));
698 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
701 struct Plugin *plugin = cls;
702 struct IPv4HttpAddressWrapper *w_t4 = NULL;
703 struct IPv6HttpAddressWrapper *w_t6 = NULL;
706 af = addr->sa_family;
710 w_t4 = plugin->ipv4_addr_head;
711 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
714 int res = memcmp (&w_t4->addr.ipv4_addr,
716 sizeof (struct in_addr));
719 if (a4->sin_port!= w_t4->addr.u4_port)
731 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
732 "Notifying transport to remove IPv4 address `%s'\n",
733 http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct IPv4HttpAddress)));
735 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
736 sizeof (struct IPv4HttpAddress));
738 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
743 w_t6 = plugin->ipv6_addr_head;
744 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
747 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
748 sizeof (struct in6_addr));
751 if (a6->sin6_port != w_t6->addr6.u6_port)
761 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
762 "Notifying transport to remove IPv6 address `%s'\n",
763 http_plugin_address_to_string(NULL, &w_t6->addr6, sizeof (struct IPv6HttpAddress)));
765 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
766 sizeof (struct IPv6HttpAddress));
768 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
779 * Our external IP address/port mapping has changed.
781 * @param cls closure, the 'struct LocalAddrList'
782 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
783 * the previous (now invalid) one
784 * @param addr either the previous or the new public IP address
785 * @param addrlen actual lenght of the address
788 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
791 GNUNET_assert (cls != NULL);
793 struct Plugin *plugin = cls;
796 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
797 "NPMC called %s to address `%s'\n",
798 (add_remove == GNUNET_NO) ? "remove" : "add",
799 GNUNET_a2s (addr, addrlen));
804 nat_add_address (cls, add_remove, addr, addrlen);
807 nat_remove_address (cls, add_remove, addr, addrlen);
813 http_check_ipv6 (struct Plugin *plugin)
815 struct GNUNET_NETWORK_Handle *desc = NULL;
816 if (plugin->ipv6 == GNUNET_YES)
818 /* probe IPv6 support */
819 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
822 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
825 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
827 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
829 ("Disabling IPv6 since it is not supported on this system!\n"));
830 plugin->ipv6 = GNUNET_NO;
834 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
838 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
839 "Testing IPv6 on this system: %s\n", (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
844 http_get_addresses (struct Plugin *plugin,
845 const char *serviceName,
846 const struct GNUNET_CONFIGURATION_Handle
847 *cfg, struct sockaddr ***addrs,
848 socklen_t ** addr_lens)
851 unsigned long long port;
852 struct addrinfo hints;
853 struct addrinfo *res;
854 struct addrinfo *pos;
855 struct addrinfo *next;
859 struct sockaddr **saddrs;
860 socklen_t *saddrlens;
866 disablev6 = !plugin->ipv6;
869 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
871 GNUNET_break (GNUNET_OK ==
872 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
876 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
878 ("Require valid port number for service in configuration!\n"));
879 return GNUNET_SYSERR;
883 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
885 GNUNET_break (GNUNET_OK ==
886 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
887 "BINDTO", &hostname));
892 if (hostname != NULL)
894 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
895 "Resolving `%s' since that is where `%s' will bind to.\n",
896 hostname, serviceName);
897 memset (&hints, 0, sizeof (struct addrinfo));
899 hints.ai_family = AF_INET;
900 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
903 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
904 hostname, gai_strerror (ret));
905 GNUNET_free (hostname);
906 return GNUNET_SYSERR;
910 while (NULL != (pos = next))
913 if ((disablev6) && (pos->ai_family == AF_INET6))
919 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
920 _("Failed to find %saddress for `%s'.\n"),
921 disablev6 ? "IPv4 " : "", hostname);
923 GNUNET_free (hostname);
924 return GNUNET_SYSERR;
927 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
928 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
931 while (NULL != (pos = next))
934 if ((disablev6) && (pos->ai_family == AF_INET6))
936 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
937 continue; /* not TCP */
938 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
940 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
941 "Service will bind to `%s'\n",
942 GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
943 if (pos->ai_family == AF_INET)
945 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
946 saddrlens[i] = pos->ai_addrlen;
947 saddrs[i] = GNUNET_malloc (saddrlens[i]);
948 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
949 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
953 GNUNET_assert (pos->ai_family == AF_INET6);
954 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
955 saddrlens[i] = pos->ai_addrlen;
956 saddrs[i] = GNUNET_malloc (saddrlens[i]);
957 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
958 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
962 GNUNET_free (hostname);
968 /* will bind against everything, just set port */
974 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
975 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
977 saddrlens[i] = sizeof (struct sockaddr_in);
978 saddrs[i] = GNUNET_malloc (saddrlens[i]);
979 #if HAVE_SOCKADDR_IN_SIN_LEN
980 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
982 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
983 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
989 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
990 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
992 saddrlens[i] = sizeof (struct sockaddr_in6);
993 saddrs[i] = GNUNET_malloc (saddrlens[i]);
994 #if HAVE_SOCKADDR_IN_SIN_LEN
995 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
997 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
998 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1000 saddrlens[i] = sizeof (struct sockaddr_in);
1001 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1002 #if HAVE_SOCKADDR_IN_SIN_LEN
1003 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1005 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1006 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1010 *addr_lens = saddrlens;
1015 start_report_addresses (struct Plugin *plugin)
1017 int res = GNUNET_OK;
1018 struct sockaddr **addrs;
1019 socklen_t *addrlens;
1022 http_get_addresses (plugin, plugin->name, plugin->env->cfg,
1024 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1025 _("Found %u addresses to report to NAT service\n"),res);
1027 if (res != GNUNET_SYSERR)
1030 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1032 (const struct sockaddr **) addrs, addrlens,
1033 &nat_port_map_callback, NULL,
1039 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1041 GNUNET_a2s (addrs[res], addrlens[res]));
1043 GNUNET_assert (addrs[res] != NULL);
1044 GNUNET_free (addrs[res]);
1046 GNUNET_free_non_null (addrs);
1047 GNUNET_free_non_null (addrlens);
1052 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1053 NULL, NULL, plugin);
1058 stop_report_addresses (struct Plugin *plugin)
1060 /* Stop NAT handle */
1061 GNUNET_NAT_unregister (plugin->nat);
1063 /* Clean up addresses */
1064 struct IPv4HttpAddressWrapper *w_t4;
1065 struct IPv6HttpAddressWrapper *w_t6;
1067 while (plugin->ipv4_addr_head != NULL)
1069 w_t4 = plugin->ipv4_addr_head;
1070 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1075 while (plugin->ipv6_addr_head != NULL)
1077 w_t6 = plugin->ipv6_addr_head;
1078 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1085 configure_plugin (struct Plugin *plugin)
1087 int res = GNUNET_OK;
1090 if (GNUNET_CONFIGURATION_have_value
1091 (plugin->env->cfg, plugin->name, "USE_IPv4"))
1094 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1098 plugin->ipv4 = GNUNET_YES;
1101 if (GNUNET_CONFIGURATION_have_value
1102 (plugin->env->cfg, plugin->name, "USE_IPv6"))
1105 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1109 plugin->ipv6 = GNUNET_YES;
1111 if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1113 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1115 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1117 res = GNUNET_SYSERR;
1120 /* Reading port number from config file */
1121 unsigned long long port;
1124 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1125 "PORT", &port)) || (port > 65535))
1127 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1128 _("Port is required! Fix in configuration\n"),
1130 res = GNUNET_SYSERR;
1133 plugin->port = port;
1135 plugin->client_only = GNUNET_NO;
1136 if (plugin->port == 0)
1138 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1139 _("Port 0, client only mode\n"));
1140 plugin->client_only = GNUNET_YES;
1143 char * bind4_address = NULL;
1144 if ((plugin->ipv4 == GNUNET_YES) && (GNUNET_YES ==
1145 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1146 "BINDTO", &bind4_address)))
1148 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1149 "Binding %s plugin to specific IPv4 address: `%s'\n",
1150 plugin->protocol, bind4_address);
1151 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1152 if (1 != inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1154 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1155 _("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1156 bind4_address, plugin->protocol);
1157 GNUNET_free (plugin->server_addr_v4);
1158 plugin->server_addr_v4 = NULL;
1162 plugin->server_addr_v4->sin_family = AF_INET;
1163 plugin->server_addr_v4->sin_port = htons (plugin->port);
1165 GNUNET_free (bind4_address);
1169 char * bind6_address = NULL;
1170 if ((plugin->ipv6 == GNUNET_YES) && (GNUNET_YES ==
1171 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1172 "BINDTO6", &bind6_address)))
1174 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1175 "Binding %s plugin to specific IPv6 address: `%s'\n",
1176 plugin->protocol, bind6_address);
1177 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1178 if (1 != inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1180 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1181 _("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1182 bind6_address, plugin->protocol);
1183 GNUNET_free (plugin->server_addr_v6);
1184 plugin->server_addr_v6 = NULL;
1188 plugin->server_addr_v6->sin6_family = AF_INET6;
1189 plugin->server_addr_v6->sin6_port = htons (plugin->port);
1191 GNUNET_free (bind6_address);
1195 /* Optional parameters */
1196 unsigned long long maxneigh;
1198 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1199 "MAX_CONNECTIONS", &maxneigh))
1201 plugin->max_connections = maxneigh;
1208 * Entry point for the plugin.
1211 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1213 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1214 struct GNUNET_TRANSPORT_PluginFunctions *api;
1215 struct Plugin *plugin;
1218 plugin = GNUNET_malloc (sizeof (struct Plugin));
1220 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1222 api->send = &http_plugin_send;
1223 api->disconnect = &http_plugin_disconnect;
1224 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1225 api->check_address = &http_plugin_address_suggested;
1226 api->address_to_string = &http_plugin_address_to_string;
1229 plugin->name = "transport-https";
1230 plugin->protocol = "https";
1232 plugin->name = "transport-http";
1233 plugin->protocol = "http";
1235 /* Configure plugin from configuration */
1236 res = configure_plugin (plugin);
1237 if (res == GNUNET_SYSERR)
1239 GNUNET_free_non_null (plugin->server_addr_v4);
1240 GNUNET_free_non_null (plugin->server_addr_v6);
1241 GNUNET_free (plugin);
1246 /* checking IPv6 support */
1247 http_check_ipv6 (plugin);
1250 res = client_start (plugin);
1251 if (res == GNUNET_SYSERR)
1253 GNUNET_free_non_null (plugin->server_addr_v4);
1254 GNUNET_free_non_null (plugin->server_addr_v6);
1255 GNUNET_free (plugin);
1261 if (plugin->client_only == GNUNET_NO)
1263 res = server_start (plugin);
1264 if (res == GNUNET_SYSERR)
1266 server_stop (plugin);
1267 client_stop (plugin);
1269 GNUNET_free_non_null (plugin->server_addr_v4);
1270 GNUNET_free_non_null (plugin->server_addr_v6);
1271 GNUNET_free (plugin);
1276 /* Report addresses to transport service */
1277 start_report_addresses (plugin);
1280 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1281 "Plugin `%s' loaded\n", plugin->name);
1289 * Exit point from the plugin.
1292 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1294 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1295 struct Plugin *plugin = api->cls;
1296 struct Session *s = NULL;
1298 /* Stop reporting addresses to transport service */
1299 stop_report_addresses (plugin);
1301 /* cleaning up sessions */
1306 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1307 "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1309 if (s->inbound == GNUNET_NO)
1310 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1312 GNUNET_assert (GNUNET_OK == server_disconnect (s));
1317 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1318 "Stopping server\n");
1321 server_stop (plugin);
1324 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1325 "Stopping client\n");
1328 client_stop (plugin);
1330 /* deleting up sessions */
1334 struct Session *t = s->next;
1335 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1342 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1343 "Plugin `%s' unloaded\n", plugin->name);
1346 GNUNET_free_non_null (plugin->server_addr_v4);
1347 GNUNET_free_non_null (plugin->server_addr_v6);
1348 GNUNET_free (plugin);
1354 /* end of plugin_transport_http.c */