void init()
{
    mode_t old_umask;
    /* FIXME: This 0 is remote domain ID */
    ctrl_vchan = libvchan_server_init(0, VCHAN_BASE_PORT, 4096, 4096);
    if (!ctrl_vchan)
        handle_vchan_error("server_init");
    if (handle_handshake(ctrl_vchan) < 0)
        exit(1);
    old_umask = umask(0);
    trigger_fd = get_server_socket(QREXEC_AGENT_TRIGGER_PATH);
    umask(old_umask);
    register_exec_func(do_exec);

    /* wait for qrexec daemon */
    while (!libvchan_is_open(ctrl_vchan))
        libvchan_wait(ctrl_vchan);
}
示例#2
0
	void response_base< Timer >::handle_request( const json::object& request,
	    const boost::shared_ptr< response_interface >& self, const std::string& connection_name, bool last_message )
	{
		const json::string channel = extract_channel( request );

		if ( channel == meta_handshake_channel )
		{
			handle_handshake( request, connection_name );
			return;
		}

		const json::string client_id = check_client_id( request, channel );
		if ( client_id.empty() )
		    return;

		if ( !check_session( request, client_id, channel ) )
		    return;

        if ( channel == meta_connect_channel )
        {
            handle_connect( request, self, last_message );
        }
        else if ( channel == meta_disconnect_channel )
        {
            handle_disconnect( request );
        }
        else if ( channel == meta_subscribe_channel )
        {
            handle_subscribe( request );
        }
        else if ( channel == meta_unsubscribe_channel )
        {
            handle_unsubscribe( request );
        }
        else
        {
            handle_publish( channel, request );
        }
	}
示例#3
0
文件: miro_client.cpp 项目: karhu/sol
void ClientConnection::connection_handler(networking::error_ref e)
{
   if (check_error(e)) {
       handle_handshake();
   }
}
示例#4
0
文件: client.c 项目: schoentoon/mtop
int process_line(struct client* client, char* line, size_t len) {
  DEBUG(255, "Raw line: %s", line);
  char buf[65];
  double double_number;
  int number;
  struct timeval tv = { 0, 0 };
  if (strcmp(line, "MODULES") == 0)
    send_loaded_modules_info(client);
  else if (sscanf(line, "ENABLE %64s", buf) == 1) {
    struct module* module = get_module(buf);
    if (module) {
      struct enabled_mod* em = malloc(sizeof(struct enabled_mod));
      em->module = module;
      em->id = 0;
      em->next = NULL;
      if (!client->mods) {
        client->mods = em;
        if (em->module->array_length == 1)
          client_send_data(client, "LOADED: %s %s ID %d", module_type_to_string(em->module->type), buf, em->id);
        else
          client_send_data(client, "LOADED: %s %s ID %d WITH %d ITEMS", module_type_to_string(em->module->type), buf, em->id, em->module->array_length);
      } else {
        em->id++;
        struct enabled_mod* lm = client->mods;
        if (lm->module == em->module) {
          client_send_data(client, "ERROR: ALREADY LOADED %s WITH ID %d", em->module->name, em->id);
          free(em);
        } else {
          while (lm) {
            if (lm->module == em->module) {
              client_send_data(client, "ERROR: ALREADY LOADED %s WITH ID %d", em->module->name, em->id);
              free(em);
              return 0;
            }
            em->id = lm->id + 1;
            if (!lm->next)
              break;
            lm = lm->next;
          };
          lm->next = em;
          if (em->module->array_length == 1)
          client_send_data(client, "LOADED: %s %s ID %d", module_type_to_string(em->module->type), buf, em->id);
        else
          client_send_data(client, "LOADED: %s %s ID %d WITH %d ITEMS", module_type_to_string(em->module->type), buf, em->id, em->module->array_length);
        }
      }
    } else
      client_send_data(client, "ERROR: UNABLE TO LOAD %s", buf);
  } else if (sscanf(line, "DISABLE %64s", buf) == 1) {
    struct module* module = get_module(buf);
    if (module) {
      struct enabled_mod* lm = client->mods;
      if (lm->module == module) {
        client->mods = lm->next;
        client_send_data(client, "DISABLED: %s WITH ID %d", buf, lm->id);
        free(lm);
      } else {
        while (lm->next) {
          if (lm->next->module == module) {
            struct enabled_mod* to_free = lm->next;
            lm->next = lm->next->next;
            client_send_data(client, "DISABLED: %s WITH ID %d", buf, to_free->id);
            free(to_free);
            break;
          }
          lm = lm->next;
        };
      }
    } else
      client_send_data(client, "ERROR: MODULE %s DOESN'T EXIST", buf);
  } else if (sscanf(line, "PULL %64s", buf) == 1) {
    struct module* module = get_module(buf);
    if (module) {
      char databuf[BUFSIZ];
      if (update_value(module, databuf, sizeof(databuf), client))
        client_send_data(client, "%s: %s", module->name, databuf);
    };
  } else if (sscanf(line, "ITEMNAMES %64s", buf) == 1) {
    struct module* module = get_module(buf);
    if (module) {
      char databuf[BUFSIZ];
      if (get_module_item_names(module, databuf, sizeof(databuf)))
        client_send_data(client, "%s: %s", module->name, databuf);
    };
  } else if (sscanf(line, "INTERVAL %lf", &double_number) == 1) {
    if (client->timer)
      event_free(client->timer);
    if (double_number < 0) {
      client->timer = NULL;
      return 0;
    }
    tv.tv_sec = (long) double_number;
    tv.tv_usec = (long) ((double_number - (double) tv.tv_sec) * 1000000.0) ;
    client->timer = event_new(bufferevent_get_base(client->bev), -1, EV_PERSIST, client_timer, client);
    event_add(client->timer, &tv);
  } else if (sscanf(line, "PRECISION %d", &number) == 1) {
    if (number >= 0 && number <= 255)
      client->precision = number;
    else
      client_send_data(client, "ERROR: Precision %d is out of the valid range (0-255)", number);
  } else if ((!client->websocket || !client->websocket->connected) && handle_handshake(client, line, len))
    return 0;
  else {
    switch (++client->unknown_command) {
    case 1:
      client_send_data(client, "ERROR: This is not a valid command...");
      return 0;
    case 2:
      client_send_data(client, "ERROR: I'm warning you, stop that.");
      return 0;
    case 3:
      client_eventcb(client->bev, BEV_ERROR, client);
      return 1;
    }
  }
  client->unknown_command = 0;
  return 0;
};
示例#5
0
/* Run the TCP connection
 */
void do_TCP_connection(TCP_Client_Connection *TCP_connection, void *userdata)
{
    unix_time_update();

    if (TCP_connection->status == TCP_CLIENT_DISCONNECTED) {
        return;
    }

    if (TCP_connection->status == TCP_CLIENT_PROXY_HTTP_CONNECTING) {
        if (send_pending_data(TCP_connection) == 0) {
            int ret = proxy_http_read_connection_response(TCP_connection);

            if (ret == -1) {
                TCP_connection->kill_at = 0;
                TCP_connection->status = TCP_CLIENT_DISCONNECTED;
            }

            if (ret == 1) {
                generate_handshake(TCP_connection);
                TCP_connection->status = TCP_CLIENT_CONNECTING;
            }
        }
    }

    if (TCP_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) {
        if (send_pending_data(TCP_connection) == 0) {
            int ret = socks5_read_handshake_response(TCP_connection);

            if (ret == -1) {
                TCP_connection->kill_at = 0;
                TCP_connection->status = TCP_CLIENT_DISCONNECTED;
            }

            if (ret == 1) {
                proxy_socks5_generate_connection_request(TCP_connection);
                TCP_connection->status = TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED;
            }
        }
    }

    if (TCP_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) {
        if (send_pending_data(TCP_connection) == 0) {
            int ret = proxy_socks5_read_connection_response(TCP_connection);

            if (ret == -1) {
                TCP_connection->kill_at = 0;
                TCP_connection->status = TCP_CLIENT_DISCONNECTED;
            }

            if (ret == 1) {
                generate_handshake(TCP_connection);
                TCP_connection->status = TCP_CLIENT_CONNECTING;
            }
        }
    }

    if (TCP_connection->status == TCP_CLIENT_CONNECTING) {
        if (send_pending_data(TCP_connection) == 0) {
            TCP_connection->status = TCP_CLIENT_UNCONFIRMED;
        }
    }

    if (TCP_connection->status == TCP_CLIENT_UNCONFIRMED) {
        uint8_t data[TCP_SERVER_HANDSHAKE_SIZE];
        int len = read_TCP_packet(TCP_connection->sock, data, sizeof(data));

        if (sizeof(data) == len) {
            if (handle_handshake(TCP_connection, data) == 0) {
                TCP_connection->kill_at = ~0;
                TCP_connection->status = TCP_CLIENT_CONFIRMED;
            } else {
                TCP_connection->kill_at = 0;
                TCP_connection->status = TCP_CLIENT_DISCONNECTED;
            }
        }
    }

    if (TCP_connection->status == TCP_CLIENT_CONFIRMED) {
        do_confirmed_TCP(TCP_connection, userdata);
    }

    if (TCP_connection->kill_at <= unix_time()) {
        TCP_connection->status = TCP_CLIENT_DISCONNECTED;
    }
}