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)
39 struct IPv4HttpAddress
42 * IPv4 address, in network byte order.
44 uint32_t ipv4_addr GNUNET_PACKED;
47 * Port number, in network byte order.
49 uint16_t u4_port GNUNET_PACKED;
55 struct IPv6HttpAddress
60 struct in6_addr ipv6_addr GNUNET_PACKED;
63 * Port number, in network byte order.
65 uint16_t u6_port GNUNET_PACKED;
69 * Wrapper to manage IPv4 addresses
71 struct IPv4HttpAddressWrapper
76 struct IPv4HttpAddressWrapper *next;
79 * Linked list previous
81 struct IPv4HttpAddressWrapper *prev;
83 struct IPv4HttpAddress addr;
87 * Wrapper for IPv4 addresses.
89 struct IPv6HttpAddressWrapper
94 struct IPv6HttpAddressWrapper *next;
97 * Linked list previous
99 struct IPv6HttpAddressWrapper *prev;
101 struct IPv6HttpAddress addr6;
106 * Context for address to string conversion.
108 struct PrettyPrinterContext
111 * Function to call with the result.
113 GNUNET_TRANSPORT_AddressStringCallback asc;
118 struct Plugin *plugin;
126 * Port to add after the IP address.
133 * Encapsulation of all of the state of the plugin.
140 * Append our port and forward the result.
142 * @param cls the 'struct PrettyPrinterContext*'
143 * @param hostname hostname part of the address
146 append_port (void *cls, const char *hostname)
148 struct PrettyPrinterContext *ppc = cls;
151 if (hostname == NULL)
153 ppc->asc (ppc->asc_cls, NULL);
157 GNUNET_asprintf (&ret, "%s://%s:%d", ppc->plugin->protocol, hostname,
159 ppc->asc (ppc->asc_cls, ret);
165 * Convert the transports address to a nice, human-readable
169 * @param type name of the transport that generated the address
170 * @param addr one of the addresses of the host, NULL for the last address
171 * the specific address format depends on the transport
172 * @param addrlen length of the address
173 * @param numeric should (IP) addresses be displayed in numeric form?
174 * @param timeout after how long should we give up?
175 * @param asc function to call on each string
176 * @param asc_cls closure for asc
179 http_plugin_address_pretty_printer (void *cls, const char *type,
180 const void *addr, size_t addrlen,
182 struct GNUNET_TIME_Relative timeout,
183 GNUNET_TRANSPORT_AddressStringCallback asc,
186 GNUNET_assert (cls != NULL);
187 struct PrettyPrinterContext *ppc;
192 // BUG! Transport addrs over the network must NOT be 'struct sockaddr*'s!
193 if (addrlen == sizeof (struct IPv6HttpAddress))
195 struct IPv6HttpAddress * a6 = (struct IPv6HttpAddress *) addr;
197 sbs = sizeof (struct in6_addr);
198 port = ntohs (a6->u6_port);
200 else if (addrlen == sizeof (struct IPv4HttpAddress))
202 struct IPv4HttpAddress * a4 = (struct IPv4HttpAddress *) addr;
204 sbs = sizeof (struct in_addr);
205 port = ntohs (a4->u4_port);
209 /* invalid address */
214 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
216 ppc->asc_cls = asc_cls;
219 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
225 * Another peer has suggested an address for this
226 * peer and transport plugin. Check that this could be a valid
227 * address. If so, consider adding it to the list
231 * @param addr pointer to the address
232 * @param addrlen length of addr
233 * @return GNUNET_OK if this is a plausible address for this peer
237 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
240 struct Plugin *plugin = cls;
241 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
242 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
246 GNUNET_assert (cls != NULL);
247 if ((addrlen != sizeof (struct sockaddr_in)) ||
248 (addrlen != sizeof (struct sockaddr_in6)))
249 return GNUNET_SYSERR;
251 if (addrlen == sizeof (struct IPv4HttpAddress))
253 struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
254 while (w_tv4 != NULL)
256 if ((0 == memcmp (&w_tv4->addr.ipv4_addr, &a4->ipv4_addr, sizeof (struct in_addr))) &&
257 (w_tv4->addr.u4_port == a4->u4_port))
264 return GNUNET_SYSERR;
266 if (addrlen == sizeof (struct sockaddr_in6))
268 struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
269 while (w_tv6 != NULL)
271 if ((0 == memcmp (&w_tv6->addr6.ipv6_addr, &a6->ipv6_addr, sizeof (struct in6_addr))) &&
272 (w_tv6->addr6.u6_port == a6->u6_port))
279 return GNUNET_SYSERR;
284 struct GNUNET_TIME_Relative
285 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity * peer,
286 const struct GNUNET_MessageHeader * message,
287 struct Session * session,
288 const char *sender_address,
289 uint16_t sender_address_len)
291 struct Session *s = cls;
292 struct Plugin *plugin = s->plugin;
293 struct GNUNET_TRANSPORT_ATS_Information distance[2];
294 struct GNUNET_TIME_Relative delay;
296 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
297 distance[0].value = htonl (1);
298 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
299 distance[1].value = htonl (0);
301 delay = plugin->env->receive (plugin->env->cls, &s->target, message, (const struct GNUNET_TRANSPORT_ATS_Information*) &distance, 2, s, s->addr, s->addrlen);
306 * Function called for a quick conversion of the binary address to
307 * a numeric address. Note that the caller must not free the
308 * address and that the next call to this function is allowed
309 * to override the address again.
312 * @param addr binary address
313 * @param addrlen length of the address
314 * @return string representing the same address
317 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
320 struct IPv4HttpAddress *a4;
321 struct IPv6HttpAddress *a6;
323 static char rbuf[INET6_ADDRSTRLEN + 13];
327 if (addrlen == sizeof (struct IPv6HttpAddress))
329 a6 = (struct IPv6HttpAddress *) addr;
330 address = GNUNET_malloc (INET6_ADDRSTRLEN);
331 GNUNET_assert(NULL != inet_ntop (AF_INET6, &a6->ipv6_addr, address, INET6_ADDRSTRLEN));
332 port = ntohs (a6->u6_port);
334 else if (addrlen == sizeof (struct IPv4HttpAddress))
336 a4 = (struct IPv4HttpAddress *) addr;
337 address = GNUNET_malloc (INET_ADDRSTRLEN);
338 GNUNET_assert(NULL != inet_ntop (AF_INET, &(a4->ipv4_addr), address, INET_ADDRSTRLEN));
339 port = ntohs (a4->u4_port);
343 /* invalid address */
347 char * protocol = "http";
349 char * protocol = "https";
352 GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
353 if (addrlen == sizeof (struct IPv6HttpAddress))
354 res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, address, port);
355 else if (addrlen == sizeof (struct IPv4HttpAddress))
356 res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address, port);
358 GNUNET_free (address);
359 GNUNET_assert (res != 0);
364 lookup_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
365 struct Session * session,
366 const void *addr, size_t addrlen, int force_address)
368 struct Session *s = NULL;
369 struct Session *t = NULL;
379 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
380 "Comparing peer `%s' address `%s' len %i session %X to \n", GNUNET_i2s(target), GNUNET_a2s(addr,addrlen), addrlen, session);
381 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"peer `%s' address `%s' len %i session %X \n\n", GNUNET_i2s(&t->target), GNUNET_a2s(t->addr,t->addrlen), t->addrlen, t);
382 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"memcmp %i \n", memcmp (addr, t->addr, addrlen));
387 if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
390 if (addrlen == t->addrlen)
392 if (0 == memcmp (addr, t->addr, addrlen))
399 if(t->addrlen == session->addrlen)
401 if (0 == memcmp (session->addr, t->addr, t->addrlen))
409 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
414 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES))
419 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
434 delete_session (struct Session *s)
436 if (s->msg_tk != NULL)
438 GNUNET_SERVER_mst_destroy (s->msg_tk);
441 GNUNET_free (s->addr);
442 GNUNET_free_non_null(s->server_recv);
443 GNUNET_free_non_null(s->server_send);
448 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
449 const void *addr, size_t addrlen,
450 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
452 struct Session *s = NULL;
454 s = GNUNET_malloc (sizeof (struct Session));
455 memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
457 s->addr = GNUNET_malloc (addrlen);
458 memcpy (s->addr, addr, addrlen);
459 s->addrlen = addrlen;
461 s->next_receive = GNUNET_TIME_absolute_get_zero();
466 notify_session_end (void *cls,
467 const struct GNUNET_PeerIdentity *
468 peer, struct Session * s)
470 struct Plugin *plugin = cls;
472 plugin->env->session_end (NULL, peer, s);
473 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
479 * Function that can be used by the transport service to transmit
480 * a message using the plugin. Note that in the case of a
481 * peer disconnecting, the continuation MUST be called
482 * prior to the disconnect notification itself. This function
483 * will be called with this peer's HELLO message to initiate
484 * a fresh connection to another peer.
487 * @param target who should receive this message
488 * @param msgbuf the message to transmit
489 * @param msgbuf_size number of bytes in 'msgbuf'
490 * @param priority how important is the message (most plugins will
491 * ignore message priority and just FIFO)
492 * @param to how long to wait at most for the transmission (does not
493 * require plugins to discard the message after the timeout,
494 * just advisory for the desired delay; most plugins will ignore
496 * @param session which session must be used (or NULL for "any")
497 * @param addr the address to use (can be NULL if the plugin
498 * is "on its own" (i.e. re-use existing TCP connection))
499 * @param addrlen length of the address in bytes
500 * @param force_address GNUNET_YES if the plugin MUST use the given address,
501 * GNUNET_NO means the plugin may use any other address and
502 * GNUNET_SYSERR means that only reliable existing
503 * bi-directional connections should be used (regardless
505 * @param cont continuation to call once the message has
506 * been transmitted (or if the transport is ready
507 * for the next transmission call; or if the
508 * peer disconnected...); can be NULL
509 * @param cont_cls closure for cont
510 * @return number of bytes used (on the physical network, with overheads);
511 * -1 on hard errors (i.e. address invalid); 0 is a legal value
512 * and does NOT mean that the message was not transmitted (DV)
515 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
516 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
517 struct GNUNET_TIME_Relative to, struct Session *session,
518 const void *addr, size_t addrlen, int force_address,
519 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
521 struct Plugin *plugin = cls;
522 struct HTTP_Message *msg;
523 GNUNET_assert (plugin != NULL);
525 int res = GNUNET_SYSERR;
528 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
529 "Sending %u bytes to peer `%s' on address `%s' %X %i\n", msgbuf_size,
530 GNUNET_i2s (target), ((addr != NULL) && (addrlen != 0)) ? http_plugin_address_to_string(plugin, addr, addrlen) : "<inbound>", session, force_address);
533 struct Session *s = NULL;
535 /* look for existing connection */
536 s = lookup_session (plugin, target, session, addr, addrlen, 1);
538 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
539 "%s existing session: %s\n", (s!=NULL) ? "Found" : "NOT Found", ((s != NULL) && (s->inbound == GNUNET_YES)) ? "inbound" : "outbound");
542 /* create new outbound connection */
545 if (plugin->max_connections <= plugin->cur_connections)
547 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
548 "Maximum number of connections reached, "
549 "cannot connect to peer `%s'\n",
550 GNUNET_i2s (target));
555 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
556 "Initiiating new connection to peer `%s'\n",
557 GNUNET_i2s (target));
559 s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
560 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
561 // initiate new connection
562 if (GNUNET_SYSERR == (res = client_connect (s)))
564 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
566 return GNUNET_SYSERR;
570 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
572 msg->size = msgbuf_size;
574 msg->buf = (char *) &msg[1];
575 msg->transmit_cont = cont;
576 msg->transmit_cont_cls = cont_cls;
577 memcpy (msg->buf, msgbuf, msgbuf_size);
579 if (s->inbound == GNUNET_NO)
582 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
583 "Using outbound client session to send to `%s'\n",
584 GNUNET_i2s (target));
586 client_send (s, msg);
589 if (s->inbound == GNUNET_YES)
591 server_send (s, msg);
594 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
595 "Using inbound server session to send to `%s'\n",
596 GNUNET_i2s (target));
605 * Function that can be used to force the plugin to disconnect
606 * from the given peer and cancel all previous transmissions
607 * (and their continuationc).
610 * @param target peer from which to disconnect
613 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
615 struct Plugin *plugin = cls;
616 struct Session *next = NULL;
617 struct Session *s = plugin->head;
619 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
620 "Transport tells me to disconnect `%s'\n",
621 GNUNET_i2s (target));
625 if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
627 if (s->inbound == GNUNET_NO)
628 GNUNET_assert (GNUNET_OK == client_disconnect (s));
630 GNUNET_assert (GNUNET_OK == server_disconnect (s));
631 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
639 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
642 struct Plugin *plugin = cls;
643 struct IPv4HttpAddressWrapper *w_t4 = NULL;
644 struct IPv6HttpAddressWrapper *w_t6 = NULL;
647 af = addr->sa_family;
651 w_t4 = plugin->ipv4_addr_head;
652 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
655 int res = memcmp (&w_t4->addr.ipv4_addr,
657 sizeof (struct in_addr));
660 if (a4->sin_port!= w_t4->addr.u4_port)
670 w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
671 memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr,
672 sizeof (struct in_addr));
673 w_t4->addr.u4_port = a4->sin_port;
675 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
676 plugin->ipv4_addr_tail, w_t4);
679 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
680 "Notifying transport to add IPv4 address `%s'\n",
681 http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct IPv4HttpAddress)));
683 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr, sizeof (struct IPv4HttpAddress));
687 w_t6 = plugin->ipv6_addr_head;
688 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
691 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
692 sizeof (struct in6_addr));
695 if (a6->sin6_port != w_t6->addr6.u6_port)
704 w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
706 memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
707 sizeof (struct in6_addr));
708 w_t6->addr6.u6_port = a6->sin6_port;
710 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
711 plugin->ipv6_addr_tail, w_t6);
714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
715 "Notifying transport to add IPv6 address `%s'\n",
716 http_plugin_address_to_string(NULL, &w_t6->addr6, sizeof (struct IPv6HttpAddress)));
718 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6, sizeof (struct IPv6HttpAddress));
727 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
730 struct Plugin *plugin = cls;
731 struct IPv4HttpAddressWrapper *w_t4 = NULL;
732 struct IPv6HttpAddressWrapper *w_t6 = NULL;
735 af = addr->sa_family;
739 w_t4 = plugin->ipv4_addr_head;
740 struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
743 int res = memcmp (&w_t4->addr.ipv4_addr,
745 sizeof (struct in_addr));
748 if (a4->sin_port!= w_t4->addr.u4_port)
760 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
761 "Notifying transport to remove IPv4 address `%s'\n",
762 http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct IPv4HttpAddress)));
764 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
765 sizeof (struct IPv4HttpAddress));
767 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
772 w_t6 = plugin->ipv6_addr_head;
773 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
776 int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
777 sizeof (struct in6_addr));
780 if (a6->sin6_port != w_t6->addr6.u6_port)
790 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
791 "Notifying transport to remove IPv6 address `%s'\n",
792 http_plugin_address_to_string(NULL, &w_t6->addr6, sizeof (struct IPv6HttpAddress)));
794 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
795 sizeof (struct IPv6HttpAddress));
797 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
808 * Our external IP address/port mapping has changed.
810 * @param cls closure, the 'struct LocalAddrList'
811 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
812 * the previous (now invalid) one
813 * @param addr either the previous or the new public IP address
814 * @param addrlen actual lenght of the address
817 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
820 GNUNET_assert (cls != NULL);
822 struct Plugin *plugin = cls;
825 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
826 "NPMC called %s to address `%s'\n",
827 (add_remove == GNUNET_NO) ? "remove" : "add",
828 GNUNET_a2s (addr, addrlen));
833 nat_add_address (cls, add_remove, addr, addrlen);
836 nat_remove_address (cls, add_remove, addr, addrlen);
842 http_check_ipv6 (struct Plugin *plugin)
844 struct GNUNET_NETWORK_Handle *desc = NULL;
845 if (plugin->ipv6 == GNUNET_YES)
847 /* probe IPv6 support */
848 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
851 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
854 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
856 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
858 ("Disabling IPv6 since it is not supported on this system!\n"));
859 plugin->ipv6 = GNUNET_NO;
863 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
867 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
868 "Testing IPv6 on this system: %s\n", (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
873 http_get_addresses (struct Plugin *plugin,
874 const char *serviceName,
875 const struct GNUNET_CONFIGURATION_Handle
876 *cfg, struct sockaddr ***addrs,
877 socklen_t ** addr_lens)
880 unsigned long long port;
881 struct addrinfo hints;
882 struct addrinfo *res;
883 struct addrinfo *pos;
884 struct addrinfo *next;
888 struct sockaddr **saddrs;
889 socklen_t *saddrlens;
895 disablev6 = !plugin->ipv6;
898 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
900 GNUNET_break (GNUNET_OK ==
901 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
905 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
907 ("Require valid port number for service in configuration!\n"));
908 return GNUNET_SYSERR;
912 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
914 GNUNET_break (GNUNET_OK ==
915 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
916 "BINDTO", &hostname));
921 if (hostname != NULL)
923 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
924 "Resolving `%s' since that is where `%s' will bind to.\n",
925 hostname, serviceName);
926 memset (&hints, 0, sizeof (struct addrinfo));
928 hints.ai_family = AF_INET;
929 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
932 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
933 hostname, gai_strerror (ret));
934 GNUNET_free (hostname);
935 return GNUNET_SYSERR;
939 while (NULL != (pos = next))
942 if ((disablev6) && (pos->ai_family == AF_INET6))
948 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
949 _("Failed to find %saddress for `%s'.\n"),
950 disablev6 ? "IPv4 " : "", hostname);
952 GNUNET_free (hostname);
953 return GNUNET_SYSERR;
956 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
957 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
960 while (NULL != (pos = next))
963 if ((disablev6) && (pos->ai_family == AF_INET6))
965 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
966 continue; /* not TCP */
967 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
969 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
970 "Service will bind to `%s'\n",
971 GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
972 if (pos->ai_family == AF_INET)
974 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
975 saddrlens[i] = pos->ai_addrlen;
976 saddrs[i] = GNUNET_malloc (saddrlens[i]);
977 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
978 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
982 GNUNET_assert (pos->ai_family == AF_INET6);
983 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
984 saddrlens[i] = pos->ai_addrlen;
985 saddrs[i] = GNUNET_malloc (saddrlens[i]);
986 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
987 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
991 GNUNET_free (hostname);
997 /* will bind against everything, just set port */
1003 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1004 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1006 saddrlens[i] = sizeof (struct sockaddr_in);
1007 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1008 #if HAVE_SOCKADDR_IN_SIN_LEN
1009 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1011 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1012 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1018 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1019 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1021 saddrlens[i] = sizeof (struct sockaddr_in6);
1022 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1023 #if HAVE_SOCKADDR_IN_SIN_LEN
1024 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1026 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1027 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1029 saddrlens[i] = sizeof (struct sockaddr_in);
1030 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1031 #if HAVE_SOCKADDR_IN_SIN_LEN
1032 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1034 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1035 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1039 *addr_lens = saddrlens;
1044 start_report_addresses (struct Plugin *plugin)
1046 int res = GNUNET_OK;
1047 struct sockaddr **addrs;
1048 socklen_t *addrlens;
1051 http_get_addresses (plugin, plugin->name, plugin->env->cfg,
1053 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1054 _("Found %u addresses to report to NAT service\n"),res);
1056 if (res != GNUNET_SYSERR)
1059 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1061 (const struct sockaddr **) addrs, addrlens,
1062 &nat_port_map_callback, NULL,
1068 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1070 GNUNET_a2s (addrs[res], addrlens[res]));
1072 GNUNET_assert (addrs[res] != NULL);
1073 GNUNET_free (addrs[res]);
1075 GNUNET_free_non_null (addrs);
1076 GNUNET_free_non_null (addrlens);
1081 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1082 NULL, NULL, plugin);
1087 stop_report_addresses (struct Plugin *plugin)
1089 /* Stop NAT handle */
1090 GNUNET_NAT_unregister (plugin->nat);
1092 /* Clean up addresses */
1093 struct IPv4HttpAddressWrapper *w_t4;
1094 struct IPv6HttpAddressWrapper *w_t6;
1096 while (plugin->ipv4_addr_head != NULL)
1098 w_t4 = plugin->ipv4_addr_head;
1099 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1104 while (plugin->ipv6_addr_head != NULL)
1106 w_t6 = plugin->ipv6_addr_head;
1107 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1114 configure_plugin (struct Plugin *plugin)
1116 int res = GNUNET_OK;
1119 if (GNUNET_CONFIGURATION_have_value
1120 (plugin->env->cfg, plugin->name, "USE_IPv4"))
1123 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1127 plugin->ipv4 = GNUNET_YES;
1130 if (GNUNET_CONFIGURATION_have_value
1131 (plugin->env->cfg, plugin->name, "USE_IPv6"))
1134 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1138 plugin->ipv6 = GNUNET_YES;
1140 if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1142 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1144 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1146 res = GNUNET_SYSERR;
1149 /* Reading port number from config file */
1150 unsigned long long port;
1153 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1154 "PORT", &port)) || (port > 65535))
1156 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1157 _("Port is required! Fix in configuration\n"),
1159 res = GNUNET_SYSERR;
1162 plugin->port = port;
1164 plugin->client_only = GNUNET_NO;
1165 if (plugin->port == 0)
1167 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1168 _("Port 0, client only mode\n"));
1169 plugin->client_only = GNUNET_YES;
1172 char * bind4_address = NULL;
1173 if ((plugin->ipv4 == GNUNET_YES) && (GNUNET_YES ==
1174 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1175 "BINDTO", &bind4_address)))
1177 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1178 "Binding %s plugin to specific IPv4 address: `%s'\n",
1179 plugin->protocol, bind4_address);
1180 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1181 if (1 != inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1183 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1184 _("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1185 bind4_address, plugin->protocol);
1186 GNUNET_free (plugin->server_addr_v4);
1187 plugin->server_addr_v4 = NULL;
1191 plugin->server_addr_v4->sin_family = AF_INET;
1192 plugin->server_addr_v4->sin_port = htons (plugin->port);
1194 GNUNET_free (bind4_address);
1198 char * bind6_address = NULL;
1199 if ((plugin->ipv6 == GNUNET_YES) && (GNUNET_YES ==
1200 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1201 "BINDTO6", &bind6_address)))
1203 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1204 "Binding %s plugin to specific IPv6 address: `%s'\n",
1205 plugin->protocol, bind6_address);
1206 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1207 if (1 != inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1209 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1210 _("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1211 bind6_address, plugin->protocol);
1212 GNUNET_free (plugin->server_addr_v6);
1213 plugin->server_addr_v6 = NULL;
1217 plugin->server_addr_v6->sin6_family = AF_INET6;
1218 plugin->server_addr_v6->sin6_port = htons (plugin->port);
1220 GNUNET_free (bind6_address);
1224 /* Optional parameters */
1225 unsigned long long maxneigh;
1227 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1228 "MAX_CONNECTIONS", &maxneigh))
1230 plugin->max_connections = maxneigh;
1237 * Entry point for the plugin.
1240 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1242 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1243 struct GNUNET_TRANSPORT_PluginFunctions *api;
1244 struct Plugin *plugin;
1247 plugin = GNUNET_malloc (sizeof (struct Plugin));
1249 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1251 api->send = &http_plugin_send;
1252 api->disconnect = &http_plugin_disconnect;
1253 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1254 api->check_address = &http_plugin_address_suggested;
1255 api->address_to_string = &http_plugin_address_to_string;
1258 plugin->name = "transport-https";
1259 plugin->protocol = "https";
1261 plugin->name = "transport-http";
1262 plugin->protocol = "http";
1264 /* Configure plugin from configuration */
1265 res = configure_plugin (plugin);
1266 if (res == GNUNET_SYSERR)
1268 GNUNET_free_non_null (plugin->server_addr_v4);
1269 GNUNET_free_non_null (plugin->server_addr_v6);
1270 GNUNET_free (plugin);
1275 /* checking IPv6 support */
1276 http_check_ipv6 (plugin);
1279 res = client_start (plugin);
1280 if (res == GNUNET_SYSERR)
1282 GNUNET_free_non_null (plugin->server_addr_v4);
1283 GNUNET_free_non_null (plugin->server_addr_v6);
1284 GNUNET_free (plugin);
1290 if (plugin->client_only == GNUNET_NO)
1292 res = server_start (plugin);
1293 if (res == GNUNET_SYSERR)
1295 server_stop (plugin);
1296 client_stop (plugin);
1298 GNUNET_free_non_null (plugin->server_addr_v4);
1299 GNUNET_free_non_null (plugin->server_addr_v6);
1300 GNUNET_free (plugin);
1305 /* Report addresses to transport service */
1306 start_report_addresses (plugin);
1309 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1310 "Plugin `%s' loaded\n", plugin->name);
1318 * Exit point from the plugin.
1321 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1323 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1324 struct Plugin *plugin = api->cls;
1325 struct Session *s = NULL;
1327 /* Stop reporting addresses to transport service */
1328 stop_report_addresses (plugin);
1330 /* cleaning up sessions */
1335 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1336 "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1338 if (s->inbound == GNUNET_NO)
1339 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1341 GNUNET_assert (GNUNET_OK == server_disconnect (s));
1346 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1347 "Stopping server\n");
1350 server_stop (plugin);
1353 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1354 "Stopping client\n");
1357 client_stop (plugin);
1359 /* deleting up sessions */
1363 struct Session *t = s->next;
1364 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1371 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1372 "Plugin `%s' unloaded\n", plugin->name);
1375 GNUNET_free_non_null (plugin->server_addr_v4);
1376 GNUNET_free_non_null (plugin->server_addr_v6);
1377 GNUNET_free (plugin);
1383 /* end of plugin_transport_http.c */