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 sockaddr_in addr;
55 * Wrapper for IPv4 addresses.
57 struct IPv6HttpAddressWrapper
62 struct IPv6HttpAddressWrapper *next;
65 * Linked list previous
67 struct IPv6HttpAddressWrapper *prev;
72 struct sockaddr_in6 addr GNUNET_PACKED;
77 * Context for address to string conversion.
79 struct PrettyPrinterContext
82 * Function to call with the result.
84 GNUNET_TRANSPORT_AddressStringCallback asc;
89 struct Plugin *plugin;
97 * Port to add after the IP address.
104 * Encapsulation of all of the state of the plugin.
111 * Append our port and forward the result.
113 * @param cls the 'struct PrettyPrinterContext*'
114 * @param hostname hostname part of the address
117 append_port (void *cls, const char *hostname)
119 struct PrettyPrinterContext *ppc = cls;
122 if (hostname == NULL)
124 ppc->asc (ppc->asc_cls, NULL);
128 GNUNET_asprintf (&ret, "%s://%s:%d", ppc->plugin->protocol, hostname,
130 ppc->asc (ppc->asc_cls, ret);
136 * Convert the transports address to a nice, human-readable
140 * @param type name of the transport that generated the address
141 * @param addr one of the addresses of the host, NULL for the last address
142 * the specific address format depends on the transport
143 * @param addrlen length of the address
144 * @param numeric should (IP) addresses be displayed in numeric form?
145 * @param timeout after how long should we give up?
146 * @param asc function to call on each string
147 * @param asc_cls closure for asc
150 http_plugin_address_pretty_printer (void *cls, const char *type,
151 const void *addr, size_t addrlen,
153 struct GNUNET_TIME_Relative timeout,
154 GNUNET_TRANSPORT_AddressStringCallback asc,
157 GNUNET_assert (cls != NULL);
158 struct PrettyPrinterContext *ppc;
163 if (addrlen == sizeof (struct sockaddr_in6))
166 sbs = sizeof (struct sockaddr_in6);
168 port = ntohs (((struct sockaddr_in6 *)addr)->sin6_port);
170 else if (addrlen == sizeof (struct sockaddr_in))
173 sbs = sizeof (struct sockaddr_in);
174 port = ntohs (((struct sockaddr_in *)addr)->sin_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;
213 GNUNET_assert (cls != NULL);
214 if ((addrlen != sizeof (struct sockaddr_in)) ||
215 (addrlen != sizeof (struct sockaddr_in6)))
216 return GNUNET_SYSERR;
218 if (addrlen == sizeof (struct sockaddr_in))
220 while (w_tv4 != NULL)
222 if (0 == memcmp (&w_tv4->addr, addr, sizeof (struct sockaddr_in)))
229 return GNUNET_SYSERR;
231 if (addrlen == sizeof (struct sockaddr_in6))
233 while (w_tv6 != NULL)
236 memcmp (&w_tv6->addr, addr,
237 sizeof (struct sockaddr_in6)))
244 return GNUNET_SYSERR;
249 struct GNUNET_TIME_Relative
250 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity * peer,
251 const struct GNUNET_MessageHeader * message,
252 struct Session * session,
253 const char *sender_address,
254 uint16_t sender_address_len)
256 struct Session *s = cls;
257 struct Plugin *plugin = s->plugin;
258 struct GNUNET_TRANSPORT_ATS_Information distance[2];
259 struct GNUNET_TIME_Relative delay;
261 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
262 distance[0].value = htonl (1);
263 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
264 distance[1].value = htonl (0);
266 delay = plugin->env->receive (plugin->env->cls, &s->target, message, (const struct GNUNET_TRANSPORT_ATS_Information*) &distance, 2, s, s->addr, s->addrlen);
271 * Function called for a quick conversion of the binary address to
272 * a numeric address. Note that the caller must not free the
273 * address and that the next call to this function is allowed
274 * to override the address again.
277 * @param addr binary address
278 * @param addrlen length of the address
279 * @return string representing the same address
282 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
285 struct sockaddr_in *a4;
286 struct sockaddr_in6 *a6;
288 static char rbuf[INET6_ADDRSTRLEN + 13];
292 if (addrlen == sizeof (struct sockaddr_in6))
294 a6 = (struct sockaddr_in6 *) addr;
295 address = GNUNET_malloc (INET6_ADDRSTRLEN);
296 inet_ntop (AF_INET6, &(a6->sin6_addr), address, INET6_ADDRSTRLEN);
297 port = ntohs (a6->sin6_port);
299 else if (addrlen == sizeof (struct sockaddr_in))
301 a4 = (struct sockaddr_in *) addr;
302 address = GNUNET_malloc (INET_ADDRSTRLEN);
303 inet_ntop (AF_INET, &(a4->sin_addr), address, INET_ADDRSTRLEN);
304 port = ntohs (a4->sin_port);
308 /* invalid address */
312 char * protocol = "http";
314 char * protocol = "https";
317 GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
318 if (addrlen == sizeof (struct sockaddr_in6))
319 res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, address, port);
320 else if (addrlen == sizeof (struct sockaddr_in))
321 res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address, port);
323 GNUNET_free (address);
324 GNUNET_assert (res != 0);
329 lookup_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
330 struct Session * session,
331 const void *addr, size_t addrlen, int force_address)
333 struct Session *s = NULL;
334 struct Session *t = NULL;
344 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
345 "Comparing peer `%s' address `%s' len %i session %X to \n", GNUNET_i2s(target), GNUNET_a2s(addr,addrlen), addrlen, session);
346 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);
347 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"memcmp %i \n", memcmp (addr, t->addr, addrlen));
352 if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
355 if (addrlen == t->addrlen)
357 if (0 == memcmp (addr, t->addr, addrlen))
364 if(t->addrlen == session->addrlen)
366 if (0 == memcmp (session->addr, t->addr, t->addrlen))
374 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
379 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES))
384 if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
399 delete_session (struct Session *s)
401 if (s->msg_tk != NULL)
403 GNUNET_SERVER_mst_destroy (s->msg_tk);
406 GNUNET_free (s->addr);
407 GNUNET_free_non_null(s->server_recv);
408 GNUNET_free_non_null(s->server_send);
413 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
414 const void *addr, size_t addrlen,
415 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
417 struct Session *s = NULL;
419 s = GNUNET_malloc (sizeof (struct Session));
420 memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
422 s->addr = GNUNET_malloc (addrlen);
423 memcpy (s->addr, addr, addrlen);
424 s->addrlen = addrlen;
426 s->next_receive = GNUNET_TIME_absolute_get_zero();
431 notify_session_end (void *cls,
432 const struct GNUNET_PeerIdentity *
433 peer, struct Session * s)
435 struct Plugin *plugin = cls;
437 plugin->env->session_end (NULL, peer, s);
438 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
444 * Function that can be used by the transport service to transmit
445 * a message using the plugin. Note that in the case of a
446 * peer disconnecting, the continuation MUST be called
447 * prior to the disconnect notification itself. This function
448 * will be called with this peer's HELLO message to initiate
449 * a fresh connection to another peer.
452 * @param target who should receive this message
453 * @param msgbuf the message to transmit
454 * @param msgbuf_size number of bytes in 'msgbuf'
455 * @param priority how important is the message (most plugins will
456 * ignore message priority and just FIFO)
457 * @param to how long to wait at most for the transmission (does not
458 * require plugins to discard the message after the timeout,
459 * just advisory for the desired delay; most plugins will ignore
461 * @param session which session must be used (or NULL for "any")
462 * @param addr the address to use (can be NULL if the plugin
463 * is "on its own" (i.e. re-use existing TCP connection))
464 * @param addrlen length of the address in bytes
465 * @param force_address GNUNET_YES if the plugin MUST use the given address,
466 * GNUNET_NO means the plugin may use any other address and
467 * GNUNET_SYSERR means that only reliable existing
468 * bi-directional connections should be used (regardless
470 * @param cont continuation to call once the message has
471 * been transmitted (or if the transport is ready
472 * for the next transmission call; or if the
473 * peer disconnected...); can be NULL
474 * @param cont_cls closure for cont
475 * @return number of bytes used (on the physical network, with overheads);
476 * -1 on hard errors (i.e. address invalid); 0 is a legal value
477 * and does NOT mean that the message was not transmitted (DV)
480 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
481 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
482 struct GNUNET_TIME_Relative to, struct Session *session,
483 const void *addr, size_t addrlen, int force_address,
484 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
486 struct Plugin *plugin = cls;
487 struct HTTP_Message *msg;
488 GNUNET_assert (plugin != NULL);
490 int res = GNUNET_SYSERR;
493 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
494 "Sending %u bytes to peer `%s' on address `%s' %X %i\n", msgbuf_size,
495 GNUNET_i2s (target), ((addr != NULL) && (addrlen != 0)) ? http_plugin_address_to_string(plugin, addr, addrlen) : "<inbound>", session, force_address);
498 struct Session *s = NULL;
500 /* look for existing connection */
501 s = lookup_session (plugin, target, session, addr, addrlen, 1);
503 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
504 "%s existing session: %s\n", (s!=NULL) ? "Found" : "NOT Found", ((s != NULL) && (s->inbound == GNUNET_YES)) ? "inbound" : "outbound");
507 /* create new outbound connection */
510 if (plugin->max_connections <= plugin->cur_connections)
512 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
513 "Maximum number of connections reached, "
514 "cannot connect to peer `%s'\n",
515 GNUNET_i2s (target));
520 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
521 "Initiiating new connection to peer `%s'\n",
522 GNUNET_i2s (target));
524 s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
525 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
526 // initiate new connection
527 if (GNUNET_SYSERR == (res = client_connect (s)))
529 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
531 return GNUNET_SYSERR;
535 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
537 msg->size = msgbuf_size;
539 msg->buf = (char *) &msg[1];
540 msg->transmit_cont = cont;
541 msg->transmit_cont_cls = cont_cls;
542 memcpy (msg->buf, msgbuf, msgbuf_size);
544 if (s->inbound == GNUNET_NO)
547 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
548 "Using outbound client session to send to `%s'\n",
549 GNUNET_i2s (target));
551 client_send (s, msg);
554 if (s->inbound == GNUNET_YES)
556 server_send (s, msg);
559 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
560 "Using inbound server session to send to `%s'\n",
561 GNUNET_i2s (target));
570 * Function that can be used to force the plugin to disconnect
571 * from the given peer and cancel all previous transmissions
572 * (and their continuationc).
575 * @param target peer from which to disconnect
578 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
580 struct Plugin *plugin = cls;
581 struct Session *next = NULL;
582 struct Session *s = plugin->head;
584 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
585 "Transport tells me to disconnect `%s'\n",
586 GNUNET_i2s (target));
590 if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
592 if (s->inbound == GNUNET_NO)
593 GNUNET_assert (GNUNET_OK == client_disconnect (s));
595 GNUNET_assert (GNUNET_OK == server_disconnect (s));
596 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
604 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
607 struct Plugin *plugin = cls;
608 struct IPv4HttpAddressWrapper *w_t4 = NULL;
609 struct IPv6HttpAddressWrapper *w_t6 = NULL;
612 af = addr->sa_family;
616 w_t4 = plugin->ipv4_addr_head;
619 int res = memcmp (&w_t4->addr,
620 (struct sockaddr_in *) addr,
621 sizeof (struct sockaddr_in));
629 w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
630 memcpy (&w_t4->addr, (struct sockaddr_in *) addr,
631 sizeof (struct sockaddr_in));
633 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
634 plugin->ipv4_addr_tail, w_t4);
637 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
638 "Notifying transport to add IPv4 address `%s'\n",
639 http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
641 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr, sizeof (struct sockaddr_in));
645 w_t6 = plugin->ipv6_addr_head;
648 int res = memcmp (&w_t6->addr,
649 (struct sockaddr_in6 *) addr,
650 sizeof (struct sockaddr_in6));
658 w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
659 memcpy (&w_t6->addr, (struct sockaddr_in6 *) addr,
660 sizeof (struct sockaddr_in6));
662 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
663 plugin->ipv6_addr_tail, w_t6);
666 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
667 "Notifying transport to add IPv6 address `%s'\n",
668 http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
670 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr, sizeof (struct sockaddr_in6));
679 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
682 struct Plugin *plugin = cls;
683 struct IPv4HttpAddressWrapper *w_t4 = NULL;
684 struct IPv6HttpAddressWrapper *w_t6 = NULL;
687 af = addr->sa_family;
691 w_t4 = plugin->ipv4_addr_head;
694 int res = memcmp (&(w_t4->addr.sin_addr),
695 &((struct sockaddr_in *) addr)->sin_addr,
696 sizeof (struct in_addr));
706 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
707 "Notifying transport to remove IPv4 address `%s'\n",
708 http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
710 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
711 sizeof (struct sockaddr_in));
713 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
718 w_t6 = plugin->ipv6_addr_head;
721 int res = memcmp (&(w_t6->addr.sin6_addr),
722 &((struct sockaddr_in6 *) addr)->sin6_addr,
723 sizeof (struct in6_addr));
732 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
733 "Notifying transport to remove IPv6 address `%s'\n",
734 http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
736 plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr,
737 sizeof (struct sockaddr_in6 ));
739 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
750 * Our external IP address/port mapping has changed.
752 * @param cls closure, the 'struct LocalAddrList'
753 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
754 * the previous (now invalid) one
755 * @param addr either the previous or the new public IP address
756 * @param addrlen actual lenght of the address
759 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
762 GNUNET_assert (cls != NULL);
764 struct Plugin *plugin = cls;
767 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
768 "NPMC called %s to address `%s'\n",
769 (add_remove == GNUNET_NO) ? "remove" : "add",
770 GNUNET_a2s (addr, addrlen));
775 nat_add_address (cls, add_remove, addr, addrlen);
778 nat_remove_address (cls, add_remove, addr, addrlen);
784 http_check_ipv6 (struct Plugin *plugin)
786 struct GNUNET_NETWORK_Handle *desc = NULL;
787 if (plugin->ipv6 == GNUNET_YES)
789 /* probe IPv6 support */
790 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
793 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
796 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
798 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
800 ("Disabling IPv6 since it is not supported on this system!\n"));
801 plugin->ipv6 = GNUNET_NO;
805 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
809 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
810 "Testing IPv6 on this system: %s\n", (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
815 http_get_addresses (struct Plugin *plugin,
816 const char *serviceName,
817 const struct GNUNET_CONFIGURATION_Handle
818 *cfg, struct sockaddr ***addrs,
819 socklen_t ** addr_lens)
822 unsigned long long port;
823 struct addrinfo hints;
824 struct addrinfo *res;
825 struct addrinfo *pos;
826 struct addrinfo *next;
830 struct sockaddr **saddrs;
831 socklen_t *saddrlens;
837 disablev6 = !plugin->ipv6;
840 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
842 GNUNET_break (GNUNET_OK ==
843 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
849 ("Require valid port number for service in configuration!\n"));
850 return GNUNET_SYSERR;
854 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
856 GNUNET_break (GNUNET_OK ==
857 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
858 "BINDTO", &hostname));
863 if (hostname != NULL)
865 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
866 "Resolving `%s' since that is where `%s' will bind to.\n",
867 hostname, serviceName);
868 memset (&hints, 0, sizeof (struct addrinfo));
870 hints.ai_family = AF_INET;
871 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
874 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
875 hostname, gai_strerror (ret));
876 GNUNET_free (hostname);
877 return GNUNET_SYSERR;
881 while (NULL != (pos = next))
884 if ((disablev6) && (pos->ai_family == AF_INET6))
890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
891 _("Failed to find %saddress for `%s'.\n"),
892 disablev6 ? "IPv4 " : "", hostname);
894 GNUNET_free (hostname);
895 return GNUNET_SYSERR;
898 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
899 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
902 while (NULL != (pos = next))
905 if ((disablev6) && (pos->ai_family == AF_INET6))
907 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
908 continue; /* not TCP */
909 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
911 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
912 "Service will bind to `%s'\n",
913 GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
914 if (pos->ai_family == AF_INET)
916 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
917 saddrlens[i] = pos->ai_addrlen;
918 saddrs[i] = GNUNET_malloc (saddrlens[i]);
919 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
920 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
924 GNUNET_assert (pos->ai_family == AF_INET6);
925 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
926 saddrlens[i] = pos->ai_addrlen;
927 saddrs[i] = GNUNET_malloc (saddrlens[i]);
928 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
929 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
933 GNUNET_free (hostname);
939 /* will bind against everything, just set port */
945 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
946 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
948 saddrlens[i] = sizeof (struct sockaddr_in);
949 saddrs[i] = GNUNET_malloc (saddrlens[i]);
950 #if HAVE_SOCKADDR_IN_SIN_LEN
951 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
953 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
954 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
960 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
961 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
963 saddrlens[i] = sizeof (struct sockaddr_in6);
964 saddrs[i] = GNUNET_malloc (saddrlens[i]);
965 #if HAVE_SOCKADDR_IN_SIN_LEN
966 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
968 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
969 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
971 saddrlens[i] = sizeof (struct sockaddr_in);
972 saddrs[i] = GNUNET_malloc (saddrlens[i]);
973 #if HAVE_SOCKADDR_IN_SIN_LEN
974 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
976 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
977 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
981 *addr_lens = saddrlens;
986 start_report_addresses (struct Plugin *plugin)
989 struct sockaddr **addrs;
993 http_get_addresses (plugin, plugin->name, plugin->env->cfg,
995 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
996 _("Found %u addresses to report to NAT service\n"),res);
998 if (res != GNUNET_SYSERR)
1001 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1003 (const struct sockaddr **) addrs, addrlens,
1004 &nat_port_map_callback, NULL,
1010 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1012 GNUNET_a2s (addrs[res], addrlens[res]));
1014 GNUNET_assert (addrs[res] != NULL);
1015 GNUNET_free (addrs[res]);
1017 GNUNET_free_non_null (addrs);
1018 GNUNET_free_non_null (addrlens);
1023 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1024 NULL, NULL, plugin);
1029 stop_report_addresses (struct Plugin *plugin)
1031 /* Stop NAT handle */
1032 GNUNET_NAT_unregister (plugin->nat);
1034 /* Clean up addresses */
1035 struct IPv4HttpAddressWrapper *w_t4;
1036 struct IPv6HttpAddressWrapper *w_t6;
1038 while (plugin->ipv4_addr_head != NULL)
1040 w_t4 = plugin->ipv4_addr_head;
1041 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1046 while (plugin->ipv6_addr_head != NULL)
1048 w_t6 = plugin->ipv6_addr_head;
1049 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1056 configure_plugin (struct Plugin *plugin)
1058 int res = GNUNET_OK;
1061 if (GNUNET_CONFIGURATION_have_value
1062 (plugin->env->cfg, plugin->name, "USE_IPv4"))
1065 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1069 plugin->ipv4 = GNUNET_YES;
1072 if (GNUNET_CONFIGURATION_have_value
1073 (plugin->env->cfg, plugin->name, "USE_IPv6"))
1076 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1080 plugin->ipv6 = GNUNET_YES;
1082 if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1084 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1086 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1088 res = GNUNET_SYSERR;
1091 /* Reading port number from config file */
1092 unsigned long long port;
1095 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1096 "PORT", &port)) || (port > 65535))
1098 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1099 _("Port is required! Fix in configuration\n"),
1101 res = GNUNET_SYSERR;
1104 plugin->port = port;
1106 plugin->client_only = GNUNET_NO;
1107 if (plugin->port == 0)
1109 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1110 _("Port 0, client only mode\n"));
1111 plugin->client_only = GNUNET_YES;
1114 char * bind4_address = NULL;
1115 if ((plugin->ipv4 == GNUNET_YES) && (GNUNET_YES ==
1116 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1117 "BINDTO", &bind4_address)))
1119 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1120 "Binding %s plugin to specific IPv4 address: `%s'\n",
1121 plugin->protocol, bind4_address);
1122 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1123 if (1 != inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1125 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1126 _("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1127 bind4_address, plugin->protocol);
1128 GNUNET_free (plugin->server_addr_v4);
1129 plugin->server_addr_v4 = NULL;
1133 plugin->server_addr_v4->sin_family = AF_INET;
1134 plugin->server_addr_v4->sin_port = htons (plugin->port);
1136 GNUNET_free (bind4_address);
1140 char * bind6_address = NULL;
1141 if ((plugin->ipv6 == GNUNET_YES) && (GNUNET_YES ==
1142 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1143 "BINDTO6", &bind6_address)))
1145 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1146 "Binding %s plugin to specific IPv6 address: `%s'\n",
1147 plugin->protocol, bind6_address);
1148 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1149 if (1 != inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1151 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1152 _("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1153 bind6_address, plugin->protocol);
1154 GNUNET_free (plugin->server_addr_v6);
1155 plugin->server_addr_v6 = NULL;
1159 plugin->server_addr_v6->sin6_family = AF_INET6;
1160 plugin->server_addr_v6->sin6_port = htons (plugin->port);
1162 GNUNET_free (bind6_address);
1166 /* Optional parameters */
1167 unsigned long long maxneigh;
1169 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1170 "MAX_CONNECTIONS", &maxneigh))
1172 plugin->max_connections = maxneigh;
1179 * Entry point for the plugin.
1182 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1184 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1185 struct GNUNET_TRANSPORT_PluginFunctions *api;
1186 struct Plugin *plugin;
1189 plugin = GNUNET_malloc (sizeof (struct Plugin));
1191 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1193 api->send = &http_plugin_send;
1194 api->disconnect = &http_plugin_disconnect;
1195 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1196 api->check_address = &http_plugin_address_suggested;
1197 api->address_to_string = &http_plugin_address_to_string;
1200 plugin->name = "transport-https";
1201 plugin->protocol = "https";
1203 plugin->name = "transport-http";
1204 plugin->protocol = "http";
1206 /* Configure plugin from configuration */
1207 res = configure_plugin (plugin);
1208 if (res == GNUNET_SYSERR)
1210 GNUNET_free_non_null (plugin->server_addr_v4);
1211 GNUNET_free_non_null (plugin->server_addr_v6);
1212 GNUNET_free (plugin);
1217 /* checking IPv6 support */
1218 http_check_ipv6 (plugin);
1221 res = client_start (plugin);
1222 if (res == GNUNET_SYSERR)
1224 GNUNET_free_non_null (plugin->server_addr_v4);
1225 GNUNET_free_non_null (plugin->server_addr_v6);
1226 GNUNET_free (plugin);
1232 if (plugin->client_only == GNUNET_NO)
1234 res = server_start (plugin);
1235 if (res == GNUNET_SYSERR)
1237 server_stop (plugin);
1238 client_stop (plugin);
1240 GNUNET_free_non_null (plugin->server_addr_v4);
1241 GNUNET_free_non_null (plugin->server_addr_v6);
1242 GNUNET_free (plugin);
1247 /* Report addresses to transport service */
1248 start_report_addresses (plugin);
1251 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1252 "Plugin `%s' loaded\n", plugin->name);
1260 * Exit point from the plugin.
1263 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1265 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1266 struct Plugin *plugin = api->cls;
1267 struct Session *s = NULL;
1269 /* Stop reporting addresses to transport service */
1270 stop_report_addresses (plugin);
1272 /* cleaning up sessions */
1277 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1278 "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1280 if (s->inbound == GNUNET_NO)
1281 GNUNET_assert (GNUNET_OK == client_disconnect (s));
1283 GNUNET_assert (GNUNET_OK == server_disconnect (s));
1288 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1289 "Stopping server\n");
1292 server_stop (plugin);
1295 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1296 "Stopping client\n");
1299 client_stop (plugin);
1301 /* deleting up sessions */
1305 struct Session *t = s->next;
1306 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1313 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1314 "Plugin `%s' unloaded\n", plugin->name);
1317 GNUNET_free_non_null (plugin->server_addr_v4);
1318 GNUNET_free_non_null (plugin->server_addr_v6);
1319 GNUNET_free (plugin);
1325 /* end of plugin_transport_http.c */