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)
38 * Wrapper to manage addresses
40 struct HttpAddressWrapper
45 struct HttpAddressWrapper *next;
48 * Linked list previous
50 struct HttpAddressWrapper *prev;
52 struct HttpAddress *addr;
57 * Encapsulation of all of the state of the plugin.
62 * Start session timeout
65 start_session_timeout (struct Session *s);
68 * Increment session timeout due to activity
71 reschedule_session_timeout (struct Session *s);
77 stop_session_timeout (struct Session *s);
81 * Convert the transports address to a nice, human-readable
85 * @param type name of the transport that generated the address
86 * @param addr one of the addresses of the host, NULL for the last address
87 * the specific address format depends on the transport
88 * @param addrlen length of the address
89 * @param numeric should (IP) addresses be displayed in numeric form?
90 * @param timeout after how long should we give up?
91 * @param asc function to call on each string
92 * @param asc_cls closure for @a asc
95 http_plugin_address_pretty_printer (void *cls, const char *type,
96 const void *addr, size_t addrlen,
98 struct GNUNET_TIME_Relative timeout,
99 GNUNET_TRANSPORT_AddressStringCallback asc,
102 const struct HttpAddress *haddr = addr;
104 GNUNET_assert (cls != NULL);
105 if (addrlen < (sizeof (struct HttpAddress)))
107 /* invalid address */
112 asc (asc_cls, haddr->addr);
118 * Another peer has suggested an address for this
119 * peer and transport plugin. Check that this could be a valid
120 * address. If so, consider adding it to the list
124 * @param addr pointer to the address
125 * @param addrlen length of addr
126 * @return GNUNET_OK if this is a plausible address for this peer
130 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
132 struct Plugin *plugin = cls;
133 struct HttpAddressWrapper *w = plugin->addr_head;
134 struct HttpAddress *haddr = (struct HttpAddress *) addr;
136 GNUNET_assert (cls != NULL);
138 if (addrlen <= sizeof (struct HttpAddress))
139 return GNUNET_SYSERR;
141 if (0 == (strcmp (plugin->ext_addr->addr, haddr->addr)))
146 if (0 == (strcmp (w->addr->addr, haddr->addr)))
150 return GNUNET_SYSERR;
153 struct GNUNET_TIME_Relative
154 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
155 const struct GNUNET_MessageHeader *message,
156 struct Session *session, const char *sender_address,
157 uint16_t sender_address_len)
159 struct Session *s = cls;
160 struct Plugin *plugin = s->plugin;
161 struct GNUNET_TIME_Relative delay;
162 struct GNUNET_ATS_Information atsi;
164 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
165 atsi.value = session->ats_address_network_type;
166 GNUNET_break (session->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
168 reschedule_session_timeout (session);
171 plugin->env->receive (plugin->env->cls, &s->target, message,
173 1, s, s->addr, s->addrlen);
179 * Function called to convert a string address to
182 * @param cls closure ('struct Plugin*')
183 * @param addr string address
184 * @param addrlen length of the address
185 * @param buf location to store the buffer
186 * If the function returns GNUNET_SYSERR, its contents are undefined.
187 * @param added length of created address
188 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
191 http_string_to_address (void *cls,
199 char *protocol = "http";
201 char *protocol = "https";
203 char *addr_str = NULL;
204 struct sockaddr_in addr_4;
205 struct sockaddr_in6 addr_6;
206 struct IPv4HttpAddress * http_4addr;
207 struct IPv6HttpAddress * http_6addr;
209 if ((NULL == addr) || (addrlen == 0))
212 return GNUNET_SYSERR;
215 if ('\0' != addr[addrlen - 1])
218 return GNUNET_SYSERR;
221 if (strlen (addr) != addrlen - 1)
224 return GNUNET_SYSERR;
227 /* protocoll + "://" + ":" */
228 if (addrlen <= (strlen (protocol) + 4))
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "Invalid address string `%s' to convert to address\n",
234 return GNUNET_SYSERR;
237 if (NULL == (addr_str = strstr(addr, "://")))
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240 "Invalid address string `%s' to convert to address\n",
243 return GNUNET_SYSERR;
245 addr_str = &addr_str[3];
247 if (addr_str[strlen(addr_str)-1] == '/')
248 addr_str[strlen(addr_str)-1] = '\0';
250 if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv4(addr_str, strlen(addr_str), &addr_4))
252 http_4addr = GNUNET_malloc (sizeof (struct IPv4HttpAddress));
253 http_4addr->u4_port = addr_4.sin_port;
254 http_4addr->ipv4_addr = (uint32_t) addr_4.sin_addr.s_addr;
256 (*added) = sizeof (struct IPv4HttpAddress);
259 if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv6(addr_str, strlen(addr_str), &addr_6))
261 http_6addr = GNUNET_malloc (sizeof (struct IPv6HttpAddress));
262 http_6addr->u6_port = addr_6.sin6_port;
263 http_6addr->ipv6_addr = addr_6.sin6_addr;
265 (*added) = sizeof (struct IPv6HttpAddress);
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269 "Invalid address string `%s' to convert to address\n",
272 return GNUNET_SYSERR;
278 * Function called for a quick conversion of the binary address to
279 * a numeric address. Note that the caller must not free the
280 * address and that the next call to this function is allowed
281 * to override the address again.
284 * @param addr binary address
285 * @param addrlen length of the address
286 * @return string representing the same address
289 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
291 struct HttpAddress *haddr;
292 if (addrlen < sizeof (struct HttpAddress))
294 /* invalid address */
300 haddr = (struct HttpAddress *) addr;
301 GNUNET_assert (NULL != haddr->addr);
302 return (const char *) haddr->addr;
308 lookup_session_old (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
309 struct Session *session, const void *addr, size_t addrlen,
316 for (t = plugin->head; NULL != t; t = t->next)
319 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
320 "Comparing peer `%s' address `%s' len %i session %p to \n",
321 GNUNET_i2s (target), GNUNET_a2s (addr, addrlen), addrlen,
323 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
324 "peer `%s' address `%s' len %i session %p \n\n",
325 GNUNET_i2s (&t->target), GNUNET_a2s (t->addr, t->addrlen),
327 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "memcmp %i \n",
328 memcmp (addr, t->addr, addrlen));
332 if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
335 if ( (addrlen == t->addrlen) &&
336 (0 == memcmp (addr, t->addr, addrlen)) )
338 if ( (t == session) &&
339 (t->addrlen == session->addrlen) &&
340 (0 == memcmp (session->addr, t->addr, t->addrlen)) )
344 if ( ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO)) ||
345 ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES)) ||
346 ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR)) )
354 lookup_session (struct Plugin *plugin,
355 const struct GNUNET_HELLO_Address *address)
359 for (pos = plugin->head; NULL != pos; pos = pos->next)
360 if ( (0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
361 (address->address_length == pos->addrlen) &&
362 (0 == memcmp (address->address, pos->addr, pos->addrlen)) )
369 exist_session (struct Plugin *plugin, struct Session *s)
371 struct Session * head;
373 GNUNET_assert (NULL != plugin);
374 GNUNET_assert (NULL != s);
376 for (head = plugin->head; head != NULL; head = head->next)
385 * Deleting the session
386 * Must not be used afterwards
390 delete_session (struct Session *s)
392 struct Plugin *plugin = s->plugin;
393 stop_session_timeout(s);
395 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
396 struct HTTP_Message *msg = s->msg_head;
397 struct HTTP_Message *tmp = NULL;
403 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
404 if (msg->transmit_cont != NULL)
406 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
412 if (s->msg_tk != NULL)
414 GNUNET_SERVER_mst_destroy (s->msg_tk);
417 GNUNET_free (s->addr);
418 GNUNET_free_non_null (s->server_recv);
419 GNUNET_free_non_null (s->server_send);
425 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
426 const void *addr, size_t addrlen)
428 struct Session *s = NULL;
429 struct GNUNET_ATS_Information ats;
432 * ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
434 if (addrlen < sizeof (struct HttpAddress))
442 s = GNUNET_malloc (sizeof (struct Session));
443 memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
445 s->addr = GNUNET_malloc (addrlen);
446 memcpy (s->addr, addr, addrlen);
447 s->addrlen = addrlen;
448 s->ats_address_network_type = ats.value;
451 start_session_timeout(s);
457 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
460 struct Plugin *plugin = cls;
462 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
463 "Notifying transport about ending session %p (`%s')\n",
465 http_plugin_address_to_string(NULL, s->addr,s->addrlen));
467 plugin->env->session_end (plugin->env->cls, peer, s);
473 * Creates a new outbound session the transport service will use to send data to the
476 * @param cls the plugin
477 * @param address the address
478 * @return the session or NULL of max connections exceeded
480 static struct Session *
481 http_get_session (void *cls,
482 const struct GNUNET_HELLO_Address *address)
484 struct Plugin *plugin = cls;
485 struct Session * s = NULL;
488 GNUNET_assert (plugin != NULL);
489 GNUNET_assert (address != NULL);
490 GNUNET_assert (address->address != NULL);
492 /* find existing session */
493 s = lookup_session (plugin, address);
497 if (plugin->max_connections <= plugin->cur_connections)
499 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
500 "Maximum number of connections reached, "
501 "cannot connect to peer `%s'\n", GNUNET_i2s (&address->peer));
505 /* create new session */
506 addrlen = address->address_length;
508 GNUNET_assert (addrlen > sizeof (struct HttpAddress));
510 s = create_session (plugin, &address->peer, address->address, address->address_length);
512 /* add new session */
513 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
514 /* initiate new connection */
515 if (GNUNET_SYSERR == client_connect (s))
517 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
518 "Cannot connect to peer `%s' address `%s''\n",
519 http_plugin_address_to_string(NULL, s->addr, s->addrlen),
520 GNUNET_i2s (&s->target));
521 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
531 * Function that can be used by the transport service to transmit
532 * a message using the plugin. Note that in the case of a
533 * peer disconnecting, the continuation MUST be called
534 * prior to the disconnect notification itself. This function
535 * will be called with this peer's HELLO message to initiate
536 * a fresh connection to another peer.
539 * @param session which session must be used
540 * @param msgbuf the message to transmit
541 * @param msgbuf_size number of bytes in 'msgbuf'
542 * @param priority how important is the message (most plugins will
543 * ignore message priority and just FIFO)
544 * @param to how long to wait at most for the transmission (does not
545 * require plugins to discard the message after the timeout,
546 * just advisory for the desired delay; most plugins will ignore
548 * @param cont continuation to call once the message has
549 * been transmitted (or if the transport is ready
550 * for the next transmission call; or if the
551 * peer disconnected...); can be NULL
552 * @param cont_cls closure for cont
553 * @return number of bytes used (on the physical network, with overheads);
554 * -1 on hard errors (i.e. address invalid); 0 is a legal value
555 * and does NOT mean that the message was not transmitted (DV)
558 http_plugin_send (void *cls,
559 struct Session *session,
560 const char *msgbuf, size_t msgbuf_size,
561 unsigned int priority,
562 struct GNUNET_TIME_Relative to,
563 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
565 struct Plugin *plugin = cls;
566 struct HTTP_Message *msg;
570 GNUNET_assert (plugin != NULL);
571 GNUNET_assert (session != NULL);
573 /* lookup if session is really existing */
577 if ((tmp == session) &&
578 (0 == memcmp (&session->target, &tmp->target, sizeof (struct GNUNET_PeerIdentity))) &&
579 (session->addrlen == tmp->addrlen) &&
580 (0 == memcmp (session->addr, tmp->addr, tmp->addrlen)))
590 /* create new message and schedule */
591 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
593 msg->size = msgbuf_size;
595 msg->buf = (char *) &msg[1];
596 msg->transmit_cont = cont;
597 msg->transmit_cont_cls = cont_cls;
598 memcpy (msg->buf, msgbuf, msgbuf_size);
600 reschedule_session_timeout (session);
602 if (session->inbound == GNUNET_NO)
604 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
605 "Using outbound client session %p to send to `%s'\n", session,
606 GNUNET_i2s (&session->target));
607 client_send (session, msg);
610 if (session->inbound == GNUNET_YES)
612 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
613 "Using inbound server %p session to send to `%s'\n", session,
614 GNUNET_i2s (&session->target));
615 server_send (session, msg);
624 * Function that can be used to force the plugin to disconnect
625 * from the given peer and cancel all previous transmissions
626 * (and their continuationc).
629 * @param target peer from which to disconnect
632 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
634 struct Plugin *plugin = cls;
635 struct Session *next = NULL;
636 struct Session *s = plugin->head;
638 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
639 "Transport tells me to disconnect `%s'\n",
640 GNUNET_i2s (target));
644 if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
646 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
647 "Disconnecting %s session %p to `%s'\n",
648 (s->inbound == GNUNET_NO) ? "outbound" : "inbound",
649 s, GNUNET_i2s (target));
651 if (s->inbound == GNUNET_NO)
652 GNUNET_assert (GNUNET_OK == client_disconnect (s));
654 GNUNET_assert (GNUNET_OK == server_disconnect (s));
662 find_address (struct Plugin *plugin, const struct sockaddr *addr, socklen_t addrlen)
664 struct HttpAddressWrapper *w = NULL;
667 GNUNET_asprintf(&saddr, "%s://%s", plugin->protocol, GNUNET_a2s (addr, addrlen));
668 w = plugin->addr_head;
671 if (0 == strcmp (saddr, w->addr->addr))
684 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
687 struct Plugin *plugin = cls;
688 struct HttpAddressWrapper *w = NULL;
692 GNUNET_asprintf(&saddr, "%s://%s", plugin->protocol, GNUNET_a2s (addr, addrlen));
694 haddrlen = sizeof (struct HttpAddress) + strlen(saddr) + 1;
695 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
696 w->addr = GNUNET_malloc (haddrlen);
697 w->addr->addr = &w->addr[1];
698 w->addr->addr_len = htonl (strlen(saddr) + 1);
699 memcpy (w->addr->addr, saddr, strlen(saddr) + 1);
702 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
703 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
704 "Notifying transport to add address `%s'\n", w->addr->addr);
706 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, haddrlen, "http");
711 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
714 struct Plugin *plugin = cls;
715 struct HttpAddressWrapper *w = NULL;
718 w = find_address (plugin, addr, addrlen);
722 haddrlen = sizeof (struct HttpAddress) + ntohl (w->addr->addr_len);
723 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
724 "Notifying transport to remove address `%s'\n", http_plugin_address_to_string(NULL, w->addr, haddrlen));
727 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
728 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr,
729 sizeof (struct HttpAddress) + ntohl (w->addr->addr_len), "http");
730 GNUNET_free (w->addr);
736 * Our external IP address/port mapping has changed.
738 * @param cls closure, the 'struct LocalAddrList'
739 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
740 * the previous (now invalid) one
741 * @param addr either the previous or the new public IP address
742 * @param addrlen actual lenght of the address
745 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
748 GNUNET_assert (cls != NULL);
749 struct Plugin *plugin = cls;
751 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
752 "NPMC called %s to address `%s'\n",
753 (add_remove == GNUNET_NO) ? "remove" : "add",
754 GNUNET_a2s (addr, addrlen));
759 nat_add_address (cls, add_remove, addr, addrlen);
762 nat_remove_address (cls, add_remove, addr, addrlen);
769 http_check_ipv6 (struct Plugin *plugin)
771 struct GNUNET_NETWORK_Handle *desc = NULL;
773 if (plugin->ipv6 == GNUNET_YES)
775 /* probe IPv6 support */
776 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
779 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
782 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
784 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
786 ("Disabling IPv6 since it is not supported on this system!\n"));
787 plugin->ipv6 = GNUNET_NO;
791 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
795 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
796 "Testing IPv6 on this system: %s\n",
797 (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
803 http_get_addresses (struct Plugin *plugin, const char *service_name,
804 const struct GNUNET_CONFIGURATION_Handle *cfg,
805 struct sockaddr ***addrs, socklen_t ** addr_lens)
808 unsigned long long port;
809 struct addrinfo hints;
810 struct addrinfo *res;
811 struct addrinfo *pos;
812 struct addrinfo *next;
816 struct sockaddr **saddrs;
817 socklen_t *saddrlens;
823 disablev6 = !plugin->ipv6;
826 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
828 GNUNET_break (GNUNET_OK ==
829 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
833 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
835 ("Require valid port number for service in configuration!\n"));
836 return GNUNET_SYSERR;
841 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
842 "Starting in listen only mode\n");
843 return -1; /* listen only */
847 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
849 GNUNET_break (GNUNET_OK ==
850 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
851 "BINDTO", &hostname));
856 if (hostname != NULL)
858 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
859 "Resolving `%s' since that is where `%s' will bind to.\n",
860 hostname, service_name);
861 memset (&hints, 0, sizeof (struct addrinfo));
863 hints.ai_family = AF_INET;
864 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
867 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
868 hostname, gai_strerror (ret));
869 GNUNET_free (hostname);
870 return GNUNET_SYSERR;
874 while (NULL != (pos = next))
877 if ((disablev6) && (pos->ai_family == AF_INET6))
883 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
884 _("Failed to find %saddress for `%s'.\n"),
885 disablev6 ? "IPv4 " : "", hostname);
887 GNUNET_free (hostname);
888 return GNUNET_SYSERR;
891 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
892 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
895 while (NULL != (pos = next))
898 if ((disablev6) && (pos->ai_family == AF_INET6))
900 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
901 continue; /* not TCP */
902 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
904 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
905 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
907 if (pos->ai_family == AF_INET)
909 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
910 saddrlens[i] = pos->ai_addrlen;
911 saddrs[i] = GNUNET_malloc (saddrlens[i]);
912 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
913 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
917 GNUNET_assert (pos->ai_family == AF_INET6);
918 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
919 saddrlens[i] = pos->ai_addrlen;
920 saddrs[i] = GNUNET_malloc (saddrlens[i]);
921 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
922 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
926 GNUNET_free (hostname);
932 /* will bind against everything, just set port */
938 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
939 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
941 saddrlens[i] = sizeof (struct sockaddr_in);
942 saddrs[i] = GNUNET_malloc (saddrlens[i]);
943 #if HAVE_SOCKADDR_IN_SIN_LEN
944 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
946 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
947 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
953 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
954 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
956 saddrlens[i] = sizeof (struct sockaddr_in6);
957 saddrs[i] = GNUNET_malloc (saddrlens[i]);
958 #if HAVE_SOCKADDR_IN_SIN_LEN
959 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
961 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
962 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
964 saddrlens[i] = sizeof (struct sockaddr_in);
965 saddrs[i] = GNUNET_malloc (saddrlens[i]);
966 #if HAVE_SOCKADDR_IN_SIN_LEN
967 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
969 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
970 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
974 *addr_lens = saddrlens;
979 start_report_addresses (struct Plugin *plugin)
982 struct sockaddr **addrs;
986 http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
988 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
989 _("Found %u addresses to report to NAT service\n"), res);
991 if (GNUNET_SYSERR == res)
998 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1000 (const struct sockaddr **) addrs, addrlens,
1001 &nat_port_map_callback, NULL, plugin);
1005 GNUNET_assert (addrs[res] != NULL);
1006 GNUNET_free (addrs[res]);
1008 GNUNET_free_non_null (addrs);
1009 GNUNET_free_non_null (addrlens);
1014 stop_report_addresses (struct Plugin *plugin)
1017 /* Stop NAT handle */
1018 if (NULL != plugin->nat)
1019 GNUNET_NAT_unregister (plugin->nat);
1021 /* Clean up addresses */
1022 struct HttpAddressWrapper *w;
1024 while (plugin->addr_head != NULL)
1026 w = plugin->addr_head;
1027 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
1028 GNUNET_free (w->addr);
1034 * Function called when the service shuts down. Unloads our plugins
1035 * and cancels pending validations.
1037 * @param cls closure, unused
1038 * @param tc task context (unused)
1041 notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1043 struct Plugin *plugin = cls;
1044 struct HttpAddress *eaddr;
1049 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
1051 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1054 GNUNET_asprintf(&addr, "%s://%s", plugin->protocol, plugin->external_hostname);
1055 uri_len = strlen (addr) + 1;
1056 eaddr_len = sizeof (struct HttpAddress) + uri_len;
1057 eaddr = GNUNET_malloc (eaddr_len);
1058 eaddr->addr_len = htonl (uri_len);
1059 eaddr->addr = (void *) &eaddr[1];
1060 memcpy (&eaddr->addr, addr, uri_len);
1061 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1062 "Notifying transport about external hostname address `%s'\n", addr);
1065 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, eaddr, eaddr_len, "http");
1066 plugin->ext_addr = eaddr;
1067 plugin->ext_addr_len = eaddr_len;
1072 configure_plugin (struct Plugin *plugin)
1074 int res = GNUNET_OK;
1077 if (GNUNET_CONFIGURATION_have_value
1078 (plugin->env->cfg, plugin->name, "USE_IPv4"))
1081 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1085 plugin->ipv4 = GNUNET_YES;
1088 if (GNUNET_CONFIGURATION_have_value
1089 (plugin->env->cfg, plugin->name, "USE_IPv6"))
1092 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1096 plugin->ipv6 = GNUNET_YES;
1098 if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1100 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1102 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1104 res = GNUNET_SYSERR;
1107 /* Reading port number from config file */
1108 unsigned long long port;
1111 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1112 "PORT", &port)) || (port > 65535))
1114 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1115 _("Port is required! Fix in configuration\n"),
1117 res = GNUNET_SYSERR;
1120 plugin->port = port;
1122 plugin->client_only = GNUNET_NO;
1123 if (plugin->port == 0)
1125 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1126 _("Port 0, client only mode\n"));
1127 plugin->client_only = GNUNET_YES;
1130 char *bind4_address = NULL;
1132 if ((plugin->ipv4 == GNUNET_YES) &&
1134 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1135 "BINDTO", &bind4_address)))
1137 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1138 "Binding %s plugin to specific IPv4 address: `%s'\n",
1139 plugin->protocol, bind4_address);
1140 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1142 inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1144 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1146 ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1147 bind4_address, plugin->protocol);
1148 GNUNET_free (plugin->server_addr_v4);
1149 plugin->server_addr_v4 = NULL;
1153 plugin->server_addr_v4->sin_family = AF_INET;
1154 plugin->server_addr_v4->sin_port = htons (plugin->port);
1156 GNUNET_free (bind4_address);
1159 char *bind6_address = NULL;
1161 if ((plugin->ipv6 == GNUNET_YES) &&
1163 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1164 "BINDTO6", &bind6_address)))
1166 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1167 "Binding %s plugin to specific IPv6 address: `%s'\n",
1168 plugin->protocol, bind6_address);
1169 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1171 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1173 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1175 ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1176 bind6_address, plugin->protocol);
1177 GNUNET_free (plugin->server_addr_v6);
1178 plugin->server_addr_v6 = NULL;
1182 plugin->server_addr_v6->sin6_family = AF_INET6;
1183 plugin->server_addr_v6->sin6_port = htons (plugin->port);
1185 GNUNET_free (bind6_address);
1188 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1189 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
1191 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1192 _("Using external hostname `%s'\n"), plugin->external_hostname);
1193 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (¬ify_external_hostname, plugin);
1196 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1197 _("No external hostname configured\n"));
1200 /* Optional parameters */
1201 unsigned long long maxneigh;
1204 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1205 "MAX_CONNECTIONS", &maxneigh))
1207 plugin->max_connections = maxneigh;
1213 #define TESTING GNUNET_NO
1216 #define TIMEOUT_LOG GNUNET_ERROR_TYPE_ERROR
1217 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
1219 #define TIMEOUT_LOG GNUNET_ERROR_TYPE_DEBUG
1220 #define TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
1225 * Session was idle, so disconnect it
1228 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1230 GNUNET_assert (NULL != cls);
1231 struct Session *s = cls;
1233 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1234 GNUNET_log (TIMEOUT_LOG,
1235 "Session %p was idle for %s, disconnecting\n",
1237 GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
1240 /* call session destroy function */
1241 if (s->inbound == GNUNET_NO)
1242 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1244 GNUNET_assert (GNUNET_OK == server_disconnect (s));
1249 * Start session timeout
1252 start_session_timeout (struct Session *s)
1254 GNUNET_assert (NULL != s);
1255 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1256 s->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1259 GNUNET_log (TIMEOUT_LOG,
1260 "Timeout for session %p set to %s\n",
1262 GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
1268 * Increment session timeout due to activity
1271 reschedule_session_timeout (struct Session *s)
1273 GNUNET_assert (NULL != s);
1274 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1276 GNUNET_SCHEDULER_cancel (s->timeout_task);
1277 s->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1280 GNUNET_log (TIMEOUT_LOG,
1281 "Timeout rescheduled for session %p set to %s\n",
1283 GNUNET_STRINGS_relative_time_to_String (TIMEOUT,
1292 stop_session_timeout (struct Session *s)
1294 GNUNET_assert (NULL != s);
1296 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1298 GNUNET_SCHEDULER_cancel (s->timeout_task);
1299 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1300 GNUNET_log (TIMEOUT_LOG,
1301 "Timeout stopped for session %p\n",
1307 * Entry point for the plugin.
1310 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1312 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1313 struct GNUNET_TRANSPORT_PluginFunctions *api;
1314 struct Plugin *plugin;
1317 if (NULL == env->receive)
1319 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1320 initialze the plugin or the API */
1321 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1323 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1324 api->address_to_string = &http_plugin_address_to_string;
1325 api->string_to_address = &http_string_to_address;
1329 plugin = GNUNET_malloc (sizeof (struct Plugin));
1331 plugin->outbound_sessions = 0;
1332 plugin->inbound_sessions = 0;
1333 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1335 api->disconnect = &http_plugin_disconnect;
1336 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1337 api->check_address = &http_plugin_address_suggested;
1338 api->address_to_string = &http_plugin_address_to_string;
1339 api->string_to_address = &http_string_to_address;
1340 api->get_session = &http_get_session;
1341 api->send = &http_plugin_send;
1344 plugin->name = "transport-https";
1345 plugin->protocol = "https";
1347 plugin->name = "transport-http";
1348 plugin->protocol = "http";
1350 /* Configure plugin from configuration */
1351 res = configure_plugin (plugin);
1352 if (res == GNUNET_SYSERR)
1354 GNUNET_free_non_null (plugin->server_addr_v4);
1355 GNUNET_free_non_null (plugin->server_addr_v6);
1356 GNUNET_free (plugin);
1361 /* checking IPv6 support */
1362 http_check_ipv6 (plugin);
1365 res = client_start (plugin);
1366 if (res == GNUNET_SYSERR)
1368 GNUNET_free_non_null (plugin->server_addr_v4);
1369 GNUNET_free_non_null (plugin->server_addr_v6);
1370 GNUNET_free (plugin);
1376 if (plugin->client_only == GNUNET_NO)
1378 res = server_start (plugin);
1379 if (res == GNUNET_SYSERR)
1381 server_stop (plugin);
1382 client_stop (plugin);
1384 GNUNET_free_non_null (plugin->server_addr_v4);
1385 GNUNET_free_non_null (plugin->server_addr_v6);
1386 GNUNET_free (plugin);
1391 /* Report addresses to transport service */
1392 start_report_addresses (plugin);
1394 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1395 "Plugin `%s' loaded\n", plugin->name);
1401 * Exit point from the plugin.
1404 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1406 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1407 struct Plugin *plugin = api->cls;
1409 struct Session *next;
1417 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
1419 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
1420 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
1423 if (NULL != plugin->ext_addr)
1425 plugin->env->notify_address (plugin->env->cls, GNUNET_NO, plugin->ext_addr, plugin->ext_addr_len, "http");
1426 GNUNET_free (plugin->ext_addr);
1429 /* Stop reporting addresses to transport service */
1430 stop_report_addresses (plugin);
1432 /* cleaning up sessions */
1437 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1438 "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1439 if (s->inbound == GNUNET_NO)
1440 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1442 GNUNET_assert (GNUNET_OK == server_disconnect (s));
1446 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
1448 server_stop (plugin);
1450 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
1452 client_stop (plugin);
1454 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1455 "Plugin `%s' unloaded\n", plugin->name);
1456 GNUNET_free_non_null (plugin->server_addr_v4);
1457 GNUNET_free_non_null (plugin->server_addr_v6);
1458 GNUNET_free_non_null (plugin->external_hostname);
1459 GNUNET_free (plugin);
1464 /* end of plugin_transport_http.c */