Exemplo n.º 1
0
OPJ_BOOL close_channel(query_param_t query_param,
                       sessionlist_param_t *sessionlist,
                       session_param_t **cursession,
                       channel_param_t **curchannel)
{
    char *cclose;
    int i;

    if (query_param.cclose[0] == '*') {
#ifndef SERVER
        fprintf(logstream, "local log: close all\n");
#endif
        /* all channels associatd with the session will be closed */
        if (!delete_session(cursession, sessionlist)) {
            return OPJ_FALSE;
        }
    } else {
        /* check if all entry belonging to the same session */

        for (i = 0, cclose = query_param.cclose; i < query_param.numOfcclose;
                i++, cclose += (strlen(cclose) + 1)) {

            /* In case of the first entry of close cid */
            if (*cursession == NULL) {
                if (!search_session_and_channel(cclose, sessionlist, cursession, curchannel)) {
                    return OPJ_FALSE;
                }
            } else /* second or more entry of close cid */
                if (!(*curchannel = search_channel(cclose, (*cursession)->channellist))) {
                    fprintf(FCGI_stdout, "Reason: Cclose id %s is from another session\r\n",
                            cclose);
                    return OPJ_FALSE;
                }
        }

        /* delete channels */
        for (i = 0, cclose = query_param.cclose; i < query_param.numOfcclose;
                i++, cclose += (strlen(cclose) + 1)) {
            *curchannel = search_channel(cclose, (*cursession)->channellist);
            delete_channel(curchannel, (*cursession)->channellist);
        }

        if ((*cursession)->channellist->first == NULL ||
                (*cursession)->channellist->last == NULL)
            /* In case of empty session */
        {
            delete_session(cursession, sessionlist);
        }
    }
    return OPJ_TRUE;
}
Exemplo n.º 2
0
void detach_process(PGW &pgw){

	if(pgw.subtype == 1){
		delete_session(pgw);
	}
	else{
		cout << "Incorrect subtype for type 3 -> " << pgw.subtype << endl;	
	}
}
void
notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
                    struct Session *s)
{
  struct Plugin *plugin = cls;

  plugin->env->session_end (plugin->env->cls, peer, s);
  GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
  delete_session (s);
}
Exemplo n.º 4
0
void process_status(const char *fcn_name, CURL *session, const CURLcode res, 
        const long response_code, const long elapsed_time, const int iter, 
        const char *path, bool tmp_session) {

    stats_counter("attempts", 1);

    if (res != CURLE_OK) {
        print_errors(iter, "curl_failures", fcn_name, res, response_code, elapsed_time, path);
        increment_node_failure(nodeaddr, res, response_code, elapsed_time);
        delete_session(session, tmp_session);
        return;
    }

    if (response_code >= 500) {
        print_errors(iter, "status500_failures", fcn_name, res, response_code, elapsed_time, path);
        increment_node_failure(nodeaddr, res, response_code, elapsed_time);
        delete_session(session, tmp_session);
        return;
    }

    if (elapsed_time > time_limit) {
        print_errors(iter, "slow_requests", fcn_name, res, response_code, elapsed_time, path);
        increment_node_failure(nodeaddr, res, response_code, elapsed_time);
        if (health_status_all_nodes() == UNHEALTHY) {
            trigger_saint_event(CLUSTER_FAILURE);
            set_dynamic_logging();
        }
        delete_session(session, tmp_session);
        return;
    }

    // If it wasn't an error, and it isn't the 0'th iter, then we must have failed previously and now recovered
    if (iter > 0) {
        stats_counter("recoveries", 1);
        log_print(LOG_NOTICE, SECTION_SESSION_DEFAULT,
            "%s: curl iter %d on path %s -- fusedav.%s.server-%s.recoveries", fcn_name, iter, path, filesystem_cluster, nodeaddr);
        increment_node_success(nodeaddr);
    }
}
/**
 * Function that can be used to force the plugin to disconnect
 * from the given peer and cancel all previous transmissions
 * (and their continuationc).
 *
 * @param cls closure
 * @param target peer from which to disconnect
 */
static void
http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
{
  struct Plugin *plugin = cls;
  struct Session *next = NULL;
  struct Session *s = plugin->head;

  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                   "Transport tells me to disconnect `%s'\n",
                   GNUNET_i2s (target));
  while (s != NULL)
  {
    next = s->next;
    if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
    {
      if (s->inbound == GNUNET_NO)
        GNUNET_assert (GNUNET_OK == client_disconnect (s));
      else
        GNUNET_assert (GNUNET_OK == server_disconnect (s));
      GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);

      struct HTTP_Message *msg = s->msg_head;
      struct HTTP_Message *tmp = NULL;

      while (msg != NULL)
      {
        tmp = msg->next;

        GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
        if (msg->transmit_cont != NULL)
        {
          msg->transmit_cont (msg->transmit_cont_cls, target, GNUNET_SYSERR);
        }
        GNUNET_free (msg);
        msg = tmp;
      }

      delete_session (s);
    }
    s = next;
  }
}
Exemplo n.º 6
0
void fa_audio_end_session(session_t session)
{
    if (!pa_mutex) {
        assert(false && "Module not initalized");
    }

    inform(string("Terminating real-time audio session"));

    fa_thread_lock(pa_mutex);
    {
        if (pa_status) {
            Pa_Terminate();
            pa_status = false;
        }

        current_session = NULL;
    }
    fa_thread_unlock(pa_mutex);
    delete_session(session);
}
Exemplo n.º 7
0
static int do_del(int argc, char **argv)
{
	struct l2tp_parm p;

	if (parse_args(argc, argv, L2TP_DEL, &p) < 0)
		return -1;

	if (!p.tunnel && !p.session)
		missarg("tunnel or session");

	if ((p.tunnel) && (p.tunnel_id == 0))
		missarg("tunnel_id");
	if ((p.session) && (p.session_id == 0))
		missarg("session_id");

	if (p.session_id)
		return delete_session(&p);
	else
		return delete_tunnel(&p);

	return -1;
}
Exemplo n.º 8
0
int process_worker_answer(worker_data *wd, message *msg, session *s)
{

    //If we are waiting a message for this session, we check how many messages we have so far.
    if (s->thread_id == msg->thread_id && s->waiting > 1){
        //We save the message we just received, and if we still haven't got them all, we return
        add_msg_to_pending(s, msg);
        s->waiting--;
        return 0;
    }
    add_msg_to_pending(s, msg);
    s->waiting--;

    message *answer = NULL;
    char *buf = NULL;
    int i;
    int status;
    int n_args;

    switch (msg->op) {
        case LSD:
            buf = malloc(MAX_ARGS_LEN * sizeof *buf);
            if (buf == NULL) {
                status = errno;
                answer = create_error_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, status);
                break;
            }
            n_args = concat_messages_args(s->pending, NWORKERS, buf, MAX_ARGS_LEN);
            answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, OK, n_args, buf);
            break;

            //If we got all the answers we were waiting for to create a file
        case EXSCRE:
            if (s->last_op == CRE) {
            //First we remove the file from the list of files being created
                remove_file_being_created(wd->lfs, msg->args + strlen("N "));
                //Now we check if someone has it
                for (i = 0; i < NWORKERS; i++) {
                    if (strncmp(s->pending[i]->args, "Y", 1) == 0) {
                        break;
                    }
                }
                //If the file already exists
                if (i < NWORKERS) {
                    answer = create_error_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, EEXIST);
                }
                else {
                    status = create_file(wd->lfs, (msg->args + strlen("N ")));
                    if (status != 0) {
                        answer = create_error_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, status);
                    } else {
                    answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, OK, 0, NULL);
                    }
                }
            }
        break;

        case EXS:
            if(s->last_op == OPN) {
                for (i = 0; i < NWORKERS; i++) {
                    if (strncmp(s->pending[i]->args, "Y", 1) == 0) {
                        break;
                    }
                }
                //If no worker returned "Y", the file doesn't exist in any of them
                if (i == NWORKERS) {
                    answer = create_error_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, ENOENT);
                }
                //If a worker has it, we ask it to open the file
                else {
                    answer = create_message(WORKER, wd->id, WORKER, s->pending[i]->origin_id, msg->thread_id, REQUEST, OPN, 1, (msg->args + strlen("Y ")));
                    //We are waiting for just one answer
                    s->waiting = 1;
                }
            }
            else if(s->last_op == DEL) {
                for (i = 0; i < NWORKERS; i++) {
                    if (strncmp(s->pending[i]->args, "Y", 1) == 0) {
                        break;
                    }
                }
                //If no worker returned "Y", the file doesn't exist in any of them
                if (i == NWORKERS) {
                    answer = create_error_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, ENOENT);
                }
                //If a worker has it, we ask it to delete the file
                else {
                    answer = create_message(WORKER, wd->id, WORKER, s->pending[i]->origin_id, msg->thread_id, REQUEST, DEL, 1, (msg->args + strlen("Y ")));
                    //We are waiting for just one answer
                    s->waiting = 1;
                }
            }
            break;

        case DEL:
            if (strncmp(msg->args, "OK", strlen("OK")) != 0) {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, 2, msg->args);
            }
            else {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, OK, 0, NULL);
            }
            break;

        case OPN:
            if (strncmp(msg->args, "OK", strlen("OK")) != 0) {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, 2, msg->args);
            }
            else {
                add_fd_to_session(s, (msg->args + strlen("OK ")), msg->origin_id, wd->next_fd);
                buf = malloc((MAX_FD_LENGTH + strlen("FD ") + 1) * sizeof *buf);
                if (buf == NULL) {
                    status = errno;
                    answer = create_error_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, status);
                    break;
                }
                sprintf(buf,"FD %d", wd->next_fd);
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, OK, 2, buf);
                wd->next_fd++;
            }
            break;

        case WRT:
            if (strncmp(msg->args, "OK", strlen("OK")) != 0) {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, 2, msg->args);
            }
            else {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, OK, 0, NULL);
            }
            break;

        case REA:
            if (strncmp(msg->args, "OK", strlen("OK")) != 0) {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, 2, msg->args);
                break;
            }
            else {
                char *args = msg->args + strlen("OK ");
                //buf will have "SIZE read_size read_characters\0"
                buf = malloc((strlen("SIZE ") + strlen(args) + 1) * sizeof *buf);
                if (buf == NULL) {
                    status = errno;
                    answer = create_error_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, status);
                    break;
                }
                sprintf(buf, "SIZE %s", args);

                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, OK, 3, buf);
            }
            break;

        case CLO:
            if (strncmp(msg->args, "OK ", strlen("OK ")) != 0) {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, 2, msg->args);
            }
            else {
                char *filename = msg->args + strlen("OK ");
                delete_fd_from_session(s, filename);
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, OK, 0, NULL);
            }

            break;

         case BYE:
            for (i = 0; i < NWORKERS; i++) {
                if (strncmp(s->pending[i]->args, "OK", strlen("OK")) != 0) {
                    break;
                }
            }
            //If one of the workers returned an error
            if (i < NWORKERS) {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, ERROR, 2, s->pending[i]->args);
            }
            else {
                answer = create_message(WORKER, wd->id, THREAD, msg->thread_id, msg->thread_id, ANSWER, OK, 0, NULL);
                delete_session(wd, msg->thread_id);
                send_message(answer);
                delete_message(answer);
                if (buf != NULL) {
                    free(buf);
                }
                return 0;

            }

            break;

        default:
            break;
    }

    clear_session_last_operation(s);
    send_message(answer);
    delete_message(answer);
    if (buf != NULL) {
        free(buf);
    }

    return 0;

}
Exemplo n.º 9
0
static int callback_nyx_websockets(struct libwebsocket_context *context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len)
{
  struct per_session_data_nyx *pss =(struct per_session_data_nyx*)user;
  int n;

  node *wsd_state = (node*)libwebsocket_context_user(context);
  //node *daemon = (node*)libwebsocket_context_user(context);
  //node *wsd_state = node_GetNode(get_value(daemon));
  node *found_prot = NULL;
  node *state = NULL;
  node *block = NULL;
  //node *daemon = NULL;
  node *daemon_obj = NULL;
  node *session_uid = NULL;
  long lsession_uid = 0;
  node *sessions_num = NULL;
  node *sessions = NULL;
  long lsessions_num = 0;
  
  if(wsd_state)
  {
    state = node_GetItem(wsd_state,0);
    block = node_GetItem(wsd_state,1);
    //daemon = node_GetItem(wsd_state,2);
    node *protocols = node_GetItem(wsd_state,3);
    session_uid = node_GetItem(wsd_state,4);
    node *session_uid_value = node_GetItemByKey(session_uid,"value");
    lsession_uid = node_GetSint32(session_uid_value);
    sessions_num = node_GetItem(wsd_state,5);
    sessions = node_GetItem(wsd_state,6);
    node *sessions_num_value = node_GetItemByKey(sessions_num,"value");
    lsessions_num = node_GetSint32(sessions_num_value);
    daemon_obj = node_GetItem(wsd_state,9);
    if(wsi)
    {
      node *protocols_items = node_GetItemByKey(protocols,"items");
      const struct libwebsocket_protocols *prot = libwebsockets_get_protocol(wsi);
      if(prot && prot->name)
      {
        node_ItemIterationReset(protocols_items);
        while(node_ItemIterationUnfinished(protocols_items))
        {
          node *proto = node_ItemIterate(protocols_items);
          if(!strcmp(get_obj_name(proto),prot->name))
          {
            found_prot = proto;
          }
        }
      }
    }
  }

  switch(reason) 
  {

    //case LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED:
    case LWS_CALLBACK_CLIENT_ESTABLISHED:
      printf("new session created:%d, num:%d\n",lsession_uid,lsessions_num);
      pss->session = NULL;
      break;

    case LWS_CALLBACK_HTTP:
      if(len < 1)
      {
        libwebsockets_return_http_status(context,wsi,HTTP_STATUS_BAD_REQUEST,NULL);
        return(-1);
      }
      if(lws_hdr_total_length(wsi,WSI_TOKEN_POST_URI))
        return(0);

      if(found_prot)
      {
        //printf("found prot in http callback : uid:%d,num:%d (sess:%x)\n",lsession_uid+1,lsessions_num,pss->session);
        if(!pss->session)
        {
          lsession_uid++;
          node *session_uid_value = node_GetItemByKey(session_uid,"value");
          node_SetSint32(session_uid_value,lsession_uid);
          lsessions_num++;
          node *sessions_num_value = node_GetItemByKey(sessions_num,"value");
          node_SetSint32(sessions_num_value,lsessions_num);
          pss->session = create_session(state,sessions,lsession_uid,get_obj_name(found_prot));
          node *session_privates = node_GetItemByKey(pss->session,"privates");
          set_obj_int(session_privates,"is_http",1);
          //printf("created new session :%d actual sessions num:%d\n",lsession_uid,lsessions_num);
        }
        

        node *parameters = create_obj("parameters");
        node *base_class = get_base_class(state);
        node *prot_value = create_class_instance(base_class);
        set_obj_string(prot_value,"name","protocol");
        set_obj_string(prot_value,"value",get_obj_name(found_prot));
        node_AddItem(parameters,prot_value);
        inc_obj_refcount(prot_value);
        char *url = str_CreateEmpty();
        url = str_AddChars(url,in,len);
        node *url_value = create_class_instance(base_class);
        set_obj_string(url_value,"name","url");
        set_obj_string(url_value,"value",url);
        node_AddItem(parameters,url_value);
        inc_obj_refcount(url_value);
        free(url);
        node_AddItem(parameters,pss->session);
        inc_obj_refcount(pss->session);
        //node_AddItem(parameters,daemon_obj);
        node_AddItem(parameters,sessions);
        inc_obj_refcount(sessions);
        node *tmp_parent = node_GetParent(found_prot);
        node *bmembers = node_GetItemByKey(block,"members");
        node_SetParent(found_prot,bmembers);
        node *ret_obj = execute_obj(state,found_prot,block,parameters,True,False);//,True);resolve
        node_SetParent(found_prot,tmp_parent);
        //dec_obj_refcount(msg_value);
        dec_obj_refcount(prot_value);
        //add_garbage(state,msg_value);//TODO check if "just survives"
        add_garbage(state,prot_value);
        dec_obj_refcount(url_value);
        add_garbage(state,url_value);
        dec_obj_refcount(pss->session);
        dec_obj_refcount(sessions);
 
        node *ret_obj_value = node_GetItemByKey(ret_obj,"value");
        if( (node_GetType(ret_obj_value)==NODE_TYPE_STRING && strlen(node_GetString(ret_obj_value))) || (node_GetType(ret_obj_value)==NODE_TYPE_BINARY && node_GetBinaryLength(ret_obj_value)) )
        {
          //printf("returning http message: [%s] :%d\n",node_GetString(ret_obj_value),strlen(node_GetString(ret_obj_value)));
          //node *ret_obj_copy = node_CopyTree(ret_obj,True,True);
          node *ret_obj_copy = copy_class(ret_obj);
          //reset_obj_refcount(ret_obj_copy);
          set_obj_string(ret_obj_copy,"name","message");
          add_member(pss->session,ret_obj_copy);
          inc_obj_refcount(ret_obj_copy);
        }
        libwebsocket_callback_on_writable(context, wsi);
      }
      break;

    case LWS_CALLBACK_HTTP_BODY_COMPLETION:
      if(found_prot)
      {
        printf("found prot in http body complete : %d,num:%d\n",lsession_uid,lsessions_num);
        if(daemon_obj)
        {
          printf("body: found daemon_obj\n");
        }
      }
      else
        printf("body closed: prot not found\n");
      //lwsl_notice("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
      libwebsockets_return_http_status(context,wsi,HTTP_STATUS_OK,NULL);
     return(-1);

    case LWS_CALLBACK_HTTP_FILE_COMPLETION:
      if(found_prot)
     {
        //printf("found prot in http file complete : %d,num:%d\n",lsession_uid,lsessions_num);
        lsessions_num--;
        node *sessions_num_value = node_GetItemByKey(sessions_num,"value");
        node_SetSint32(sessions_num_value,lsessions_num);
        delete_session(state,sessions,pss->session);
        pss->session = NULL;
        if(daemon_obj)
        {
          printf("http: found daemon_obj\n");
        }
      }
      else
        printf("file closed: prot not found\n");
      return(-1);

    case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
      if(found_prot)
      {
      int n;
      static const char *token_names[] = 
      {
        /*[WSI_TOKEN_GET_URI]   =*/ "GET URI",
        /*[WSI_TOKEN_POST_URI]    =*/ "POST URI",
        /*[WSI_TOKEN_OPTIONS]    =*/ "Options",
        /*[WSI_TOKEN_HOST]    =*/ "Host",
        /*[WSI_TOKEN_CONNECTION]  =*/ "Connection",
        /*[WSI_TOKEN_KEY1]    =*/ "key 1",
        /*[WSI_TOKEN_KEY2]    =*/ "key 2",
        /*[WSI_TOKEN_PROTOCOL]    =*/ "Protocol",
        /*[WSI_TOKEN_UPGRADE]   =*/ "Upgrade",
        /*[WSI_TOKEN_ORIGIN]    =*/ "Origin",
        /*[WSI_TOKEN_DRAFT]   =*/ "Draft",
        /*[WSI_TOKEN_CHALLENGE]   =*/ "Challenge",
        /* new for 04 */
        /*[WSI_TOKEN_KEY]   =*/ "Key",
        /*[WSI_TOKEN_VERSION]   =*/ "Version",
        /*[WSI_TOKEN_SWORIGIN]    =*/ "Sworigin",
        /* new for 05 */
        /*[WSI_TOKEN_EXTENSIONS]  =*/ "Extensions",
        /* client receives these */
        /*[WSI_TOKEN_ACCEPT]    =*/ "Accept",
        /*[WSI_TOKEN_NONCE]   =*/ "Nonce",
        /*[WSI_TOKEN_HTTP]    =*/ "Http",
        "Accept:",
        "Accept_Request_Headers:",
        "If-None-Match:",
        "If-Modified-Since:",
        "Accept-Encoding:",
        "Accept-Language:",
        "Pragma:",
        "Cache-Control:",
        "Authorization:",
        "Cookie:",
        "Content-Length:",
        "Content-Type:",
        "Date:",
        "Range:",
        "Referer:",
        "Uri-Args:",
        /*[WSI_TOKEN_MUXURL]  =*/ "MuxURL",
      };

      //printf("found prot in http filter callback : uid:%d,num:%d (sess:%x)\n",lsession_uid+1,lsessions_num,pss->session);
      if(!pss->session)
      {
        lsession_uid++;
        node *session_uid_value = node_GetItemByKey(session_uid,"value");
        node_SetSint32(session_uid_value,lsession_uid);
        lsessions_num++;
        node *sessions_num_value = node_GetItemByKey(sessions_num,"value");
        node_SetSint32(sessions_num_value,lsessions_num);
        pss->session = create_session(state,sessions,lsession_uid,get_obj_name(found_prot));
        //node *session_privates = node_GetItemByKey(pss->session,"privates");
        //set_obj_int(session_privates,"is_http",1);
      }
      //printf("filter sess:%x\n",pss->session);


      for(n=0;n<(int)(sizeof(token_names)/sizeof(token_names[0]));n++) 
      {
        if (!lws_hdr_total_length(wsi, n))
          continue;
        char *cookies = (char*)malloc(512);
        memset(cookies,0,512);
        lws_hdr_copy(wsi,cookies,511,n);
        //printf("header:%s = [%s]\n",token_names[n],cookies);
        //fflush(stdout);
        if(pss->session && !strcmp("Cookie:",token_names[n]))
        {
          //printf("cookie found:%s = [%s]\n",token_names[n],cookies);
          //fflush(stdout);
          node *base_class = get_base_class(state);
          node *cookie_value = create_class_instance(base_class);
          set_obj_string(cookie_value,"name","cookie");
          set_obj_string(cookie_value,"value",cookies);
          add_member(pss->session,cookie_value);
          inc_obj_refcount(cookie_value);
        }
        free(cookies);
      }
      }
    break;

    case LWS_CALLBACK_HTTP_WRITEABLE: 
    case LWS_CALLBACK_SERVER_WRITEABLE:
      {
        //node_PrintTree(pss->session);
        node *message = get_member(pss->session,"message");
        node *session_privates = node_GetItemByKey(pss->session,"privates");
        node *http_only = node_GetItemByKey(session_privates,"is_http");

        while(message)
        {
          //node *session_id = get_member(pss->session,"id");
          //node *session_id_value = node_GetItemByKey(session_id,"value");
          node *message_value = node_GetItemByKey(message,"value");
          unsigned char *me = NULL;
          unsigned long me_len = 0;
          if(node_GetType(message_value)==NODE_TYPE_STRING)
          {
            me = (unsigned char*)node_GetString(message_value);
            me_len = strlen((char*)me);
          }
          else if(node_GetType(message_value)==NODE_TYPE_BINARY)
          {
            me = (unsigned char*)node_GetBinary(message_value);
            me_len = node_GetBinaryLength(message_value);
          }
          //printf("sending message now: [%s] to: %d\n",me,node_GetSint32(session_id_value));
          //fflush(stdout);
          unsigned char *buf = (unsigned char*)malloc(LWS_SEND_BUFFER_PRE_PADDING + me_len + LWS_SEND_BUFFER_POST_PADDING);
          memcpy(buf+LWS_SEND_BUFFER_PRE_PADDING,me,me_len);

          if(http_only)
            //n = libwebsocket_write(wsi, me, me_len, LWS_WRITE_HTTP);
            n = libwebsocket_write(wsi,buf+LWS_SEND_BUFFER_PRE_PADDING,me_len,LWS_WRITE_HTTP);
          else
            //n = libwebsocket_write(wsi, me, me_len, LWS_WRITE_TEXT);
            n = libwebsocket_write(wsi,buf+LWS_SEND_BUFFER_PRE_PADDING,me_len,LWS_WRITE_TEXT);
          free(buf);
          if(n<0)
          {
            printf("ERROR %d writing to socket, hanging up\n", n);
            return(1);
          }
          if(n<(long)me_len)
          {
            printf("Partial write\n");
            return(-1);
          }
          //node_FreeTree(pss->message);
          remove_member(pss->session,message);
          dec_obj_refcount(message);
          //printf("removing message from queue:%x (%d)\n",message,get_obj_refcount(message));
          add_garbage(state,message);
          message = get_member(pss->session,"message");          
        }
        if(http_only)
        {
          //if(lws_http_transaction_completed(wsi))
          //{
            //printf("removing http session num:%d\n",lsessions_num);
            lsessions_num--;
            node *sessions_num_value = node_GetItemByKey(sessions_num,"value");
            node_SetSint32(sessions_num_value,lsessions_num);
            delete_session(state,sessions,pss->session);
            pss->session = NULL;
            //printf("removed http\n");
              return -1;
            //return(-1);
          //}
          //else
          //  libwebsocket_callback_on_writable(context, wsi);
        }
      }
      break;

    case LWS_CALLBACK_ESTABLISHED:
      if(found_prot)
      {
        //printf("found prot in establish callback : uid:%d,num:%d (sess:%x)\n",lsession_uid+1,lsessions_num,pss->session);
        if(!pss->session)
        {
          lsession_uid++;
          node *session_uid_value = node_GetItemByKey(session_uid,"value");
          node_SetSint32(session_uid_value,lsession_uid);
          lsessions_num++;
          node *sessions_num_value = node_GetItemByKey(sessions_num,"value");
          node_SetSint32(sessions_num_value,lsessions_num);
          pss->session = create_session(state,sessions,lsession_uid,get_obj_name(found_prot));
        }
        if(daemon_obj)
        {
          node *connect_handler = get_member(daemon_obj,"connect_handler");
          if(connect_handler)
          {
            connect_handler = resolve_object(state,connect_handler);
            node *parameters = create_obj("parameters");
            node *base_class = get_base_class(state);
      
            node *prot_value = create_class_instance(base_class);
            set_obj_string(prot_value,"name","protocol");
            set_obj_string(prot_value,"value",get_obj_name(found_prot));
            node_AddItem(parameters,prot_value);
            inc_obj_refcount(prot_value);

            node_AddItem(parameters,pss->session);
            inc_obj_refcount(pss->session);
            node_AddItem(parameters,sessions);
            inc_obj_refcount(sessions);
            node *tmp_parent = node_GetParent(connect_handler);
            node *bmembers = node_GetItemByKey(block,"members");
            node_SetParent(connect_handler,bmembers);
            node *ret_obj = execute_obj(state,connect_handler,block,parameters,True,False);//,True);resolve
            node_SetParent(connect_handler,tmp_parent);
            dec_obj_refcount(prot_value);
            add_garbage(state,prot_value);
            dec_obj_refcount(pss->session);
            dec_obj_refcount(sessions);
            node *ret_obj_value = node_GetItemByKey(ret_obj,"value");
            if(node_GetType(ret_obj_value)==NODE_TYPE_STRING && strlen(node_GetString(ret_obj_value)))
            {
            }
          }
        }
      }
      break;

    case LWS_CALLBACK_CLOSED_HTTP:
    break;
    
    case LWS_CALLBACK_CLOSED:
    case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
      if(found_prot)
      {
        //printf("found prot in closed callback : uid:%d,num:%d (sess:%x)\n",lsession_uid,lsessions_num,pss->session);
        if(daemon_obj)
        {
        //printf("closed: found daemon_obj\n");
          node *disconnect_handler = get_member(daemon_obj,"disconnect_handler");
          if(disconnect_handler)
          {
            //printf("disc found\n");
            disconnect_handler = resolve_object(state,disconnect_handler);
            node *parameters = create_obj("parameters");
            node *base_class = get_base_class(state);
      
            node *prot_value = create_class_instance(base_class);
            set_obj_string(prot_value,"name","protocol");
            set_obj_string(prot_value,"value",get_obj_name(found_prot));
            node_AddItem(parameters,prot_value);
            inc_obj_refcount(prot_value);

            node_AddItem(parameters,pss->session);
            inc_obj_refcount(pss->session);
            node_AddItem(parameters,sessions);
            inc_obj_refcount(sessions);
            node *tmp_parent = node_GetParent(disconnect_handler);
            node *bmembers = node_GetItemByKey(block,"members");
            node_SetParent(disconnect_handler,bmembers);
            node *ret_obj = execute_obj(state,disconnect_handler,block,parameters,True,False);//,True);resolve
            node_SetParent(disconnect_handler,tmp_parent);
            dec_obj_refcount(prot_value);
            add_garbage(state,prot_value);
            dec_obj_refcount(pss->session);
            dec_obj_refcount(sessions);
            node *ret_obj_value = node_GetItemByKey(ret_obj,"value");
            if(node_GetType(ret_obj_value)==NODE_TYPE_STRING && strlen(node_GetString(ret_obj_value)))
            {
            }
          }
        }
        lsessions_num--;
        node *sessions_num_value = node_GetItemByKey(sessions_num,"value");
        node_SetSint32(sessions_num_value,lsessions_num);
        delete_session(state,sessions,pss->session);
        pss->session = NULL;
        //printf("disconnected\n");
      }
      else 
      {
        printf("closed connection without prot found\n");
        if(pss->session)
          printf("but a session was found\n");
      }
      break;

    case LWS_CALLBACK_RECEIVE:
      if(len>1024) 
      { //TODO use some variable
      lwsl_err("Server received packet bigger than %u, hanging up\n", 1024);
      return(1);
      }
      if(found_prot)
      {
      node *parameters = create_obj("parameters");
      node *base_class = get_base_class(state);
      
      node *prot_value = create_class_instance(base_class);
      set_obj_string(prot_value,"name","protocol");
      set_obj_string(prot_value,"value",get_obj_name(found_prot));
      node_AddItem(parameters,prot_value);
      inc_obj_refcount(prot_value);

      char *msg = str_CreateEmpty();
      msg = str_AddChars(msg,in,len);
      node *msg_value = create_class_instance(base_class);
      set_obj_string(msg_value,"name","message");
      set_obj_string(msg_value,"value",msg);
      node_AddItem(parameters,msg_value);
      inc_obj_refcount(msg_value);
      free(msg);

      /*node *session_value = create_class_instance(base_class);
      reset_obj_refcount(session_value);
      add_garbage(state,session_value);
      set_obj_string(session_value,"name","session_id");
      set_obj_int(session_value,"value",lsession_uid);
      set_obj_int(session_value,"item_index",2);
      node_AddItem(parameters,session_value);
      */
      node_AddItem(parameters,pss->session);
      inc_obj_refcount(pss->session);
      //node_AddItem(parameters,daemon_obj);
      //inc_obj_refcount(daemon_obj);

      node_AddItem(parameters,sessions);
      inc_obj_refcount(sessions);

      //printf("recv callback\n");
      //fflush(stdout);
      node *tmp_parent = node_GetParent(found_prot);
      node *bmembers = node_GetItemByKey(block,"members");
      node_SetParent(found_prot,bmembers);

      node *ret_obj = execute_obj(state,found_prot,block,parameters,True,False);//,True);resolve
      node_SetParent(found_prot,tmp_parent);
      //printf("recv callback finished\n");
      //fflush(stdout);

      dec_obj_refcount(msg_value);
      dec_obj_refcount(prot_value);
      add_garbage(state,msg_value);//TODO check if "just survives"
      add_garbage(state,prot_value);

      dec_obj_refcount(pss->session);
      dec_obj_refcount(sessions);
      //dec_obj_refcount(daemon_obj);
      //printf("recv gc\n");
      //fflush(stdout);
      //node *ret_obj_value = node_GetItemByKey(ret_obj,"value");
      //char *me = node_GetString(ret_obj_value);
      //printf("returned string:[%s]\n",me);
      node *ret_obj_value = node_GetItemByKey(ret_obj,"value");
      if(node_GetType(ret_obj_value)==NODE_TYPE_STRING && strlen(node_GetString(ret_obj_value)))
      {
        //printf("returning message: [%s] :%d\n",node_GetString(ret_obj_value),strlen(node_GetString(ret_obj_value)));
        //node *ret_obj_copy = node_CopyTree(ret_obj,True,True);
        node *ret_obj_copy = copy_class(ret_obj);
        //reset_obj_refcount(ret_obj_copy);
        set_obj_string(ret_obj_copy,"name","message");
        add_member(pss->session,ret_obj_copy);
        inc_obj_refcount(ret_obj_copy);
        //set_obj_string(ret_obj,"name","message");
        //add_member(pss->session,ret_obj);
        //inc_obj_refcount(ret_obj);
      }
      libwebsocket_callback_on_writable(context, wsi);
      }
      break;
    default:
      break;
  }

  return(0);
}
/**
 * Creates a new outbound session the transport service will use to send data to the
 * peer
 *
 * @param cls the plugin
 * @param address the address
 * @return the session or NULL of max connections exceeded
 */
static struct Session *
http_get_session (void *cls,
                  const struct GNUNET_HELLO_Address *address)
{
  struct Plugin *plugin = cls;
  struct Session * s = NULL;
  struct GNUNET_ATS_Information ats;
  size_t addrlen;

  GNUNET_assert (plugin != NULL);
  GNUNET_assert (address != NULL);
  GNUNET_assert (address->address != NULL);

  ats.type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
  ats.value = htonl (GNUNET_ATS_ARRAY_TERMINATOR);

  /* find existing session */
  s = lookup_session (plugin, address);
  if (s != NULL)
    return s;

  if (plugin->max_connections <= plugin->cur_connections)
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
                     "Maximum number of connections reached, "
                     "cannot connect to peer `%s'\n", GNUNET_i2s (&address->peer));
    return NULL;
  }

  /* create new session */
  addrlen = address->address_length;

  GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
                 (addrlen == sizeof (struct IPv4HttpAddress)));

  s = create_session (plugin, &address->peer, address->address, address->address_length);

  /* Get ATS type */
  if (addrlen == sizeof (struct IPv4HttpAddress))
  {
    struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) address->address;
    struct sockaddr_in s4;

    s4.sin_family = AF_INET;
    s4.sin_addr.s_addr = a4->ipv4_addr;
    s4.sin_port = a4->u4_port;
#if HAVE_SOCKADDR_IN_SIN_LEN
    s4.sin_len = sizeof (struct sockaddr_in);
#endif
    ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s4, sizeof (struct sockaddr_in));
  }
  if (addrlen == sizeof (struct IPv6HttpAddress))
  {
    struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) address->address;
    struct sockaddr_in6 s6;

    s6.sin6_family = AF_INET6;
    s6.sin6_addr = a6->ipv6_addr;
    s6.sin6_port = a6->u6_port;
#if HAVE_SOCKADDR_IN_SIN_LEN
    s6.sin6_len = sizeof (struct sockaddr_in6);
#endif
    ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
  }
  s->ats_address_network_type = ats.value;

  /* add new session */
  GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
  /* initiate new connection */
  if (GNUNET_SYSERR == client_connect (s))
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
                     "Cannot connect to peer `%s' address `%s''\n",
                     http_plugin_address_to_string(NULL, s->addr, s->addrlen),
                     GNUNET_i2s (&s->target));
    GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
    delete_session (s);
    return NULL;
  }

  return s;
}
/**
 * Exit point from the plugin.
 */
void *
LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
{
  struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
  struct Plugin *plugin = api->cls;
  struct Session *s;

  if (NULL == plugin)
  {
    GNUNET_free (api);
    return NULL;
  }

  /* Stop reporting addresses to transport service */
  stop_report_addresses (plugin);

  /* cleaning up sessions */
  s = plugin->head;
  while (s != NULL)
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                     "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
    if (s->inbound == GNUNET_NO)
      GNUNET_assert (GNUNET_OK == client_disconnect (s));
    else
      GNUNET_assert (GNUNET_OK == server_disconnect (s));
    s = s->next;
  }

  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
  /* Stop server */
  server_stop (plugin);

  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
  /* Stop client */
  client_stop (plugin);

  /* deleting up sessions */
  s = plugin->head;
  while (s != NULL)
  {
    struct Session *t = s->next;

    GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);

    struct HTTP_Message *msg = s->msg_head;
    struct HTTP_Message *tmp = NULL;

    while (msg != NULL)
    {
      tmp = msg->next;

      GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
      if (msg->transmit_cont != NULL)
      {
        msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
      }
      GNUNET_free (msg);
      msg = tmp;
    }

    delete_session (s);
    s = t;
  }

  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                   "Plugin `%s' unloaded\n", plugin->name);
  GNUNET_free_non_null (plugin->server_addr_v4);
  GNUNET_free_non_null (plugin->server_addr_v6);
  GNUNET_free (plugin);
  GNUNET_free (api);
  return NULL;
}