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);
158 * Convert the transports address to a nice, human-readable
162 * @param type name of the transport that generated the address
163 * @param addr one of the addresses of the host, NULL for the last address
164 * the specific address format depends on the transport
165 * @param addrlen length of the address
166 * @param numeric should (IP) addresses be displayed in numeric form?
167 * @param timeout after how long should we give up?
168 * @param asc function to call on each string
169 * @param asc_cls closure for asc
172 http_plugin_address_pretty_printer (void *cls, const char *type,
173 const void *addr, size_t addrlen,
175 struct GNUNET_TIME_Relative timeout,
176 GNUNET_TRANSPORT_AddressStringCallback asc,
179 GNUNET_assert (cls != NULL);
180 struct PrettyPrinterContext *ppc;
182 struct sockaddr_in s4;
183 struct sockaddr_in6 s6;
187 if ((addrlen == sizeof (struct IPv6HttpAddress)) && (addr != NULL))
189 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
190 s6.sin6_family = AF_INET6;
191 s6.sin6_addr = a6->ipv6_addr;
192 s6.sin6_port = a6->u6_port;
193 #if HAVE_SOCKADDR_IN_SIN_LEN
194 s6.sin6_len = sizeof (struct sockaddr_in6);
197 sbs = sizeof (struct sockaddr_in6);
198 port = ntohs (a6->u6_port);
201 else if ((addrlen == sizeof (struct IPv4HttpAddress)) && (addr != NULL))
203 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
205 s4.sin_family = AF_INET;
206 s4.sin_addr.s_addr = a4->ipv4_addr;
207 s4.sin_port = a4->u4_port;
208 #if HAVE_SOCKADDR_IN_SIN_LEN
209 s4.sin_len = sizeof (struct sockaddr_in);
212 sbs = sizeof (struct sockaddr_in);
213 port = ntohs (a4->u4_port);
217 /* invalid address */
222 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
224 ppc->asc_cls = asc_cls;
227 ppc->addrlen = addrlen;
228 ppc->numeric = numeric;
229 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
235 * Another peer has suggested an address for this
236 * peer and transport plugin. Check that this could be a valid
237 * address. If so, consider adding it to the list
241 * @param addr pointer to the address
242 * @param addrlen length of addr
243 * @return GNUNET_OK if this is a plausible address for this peer
247 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
250 struct Plugin *plugin = cls;
251 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
252 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
256 GNUNET_assert (cls != NULL);
257 if ((addrlen != sizeof (struct sockaddr_in)) ||
258 (addrlen != sizeof (struct sockaddr_in6)))
259 return GNUNET_SYSERR;
261 if (addrlen == sizeof (struct IPv4HttpAddress))
263 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
265 while (w_tv4 != NULL)
268 memcmp (&w_tv4->addr.ipv4_addr, &a4->ipv4_addr,
269 sizeof (struct in_addr))) &&
270 (w_tv4->addr.u4_port == a4->u4_port))
277 return GNUNET_SYSERR;
279 if (addrlen == sizeof (struct sockaddr_in6))
281 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
283 while (w_tv6 != NULL)
286 memcmp (&w_tv6->addr6.ipv6_addr, &a6->ipv6_addr,
287 sizeof (struct in6_addr))) &&
288 (w_tv6->addr6.u6_port == a6->u6_port))
295 return GNUNET_SYSERR;
300 struct GNUNET_TIME_Relative
301 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
302 const struct GNUNET_MessageHeader *message,
303 struct Session *session, const char *sender_address,
304 uint16_t sender_address_len)
306 struct Session *s = cls;
307 struct Plugin *plugin = s->plugin;
308 struct GNUNET_TIME_Relative delay;
309 struct GNUNET_ATS_Information atsi[2];
311 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
312 atsi[0].value = htonl (1);
313 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
314 atsi[1].value = session->ats_address_network_type;
315 GNUNET_break (session->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
318 plugin->env->receive (plugin->env->cls, &s->target, message,
319 (const struct GNUNET_ATS_Information *) &atsi,
320 2, s, s->addr, s->addrlen);
326 * Function called to convert a string address to
329 * @param cls closure ('struct Plugin*')
330 * @param addr string address
331 * @param addrlen length of the address
332 * @param buf location to store the buffer
333 * If the function returns GNUNET_SYSERR, its contents are undefined.
334 * @param added length of created address
335 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
337 int http_string_to_address (void *cls,
344 char *protocol = "http";
346 char *protocol = "https";
348 char *addr_str = NULL;
349 struct sockaddr_in addr_4;
350 struct sockaddr_in6 addr_6;
351 struct IPv4HttpAddress * http_4addr;
352 struct IPv6HttpAddress * http_6addr;
353 if ((addr == NULL) || (addrlen == 0) || (buf == NULL))
354 return GNUNET_SYSERR;
356 /* protocoll + "://" + ":" */
357 if (addrlen <= (strlen (protocol) + 4))
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 "Invalid address string `%s' to convert to address\n",
363 return GNUNET_SYSERR;
366 if (NULL == (addr_str = strstr(addr, "://")))
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 "Invalid address string `%s' to convert to address\n",
372 return GNUNET_SYSERR;
374 addr_str = &addr_str[3];
376 if (addr_str[strlen(addr_str)-1] == '/')
377 addr_str[strlen(addr_str)-1] = '\0';
379 if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv4(addr_str, strlen(addr_str), &addr_4))
381 http_4addr = GNUNET_malloc (sizeof (struct IPv4HttpAddress));
382 http_4addr->u4_port = addr_4.sin_port;
383 http_4addr->ipv4_addr = (uint32_t) addr_4.sin_addr.s_addr;
385 (*added) = sizeof (struct IPv4HttpAddress);
388 else if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv6(addr_str, strlen(addr_str), &addr_6))
390 http_6addr = GNUNET_malloc (sizeof (struct IPv6HttpAddress));
391 http_6addr->u6_port = addr_6.sin6_port;
392 http_6addr->ipv6_addr = addr_6.sin6_addr;
394 (*added) = sizeof (struct IPv6HttpAddress);
399 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
400 "Invalid address string `%s' to convert to address\n",
403 return GNUNET_SYSERR;
410 * Function called for a quick conversion of the binary address to
411 * a numeric address. Note that the caller must not free the
412 * address and that the next call to this function is allowed
413 * to override the address again.
416 * @param addr binary address
417 * @param addrlen length of the address
418 * @return string representing the same address
421 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
424 struct IPv4HttpAddress *a4;
425 struct IPv6HttpAddress *a6;
427 static char rbuf[INET6_ADDRSTRLEN + 13];
431 if (addrlen == sizeof (struct IPv6HttpAddress))
433 a6 = (struct IPv6HttpAddress *) addr;
434 address = GNUNET_malloc (INET6_ADDRSTRLEN);
435 GNUNET_assert (NULL !=
436 inet_ntop (AF_INET6, &a6->ipv6_addr, address,
438 port = ntohs (a6->u6_port);
440 else if (addrlen == sizeof (struct IPv4HttpAddress))
442 a4 = (struct IPv4HttpAddress *) addr;
443 address = GNUNET_malloc (INET_ADDRSTRLEN);
444 GNUNET_assert (NULL !=
445 inet_ntop (AF_INET, &(a4->ipv4_addr), address,
447 port = ntohs (a4->u4_port);
451 /* invalid address */
456 char *protocol = "http";
458 char *protocol = "https";
461 GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
462 if (addrlen == sizeof (struct IPv6HttpAddress))
464 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol,
466 else if (addrlen == sizeof (struct IPv4HttpAddress))
468 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address,
471 GNUNET_free (address);
472 GNUNET_assert (res != 0);
477 lookup_session_old (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
478 struct Session *session, const void *addr, size_t addrlen,
485 for (t = plugin->head; NULL != t; t = t->next)
488 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
489 "Comparing peer `%s' address `%s' len %i session %X to \n",
490 GNUNET_i2s (target), GNUNET_a2s (addr, addrlen), addrlen,
492 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
493 "peer `%s' address `%s' len %i session %X \n\n",
494 GNUNET_i2s (&t->target), GNUNET_a2s (t->addr, t->addrlen),
496 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "memcmp %i \n",
497 memcmp (addr, t->addr, addrlen));
501 if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
504 if ( (addrlen == t->addrlen) &&
505 (0 == memcmp (addr, t->addr, addrlen)) )
507 if ( (t == session) &&
508 (t->addrlen == session->addrlen) &&
509 (0 == memcmp (session->addr, t->addr, t->addrlen)) )
513 if ( ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO)) ||
514 ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES)) ||
515 ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR)) )
522 lookup_session (struct Plugin *plugin,
523 const struct GNUNET_HELLO_Address *address)
527 for (pos = plugin->head; NULL != pos; pos = pos->next)
528 if ( (0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
529 (address->address_length == pos->addrlen) &&
530 (0 == memcmp (address->address, pos->addr, pos->addrlen)) )
537 delete_session (struct Session *s)
539 if (s->msg_tk != NULL)
541 GNUNET_SERVER_mst_destroy (s->msg_tk);
544 GNUNET_free (s->addr);
545 GNUNET_free_non_null (s->server_recv);
546 GNUNET_free_non_null (s->server_send);
551 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
552 const void *addr, size_t addrlen,
553 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
555 struct Session *s = NULL;
557 GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
558 (addrlen == sizeof (struct IPv4HttpAddress)));
559 s = GNUNET_malloc (sizeof (struct Session));
560 memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
562 s->addr = GNUNET_malloc (addrlen);
563 memcpy (s->addr, addr, addrlen);
564 s->addrlen = addrlen;
566 s->next_receive = GNUNET_TIME_absolute_get_zero ();
567 s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
572 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
575 struct Plugin *plugin = cls;
577 plugin->env->session_end (plugin->env->cls, peer, s);
578 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
583 * Creates a new outbound session the transport service will use to send data to the
586 * @param cls the plugin
587 * @param address the address
588 * @return the session or NULL of max connections exceeded
591 static struct Session *
592 http_get_session (void *cls,
593 const struct GNUNET_HELLO_Address *address)
595 struct Plugin *plugin = cls;
596 struct Session * s = NULL;
597 struct GNUNET_ATS_Information ats;
600 GNUNET_assert (plugin != NULL);
601 GNUNET_assert (address != NULL);
602 GNUNET_assert (address->address != NULL);
604 ats.type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
605 ats.value = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
607 /* find existing session */
608 s = lookup_session (plugin, address);
612 if (plugin->max_connections <= plugin->cur_connections)
614 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
615 "Maximum number of connections reached, "
616 "cannot connect to peer `%s'\n", GNUNET_i2s (&address->peer));
620 /* create new session */
621 addrlen = address->address_length;
623 GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
624 (addrlen == sizeof (struct IPv4HttpAddress)));
626 s = GNUNET_malloc (sizeof (struct Session));
627 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
629 s->addr = GNUNET_malloc (address->address_length);
630 memcpy (s->addr, address->address, address->address_length);
631 s->addrlen = addrlen;
633 s->next_receive = GNUNET_TIME_absolute_get_zero ();
634 s->inbound = GNUNET_NO;
635 s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
638 if (addrlen == sizeof (struct IPv4HttpAddress))
640 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) address->address;
641 struct sockaddr_in s4;
643 s4.sin_family = AF_INET;
644 s4.sin_addr.s_addr = a4->ipv4_addr;
645 s4.sin_port = a4->u4_port;
646 #if HAVE_SOCKADDR_IN_SIN_LEN
647 s4.sin_len = sizeof (struct sockaddr_in);
649 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s4, sizeof (struct sockaddr_in));
651 if (addrlen == sizeof (struct IPv6HttpAddress))
653 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) address->address;
654 struct sockaddr_in6 s6;
656 s6.sin6_family = AF_INET6;
657 s6.sin6_addr = a6->ipv6_addr;
658 s6.sin6_port = a6->u6_port;
659 #if HAVE_SOCKADDR_IN_SIN_LEN
660 s6.sin6_len = sizeof (struct sockaddr_in6);
662 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
664 s->ats_address_network_type = ats.value;
666 /* add new session */
667 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
668 /* initiate new connection */
669 if (GNUNET_SYSERR == client_connect (s))
671 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
672 "Cannot connect to peer `%s' address `%s''\n",
673 http_plugin_address_to_string(NULL, s->addr, s->addrlen),
674 GNUNET_i2s (&s->target));
675 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
684 * Function that can be used by the transport service to transmit
685 * a message using the plugin. Note that in the case of a
686 * peer disconnecting, the continuation MUST be called
687 * prior to the disconnect notification itself. This function
688 * will be called with this peer's HELLO message to initiate
689 * a fresh connection to another peer.
692 * @param session which session must be used
693 * @param msgbuf the message to transmit
694 * @param msgbuf_size number of bytes in 'msgbuf'
695 * @param priority how important is the message (most plugins will
696 * ignore message priority and just FIFO)
697 * @param to how long to wait at most for the transmission (does not
698 * require plugins to discard the message after the timeout,
699 * just advisory for the desired delay; most plugins will ignore
701 * @param cont continuation to call once the message has
702 * been transmitted (or if the transport is ready
703 * for the next transmission call; or if the
704 * peer disconnected...); can be NULL
705 * @param cont_cls closure for cont
706 * @return number of bytes used (on the physical network, with overheads);
707 * -1 on hard errors (i.e. address invalid); 0 is a legal value
708 * and does NOT mean that the message was not transmitted (DV)
711 http_plugin_send (void *cls,
712 struct Session *session,
713 const char *msgbuf, size_t msgbuf_size,
714 unsigned int priority,
715 struct GNUNET_TIME_Relative to,
716 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
718 struct Plugin *plugin = cls;
719 struct HTTP_Message *msg;
723 GNUNET_assert (plugin != NULL);
724 GNUNET_assert (session != NULL);
726 /* lookup if session is really existing */
730 if ((tmp == session) &&
731 (0 == memcmp (&session->target, &tmp->target, sizeof (struct GNUNET_PeerIdentity))) &&
732 (session->addrlen == tmp->addrlen) &&
733 (0 == memcmp (session->addr, tmp->addr, tmp->addrlen)))
743 /* create new message and schedule */
745 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
747 msg->size = msgbuf_size;
749 msg->buf = (char *) &msg[1];
750 msg->transmit_cont = cont;
751 msg->transmit_cont_cls = cont_cls;
752 memcpy (msg->buf, msgbuf, msgbuf_size);
754 if (session->inbound == GNUNET_NO)
757 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
758 "Using outbound client session %p to send to `%session'\n", session,
759 GNUNET_i2s (&session->target));
762 client_send (session, msg);
765 if (session->inbound == GNUNET_YES)
768 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
769 "Using inbound server %p session to send to `%session'\n", session,
770 GNUNET_i2s (&session->target));
773 server_send (session, msg);
782 * Function that can be used to force the plugin to disconnect
783 * from the given peer and cancel all previous transmissions
784 * (and their continuationc).
787 * @param target peer from which to disconnect
790 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
792 struct Plugin *plugin = cls;
793 struct Session *next = NULL;
794 struct Session *s = plugin->head;
796 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
797 "Transport tells me to disconnect `%s'\n",
798 GNUNET_i2s (target));
802 if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
804 if (s->inbound == GNUNET_NO)
805 GNUNET_assert (GNUNET_OK == client_disconnect (s));
807 GNUNET_assert (GNUNET_OK == server_disconnect (s));
808 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
810 struct HTTP_Message *msg = s->msg_head;
811 struct HTTP_Message *tmp = NULL;
817 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
818 if (msg->transmit_cont != NULL)
820 msg->transmit_cont (msg->transmit_cont_cls, target, GNUNET_SYSERR);
833 find_address (struct Plugin *plugin, const struct sockaddr *addr, socklen_t addrlen)
836 struct IPv4HttpAddressWrapper *w_t4 = NULL;
837 struct IPv6HttpAddressWrapper *w_t6 = NULL;
839 af = addr->sa_family;
843 w_t4 = plugin->ipv4_addr_head;
844 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
848 int res = memcmp (&w_t4->addr.ipv4_addr,
850 sizeof (struct in_addr));
854 if (a4->sin_port != w_t4->addr.u4_port)
865 w_t6 = plugin->ipv6_addr_head;
866 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
870 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
871 sizeof (struct in6_addr));
875 if (a6->sin6_port != w_t6->addr6.u6_port)
891 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
894 struct Plugin *plugin = cls;
895 struct IPv4HttpAddressWrapper *w_t4 = NULL;
896 struct IPv6HttpAddressWrapper *w_t6 = NULL;
899 af = addr->sa_family;
903 w_t4 = find_address (plugin, addr, addrlen);
906 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
907 w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
908 memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
909 w_t4->addr.u4_port = a4->sin_port;
911 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
912 plugin->ipv4_addr_tail, w_t4);
914 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
915 "Notifying transport to add IPv4 address `%s'\n",
916 http_plugin_address_to_string (NULL, &w_t4->addr,
919 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
920 sizeof (struct IPv4HttpAddress));
924 w_t6 = find_address (plugin, addr, addrlen);
927 w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
928 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
929 memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
930 w_t6->addr6.u6_port = a6->sin6_port;
932 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
933 plugin->ipv6_addr_tail, w_t6);
935 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
936 "Notifying transport to add IPv6 address `%s'\n",
937 http_plugin_address_to_string (NULL, &w_t6->addr6,
940 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
941 sizeof (struct IPv6HttpAddress));
951 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
954 struct Plugin *plugin = cls;
955 struct IPv4HttpAddressWrapper *w_t4 = NULL;
956 struct IPv6HttpAddressWrapper *w_t6 = NULL;
959 af = addr->sa_family;
963 w_t4 = find_address (plugin, addr, addrlen);
968 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
969 "Notifying transport to remove IPv4 address `%s'\n",
970 http_plugin_address_to_string (NULL, &w_t4->addr,
973 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
974 sizeof (struct IPv4HttpAddress));
976 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
981 w_t6 = find_address (plugin, addr, addrlen);
985 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
986 "Notifying transport to remove IPv6 address `%s'\n",
987 http_plugin_address_to_string (NULL, &w_t6->addr6,
991 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
992 sizeof (struct IPv6HttpAddress));
994 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1005 * Our external IP address/port mapping has changed.
1007 * @param cls closure, the 'struct LocalAddrList'
1008 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1009 * the previous (now invalid) one
1010 * @param addr either the previous or the new public IP address
1011 * @param addrlen actual lenght of the address
1014 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
1017 GNUNET_assert (cls != NULL);
1018 struct Plugin *plugin = cls;
1020 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1021 "NPMC called %s to address `%s'\n",
1022 (add_remove == GNUNET_NO) ? "remove" : "add",
1023 GNUNET_a2s (addr, addrlen));
1028 nat_add_address (cls, add_remove, addr, addrlen);
1031 nat_remove_address (cls, add_remove, addr, addrlen);
1037 http_check_ipv6 (struct Plugin *plugin)
1039 struct GNUNET_NETWORK_Handle *desc = NULL;
1041 if (plugin->ipv6 == GNUNET_YES)
1043 /* probe IPv6 support */
1044 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
1047 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
1050 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1052 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1054 ("Disabling IPv6 since it is not supported on this system!\n"));
1055 plugin->ipv6 = GNUNET_NO;
1059 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1063 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1064 "Testing IPv6 on this system: %s\n",
1065 (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
1070 http_get_addresses (struct Plugin *plugin, const char *serviceName,
1071 const struct GNUNET_CONFIGURATION_Handle *cfg,
1072 struct sockaddr ***addrs, socklen_t ** addr_lens)
1075 unsigned long long port;
1076 struct addrinfo hints;
1077 struct addrinfo *res;
1078 struct addrinfo *pos;
1079 struct addrinfo *next;
1083 struct sockaddr **saddrs;
1084 socklen_t *saddrlens;
1090 disablev6 = !plugin->ipv6;
1093 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
1095 GNUNET_break (GNUNET_OK ==
1096 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
1100 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1102 ("Require valid port number for service in configuration!\n"));
1103 return GNUNET_SYSERR;
1107 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
1109 GNUNET_break (GNUNET_OK ==
1110 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
1111 "BINDTO", &hostname));
1116 if (hostname != NULL)
1118 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1119 "Resolving `%s' since that is where `%s' will bind to.\n",
1120 hostname, serviceName);
1121 memset (&hints, 0, sizeof (struct addrinfo));
1123 hints.ai_family = AF_INET;
1124 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
1128 hostname, gai_strerror (ret));
1129 GNUNET_free (hostname);
1130 return GNUNET_SYSERR;
1134 while (NULL != (pos = next))
1136 next = pos->ai_next;
1137 if ((disablev6) && (pos->ai_family == AF_INET6))
1143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1144 _("Failed to find %saddress for `%s'.\n"),
1145 disablev6 ? "IPv4 " : "", hostname);
1147 GNUNET_free (hostname);
1148 return GNUNET_SYSERR;
1151 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1152 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1155 while (NULL != (pos = next))
1157 next = pos->ai_next;
1158 if ((disablev6) && (pos->ai_family == AF_INET6))
1160 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
1161 continue; /* not TCP */
1162 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
1163 continue; /* huh? */
1164 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1165 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
1167 if (pos->ai_family == AF_INET)
1169 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
1170 saddrlens[i] = pos->ai_addrlen;
1171 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1172 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1173 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1177 GNUNET_assert (pos->ai_family == AF_INET6);
1178 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1179 saddrlens[i] = pos->ai_addrlen;
1180 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1181 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1182 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1186 GNUNET_free (hostname);
1192 /* will bind against everything, just set port */
1198 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1199 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1201 saddrlens[i] = sizeof (struct sockaddr_in);
1202 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1203 #if HAVE_SOCKADDR_IN_SIN_LEN
1204 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1206 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1207 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1213 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1214 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1216 saddrlens[i] = sizeof (struct sockaddr_in6);
1217 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1218 #if HAVE_SOCKADDR_IN_SIN_LEN
1219 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1221 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1222 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1224 saddrlens[i] = sizeof (struct sockaddr_in);
1225 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1226 #if HAVE_SOCKADDR_IN_SIN_LEN
1227 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1229 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1230 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1234 *addr_lens = saddrlens;
1239 start_report_addresses (struct Plugin *plugin)
1241 int res = GNUNET_OK;
1242 struct sockaddr **addrs;
1243 socklen_t *addrlens;
1246 http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
1248 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1249 _("Found %u addresses to report to NAT service\n"), res);
1251 if (res != GNUNET_SYSERR)
1254 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1256 (const struct sockaddr **) addrs, addrlens,
1257 &nat_port_map_callback, NULL, plugin);
1262 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, _("FREEING %s\n"),
1263 GNUNET_a2s (addrs[res], addrlens[res]));
1265 GNUNET_assert (addrs[res] != NULL);
1266 GNUNET_free (addrs[res]);
1268 GNUNET_free_non_null (addrs);
1269 GNUNET_free_non_null (addrlens);
1274 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1275 NULL, NULL, plugin);
1280 stop_report_addresses (struct Plugin *plugin)
1282 /* Stop NAT handle */
1283 GNUNET_NAT_unregister (plugin->nat);
1285 /* Clean up addresses */
1286 struct IPv4HttpAddressWrapper *w_t4;
1287 struct IPv6HttpAddressWrapper *w_t6;
1289 while (plugin->ipv4_addr_head != NULL)
1291 w_t4 = plugin->ipv4_addr_head;
1292 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1297 while (plugin->ipv6_addr_head != NULL)
1299 w_t6 = plugin->ipv6_addr_head;
1300 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1307 configure_plugin (struct Plugin *plugin)
1309 int res = GNUNET_OK;
1312 if (GNUNET_CONFIGURATION_have_value
1313 (plugin->env->cfg, plugin->name, "USE_IPv4"))
1316 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1320 plugin->ipv4 = GNUNET_YES;
1323 if (GNUNET_CONFIGURATION_have_value
1324 (plugin->env->cfg, plugin->name, "USE_IPv6"))
1327 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1331 plugin->ipv6 = GNUNET_YES;
1333 if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1335 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1337 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1339 res = GNUNET_SYSERR;
1342 /* Reading port number from config file */
1343 unsigned long long port;
1346 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1347 "PORT", &port)) || (port > 65535))
1349 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1350 _("Port is required! Fix in configuration\n"),
1352 res = GNUNET_SYSERR;
1355 plugin->port = port;
1357 plugin->client_only = GNUNET_NO;
1358 if (plugin->port == 0)
1360 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1361 _("Port 0, client only mode\n"));
1362 plugin->client_only = GNUNET_YES;
1365 char *bind4_address = NULL;
1367 if ((plugin->ipv4 == GNUNET_YES) &&
1369 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1370 "BINDTO", &bind4_address)))
1372 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1373 "Binding %s plugin to specific IPv4 address: `%s'\n",
1374 plugin->protocol, bind4_address);
1375 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1377 inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1379 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1381 ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1382 bind4_address, plugin->protocol);
1383 GNUNET_free (plugin->server_addr_v4);
1384 plugin->server_addr_v4 = NULL;
1388 plugin->server_addr_v4->sin_family = AF_INET;
1389 plugin->server_addr_v4->sin_port = htons (plugin->port);
1391 GNUNET_free (bind4_address);
1395 char *bind6_address = NULL;
1397 if ((plugin->ipv6 == GNUNET_YES) &&
1399 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1400 "BINDTO6", &bind6_address)))
1402 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1403 "Binding %s plugin to specific IPv6 address: `%s'\n",
1404 plugin->protocol, bind6_address);
1405 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1407 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1409 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1411 ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1412 bind6_address, plugin->protocol);
1413 GNUNET_free (plugin->server_addr_v6);
1414 plugin->server_addr_v6 = NULL;
1418 plugin->server_addr_v6->sin6_family = AF_INET6;
1419 plugin->server_addr_v6->sin6_port = htons (plugin->port);
1421 GNUNET_free (bind6_address);
1425 /* Optional parameters */
1426 unsigned long long maxneigh;
1429 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1430 "MAX_CONNECTIONS", &maxneigh))
1432 plugin->max_connections = maxneigh;
1439 * Entry point for the plugin.
1442 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1444 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1445 struct GNUNET_TRANSPORT_PluginFunctions *api;
1446 struct Plugin *plugin;
1449 if (NULL == env->receive)
1451 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1452 initialze the plugin or the API */
1453 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1455 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1456 api->address_to_string = &http_plugin_address_to_string;
1457 api->string_to_address = &http_string_to_address;
1461 plugin = GNUNET_malloc (sizeof (struct Plugin));
1463 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1465 api->disconnect = &http_plugin_disconnect;
1466 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1467 api->check_address = &http_plugin_address_suggested;
1468 api->address_to_string = &http_plugin_address_to_string;
1469 api->string_to_address = &http_string_to_address;
1470 api->get_session = &http_get_session;
1471 api->send = &http_plugin_send;
1474 plugin->name = "transport-https";
1475 plugin->protocol = "https";
1477 plugin->name = "transport-http";
1478 plugin->protocol = "http";
1480 /* Configure plugin from configuration */
1481 res = configure_plugin (plugin);
1482 if (res == GNUNET_SYSERR)
1484 GNUNET_free_non_null (plugin->server_addr_v4);
1485 GNUNET_free_non_null (plugin->server_addr_v6);
1486 GNUNET_free (plugin);
1491 /* checking IPv6 support */
1492 http_check_ipv6 (plugin);
1495 res = client_start (plugin);
1496 if (res == GNUNET_SYSERR)
1498 GNUNET_free_non_null (plugin->server_addr_v4);
1499 GNUNET_free_non_null (plugin->server_addr_v6);
1500 GNUNET_free (plugin);
1506 if (plugin->client_only == GNUNET_NO)
1508 res = server_start (plugin);
1509 if (res == GNUNET_SYSERR)
1511 server_stop (plugin);
1512 client_stop (plugin);
1514 GNUNET_free_non_null (plugin->server_addr_v4);
1515 GNUNET_free_non_null (plugin->server_addr_v6);
1516 GNUNET_free (plugin);
1521 /* Report addresses to transport service */
1522 start_report_addresses (plugin);
1525 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1526 "Plugin `%s' loaded\n", plugin->name);
1534 * Exit point from the plugin.
1537 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1539 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1540 struct Plugin *plugin = api->cls;
1549 /* Stop reporting addresses to transport service */
1550 stop_report_addresses (plugin);
1552 /* cleaning up sessions */
1557 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1558 "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1560 if (s->inbound == GNUNET_NO)
1561 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1563 GNUNET_assert (GNUNET_OK == server_disconnect (s));
1568 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
1571 server_stop (plugin);
1574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
1577 client_stop (plugin);
1579 /* deleting up sessions */
1583 struct Session *t = s->next;
1585 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1587 struct HTTP_Message *msg = s->msg_head;
1588 struct HTTP_Message *tmp = NULL;
1594 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1595 if (msg->transmit_cont != NULL)
1597 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1609 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1610 "Plugin `%s' unloaded\n", plugin->name);
1613 GNUNET_free_non_null (plugin->server_addr_v4);
1614 GNUNET_free_non_null (plugin->server_addr_v6);
1615 GNUNET_free (plugin);
1621 /* end of plugin_transport_http.c */