+/**
+ * Reschedule the execution of both IPv4 and IPv6 server
+ * @param plugin the plugin
+ * @param server which server to schedule v4 or v6?
+ * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
+ * until timeout
+ */
+static void
+server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now)
+{
+ if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
+ {
+ if (GNUNET_YES == plugin->server_v4_immediately)
+ return; /* No rescheduling, server will run asap */
+
+ if (GNUNET_YES == now)
+ plugin->server_v4_immediately = GNUNET_YES;
+
+ if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
+ {
+ GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
+ plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
+ }
+
+ if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
+ {
+ if (GNUNET_YES == plugin->server_v6_immediately)
+ return; /* No rescheduling, server will run asap */
+
+ if (GNUNET_YES == now)
+ plugin->server_v6_immediately = GNUNET_YES;
+
+ if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
+ {
+ GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
+ plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
+ }
+}
+
+/**
+ * Callback called by MessageStreamTokenizer when a message has arrived
+ * @param cls current session as closure
+ * @param client clien
+ * @param message the message to be forwarded to transport service
+ */
+static int
+server_receive_mst_cb (void *cls, void *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct Session *s = cls;
+
+ GNUNET_assert (NULL != p);
+ if (GNUNET_NO == exist_session(p, s))
+ return GNUNET_OK;
+
+ struct Plugin *plugin = s->plugin;
+ struct GNUNET_TIME_Relative delay;
+
+ delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
+
+ s->next_receive =
+ GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
+
+ if (delay.rel_value > 0)
+ {
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
+ GNUNET_i2s (&s->target),
+ http_plugin_address_to_string (NULL, s->addr, s->addrlen),
+ delay);
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Callback called by MHD when it needs data to send
+ * @param cls current session
+ * @param pos position in buffer
+ * @param buf the buffer to write data to
+ * @param max max number of bytes available in buffer
+ * @return bytes written to buffer
+ */
+static ssize_t
+server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
+{
+ struct Session *s = cls;
+ ssize_t bytes_read = 0;
+ struct HTTP_Message *msg;
+
+ GNUNET_assert (NULL != p);
+ if (GNUNET_NO == exist_session(p, s))
+ return 0;
+ msg = s->msg_head;
+ if (NULL != msg)
+ {
+ /* sending */
+ bytes_read = GNUNET_MIN (msg->size - msg->pos,
+ max);
+ memcpy (buf, &msg->buf[msg->pos], bytes_read);
+ msg->pos += bytes_read;
+
+ /* removing message */
+ if (msg->pos == msg->size)
+ {
+ GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
+ if (NULL != msg->transmit_cont)
+ msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
+ GNUNET_free (msg);
+ }
+ }
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
+ "Server: %p: sent %u bytes\n", s, bytes_read);
+ return bytes_read;
+}
+
+
+static struct Session *
+server_lookup_session (struct Plugin *plugin,
+ struct ServerConnection * sc)
+{
+ struct Session *s;
+
+ for (s = plugin->head; NULL != s; s = s->next)
+ if ((s->server_recv == sc) || (s->server_send == sc))
+ return s;
+ for (s = plugin->server_semi_head; NULL != s; s = s->next)
+ if ((s->server_recv == sc) || (s->server_send == sc))
+ return s;
+ return NULL;
+}
+
+
+static struct ServerConnection *
+server_lookup_serverconnection (struct Plugin *plugin,
+ struct MHD_Connection *mhd_connection, const char *url,
+ const char *method)
+{
+ struct Session *s = NULL;
+ struct Session *t;
+ struct ServerConnection *sc = NULL;
+ const union MHD_ConnectionInfo *conn_info;
+ struct GNUNET_ATS_Information ats;
+ struct IPv4HttpAddress a4;
+ struct IPv6HttpAddress a6;
+ struct sockaddr_in *s4;
+ struct sockaddr_in6 *s6;
+ void *a;
+ size_t a_len;
+ struct GNUNET_PeerIdentity target;
+ int check = GNUNET_NO;
+ uint32_t tag = 0;
+ int direction = GNUNET_SYSERR;
+
+ conn_info = MHD_get_connection_info (mhd_connection,
+ MHD_CONNECTION_INFO_CLIENT_ADDRESS);
+ if ((conn_info->client_addr->sa_family != AF_INET) &&
+ (conn_info->client_addr->sa_family != AF_INET6))
+ return NULL;
+
+ if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
+ {
+ char hash[104];
+ char *tagc = (char *) &url[105];
+
+ memcpy (&hash, &url[1], 103);
+ hash[103] = '\0';
+ if (GNUNET_OK ==
+ GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
+ &(target.hashPubKey)))
+ {
+ tag = strtoul (tagc, NULL, 10);
+ if (tagc > 0)
+ check = GNUNET_YES;
+ }
+ }
+
+ if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
+ direction = _RECEIVE;
+ else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
+ direction = _SEND;
+ else
+ {
+ GNUNET_break_op (0);
+ goto error;
+ }
+
+
+ if (check == GNUNET_NO)
+ goto error;
+
+ plugin->cur_connections++;
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: New %s connection from %s with tag %u\n",
+ method,
+ GNUNET_i2s (&target), tag);
+
+ /* find duplicate session */
+ t = plugin->head;
+ while (t != NULL)
+ {
+ if ((t->inbound) &&
+ (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
+ &&
+ /* FIXME add source address comparison */
+ (t->tag == tag))
+ break;
+ t = t->next;
+ }
+ if (t != NULL)
+ {
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Duplicate session, dismissing new connection from peer `%s'\n",
+ GNUNET_i2s (&target));
+ goto error;
+ }
+
+ /* find semi-session */
+ t = plugin->server_semi_head;
+
+ while (t != NULL)
+ {
+ /* FIXME add source address comparison */
+ if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
+ && (t->tag == tag))
+ {
+ break;
+ }
+ t = t->next;
+ }
+
+ if (t == NULL)
+ goto create;
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Found existing semi-session for `%s'\n",
+ GNUNET_i2s (&target));
+
+ if ((direction == _SEND) && (t->server_send != NULL))
+ {
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
+ GNUNET_i2s (&target));
+ goto error;
+ }
+ else
+ {
+ s = t;
+ GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
+ plugin->server_semi_tail, s);
+ GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Found matching semi-session, merging session for peer `%s'\n",
+ GNUNET_i2s (&target));
+
+ plugin->inbound_sessions ++;
+ GNUNET_STATISTICS_set (plugin->env->stats,
+ "# HTTP inbound sessions",
+ plugin->inbound_sessions,
+ GNUNET_NO);
+ GNUNET_assert (NULL != s);
+ goto found;
+ }
+ if ((direction == _RECEIVE) && (t->server_recv != NULL))
+ {
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
+ GNUNET_i2s (&target));
+ goto error;
+ }
+ else
+ {
+ s = t;
+ GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
+ plugin->server_semi_tail, s);
+ GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Found matching semi-session, merging session for peer `%s'\n",
+ GNUNET_i2s (&target));
+ plugin->inbound_sessions ++;
+ GNUNET_STATISTICS_set (plugin->env->stats,
+ "# HTTP inbound sessions",
+ plugin->inbound_sessions,
+ GNUNET_NO);
+ GNUNET_assert (NULL != s);
+ goto found;
+ }
+
+create:
+/* create new session */
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Creating new session for peer `%s' \n",
+ GNUNET_i2s (&target));
+ switch (conn_info->client_addr->sa_family)
+ {
+ case (AF_INET):
+ s4 = ((struct sockaddr_in *) conn_info->client_addr);
+ a4.u4_port = s4->sin_port;
+ memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
+ a = &a4;
+ a_len = sizeof (struct IPv4HttpAddress);
+ ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s4, sizeof (struct sockaddr_in));
+ break;
+ case (AF_INET6):
+ s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
+ a6.u6_port = s6->sin6_port;
+ memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
+ a = &a6;
+ a_len = sizeof (struct IPv6HttpAddress);
+ ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s6, sizeof (struct sockaddr_in6));
+ break;
+ default:
+ GNUNET_break (0);
+ goto error;
+ }
+ s = create_session (plugin, &target, a, a_len);
+ GNUNET_assert (NULL != s);
+ s->ats_address_network_type = ats.value;
+ s->inbound = GNUNET_YES;
+ s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
+ s->tag = tag;
+ s->server_recv = NULL;
+ s->server_send = NULL;
+
+ GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head,
+ plugin->server_semi_tail, s);
+ goto found;
+
+error:
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Invalid connection request\n");
+ return NULL;
+
+found:
+ sc = GNUNET_malloc (sizeof (struct ServerConnection));
+ sc->mhd_conn = mhd_connection;
+ sc->direction = direction;
+ sc->session = s;
+ if (direction == _SEND)
+ s->server_send = sc;
+ if (direction == _RECEIVE)
+ s->server_recv = sc;
+
+#if MHD_VERSION >= 0x00090E00
+ int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
+
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+ "Server: Setting timeout for %p to %u sec.\n", sc, to);
+ MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
+
+ struct MHD_Daemon *d = NULL;
+
+ if (s->addrlen == sizeof (struct IPv6HttpAddress))
+ d = plugin->server_v6;
+ if (s->addrlen == sizeof (struct IPv4HttpAddress))
+ d = plugin->server_v4;
+
+ server_reschedule (plugin, d, GNUNET_NO);
+#endif
+ return sc;
+}
+