コード例 #1
0
ファイル: offtp.cpp プロジェクト: Undin/OS-homework
int mread(int fd, char *buffer, int size, const char *del = NULL)
{
    int res = 1;
    int readed = 0;
    int find_del;
    while (res != 0 && readed < size)
    {
        res = read(fd, buffer + readed, size - readed);
                if (res == -1)
        {
            return -1;
        }

        if (del != NULL)
        {
            find_del = find_delimiter(buffer, readed, res, *del);
            if (find_del != -1)
            {
                return find_del;
            }
        }
        readed += res;
    }
    return readed;
}
コード例 #2
0
ファイル: simple_server.c プロジェクト: picrin/webserver
void forever_accept(){
  struct sockaddr_in connection_addrport;
  socklen_t len = sizeof(connection_addrport);
  int accept_status;
  while(1){
  accept_status = accept(server_descriptor,
      (struct sockaddr *) &connection_addrport,
      &len);
  if(accept_status == -1) report_error("socket accept error");
  
  const int recv_len = 1500;
  const int big_buffer_len = 10000;
  char recv_buffer[recv_len];// = (char *)malloc(sizeof(char) * recv_len);
  memset(recv_buffer, 0, sizeof(recv_buffer));
  
  // not to get confused with >0, 0 or -1, all of which have meaning.
  int message_length = -2;
  char received_string[big_buffer_len];
  //char dispatch_request[big_buffer_len];
  memset(received_string, 0, sizeof(received_string));

  struct indecies* indecies_state = malloc_indecies();
  indecies_state->big_buffer_index = (char *) received_string;
  indecies_state->position_index = (char *) recv_buffer;
  int has_delimiter = 0;
  char* end = NULL;
  int keep_alive = 1;
  while(message_length != 0 && keep_alive == 1){
    message_length = read(accept_status, recv_buffer, recv_len);
    //if (message_length == -1)report_error("read error");
    if (message_length + (indecies_state->big_buffer_index) >=
        (char *) received_string + big_buffer_len) report_error("GET request to"
        "o big, or too many GET requests in too short time. Support for up to 1"
        "0 kB/read");
    //print_indecies(indecies_state);
    
    //printf("recv_buffer + message_length: %lu\n", recv_buffer + message_length);
    //printf("ttuuuuuuuuuuuuuuuuuuuuuuuuuuuheasfglkeurhglksjfb%d", message_length);
    //dispatch request
    //zero the big buffer
    //do it recursively until finished 
    while((has_delimiter = find_delimiter(indecies_state,
        recv_buffer, recv_buffer + message_length)) != 0){

      end = indecies_state->big_buffer_index;
      
      indecies_state->big_buffer_index = (char*) received_string;
      dispatch_request((char*) received_string, end, accept_status, &keep_alive);
      //zero the big buffer; //could be done, but doesn't have to.  
    }
  }
  free_indecies(indecies_state);
  printf("closing\n");
  close(accept_status);
  }
}
コード例 #3
0
ファイル: stream.c プロジェクト: pandaman64/ubiquitous-bear
size_t curl_write_cb(char *ptr,size_t size,size_t number,void *userdat){
	static char* buffer = NULL;
	static size_t buffer_len = 0;

	char *tmp = realloc(buffer,buffer_len + size * number);
	if(!tmp){
		free(buffer);
		buffer = NULL;
		buffer_len = 0;
		return size * number;
	}
	buffer = tmp;
	memcpy(&buffer[buffer_len],ptr,size * number);
	buffer_len += size * number;

	size_t begin_json = 0;
	size_t begin_delim,end_delim;
	while(find_delimiter(&buffer[begin_json],buffer_len - begin_json,&begin_delim,&end_delim)){
		if(begin_delim > 0){
			void (*func)(char const*,size_t);
			func = userdat;
			func(&buffer[begin_json],begin_delim);
		}
		begin_json += end_delim;
		if(begin_json >= buffer_len){
			break;
		}
	}
	

	if(begin_json){
		if(begin_json < buffer_len){
			tmp = malloc(buffer_len - begin_json);
			memcpy(tmp,&buffer[begin_json],buffer_len - begin_json);
			free(buffer);
			buffer = tmp;
			buffer_len -= begin_json;
		}
		else{
			//printf("fed all the buffer\n");
			free(buffer);
			buffer = NULL;
			buffer_len = 0;
		}
	}
	return size * number;
}
コード例 #4
0
void
dissect_sss_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ncp_tree, guint8 subfunc, ncp_req_hash_value *request_value)
{
    guint32             foffset=0;
    guint32             subverb=0;
    guint32             msg_length=0;
    guint32             return_code=0;
    guint32             number_of_items=0;
    gint32              length_of_string=0;
    guint32             i = 0;
    const gchar         *str;

    proto_tree          *atree;
    proto_item          *aitem;
    proto_item          *expert_item;

    foffset = 8;
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "NSSS");
    if (tvb_length_remaining(tvb, foffset)<4) {
        return;
    }
    aitem = proto_tree_add_text(ncp_tree, tvb, foffset, -1, "Function: %s", val_to_str_const(subfunc, sss_func_enum, "Unknown"));
    atree = proto_item_add_subtree(aitem, ett_sss);
    switch (subfunc) {
    case 1:
        proto_tree_add_item(atree, hf_flags, tvb, foffset, 4, ENC_LITTLE_ENDIAN);
        foffset += 4;
        proto_tree_add_item(atree, hf_sss_version, tvb, foffset, 4, ENC_LITTLE_ENDIAN);
        /*foffset += 4;*/
        break;
    case 2:
        if (request_value) {
            subverb = request_value->req_nds_flags;
            str = try_val_to_str(subverb, sss_verb_enum);
            if (str) {
                proto_tree_add_text(atree, tvb, foffset, tvb_length_remaining(tvb, foffset), "Verb: %s", str);
            }
        }
        proto_tree_add_item(atree, hf_length, tvb, foffset, 4, ENC_LITTLE_ENDIAN);
        msg_length = tvb_get_letohl(tvb, foffset);
        foffset += 4;
        proto_tree_add_item(atree, hf_frag_handle, tvb, foffset, 4, ENC_LITTLE_ENDIAN);
        foffset += 4;
        msg_length -= 4;
        if ((tvb_get_letohl(tvb, foffset-4)==0xffffffff) && (msg_length > 4)) {
            foffset += 4;
            return_code = tvb_get_letohl(tvb, foffset);
            str = try_val_to_str(return_code, sss_errors_enum);
            if (str) {
                expert_item = proto_tree_add_item(atree, hf_return_code, tvb, foffset, 4, ENC_LITTLE_ENDIAN);
                expert_add_info_format(pinfo, expert_item, &ei_return_code, "SSS Error: %s", str);
                col_add_fstr(pinfo->cinfo, COL_INFO, "R Error - %s", val_to_str(return_code, sss_errors_enum, "Unknown (%d)"));
                /*foffset+=4;*/
            } else {
                proto_tree_add_text(atree, tvb, foffset, 4, "Return Code: Success (0x00000000)");
                if (tvb_length_remaining(tvb, foffset) > 8) {
                    foffset += 4;
                    if (request_value && subverb == 6) {
                        foffset += 4;
                        number_of_items = tvb_get_letohl(tvb, foffset);
                        foffset += 8;
                        for (i=0; i<number_of_items; i++) {
                            length_of_string = find_delimiter(tvb, foffset);
                            if (length_of_string > tvb_length_remaining(tvb, foffset)) {
                                return;
                            }
                            foffset = sss_string(tvb, hf_secret, atree, foffset, TRUE, length_of_string);
                            if (tvb_length_remaining(tvb, foffset) < 8) {
                                return;
                            }
                            foffset++;
                        }
                    } else {
                        proto_tree_add_item(atree, hf_enc_data, tvb, foffset, tvb_length_remaining(tvb, foffset), ENC_NA);
                    }
                }
            }
        } else {
            proto_tree_add_text(atree, tvb, foffset, 4, "Return Code: Success (0x00000000)");
            if (tvb_length_remaining(tvb, foffset) > 8) {
                foffset += 4;
                proto_tree_add_item(atree, hf_enc_data, tvb, foffset, tvb_length_remaining(tvb, foffset), ENC_NA);
            }
        }
        break;
    case 3:
        break;
    default:
        break;
    }
}
コード例 #5
0
ファイル: Codec.hpp プロジェクト: yliu120/K3
 bool decode_ready() {
  return buf_?
     find_delimiter() != std::string::npos : false;
 }
コード例 #6
0
ファイル: ImR_Client.cpp プロジェクト: milan-mpathix/ATCD
    void
    ImR_Client_Adapter_Impl::imr_notify_startup (TAO_Root_POA* poa )
    {
      CORBA::Object_var imr = poa->orb_core ().implrepo_service ();

      if (CORBA::is_nil (imr.in ()))
        {
          if (TAO_debug_level > 0)
            {
              TAOLIB_ERROR ((LM_ERROR,
                          ACE_TEXT ("TAO_ImR_Client (%P|%t) - ERROR: No usable IMR initial reference ")
                          ACE_TEXT ("available but use IMR has been specified.\n")));
            }
          throw ::CORBA::TRANSIENT (
              CORBA::SystemException::_tao_minor_code (TAO_IMPLREPO_MINOR_CODE, 0),
              CORBA::COMPLETED_NO);
        }

      if (TAO_debug_level > 0)
        {
          if (TAO_debug_level > 1)
            {
              CORBA::ORB_ptr orb = poa->orb_core ().orb ();
              CORBA::String_var ior = orb->object_to_string (imr.in ());
              TAOLIB_DEBUG ((LM_DEBUG,
                            ACE_TEXT ("TAO_ImR_Client (%P|%t) - Notifying ImR of startup IMR IOR <%C>\n"),
                            ior.in ()));
            }
        }

      ImplementationRepository::Administration_var imr_locator;

      {
        // ATTENTION: Trick locking here, see class header for details
        TAO::Portable_Server::Non_Servant_Upcall non_servant_upcall (*poa);
        ACE_UNUSED_ARG (non_servant_upcall);

        imr_locator =
          ImplementationRepository::Administration::_narrow (imr.in ());
      }

      if (CORBA::is_nil (imr_locator.in ()))
        {
          if (TAO_debug_level > 0)
            {
              TAOLIB_ERROR ((LM_ERROR,
                          ACE_TEXT ("TAO_ImR_Client (%P|%t) - ERROR: Narrowed IMR initial reference ")
                          ACE_TEXT ("is nil but use IMR has been specified.\n")));
            }

          throw ::CORBA::TRANSIENT (
              CORBA::SystemException::_tao_minor_code (TAO_IMPLREPO_MINOR_CODE, 0),
              CORBA::COMPLETED_NO);
        }

      TAO_Root_POA *root_poa = poa->object_adapter ().root_poa ();
      ACE_NEW_THROW_EX (this->server_object_,
                        ServerObject_i (poa->orb_core ().orb (),
                                        root_poa),
                        CORBA::NO_MEMORY ());

      PortableServer::ServantBase_var safe_servant (this->server_object_);
      ACE_UNUSED_ARG (safe_servant);

      // Since this method is called from the POA constructor, there
      // shouldn't be any waiting required.  Therefore,
      // <wait_occurred_restart_call_ignored> can be ignored.
      bool wait_occurred_restart_call_ignored = false;

      // Activate the servant in the root poa.
      PortableServer::ObjectId_var id =
        root_poa->activate_object_i (this->server_object_,
                                     poa->server_priority (),
                                     wait_occurred_restart_call_ignored);

      CORBA::Object_var obj = root_poa->id_to_reference_i (id.in (), false);

      ImplementationRepository::ServerObject_var svr
        = ImplementationRepository::ServerObject::_narrow (obj.in ());

      if (!svr->_stubobj () || !svr->_stubobj ()->profile_in_use ())
        {
          if (TAO_debug_level > 0)
            {
              TAOLIB_ERROR ((LM_ERROR, "TAO_ImR_Client (%P|%t) - Invalid ImR ServerObject, bailing out.\n"));
            }
          return;
        }
      CORBA::ORB_var orb = root_poa->_get_orb ();
      CORBA::String_var full_ior = orb->object_to_string (obj.in ());
      TAO_Profile& profile = *(svr->_stubobj ()->profile_in_use ());
      CORBA::String_var ior = profile.to_string();
      if (TAO_debug_level > 0)
        {
          TAOLIB_DEBUG((LM_INFO,
                        "TAO_ImR_Client (%P|%t) - full_ior <%C>\nior <%C>\n",
                        full_ior.in(),
                        ior.in()));
        }
      char* const pos = find_delimiter (ior.inout (),
                                        profile.object_key_delimiter ());

      const ACE_CString partial_ior (ior.in (), (pos - ior.in ()) + 1);

      if (TAO_debug_level > 0)
      {
        CORBA::String_var poaname = poa->the_name ();
        TAOLIB_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("TAO_ImR_Client (%P|%t) - Informing IMR that <%C> is running at <%C>\n"),
                    poaname.in(), partial_ior.c_str ()));
      }

      try
        {
          // ATTENTION: Trick locking here, see class header for details
          TAO::Portable_Server::Non_Servant_Upcall non_servant_upcall (*poa);
          ACE_UNUSED_ARG (non_servant_upcall);

          ACE_CString const serverId = poa->orb_core ().server_id ();
          ACE_CString name;
          if (serverId.empty ())
            {
              name = poa->name ();
            }
          else
            {
              name = serverId + ":" + poa->name ();
            }

          imr_locator->server_is_running (name.c_str (),
                                          partial_ior.c_str (),
                                          svr.in ());
        }
      catch (const ::CORBA::SystemException&)
        {
          throw;
        }
      catch (const ::CORBA::Exception&)
        {
          throw ::CORBA::TRANSIENT (
              CORBA::SystemException::_tao_minor_code (TAO_IMPLREPO_MINOR_CODE, 0),
              CORBA::COMPLETED_NO);
        }

      if (TAO_debug_level > 0)
        {
          TAOLIB_DEBUG ((LM_DEBUG,
                         ACE_TEXT ("TAO_ImR_Client (%P|%t) - Successfully notified ImR of Startup\n")));
        }
    }