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.
105 * Encapsulation of all of the state of the plugin.
112 * Append our port and forward the result.
114 * @param cls the 'struct PrettyPrinterContext*'
115 * @param hostname hostname part of the address
118 append_port (void *cls, const char *hostname)
120 struct PrettyPrinterContext *ppc = cls;
121 static char rbuf[INET6_ADDRSTRLEN + 13];
123 if (hostname == NULL)
125 ppc->asc (ppc->asc_cls, NULL);
131 const char *protocol = "http";
133 const char *protocol = "https";
135 GNUNET_assert ((strlen (hostname) + 7) < (INET6_ADDRSTRLEN + 13));
136 if (ppc->addrlen == sizeof (struct IPv6HttpAddress))
138 if (ppc->numeric == GNUNET_YES)
139 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, hostname, ppc->port);
142 if (strchr(hostname, ':') != NULL)
143 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, hostname, ppc->port);
145 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, hostname, ppc->port);
148 else if (ppc->addrlen == sizeof (struct IPv4HttpAddress))
149 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, hostname, ppc->port);
150 ppc->asc (ppc->asc_cls, rbuf);
155 * Convert the transports address to a nice, human-readable
159 * @param type name of the transport that generated the address
160 * @param addr one of the addresses of the host, NULL for the last address
161 * the specific address format depends on the transport
162 * @param addrlen length of the address
163 * @param numeric should (IP) addresses be displayed in numeric form?
164 * @param timeout after how long should we give up?
165 * @param asc function to call on each string
166 * @param asc_cls closure for asc
169 http_plugin_address_pretty_printer (void *cls, const char *type,
170 const void *addr, size_t addrlen,
172 struct GNUNET_TIME_Relative timeout,
173 GNUNET_TRANSPORT_AddressStringCallback asc,
176 GNUNET_assert (cls != NULL);
177 struct PrettyPrinterContext *ppc;
179 struct sockaddr_in s4;
180 struct sockaddr_in6 s6;
184 if ((addrlen == sizeof (struct IPv6HttpAddress)) && (addr != NULL))
186 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
187 s6.sin6_family = AF_INET6;
188 s6.sin6_addr = a6->ipv6_addr;
189 s6.sin6_port = a6->u6_port;
190 #if HAVE_SOCKADDR_IN_SIN_LEN
191 s6.sin6_len = sizeof (struct sockaddr_in6);
194 sbs = sizeof (struct sockaddr_in6);
195 port = ntohs (a6->u6_port);
198 else if ((addrlen == sizeof (struct IPv4HttpAddress)) && (addr != NULL))
200 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
202 s4.sin_family = AF_INET;
203 s4.sin_addr.s_addr = a4->ipv4_addr;
204 s4.sin_port = a4->u4_port;
205 #if HAVE_SOCKADDR_IN_SIN_LEN
206 s4.sin_len = sizeof (struct sockaddr_in);
209 sbs = sizeof (struct sockaddr_in);
210 port = ntohs (a4->u4_port);
214 /* invalid address */
219 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
221 ppc->asc_cls = asc_cls;
224 ppc->addrlen = addrlen;
225 ppc->numeric = numeric;
226 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
232 * Another peer has suggested an address for this
233 * peer and transport plugin. Check that this could be a valid
234 * address. If so, consider adding it to the list
238 * @param addr pointer to the address
239 * @param addrlen length of addr
240 * @return GNUNET_OK if this is a plausible address for this peer
244 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
247 struct Plugin *plugin = cls;
248 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
249 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
253 GNUNET_assert (cls != NULL);
254 if ((addrlen != sizeof (struct sockaddr_in)) ||
255 (addrlen != sizeof (struct sockaddr_in6)))
256 return GNUNET_SYSERR;
258 if (addrlen == sizeof (struct IPv4HttpAddress))
260 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
262 while (w_tv4 != NULL)
265 memcmp (&w_tv4->addr.ipv4_addr, &a4->ipv4_addr,
266 sizeof (struct in_addr))) &&
267 (w_tv4->addr.u4_port == a4->u4_port))
274 return GNUNET_SYSERR;
276 if (addrlen == sizeof (struct sockaddr_in6))
278 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
280 while (w_tv6 != NULL)
283 memcmp (&w_tv6->addr6.ipv6_addr, &a6->ipv6_addr,
284 sizeof (struct in6_addr))) &&
285 (w_tv6->addr6.u6_port == a6->u6_port))
292 return GNUNET_SYSERR;
297 struct GNUNET_TIME_Relative
298 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
299 const struct GNUNET_MessageHeader *message,
300 struct Session *session, const char *sender_address,
301 uint16_t sender_address_len)
303 struct Session *s = cls;
304 struct Plugin *plugin = s->plugin;
305 struct GNUNET_TIME_Relative delay;
306 struct GNUNET_ATS_Information atsi[2];
308 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
309 atsi[0].value = htonl (1);
310 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
311 atsi[1].value = session->ats_address_network_type;
312 GNUNET_break (session->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
315 plugin->env->receive (plugin->env->cls, &s->target, message,
316 (const struct GNUNET_ATS_Information *) &atsi,
317 2, s, s->addr, s->addrlen);
322 * Function called for a quick conversion of the binary address to
323 * a numeric address. Note that the caller must not free the
324 * address and that the next call to this function is allowed
325 * to override the address again.
328 * @param addr binary address
329 * @param addrlen length of the address
330 * @return string representing the same address
333 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
336 struct IPv4HttpAddress *a4;
337 struct IPv6HttpAddress *a6;
339 static char rbuf[INET6_ADDRSTRLEN + 13];
343 if (addrlen == sizeof (struct IPv6HttpAddress))
345 a6 = (struct IPv6HttpAddress *) addr;
346 address = GNUNET_malloc (INET6_ADDRSTRLEN);
347 GNUNET_assert (NULL !=
348 inet_ntop (AF_INET6, &a6->ipv6_addr, address,
350 port = ntohs (a6->u6_port);
352 else if (addrlen == sizeof (struct IPv4HttpAddress))
354 a4 = (struct IPv4HttpAddress *) addr;
355 address = GNUNET_malloc (INET_ADDRSTRLEN);
356 GNUNET_assert (NULL !=
357 inet_ntop (AF_INET, &(a4->ipv4_addr), address,
359 port = ntohs (a4->u4_port);
363 /* invalid address */
368 char *protocol = "http";
370 char *protocol = "https";
373 GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
374 if (addrlen == sizeof (struct IPv6HttpAddress))
376 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol,
378 else if (addrlen == sizeof (struct IPv4HttpAddress))
380 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address,
383 GNUNET_free (address);
384 GNUNET_assert (res != 0);
389 lookup_session_old (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
390 struct Session *session, const void *addr, size_t addrlen,
397 for (t = plugin->head; NULL != t; t = t->next)
400 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
401 "Comparing peer `%s' address `%s' len %i session %X to \n",
402 GNUNET_i2s (target), GNUNET_a2s (addr, addrlen), addrlen,
404 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
405 "peer `%s' address `%s' len %i session %X \n\n",
406 GNUNET_i2s (&t->target), GNUNET_a2s (t->addr, t->addrlen),
408 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "memcmp %i \n",
409 memcmp (addr, t->addr, addrlen));
413 if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
416 if ( (addrlen == t->addrlen) &&
417 (0 == memcmp (addr, t->addr, addrlen)) )
419 if ( (t == session) &&
420 (t->addrlen == session->addrlen) &&
421 (0 == memcmp (session->addr, t->addr, t->addrlen)) )
425 if ( ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO)) ||
426 ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES)) ||
427 ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR)) )
434 lookup_session (struct Plugin *plugin,
435 const struct GNUNET_HELLO_Address *address)
439 for (pos = plugin->head; NULL != pos; pos = pos->next)
440 if ( (0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
441 (address->address_length == pos->addrlen) &&
442 (0 == memcmp (address->address, pos->addr, pos->addrlen)) )
449 delete_session (struct Session *s)
451 if (s->msg_tk != NULL)
453 GNUNET_SERVER_mst_destroy (s->msg_tk);
456 GNUNET_free (s->addr);
457 GNUNET_free_non_null (s->server_recv);
458 GNUNET_free_non_null (s->server_send);
463 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
464 const void *addr, size_t addrlen,
465 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
467 struct Session *s = NULL;
469 GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
470 (addrlen == sizeof (struct IPv4HttpAddress)));
471 s = GNUNET_malloc (sizeof (struct Session));
472 memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
474 s->addr = GNUNET_malloc (addrlen);
475 memcpy (s->addr, addr, addrlen);
476 s->addrlen = addrlen;
478 s->next_receive = GNUNET_TIME_absolute_get_zero ();
479 s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
484 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
487 struct Plugin *plugin = cls;
489 plugin->env->session_end (plugin->env->cls, peer, s);
490 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
495 * Creates a new outbound session the transport service will use to send data to the
498 * @param cls the plugin
499 * @param address the address
500 * @return the session or NULL of max connections exceeded
503 static struct Session *
504 http_get_session (void *cls,
505 const struct GNUNET_HELLO_Address *address)
507 struct Plugin *plugin = cls;
508 struct Session * s = NULL;
509 struct GNUNET_ATS_Information ats;
512 GNUNET_assert (plugin != NULL);
513 GNUNET_assert (address != NULL);
515 /* find existing session */
516 s = lookup_session (plugin, address);
520 if (plugin->max_connections <= plugin->cur_connections)
522 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
523 "Maximum number of connections reached, "
524 "cannot connect to peer `%s'\n", GNUNET_i2s (&address->peer));
528 /* create new session */
529 addrlen = address->address_length;
531 GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
532 (addrlen == sizeof (struct IPv4HttpAddress)));
534 s = GNUNET_malloc (sizeof (struct Session));
535 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
537 s->addr = GNUNET_malloc (address->address_length);
538 memcpy (s->addr, address->address, address->address_length);
539 s->addrlen = addrlen;
541 s->next_receive = GNUNET_TIME_absolute_get_zero ();
542 s->inbound = GNUNET_NO;
543 s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
546 if ((addrlen == sizeof (struct IPv4HttpAddress)) && (address->address != NULL))
548 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) address->address;
549 struct sockaddr_in s4;
551 s4.sin_family = AF_INET;
552 s4.sin_addr.s_addr = a4->ipv4_addr;
553 s4.sin_port = a4->u4_port;
554 #if HAVE_SOCKADDR_IN_SIN_LEN
555 s4.sin_len = sizeof (struct sockaddr_in);
557 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s4, sizeof (struct sockaddr_in));
559 if ((addrlen == sizeof (struct IPv6HttpAddress)) && (address->address != NULL))
561 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) address->address;
562 struct sockaddr_in6 s6;
564 s6.sin6_family = AF_INET6;
565 s6.sin6_addr = a6->ipv6_addr;
566 s6.sin6_port = a6->u6_port;
567 #if HAVE_SOCKADDR_IN_SIN_LEN
568 s6.sin6_len = sizeof (struct sockaddr_in6);
570 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
572 s->ats_address_network_type = ats.value;
574 /* add new session */
575 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
576 /* initiate new connection */
577 if (GNUNET_SYSERR == client_connect (s))
579 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
580 "Cannot connect to peer `%s' address `%s''\n",
581 http_plugin_address_to_string(NULL, s->addr, s->addrlen),
582 GNUNET_i2s (&s->target));
583 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
592 * Function that can be used by the transport service to transmit
593 * a message using the plugin. Note that in the case of a
594 * peer disconnecting, the continuation MUST be called
595 * prior to the disconnect notification itself. This function
596 * will be called with this peer's HELLO message to initiate
597 * a fresh connection to another peer.
600 * @param session which session must be used
601 * @param msgbuf the message to transmit
602 * @param msgbuf_size number of bytes in 'msgbuf'
603 * @param priority how important is the message (most plugins will
604 * ignore message priority and just FIFO)
605 * @param to how long to wait at most for the transmission (does not
606 * require plugins to discard the message after the timeout,
607 * just advisory for the desired delay; most plugins will ignore
609 * @param cont continuation to call once the message has
610 * been transmitted (or if the transport is ready
611 * for the next transmission call; or if the
612 * peer disconnected...); can be NULL
613 * @param cont_cls closure for cont
614 * @return number of bytes used (on the physical network, with overheads);
615 * -1 on hard errors (i.e. address invalid); 0 is a legal value
616 * and does NOT mean that the message was not transmitted (DV)
619 http_plugin_send (void *cls,
620 struct Session *session,
621 const char *msgbuf, size_t msgbuf_size,
622 unsigned int priority,
623 struct GNUNET_TIME_Relative to,
624 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
626 struct Plugin *plugin = cls;
627 struct HTTP_Message *msg;
631 GNUNET_assert (plugin != NULL);
632 GNUNET_assert (session != NULL);
634 /* lookup if session is really existing */
638 if ((tmp == session) &&
639 (0 == memcmp (&session->target, &tmp->target, sizeof (struct GNUNET_PeerIdentity))) &&
640 (session->addrlen == tmp->addrlen) &&
641 (0 == memcmp (session->addr, tmp->addr, tmp->addrlen)))
651 /* create new message and schedule */
653 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
655 msg->size = msgbuf_size;
657 msg->buf = (char *) &msg[1];
658 msg->transmit_cont = cont;
659 msg->transmit_cont_cls = cont_cls;
660 memcpy (msg->buf, msgbuf, msgbuf_size);
662 if (session->inbound == GNUNET_NO)
665 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
666 "Using outbound client session %p to send to `%session'\n", session,
667 GNUNET_i2s (&session->target));
670 client_send (session, msg);
673 if (session->inbound == GNUNET_YES)
676 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
677 "Using inbound server %p session to send to `%session'\n", session,
678 GNUNET_i2s (&session->target));
681 server_send (session, msg);
690 * Function that can be used to force the plugin to disconnect
691 * from the given peer and cancel all previous transmissions
692 * (and their continuationc).
695 * @param target peer from which to disconnect
698 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
700 struct Plugin *plugin = cls;
701 struct Session *next = NULL;
702 struct Session *s = plugin->head;
704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
705 "Transport tells me to disconnect `%s'\n",
706 GNUNET_i2s (target));
710 if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
712 if (s->inbound == GNUNET_NO)
713 GNUNET_assert (GNUNET_OK == client_disconnect (s));
715 GNUNET_assert (GNUNET_OK == server_disconnect (s));
716 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
718 struct HTTP_Message *msg = s->msg_head;
719 struct HTTP_Message *tmp = NULL;
725 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
726 if (msg->transmit_cont != NULL)
728 msg->transmit_cont (msg->transmit_cont_cls, target, GNUNET_SYSERR);
741 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
744 struct Plugin *plugin = cls;
745 struct IPv4HttpAddressWrapper *w_t4 = NULL;
746 struct IPv6HttpAddressWrapper *w_t6 = NULL;
749 af = addr->sa_family;
753 w_t4 = plugin->ipv4_addr_head;
754 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
758 int res = memcmp (&w_t4->addr.ipv4_addr,
760 sizeof (struct in_addr));
764 if (a4->sin_port != w_t4->addr.u4_port)
774 w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
775 memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
776 w_t4->addr.u4_port = a4->sin_port;
778 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
779 plugin->ipv4_addr_tail, w_t4);
782 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
783 "Notifying transport to add IPv4 address `%s'\n",
784 http_plugin_address_to_string (NULL, &w_t4->addr,
788 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
789 sizeof (struct IPv4HttpAddress));
793 w_t6 = plugin->ipv6_addr_head;
794 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
798 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
799 sizeof (struct in6_addr));
803 if (a6->sin6_port != w_t6->addr6.u6_port)
812 w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
814 memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
815 w_t6->addr6.u6_port = a6->sin6_port;
817 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
818 plugin->ipv6_addr_tail, w_t6);
821 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
822 "Notifying transport to add IPv6 address `%s'\n",
823 http_plugin_address_to_string (NULL, &w_t6->addr6,
827 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
828 sizeof (struct IPv6HttpAddress));
837 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
840 struct Plugin *plugin = cls;
841 struct IPv4HttpAddressWrapper *w_t4 = NULL;
842 struct IPv6HttpAddressWrapper *w_t6 = NULL;
845 af = addr->sa_family;
849 w_t4 = plugin->ipv4_addr_head;
850 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
854 int res = memcmp (&w_t4->addr.ipv4_addr,
856 sizeof (struct in_addr));
860 if (a4->sin_port != w_t4->addr.u4_port)
872 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
873 "Notifying transport to remove IPv4 address `%s'\n",
874 http_plugin_address_to_string (NULL, &w_t4->addr,
878 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
879 sizeof (struct IPv4HttpAddress));
881 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
886 w_t6 = plugin->ipv6_addr_head;
887 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
891 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
892 sizeof (struct in6_addr));
896 if (a6->sin6_port != w_t6->addr6.u6_port)
906 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
907 "Notifying transport to remove IPv6 address `%s'\n",
908 http_plugin_address_to_string (NULL, &w_t6->addr6,
912 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
913 sizeof (struct IPv6HttpAddress));
915 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
926 * Our external IP address/port mapping has changed.
928 * @param cls closure, the 'struct LocalAddrList'
929 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
930 * the previous (now invalid) one
931 * @param addr either the previous or the new public IP address
932 * @param addrlen actual lenght of the address
935 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
938 GNUNET_assert (cls != NULL);
940 struct Plugin *plugin = cls;
943 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
944 "NPMC called %s to address `%s'\n",
945 (add_remove == GNUNET_NO) ? "remove" : "add",
946 GNUNET_a2s (addr, addrlen));
951 nat_add_address (cls, add_remove, addr, addrlen);
954 nat_remove_address (cls, add_remove, addr, addrlen);
960 http_check_ipv6 (struct Plugin *plugin)
962 struct GNUNET_NETWORK_Handle *desc = NULL;
964 if (plugin->ipv6 == GNUNET_YES)
966 /* probe IPv6 support */
967 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
970 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
973 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
975 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
977 ("Disabling IPv6 since it is not supported on this system!\n"));
978 plugin->ipv6 = GNUNET_NO;
982 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
986 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
987 "Testing IPv6 on this system: %s\n",
988 (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
993 http_get_addresses (struct Plugin *plugin, const char *serviceName,
994 const struct GNUNET_CONFIGURATION_Handle *cfg,
995 struct sockaddr ***addrs, socklen_t ** addr_lens)
998 unsigned long long port;
999 struct addrinfo hints;
1000 struct addrinfo *res;
1001 struct addrinfo *pos;
1002 struct addrinfo *next;
1006 struct sockaddr **saddrs;
1007 socklen_t *saddrlens;
1013 disablev6 = !plugin->ipv6;
1016 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
1018 GNUNET_break (GNUNET_OK ==
1019 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
1023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1025 ("Require valid port number for service in configuration!\n"));
1026 return GNUNET_SYSERR;
1030 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
1032 GNUNET_break (GNUNET_OK ==
1033 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
1034 "BINDTO", &hostname));
1039 if (hostname != NULL)
1041 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1042 "Resolving `%s' since that is where `%s' will bind to.\n",
1043 hostname, serviceName);
1044 memset (&hints, 0, sizeof (struct addrinfo));
1046 hints.ai_family = AF_INET;
1047 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
1051 hostname, gai_strerror (ret));
1052 GNUNET_free (hostname);
1053 return GNUNET_SYSERR;
1057 while (NULL != (pos = next))
1059 next = pos->ai_next;
1060 if ((disablev6) && (pos->ai_family == AF_INET6))
1066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1067 _("Failed to find %saddress for `%s'.\n"),
1068 disablev6 ? "IPv4 " : "", hostname);
1070 GNUNET_free (hostname);
1071 return GNUNET_SYSERR;
1074 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1075 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1078 while (NULL != (pos = next))
1080 next = pos->ai_next;
1081 if ((disablev6) && (pos->ai_family == AF_INET6))
1083 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
1084 continue; /* not TCP */
1085 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
1086 continue; /* huh? */
1087 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1088 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
1090 if (pos->ai_family == AF_INET)
1092 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
1093 saddrlens[i] = pos->ai_addrlen;
1094 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1095 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1096 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1100 GNUNET_assert (pos->ai_family == AF_INET6);
1101 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1102 saddrlens[i] = pos->ai_addrlen;
1103 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1104 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1105 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1109 GNUNET_free (hostname);
1115 /* will bind against everything, just set port */
1121 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1122 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1124 saddrlens[i] = sizeof (struct sockaddr_in);
1125 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1126 #if HAVE_SOCKADDR_IN_SIN_LEN
1127 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1129 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1130 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1136 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1137 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1139 saddrlens[i] = sizeof (struct sockaddr_in6);
1140 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1141 #if HAVE_SOCKADDR_IN_SIN_LEN
1142 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1144 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1145 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1147 saddrlens[i] = sizeof (struct sockaddr_in);
1148 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1149 #if HAVE_SOCKADDR_IN_SIN_LEN
1150 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1152 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1153 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1157 *addr_lens = saddrlens;
1162 start_report_addresses (struct Plugin *plugin)
1164 int res = GNUNET_OK;
1165 struct sockaddr **addrs;
1166 socklen_t *addrlens;
1169 http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
1171 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1172 _("Found %u addresses to report to NAT service\n"), res);
1174 if (res != GNUNET_SYSERR)
1177 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1179 (const struct sockaddr **) addrs, addrlens,
1180 &nat_port_map_callback, NULL, plugin);
1185 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, _("FREEING %s\n"),
1186 GNUNET_a2s (addrs[res], addrlens[res]));
1188 GNUNET_assert (addrs[res] != NULL);
1189 GNUNET_free (addrs[res]);
1191 GNUNET_free_non_null (addrs);
1192 GNUNET_free_non_null (addrlens);
1197 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1198 NULL, NULL, plugin);
1203 stop_report_addresses (struct Plugin *plugin)
1205 /* Stop NAT handle */
1206 GNUNET_NAT_unregister (plugin->nat);
1208 /* Clean up addresses */
1209 struct IPv4HttpAddressWrapper *w_t4;
1210 struct IPv6HttpAddressWrapper *w_t6;
1212 while (plugin->ipv4_addr_head != NULL)
1214 w_t4 = plugin->ipv4_addr_head;
1215 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1220 while (plugin->ipv6_addr_head != NULL)
1222 w_t6 = plugin->ipv6_addr_head;
1223 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1230 configure_plugin (struct Plugin *plugin)
1232 int res = GNUNET_OK;
1235 if (GNUNET_CONFIGURATION_have_value
1236 (plugin->env->cfg, plugin->name, "USE_IPv4"))
1239 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1243 plugin->ipv4 = GNUNET_YES;
1246 if (GNUNET_CONFIGURATION_have_value
1247 (plugin->env->cfg, plugin->name, "USE_IPv6"))
1250 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1254 plugin->ipv6 = GNUNET_YES;
1256 if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1258 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1260 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1262 res = GNUNET_SYSERR;
1265 /* Reading port number from config file */
1266 unsigned long long port;
1269 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1270 "PORT", &port)) || (port > 65535))
1272 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1273 _("Port is required! Fix in configuration\n"),
1275 res = GNUNET_SYSERR;
1278 plugin->port = port;
1280 plugin->client_only = GNUNET_NO;
1281 if (plugin->port == 0)
1283 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1284 _("Port 0, client only mode\n"));
1285 plugin->client_only = GNUNET_YES;
1288 char *bind4_address = NULL;
1290 if ((plugin->ipv4 == GNUNET_YES) &&
1292 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1293 "BINDTO", &bind4_address)))
1295 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1296 "Binding %s plugin to specific IPv4 address: `%s'\n",
1297 plugin->protocol, bind4_address);
1298 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1300 inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1302 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1304 ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1305 bind4_address, plugin->protocol);
1306 GNUNET_free (plugin->server_addr_v4);
1307 plugin->server_addr_v4 = NULL;
1311 plugin->server_addr_v4->sin_family = AF_INET;
1312 plugin->server_addr_v4->sin_port = htons (plugin->port);
1314 GNUNET_free (bind4_address);
1318 char *bind6_address = NULL;
1320 if ((plugin->ipv6 == GNUNET_YES) &&
1322 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1323 "BINDTO6", &bind6_address)))
1325 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1326 "Binding %s plugin to specific IPv6 address: `%s'\n",
1327 plugin->protocol, bind6_address);
1328 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1330 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1332 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1334 ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1335 bind6_address, plugin->protocol);
1336 GNUNET_free (plugin->server_addr_v6);
1337 plugin->server_addr_v6 = NULL;
1341 plugin->server_addr_v6->sin6_family = AF_INET6;
1342 plugin->server_addr_v6->sin6_port = htons (plugin->port);
1344 GNUNET_free (bind6_address);
1348 /* Optional parameters */
1349 unsigned long long maxneigh;
1352 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1353 "MAX_CONNECTIONS", &maxneigh))
1355 plugin->max_connections = maxneigh;
1362 * Entry point for the plugin.
1365 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1367 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1368 struct GNUNET_TRANSPORT_PluginFunctions *api;
1369 struct Plugin *plugin;
1372 plugin = GNUNET_malloc (sizeof (struct Plugin));
1374 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1376 api->disconnect = &http_plugin_disconnect;
1377 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1378 api->check_address = &http_plugin_address_suggested;
1379 api->address_to_string = &http_plugin_address_to_string;
1380 api->get_session = &http_get_session;
1381 api->send = &http_plugin_send;
1384 plugin->name = "transport-https";
1385 plugin->protocol = "https";
1387 plugin->name = "transport-http";
1388 plugin->protocol = "http";
1390 /* Configure plugin from configuration */
1391 res = configure_plugin (plugin);
1392 if (res == GNUNET_SYSERR)
1394 GNUNET_free_non_null (plugin->server_addr_v4);
1395 GNUNET_free_non_null (plugin->server_addr_v6);
1396 GNUNET_free (plugin);
1401 /* checking IPv6 support */
1402 http_check_ipv6 (plugin);
1405 res = client_start (plugin);
1406 if (res == GNUNET_SYSERR)
1408 GNUNET_free_non_null (plugin->server_addr_v4);
1409 GNUNET_free_non_null (plugin->server_addr_v6);
1410 GNUNET_free (plugin);
1416 if (plugin->client_only == GNUNET_NO)
1418 res = server_start (plugin);
1419 if (res == GNUNET_SYSERR)
1421 server_stop (plugin);
1422 client_stop (plugin);
1424 GNUNET_free_non_null (plugin->server_addr_v4);
1425 GNUNET_free_non_null (plugin->server_addr_v6);
1426 GNUNET_free (plugin);
1431 /* Report addresses to transport service */
1432 start_report_addresses (plugin);
1435 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1436 "Plugin `%s' loaded\n", plugin->name);
1444 * Exit point from the plugin.
1447 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1449 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1450 struct Plugin *plugin = api->cls;
1451 struct Session *s = NULL;
1453 /* Stop reporting addresses to transport service */
1454 stop_report_addresses (plugin);
1456 /* cleaning up sessions */
1461 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1462 "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1464 if (s->inbound == GNUNET_NO)
1465 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1467 GNUNET_assert (GNUNET_OK == server_disconnect (s));
1472 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
1475 server_stop (plugin);
1478 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
1481 client_stop (plugin);
1483 /* deleting up sessions */
1487 struct Session *t = s->next;
1489 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1491 struct HTTP_Message *msg = s->msg_head;
1492 struct HTTP_Message *tmp = NULL;
1498 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1499 if (msg->transmit_cont != NULL)
1501 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1514 "Plugin `%s' unloaded\n", plugin->name);
1517 GNUNET_free_non_null (plugin->server_addr_v4);
1518 GNUNET_free_non_null (plugin->server_addr_v6);
1519 GNUNET_free (plugin);
1525 /* end of plugin_transport_http.c */