+ GNUNET_free(force);
+
+ pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
+ /* Peer unknown */
+ if (pc==NULL)
+ {
+ pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
+ pc->plugin = plugin;
+ pc->session_id_counter=1;
+ memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
+ GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+ }
+
+ /* Search for existing session using the passed address */
+ if ((addr!=NULL) && (addrlen != 0))
+ {
+ ps = get_Session(plugin, pc, addr, addrlen);
+ }
+ if (ps != NULL)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Found existing connection to peer %s with given address, using %X\n", GNUNET_i2s(target), ps);
+
+ /* Search for existing session using the passed session */
+ if ((ps==NULL) && (force_address != GNUNET_YES))
+ {
+ ps_tmp = pc->head;
+ while (ps_tmp!=NULL)
+ {
+ if ((ps_tmp==session) && (ps_tmp->recv_force_disconnect==GNUNET_NO) && (ps_tmp->send_force_disconnect==GNUNET_NO) &&
+ (ps_tmp->recv_connected==GNUNET_YES) && (ps_tmp->send_connected==GNUNET_YES))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Found existing connection to peer %s with given session, using inbound session %X\n", GNUNET_i2s(target), ps_tmp);
+ ps = ps_tmp;
+ break;
+ }
+ ps_tmp=ps_tmp->next;
+ }
+ }
+
+ /* session not existing, address not forced -> looking for other session */
+ if ((ps==NULL) && (force_address != GNUNET_YES))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection, but free to choose existing, searching for existing connection to peer %s\n", GNUNET_i2s(target));
+ /* Choosing different session to peer when possible */
+ struct Session * tmp = pc->head;
+ while (tmp!=NULL)
+ {
+ if ((tmp->recv_connected) && (tmp->send_connected) && (tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
+ {
+ ps = tmp;
+ }
+ tmp = tmp->next;
+ }
+ if (ps != NULL)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection to peer %s, selected connection %X\n", GNUNET_i2s(target),ps);
+ else
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection to peer %s, no connection found\n", GNUNET_i2s(target));
+ }
+
+ /* session not existing, but address forced -> creating new session */
+ if ((ps==NULL) || ((ps==NULL) && (force_address == GNUNET_YES)))
+ {
+ if ((addr!=NULL) && (addrlen!=0))
+ {
+ if (force_address == GNUNET_YES)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new connection to peer %s\n", GNUNET_i2s(target));
+ if (force_address != GNUNET_YES)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new connection to peer %s\n", GNUNET_i2s(target));
+
+ ps = GNUNET_malloc(sizeof (struct Session));
+ if ((addrlen!=0) && (addr!=NULL))
+ {
+ ps->addr = GNUNET_malloc(addrlen);
+ memcpy(ps->addr,addr,addrlen);
+ ps->addrlen = addrlen;
+ }
+ else
+ {
+ ps->addr = NULL;
+ ps->addrlen = 0;
+ }
+ ps->direction=OUTBOUND;
+ ps->recv_connected = GNUNET_NO;
+ ps->recv_force_disconnect = GNUNET_NO;
+ ps->send_connected = GNUNET_NO;
+ ps->send_force_disconnect = GNUNET_NO;
+ ps->pending_msgs_head = NULL;
+ ps->pending_msgs_tail = NULL;
+ ps->peercontext=pc;
+ ps->session_id = pc->session_id_counter;
+ pc->session_id_counter++;
+ ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
+ if (ps->msgtok == NULL)
+ ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
+ GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing session & and no address given: no way to send this message to peer `%s'!\n", GNUNET_i2s(target));
+ return -1;
+ }
+ }