2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
27 #include "plugin_transport_http.h"
30 * After how long do we expire an address that we
31 * learned from another peer if it is not reconfirmed
34 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
37 * Wrapper to manage IPv4 addresses
39 struct IPv4HttpAddressWrapper
44 struct IPv4HttpAddressWrapper *next;
47 * Linked list previous
49 struct IPv4HttpAddressWrapper *prev;
51 struct IPv4HttpAddress addr;
55 * Wrapper for IPv4 addresses.
57 struct IPv6HttpAddressWrapper
62 struct IPv6HttpAddressWrapper *next;
65 * Linked list previous
67 struct IPv6HttpAddressWrapper *prev;
69 struct IPv6HttpAddress addr6;
74 * Context for address to string conversion.
76 struct PrettyPrinterContext
79 * Function to call with the result.
81 GNUNET_TRANSPORT_AddressStringCallback asc;
86 struct Plugin *plugin;
94 * Port to add after the IP address.
101 * Encapsulation of all of the state of the plugin.
108 * Append our port and forward the result.
110 * @param cls the 'struct PrettyPrinterContext*'
111 * @param hostname hostname part of the address
114 append_port (void *cls, const char *hostname)
116 struct PrettyPrinterContext *ppc = cls;
119 if (hostname == NULL)
121 ppc->asc (ppc->asc_cls, NULL);
125 GNUNET_asprintf (&ret, "%s://%s:%d", ppc->plugin->protocol, hostname,
127 ppc->asc (ppc->asc_cls, ret);
133 * Convert the transports address to a nice, human-readable
137 * @param type name of the transport that generated the address
138 * @param addr one of the addresses of the host, NULL for the last address
139 * the specific address format depends on the transport
140 * @param addrlen length of the address
141 * @param numeric should (IP) addresses be displayed in numeric form?
142 * @param timeout after how long should we give up?
143 * @param asc function to call on each string
144 * @param asc_cls closure for asc
147 http_plugin_address_pretty_printer (void *cls, const char *type,
148 const void *addr, size_t addrlen,
150 struct GNUNET_TIME_Relative timeout,
151 GNUNET_TRANSPORT_AddressStringCallback asc,
154 GNUNET_assert (cls != NULL);
155 struct PrettyPrinterContext *ppc;
160 if (addrlen == sizeof (struct IPv6HttpAddress))
162 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
165 sbs = sizeof (struct in6_addr);
166 port = ntohs (a6->u6_port);
168 else if (addrlen == sizeof (struct IPv4HttpAddress))
170 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
173 sbs = sizeof (struct in_addr);
174 port = ntohs (a4->u4_port);
178 /* invalid address */
183 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
185 ppc->asc_cls = asc_cls;
188 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
194 * Another peer has suggested an address for this
195 * peer and transport plugin. Check that this could be a valid
196 * address. If so, consider adding it to the list
200 * @param addr pointer to the address
201 * @param addrlen length of addr
202 * @return GNUNET_OK if this is a plausible address for this peer
206 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
209 struct Plugin *plugin = cls;
210 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
211 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
215 GNUNET_assert (cls != NULL);
216 if ((addrlen != sizeof (struct sockaddr_in)) ||
217 (addrlen != sizeof (struct sockaddr_in6)))
218 return GNUNET_SYSERR;
220 if (addrlen == sizeof (struct IPv4HttpAddress))
222 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
224 while (w_tv4 != NULL)
227 memcmp (&w_tv4->addr.ipv4_addr, &a4->ipv4_addr,
228 sizeof (struct in_addr))) &&
229 (w_tv4->addr.u4_port == a4->u4_port))
236 return GNUNET_SYSERR;
238 if (addrlen == sizeof (struct sockaddr_in6))
240 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
242 while (w_tv6 != NULL)
245 memcmp (&w_tv6->addr6.ipv6_addr, &a6->ipv6_addr,
246 sizeof (struct in6_addr))) &&
247 (w_tv6->addr6.u6_port == a6->u6_port))
254 return GNUNET_SYSERR;
259 struct GNUNET_TIME_Relative
260 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
261 const struct GNUNET_MessageHeader *message,
262 struct Session *session, const char *sender_address,
263 uint16_t sender_address_len)
265 struct Session *s = cls;
266 struct Plugin *plugin = s->plugin;
267 struct GNUNET_ATS_Information distance;
268 struct GNUNET_TIME_Relative delay;
270 distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
271 distance.value = htonl (1);
274 plugin->env->receive (plugin->env->cls, &s->target, message,
275 (const struct GNUNET_ATS_Information *) &distance,
276 1, s, s->addr, s->addrlen);
281 * Function called for a quick conversion of the binary address to
282 * a numeric address. Note that the caller must not free the
283 * address and that the next call to this function is allowed
284 * to override the address again.
287 * @param addr binary address
288 * @param addrlen length of the address
289 * @return string representing the same address
292 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
295 struct IPv4HttpAddress *a4;
296 struct IPv6HttpAddress *a6;
298 static char rbuf[INET6_ADDRSTRLEN + 13];
302 if (addrlen == sizeof (struct IPv6HttpAddress))
304 a6 = (struct IPv6HttpAddress *) addr;
305 address = GNUNET_malloc (INET6_ADDRSTRLEN);
306 GNUNET_assert (NULL !=
307 inet_ntop (AF_INET6, &a6->ipv6_addr, address,
309 port = ntohs (a6->u6_port);
311 else if (addrlen == sizeof (struct IPv4HttpAddress))
313 a4 = (struct IPv4HttpAddress *) addr;
314 address = GNUNET_malloc (INET_ADDRSTRLEN);
315 GNUNET_assert (NULL !=
316 inet_ntop (AF_INET, &(a4->ipv4_addr), address,
318 port = ntohs (a4->u4_port);
322 /* invalid address */
327 char *protocol = "http";
329 char *protocol = "https";
332 GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
333 if (addrlen == sizeof (struct IPv6HttpAddress))
335 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol,
337 else if (addrlen == sizeof (struct IPv4HttpAddress))
339 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address,
342 GNUNET_free (address);
343 GNUNET_assert (res != 0);
348 lookup_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
349 struct Session *session, const void *addr, size_t addrlen,
352 struct Session *s = NULL;
353 struct Session *t = NULL;
363 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
364 "Comparing peer `%s' address `%s' len %i session %X to \n",
365 GNUNET_i2s (target), GNUNET_a2s (addr, addrlen), addrlen,
367 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
368 "peer `%s' address `%s' len %i session %X \n\n",
369 GNUNET_i2s (&t->target), GNUNET_a2s (t->addr, t->addrlen),
371 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "memcmp %i \n",
372 memcmp (addr, t->addr, addrlen));
377 if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
380 if (addrlen == t->addrlen)
382 if (0 == memcmp (addr, t->addr, addrlen))
389 if (t->addrlen == session->addrlen)
391 if (0 == memcmp (session->addr, t->addr, t->addrlen))
399 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
404 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) &&
405 (e_addr == GNUNET_YES))
410 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
425 delete_session (struct Session *s)
427 if (s->msg_tk != NULL)
429 GNUNET_SERVER_mst_destroy (s->msg_tk);
432 GNUNET_free (s->addr);
433 GNUNET_free_non_null (s->server_recv);
434 GNUNET_free_non_null (s->server_send);
439 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
440 const void *addr, size_t addrlen,
441 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
443 struct Session *s = NULL;
445 GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
446 (addrlen == sizeof (struct IPv4HttpAddress)));
447 s = GNUNET_malloc (sizeof (struct Session));
448 memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
450 s->addr = GNUNET_malloc (addrlen);
451 memcpy (s->addr, addr, addrlen);
452 s->addrlen = addrlen;
454 s->next_receive = GNUNET_TIME_absolute_get_zero ();
459 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
462 struct Plugin *plugin = cls;
464 plugin->env->session_end (NULL, peer, s);
465 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
471 * Function that can be used by the transport service to transmit
472 * a message using the plugin. Note that in the case of a
473 * peer disconnecting, the continuation MUST be called
474 * prior to the disconnect notification itself. This function
475 * will be called with this peer's HELLO message to initiate
476 * a fresh connection to another peer.
479 * @param target who should receive this message
480 * @param msgbuf the message to transmit
481 * @param msgbuf_size number of bytes in 'msgbuf'
482 * @param priority how important is the message (most plugins will
483 * ignore message priority and just FIFO)
484 * @param to how long to wait at most for the transmission (does not
485 * require plugins to discard the message after the timeout,
486 * just advisory for the desired delay; most plugins will ignore
488 * @param session which session must be used (or NULL for "any")
489 * @param addr the address to use (can be NULL if the plugin
490 * is "on its own" (i.e. re-use existing TCP connection))
491 * @param addrlen length of the address in bytes
492 * @param force_address GNUNET_YES if the plugin MUST use the given address,
493 * GNUNET_NO means the plugin may use any other address and
494 * GNUNET_SYSERR means that only reliable existing
495 * bi-directional connections should be used (regardless
497 * @param cont continuation to call once the message has
498 * been transmitted (or if the transport is ready
499 * for the next transmission call; or if the
500 * peer disconnected...); can be NULL
501 * @param cont_cls closure for cont
502 * @return number of bytes used (on the physical network, with overheads);
503 * -1 on hard errors (i.e. address invalid); 0 is a legal value
504 * and does NOT mean that the message was not transmitted (DV)
507 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
508 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
509 struct GNUNET_TIME_Relative to, struct Session *session,
510 const void *addr, size_t addrlen, int force_address,
511 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
513 struct Plugin *plugin = cls;
514 struct HTTP_Message *msg;
516 GNUNET_assert (plugin != NULL);
518 int res = GNUNET_SYSERR;
521 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
522 "Sending %u bytes to peer `%s' on address `%s' %X %i\n",
523 msgbuf_size, GNUNET_i2s (target), ((addr != NULL) &&
526 http_plugin_address_to_string (plugin, addr,
527 addrlen) : "<inbound>",
528 session, force_address);
534 GNUNET_assert ((addrlen == sizeof (struct IPv4HttpAddress)) ||
535 (addrlen == sizeof (struct IPv6HttpAddress)));
537 /* look for existing connection */
538 s = lookup_session (plugin, target, session, addr, addrlen, 1);
540 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
541 "%s existing session: %s\n",
542 (s != NULL) ? "Found" : "NOT Found", ((s != NULL) &&
545 "inbound" : "outbound");
548 /* create new outbound connection */
551 if (plugin->max_connections <= plugin->cur_connections)
553 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
554 "Maximum number of connections reached, "
555 "cannot connect to peer `%s'\n", GNUNET_i2s (target));
560 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
561 "Initiiating new connection to peer `%s'\n",
562 GNUNET_i2s (target));
564 s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
565 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
566 // initiate new connection
567 if (GNUNET_SYSERR == (res = client_connect (s)))
569 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
571 return GNUNET_SYSERR;
575 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
577 msg->size = msgbuf_size;
579 msg->buf = (char *) &msg[1];
580 msg->transmit_cont = cont;
581 msg->transmit_cont_cls = cont_cls;
582 memcpy (msg->buf, msgbuf, msgbuf_size);
584 if (s->inbound == GNUNET_NO)
587 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
588 "Using outbound client session %p to send to `%s'\n",
590 GNUNET_i2s (target));
593 client_send (s, msg);
596 if (s->inbound == GNUNET_YES)
599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
600 "Using inbound server %p session to send to `%s'\n",
602 GNUNET_i2s (target));
605 server_send (s, msg);
613 * Function that can be used to force the plugin to disconnect
614 * from the given peer and cancel all previous transmissions
615 * (and their continuationc).
618 * @param target peer from which to disconnect
621 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
623 struct Plugin *plugin = cls;
624 struct Session *next = NULL;
625 struct Session *s = plugin->head;
627 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
628 "Transport tells me to disconnect `%s'\n",
629 GNUNET_i2s (target));
633 if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
635 if (s->inbound == GNUNET_NO)
636 GNUNET_assert (GNUNET_OK == client_disconnect (s));
638 GNUNET_assert (GNUNET_OK == server_disconnect (s));
639 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
641 struct HTTP_Message * msg = s->msg_head;
642 struct HTTP_Message * tmp = s->msg_head;
647 GNUNET_CONTAINER_DLL_remove(s->msg_head,s->msg_tail, msg);
648 if (msg->transmit_cont != NULL)
650 msg->transmit_cont(msg->transmit_cont_cls, target, GNUNET_SYSERR);
663 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
666 struct Plugin *plugin = cls;
667 struct IPv4HttpAddressWrapper *w_t4 = NULL;
668 struct IPv6HttpAddressWrapper *w_t6 = NULL;
671 af = addr->sa_family;
675 w_t4 = plugin->ipv4_addr_head;
676 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
680 int res = memcmp (&w_t4->addr.ipv4_addr,
682 sizeof (struct in_addr));
686 if (a4->sin_port != w_t4->addr.u4_port)
696 w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
697 memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
698 w_t4->addr.u4_port = a4->sin_port;
700 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
701 plugin->ipv4_addr_tail, w_t4);
704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
705 "Notifying transport to add IPv4 address `%s'\n",
706 http_plugin_address_to_string (NULL, &w_t4->addr,
710 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
711 sizeof (struct IPv4HttpAddress));
715 w_t6 = plugin->ipv6_addr_head;
716 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
720 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
721 sizeof (struct in6_addr));
725 if (a6->sin6_port != w_t6->addr6.u6_port)
734 w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
736 memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
737 w_t6->addr6.u6_port = a6->sin6_port;
739 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
740 plugin->ipv6_addr_tail, w_t6);
743 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
744 "Notifying transport to add IPv6 address `%s'\n",
745 http_plugin_address_to_string (NULL, &w_t6->addr6,
749 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
750 sizeof (struct IPv6HttpAddress));
759 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
762 struct Plugin *plugin = cls;
763 struct IPv4HttpAddressWrapper *w_t4 = NULL;
764 struct IPv6HttpAddressWrapper *w_t6 = NULL;
767 af = addr->sa_family;
771 w_t4 = plugin->ipv4_addr_head;
772 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
776 int res = memcmp (&w_t4->addr.ipv4_addr,
778 sizeof (struct in_addr));
782 if (a4->sin_port != w_t4->addr.u4_port)
794 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
795 "Notifying transport to remove IPv4 address `%s'\n",
796 http_plugin_address_to_string (NULL, &w_t4->addr,
800 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
801 sizeof (struct IPv4HttpAddress));
803 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
808 w_t6 = plugin->ipv6_addr_head;
809 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
813 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
814 sizeof (struct in6_addr));
818 if (a6->sin6_port != w_t6->addr6.u6_port)
828 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
829 "Notifying transport to remove IPv6 address `%s'\n",
830 http_plugin_address_to_string (NULL, &w_t6->addr6,
834 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
835 sizeof (struct IPv6HttpAddress));
837 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
848 * Our external IP address/port mapping has changed.
850 * @param cls closure, the 'struct LocalAddrList'
851 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
852 * the previous (now invalid) one
853 * @param addr either the previous or the new public IP address
854 * @param addrlen actual lenght of the address
857 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
860 GNUNET_assert (cls != NULL);
862 struct Plugin *plugin = cls;
865 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
866 "NPMC called %s to address `%s'\n",
867 (add_remove == GNUNET_NO) ? "remove" : "add",
868 GNUNET_a2s (addr, addrlen));
873 nat_add_address (cls, add_remove, addr, addrlen);
876 nat_remove_address (cls, add_remove, addr, addrlen);
882 http_check_ipv6 (struct Plugin *plugin)
884 struct GNUNET_NETWORK_Handle *desc = NULL;
886 if (plugin->ipv6 == GNUNET_YES)
888 /* probe IPv6 support */
889 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
892 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
895 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
897 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
899 ("Disabling IPv6 since it is not supported on this system!\n"));
900 plugin->ipv6 = GNUNET_NO;
904 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
908 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
909 "Testing IPv6 on this system: %s\n",
910 (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
915 http_get_addresses (struct Plugin *plugin, const char *serviceName,
916 const struct GNUNET_CONFIGURATION_Handle *cfg,
917 struct sockaddr ***addrs, socklen_t ** addr_lens)
920 unsigned long long port;
921 struct addrinfo hints;
922 struct addrinfo *res;
923 struct addrinfo *pos;
924 struct addrinfo *next;
928 struct sockaddr **saddrs;
929 socklen_t *saddrlens;
935 disablev6 = !plugin->ipv6;
938 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
940 GNUNET_break (GNUNET_OK ==
941 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
945 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
947 ("Require valid port number for service in configuration!\n"));
948 return GNUNET_SYSERR;
952 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
954 GNUNET_break (GNUNET_OK ==
955 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
956 "BINDTO", &hostname));
961 if (hostname != NULL)
963 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
964 "Resolving `%s' since that is where `%s' will bind to.\n",
965 hostname, serviceName);
966 memset (&hints, 0, sizeof (struct addrinfo));
968 hints.ai_family = AF_INET;
969 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
973 hostname, gai_strerror (ret));
974 GNUNET_free (hostname);
975 return GNUNET_SYSERR;
979 while (NULL != (pos = next))
982 if ((disablev6) && (pos->ai_family == AF_INET6))
988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
989 _("Failed to find %saddress for `%s'.\n"),
990 disablev6 ? "IPv4 " : "", hostname);
992 GNUNET_free (hostname);
993 return GNUNET_SYSERR;
996 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
997 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1000 while (NULL != (pos = next))
1002 next = pos->ai_next;
1003 if ((disablev6) && (pos->ai_family == AF_INET6))
1005 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
1006 continue; /* not TCP */
1007 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
1008 continue; /* huh? */
1009 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1010 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
1012 if (pos->ai_family == AF_INET)
1014 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
1015 saddrlens[i] = pos->ai_addrlen;
1016 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1017 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1018 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1022 GNUNET_assert (pos->ai_family == AF_INET6);
1023 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1024 saddrlens[i] = pos->ai_addrlen;
1025 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1026 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1027 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1031 GNUNET_free (hostname);
1037 /* will bind against everything, just set port */
1043 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1044 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1046 saddrlens[i] = sizeof (struct sockaddr_in);
1047 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1048 #if HAVE_SOCKADDR_IN_SIN_LEN
1049 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1051 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1052 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1058 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1059 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1061 saddrlens[i] = sizeof (struct sockaddr_in6);
1062 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1063 #if HAVE_SOCKADDR_IN_SIN_LEN
1064 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1066 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1067 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1069 saddrlens[i] = sizeof (struct sockaddr_in);
1070 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1071 #if HAVE_SOCKADDR_IN_SIN_LEN
1072 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1074 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1075 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1079 *addr_lens = saddrlens;
1084 start_report_addresses (struct Plugin *plugin)
1086 int res = GNUNET_OK;
1087 struct sockaddr **addrs;
1088 socklen_t *addrlens;
1091 http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
1093 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1094 _("Found %u addresses to report to NAT service\n"), res);
1096 if (res != GNUNET_SYSERR)
1099 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1101 (const struct sockaddr **) addrs, addrlens,
1102 &nat_port_map_callback, NULL, plugin);
1107 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, _("FREEING %s\n"),
1108 GNUNET_a2s (addrs[res], addrlens[res]));
1110 GNUNET_assert (addrs[res] != NULL);
1111 GNUNET_free (addrs[res]);
1113 GNUNET_free_non_null (addrs);
1114 GNUNET_free_non_null (addrlens);
1119 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1120 NULL, NULL, plugin);
1125 stop_report_addresses (struct Plugin *plugin)
1127 /* Stop NAT handle */
1128 GNUNET_NAT_unregister (plugin->nat);
1130 /* Clean up addresses */
1131 struct IPv4HttpAddressWrapper *w_t4;
1132 struct IPv6HttpAddressWrapper *w_t6;
1134 while (plugin->ipv4_addr_head != NULL)
1136 w_t4 = plugin->ipv4_addr_head;
1137 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1142 while (plugin->ipv6_addr_head != NULL)
1144 w_t6 = plugin->ipv6_addr_head;
1145 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1152 configure_plugin (struct Plugin *plugin)
1154 int res = GNUNET_OK;
1157 if (GNUNET_CONFIGURATION_have_value
1158 (plugin->env->cfg, plugin->name, "USE_IPv4"))
1161 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1165 plugin->ipv4 = GNUNET_YES;
1168 if (GNUNET_CONFIGURATION_have_value
1169 (plugin->env->cfg, plugin->name, "USE_IPv6"))
1172 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1176 plugin->ipv6 = GNUNET_YES;
1178 if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1180 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1182 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1184 res = GNUNET_SYSERR;
1187 /* Reading port number from config file */
1188 unsigned long long port;
1191 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1192 "PORT", &port)) || (port > 65535))
1194 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1195 _("Port is required! Fix in configuration\n"),
1197 res = GNUNET_SYSERR;
1200 plugin->port = port;
1202 plugin->client_only = GNUNET_NO;
1203 if (plugin->port == 0)
1205 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1206 _("Port 0, client only mode\n"));
1207 plugin->client_only = GNUNET_YES;
1210 char *bind4_address = NULL;
1212 if ((plugin->ipv4 == GNUNET_YES) &&
1214 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1215 "BINDTO", &bind4_address)))
1217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1218 "Binding %s plugin to specific IPv4 address: `%s'\n",
1219 plugin->protocol, bind4_address);
1220 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1222 inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1224 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1226 ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1227 bind4_address, plugin->protocol);
1228 GNUNET_free (plugin->server_addr_v4);
1229 plugin->server_addr_v4 = NULL;
1233 plugin->server_addr_v4->sin_family = AF_INET;
1234 plugin->server_addr_v4->sin_port = htons (plugin->port);
1236 GNUNET_free (bind4_address);
1240 char *bind6_address = NULL;
1242 if ((plugin->ipv6 == GNUNET_YES) &&
1244 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1245 "BINDTO6", &bind6_address)))
1247 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1248 "Binding %s plugin to specific IPv6 address: `%s'\n",
1249 plugin->protocol, bind6_address);
1250 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1252 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1254 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1256 ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1257 bind6_address, plugin->protocol);
1258 GNUNET_free (plugin->server_addr_v6);
1259 plugin->server_addr_v6 = NULL;
1263 plugin->server_addr_v6->sin6_family = AF_INET6;
1264 plugin->server_addr_v6->sin6_port = htons (plugin->port);
1266 GNUNET_free (bind6_address);
1270 /* Optional parameters */
1271 unsigned long long maxneigh;
1274 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1275 "MAX_CONNECTIONS", &maxneigh))
1277 plugin->max_connections = maxneigh;
1284 * Entry point for the plugin.
1287 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1289 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1290 struct GNUNET_TRANSPORT_PluginFunctions *api;
1291 struct Plugin *plugin;
1294 plugin = GNUNET_malloc (sizeof (struct Plugin));
1296 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1298 api->send = &http_plugin_send;
1299 api->disconnect = &http_plugin_disconnect;
1300 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1301 api->check_address = &http_plugin_address_suggested;
1302 api->address_to_string = &http_plugin_address_to_string;
1305 plugin->name = "transport-https";
1306 plugin->protocol = "https";
1308 plugin->name = "transport-http";
1309 plugin->protocol = "http";
1311 /* Configure plugin from configuration */
1312 res = configure_plugin (plugin);
1313 if (res == GNUNET_SYSERR)
1315 GNUNET_free_non_null (plugin->server_addr_v4);
1316 GNUNET_free_non_null (plugin->server_addr_v6);
1317 GNUNET_free (plugin);
1322 /* checking IPv6 support */
1323 http_check_ipv6 (plugin);
1326 res = client_start (plugin);
1327 if (res == GNUNET_SYSERR)
1329 GNUNET_free_non_null (plugin->server_addr_v4);
1330 GNUNET_free_non_null (plugin->server_addr_v6);
1331 GNUNET_free (plugin);
1337 if (plugin->client_only == GNUNET_NO)
1339 res = server_start (plugin);
1340 if (res == GNUNET_SYSERR)
1342 server_stop (plugin);
1343 client_stop (plugin);
1345 GNUNET_free_non_null (plugin->server_addr_v4);
1346 GNUNET_free_non_null (plugin->server_addr_v6);
1347 GNUNET_free (plugin);
1352 /* Report addresses to transport service */
1353 start_report_addresses (plugin);
1356 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1357 "Plugin `%s' loaded\n", plugin->name);
1365 * Exit point from the plugin.
1368 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1370 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1371 struct Plugin *plugin = api->cls;
1372 struct Session *s = NULL;
1374 /* Stop reporting addresses to transport service */
1375 stop_report_addresses (plugin);
1377 /* cleaning up sessions */
1382 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1383 "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1385 if (s->inbound == GNUNET_NO)
1386 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1388 GNUNET_assert (GNUNET_OK == server_disconnect (s));
1393 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
1396 server_stop (plugin);
1399 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
1402 client_stop (plugin);
1404 /* deleting up sessions */
1408 struct Session *t = s->next;
1409 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1411 struct HTTP_Message * msg = s->msg_head;
1412 struct HTTP_Message * tmp = s->msg_head;
1417 GNUNET_CONTAINER_DLL_remove(s->msg_head,s->msg_tail, msg);
1418 if (msg->transmit_cont != NULL)
1420 msg->transmit_cont(msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1432 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1433 "Plugin `%s' unloaded\n", plugin->name);
1436 GNUNET_free_non_null (plugin->server_addr_v4);
1437 GNUNET_free_non_null (plugin->server_addr_v6);
1438 GNUNET_free (plugin);
1444 /* end of plugin_transport_http.c */