Esempio n. 1
0
CacheSession::CacheSession(ClientSession *session, const char *url, uint32_t file_ttl,
						   uint32_t dir_ttl)
	: FilterSession(session)
{
	m_dir_ttl = file_ttl;
	m_dir_ttl = dir_ttl;
	string tmp(url);
	make_hash64("md5", tmp, m_url_hash, true);
	tmp = url + string(":meta:") + get_client_id();
	make_hash64("md5", tmp, m_meta_key, true);
	tmp = url + string(":data:") + get_client_id();
	make_hash64("md5", tmp, m_data_key, true);
	m_base_dir = cacheBaseDir() + SEPSTR + m_url_hash;
	if (!dir_exists(m_base_dir.c_str()) && makedirs(m_base_dir.c_str()) < 0)
	{
		m_enable = false;
		_DEBUG("Disabling Cache Session: can't create");
	}
#if 0
	else if (!can_write_dir(m_base_dir.c_str()))
	{
		m_enable = false;
		_DEBUG("Disabling Cache Session: can't write");
	}
#endif
	else
	{
		m_enable = true;
		_DEBUG("Enabling Cache Session");
	}
}
Esempio n. 2
0
void thread_impl_t::query_information( THREAD_BASIC_INFORMATION& info )
{
	info.ExitStatus = ExitStatus;
	info.TebBaseAddress = TebBaseAddress;
	get_client_id( &info.ClientId );
	// FIXME: AffinityMask, Priority, BasePriority
}
Esempio n. 3
0
 work_queue_for_tests::work_queue_for_tests (const sync_function_type & sync_func)
     : work_queue (work_queue::dont_start_worker ())
     , _current_time_point (work_queue::clock_type::now ())
     , _time_forwarding_flag (false)
     , _time_forwarding_completed ()
     , _time_forwarding_client_id (work_queue::INVALID_CLIENT_ID)
     , _sync_function (sync_func)
 {
     _time_forwarding_client_id = get_client_id ();
     start_worker ();
 }
Esempio n. 4
0
void generate_my_msqid(){
  //Get this clients msqid and set CLIENT_MSQID
  int client_id, id, msq_id;

  client_id = get_client_id();
  set_anchor_for_new_client(client_id);

  id = generate_msg_key(client_id);
  msq_id = generate_queue(id, 0600 | IPC_CREAT);
  CLIENT_MSQID = msq_id;
  M_TYPE = 1;
}
Esempio n. 5
0
bool database::authkey_auth(const char GUID[SUPLA_GUID_SIZE],
                            const char Email[SUPLA_EMAIL_MAXSIZE],
                            const char AuthKey[SUPLA_AUTHKEY_SIZE], int *UserID,
                            bool Client, const char *sql) {
  if (_mysql == NULL) {
    return false;
  }

  int ID = 0;
  int _UserID = get_user_id_by_email(Email);

  if (_UserID == 0 && !Client) {
    ID = get_device_id_and_user(GUID, &_UserID);
  }

  if (_UserID == 0) {
    return false;
  }

  if (ID == 0) ID = Client ? get_client_id(_UserID, GUID) : get_device_id(GUID);

  if (ID == 0) {  // Yes. When client not exists then is authorized
    *UserID = _UserID;
    return true;
  }

  bool is_null = false;
  char AuthKeyHash[BCRYPT_HASH_MAXSIZE];
  memset(AuthKeyHash, 0, BCRYPT_HASH_MAXSIZE);

  if (!get_authkey_hash(ID, AuthKeyHash, BCRYPT_HASH_MAXSIZE, &is_null, sql)) {
    return false;
  }

  if (is_null) {  // Yes. When is null then is authorized
    *UserID = _UserID;
    return true;
  }

  char AuthKeyHEX[SUPLA_AUTHKEY_HEXSIZE];
  memset(AuthKeyHEX, 0, SUPLA_AUTHKEY_HEXSIZE);

  st_authkey2hex(AuthKeyHEX, AuthKey);

  if (st_bcrypt_check(AuthKeyHEX, AuthKeyHash,
                      strnlen(AuthKeyHash, BCRYPT_HASH_MAXSIZE))) {
    *UserID = _UserID;
    return true;
  }

  return false;
}
Esempio n. 6
0
int main(int argc, char **argv)
{
	struct client_id c;

	setuid(getuid());

	if(get_client_id(&c) < 0)
		exit(EXIT_FAILURE);

	fprintf(stdout, "%-25s = uid=%i(%.63s) gid=%i(%.63s)\n", "Real ID", c.real_uid, c.real_pw_name, c.real_gid, c.real_gr_name);
	fprintf(stdout, "%-25s = uid=%i(%.63s) gid=%i(%.63s)\n", "Effective ID", c.effective_uid, c.effective_pw_name, c.effective_gid, c.effective_gr_name);
	fprintf(stdout, "%-25s = uid=%i(%.63s) gid=%i(%.63s) terminal=%.100s\n", "Originally logged on as", c.original_uid, c.original_pw_name, c.original_gid, c.original_gr_name, c.terminal);


	exit(EXIT_SUCCESS);
}
Esempio n. 7
0
int process_local_changes() {
  if (!stop_running) {
	  // Process local changes
	  int i,result,source_length;
	  int max_size = 100;
	  pSource *source_list;
	  char *client_id = NULL;
	  source_list = malloc(max_size*sizeof(pSource));
	  source_length = get_sources_from_database(source_list, database, max_size);
	  
	  for(i = 0; i < source_length; i++) {
		  if(client_id == NULL) {
			  client_id = get_client_id(database, source_list[i]);  
		  }
		  result = 0;
		  printf("Processing local changes for source %i...\n", source_list[i]->_source_id);
		  result += process_op_list(source_list[i], "update");
		  result += process_op_list(source_list[i], "create");
		  result += process_op_list(source_list[i], "delete");
	  }
  	
	  if (result > 0) {
		  printf("Remote update failed, not continuing with sync...\n");
	  } else {
		  /* fetch new list from sync source */
		  int available_remote = fetch_remote_changes(database, client_id);
		  if(available_remote > 0) {
			  printf("Successfully processed %i records...\n", available_remote);
  			
		  }
	  }
	  free_source_list(source_list, source_length);
	  if (client_id) {
		  free(client_id);  
	  }
  } else {
	  shutdown_database();
  }
  return 0;
}
Esempio n. 8
0
/*
 * Message validation, RFC 5007 section 4.2.1:
 *  dhcpv6.c:valid_client_msg() - unicast + lq-query option.
 */
static int
valid_query_msg(struct lq6_state *lq) {
	struct packet *packet = lq->packet;
	int ret_val = 0;
	struct option_cache *oc;

	/* INSIST((lq != NULL) || (packet != NULL)); */

	switch (get_client_id(packet, &lq->client_id)) {
		case ISC_R_SUCCESS:
			break;
		case ISC_R_NOTFOUND:
			log_debug("Discarding %s from %s; "
				  "client identifier missing", 
				  dhcpv6_type_names[packet->dhcpv6_msg_type],
				  piaddr(packet->client_addr));
			goto exit;
		default:
			log_error("Error processing %s from %s; "
				  "unable to evaluate Client Identifier",
				  dhcpv6_type_names[packet->dhcpv6_msg_type],
				  piaddr(packet->client_addr));
			goto exit;
	}

	oc = lookup_option(&dhcpv6_universe, packet->options, D6O_SERVERID);
	if (oc != NULL) {
		if (evaluate_option_cache(&lq->server_id, packet, NULL, NULL,
					  packet->options, NULL, 
					  &global_scope, oc, MDL)) {
			log_debug("Discarding %s from %s; " 
				  "server identifier found "
				  "(CLIENTID %s, SERVERID %s)", 
				  dhcpv6_type_names[packet->dhcpv6_msg_type],
				  piaddr(packet->client_addr),
				  print_hex_1(lq->client_id.len, 
				  	      lq->client_id.data, 60),
				  print_hex_2(lq->server_id.len,
				  	      lq->server_id.data, 60));
		} else {
			log_debug("Discarding %s from %s; " 
				  "server identifier found "
				  "(CLIENTID %s)", 
				  dhcpv6_type_names[packet->dhcpv6_msg_type],
				  print_hex_1(lq->client_id.len, 
				  	      lq->client_id.data, 60),
				  piaddr(packet->client_addr));
		}
		goto exit;
	}

	switch (get_lq_query(lq)) {
		case ISC_R_SUCCESS:
			break;
		case ISC_R_NOTFOUND:
			log_debug("Discarding %s from %s; lq-query missing",
				  dhcpv6_type_names[packet->dhcpv6_msg_type],
				  piaddr(packet->client_addr));
			goto exit;
		default:
			log_error("Error processing %s from %s; "
				  "unable to evaluate LQ-Query",
				  dhcpv6_type_names[packet->dhcpv6_msg_type],
				  piaddr(packet->client_addr));
			goto exit;
	}

	/* looks good */
	ret_val = 1;

exit:
	if (!ret_val) {
		if (lq->client_id.len > 0) {
			data_string_forget(&lq->client_id, MDL);
		}
		if (lq->server_id.len > 0) {
			data_string_forget(&lq->server_id, MDL);
		}
		if (lq->lq_query.len > 0) {
			data_string_forget(&lq->lq_query, MDL);
		}
	}
	return ret_val;
}
Esempio n. 9
0
void NetworkManager::handle_receive(const boost::system::error_code& error, std::size_t bytes_transferred){
	if (!error)
	{
	   	ClientMessage message = ClientMessage(std::string(recv_buffer.data(), recv_buffer.data() + bytes_transferred),  get_client_id(remote_endpoint));
		if (!message.first.empty())
		{
			boost::mutex::scoped_lock lock(mutex);
			messages.push(message);
			if(timeLeft.find(message.second) != timeLeft.end())
				timeLeft[message.second] = (int) clock();
			else
				timeLeft[nextClientID + 1] = (int) clock();
			cout << message.first <<"\n";
		}
		else
		{
			cout << "empty message\n";
		}
	    receivedBytes += bytes_transferred;
	    receivedMessages++;
	}
	else
	{
		cout << "error!" <<"\n";
	}

	start_receive();
}
Esempio n. 10
0
NTSTATUS thread_impl_t::create( CONTEXT *ctx, INITIAL_TEB *init_teb, BOOLEAN suspended )
{
	void *pLdrInitializeThunk;
	void *pKiUserApcDispatcher;
	PTEB pteb = NULL;
	BYTE *addr = 0;
	void *stack;
	NTSTATUS r;
	struct {
		void *pLdrInitializeThunk;
		void *unk1;
		void *pntdll;  /* set to pexe if running a win32 program */
		void *unk2;
		CONTEXT ctx;
		void *ret;	 /* return address (to KiUserApcDispatcher?) */
	} init_stack;

	/* allocate the TEB */
	LARGE_INTEGER sz;
	sz.QuadPart = PAGE_SIZE;
	r = create_section( &teb_section, NULL, &sz, SEC_COMMIT, PAGE_READWRITE );
	if (r < STATUS_SUCCESS)
		return r;

	r = teb_section->mapit( process->vm, addr, 0, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
	if (r < STATUS_SUCCESS)
		return r;

	teb = (PTEB) teb_section->get_kernel_address();

	pteb = (PTEB) addr;
	teb->Peb = (PPEB) process->PebBaseAddress;
	teb->Tib.Self = &pteb->Tib;
	teb->StaticUnicodeString.Buffer = pteb->StaticUnicodeBuffer;
	teb->StaticUnicodeString.MaximumLength = sizeof pteb->StaticUnicodeBuffer;
	teb->StaticUnicodeString.Length = sizeof pteb->StaticUnicodeBuffer;

	// FIXME: need a good thread test for these
	teb->DeallocationStack = init_teb->StackReserved;
	teb->Tib.StackBase = init_teb->StackCommit;
	teb->Tib.StackLimit = init_teb->StackReserved;

	get_client_id( &teb->ClientId );

	/* setup fs in the user address space */
	TebBaseAddress = pteb;

	/* find entry points */
	pLdrInitializeThunk = (BYTE*)process->pntdll + get_proc_address( ntdll_section, "LdrInitializeThunk" );
	if (!pLdrInitializeThunk)
		die("failed to find LdrInitializeThunk in ntdll\n");

	pKiUserApcDispatcher = (BYTE*)process->pntdll + get_proc_address( ntdll_section, "KiUserApcDispatcher" );
	if (!pKiUserApcDispatcher)
		die("failed to find KiUserApcDispatcher in ntdll\n");

	dprintf("LdrInitializeThunk = %p pKiUserApcDispatcher = %p\n",
		pLdrInitializeThunk, pKiUserApcDispatcher );

	// FIXME: should set initial registers then queue an APC

	/* set up the stack */
	stack = (BYTE*) ctx->Esp - sizeof init_stack;

	/* setup the registers */
	int err = set_initial_regs( pKiUserApcDispatcher, stack );
	if (0>err)
		dprintf("set_initial_regs failed (%d)\n", err);

	memset( &init_stack, 0, sizeof init_stack );
	init_stack.pntdll = process->pntdll;  /* set to pexe if running a win32 program */
	init_stack.pLdrInitializeThunk = pLdrInitializeThunk;

	/* copy the context onto the stack for NtContinue */
	memcpy( &init_stack.ctx, ctx, sizeof *ctx );
	init_stack.ret  = (void*) 0xf00baa;

	r = process->vm->copy_to_user( stack, &init_stack, sizeof init_stack );
	if (r < STATUS_SUCCESS)
		dprintf("failed to copy initial stack data\n");

	if (!suspended)
		resume( NULL );

	process->vm->set_tracer( addr, teb_trace );

	return STATUS_SUCCESS;
}
Esempio n. 11
0
}






int delete(char* dest, char* file, int client_id) {
	
	fstree_t client_tree;
	char * child_file, * position; 
	int base_client_id, file_type;
	child_file = (char *)calloc(1, MAX_PATH_LENGTH);
	strcpy(child_file, file);
	
	base_client_id = get_client_id(dest);
	client_tree = get_client_tree(base_client_id); 
	
	fstree_node_t current = client_tree->root;
	current->status = INSIDE_CHANGED;
	
	while (position = strchr(child_file, '/')) {
		*position = 0;
		current =  find_child_by_path(current,child_file);
		if (current == NULL) {
			client_send("File not found", client_id);
			client_send(END_OF_TRANSMISSION, client_id);
			return -1;
		} else { 
			current->status = INSIDE_CHANGED;	 
		}
Esempio n. 12
0
int main(int argc, char *argv[])
{
	check_args(argc, argv);
	
	/* Adresa IP a serverului */
	struct in_addr server_in_addr;
	inet_aton(ip_server, &server_in_addr);
	
	/* Adresa serverului */
	struct sockaddr_in server_addr;
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr   = server_in_addr;
	server_addr.sin_port   = htons( (uint16_t)atoi(port_server) );

	/* Socket pe care se va face comunicarea cu serverul */
	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	if( sockfd < 0 ) {
		std::cout << DBG << "Eroare deschidere socket.\n";
		CLOSE_FILE;
		exit(EXIT_FAILURE);	
	} else {
		std::cout << DBG << "Sa deschis socketul " << sockfd << ".\n";
	}

	/* Conectam clientul la server */
	int tmp = connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr));
	if(tmp < 0) {
		std::cout << DBG << "Nu sa putut face conect(). \n";
		CLOSE_FILE
		exit(EXIT_FAILURE);
	} else {
		std::cout << DBG << "Socketul creeat anterior a fost conectat la server.\n";
	}

	/* Trimitem serverului numele si portul pe care va asculta clientul */	
	send(sockfd, client_name, BUFLEN, 0);
	send(sockfd, port_client, BUFLEN, 0);

	/* Adresa IP a masini pe care ruelaza clientul */
	struct in_addr        local_in_addr; 
	local_in_addr.s_addr = INADDR_ANY;
         
	/* Adresa clientului pe masina locala */             
	struct sockaddr_in    local_sockaddr_in;		             

	/* Se prezuma ca valoare stringul port_client poate fi convertit fara probleme la uint16_t */
	uint16_t port = (uint16_t)atoi(port_client);

	local_sockaddr_in.sin_family = AF_INET;
	local_sockaddr_in.sin_addr   = local_in_addr;
	local_sockaddr_in.sin_port   = htons( port );

	/* Socketul care va scana cereri de conexiune de la alti clienti*/
	listen_sockfd = socket(AF_INET, SOCK_STREAM, 0);

	/* Legam socketul in cauza de portul de scanare */
	tmp = bind(listen_sockfd, (struct sockaddr *) &local_sockaddr_in, sizeof(struct sockaddr));
	if(tmp < 0) {
		std::cout << DBG << " Eroare bind()\n";
		CLOSE_FILE;
		exit(EXIT_FAILURE);
	} else {
		std::cout << DBG << " Socketul creeat a fost legat cu succes de portul.\n";
	}

	/* Fortam socketul de ascultare al clientului sa asculte */
	listen(listen_sockfd, MAX_CLIENTS);
	
	

	char msg[BUFLEN];
	memset(msg, '\0', BUFLEN);

	fd_set read_fds;	                       /* multimea de citire folosita in select() */
       	fd_set tmp_fds;	                                             /* multime folosita temporar */
	fd_set write_fds;
     	int fdmax = (sockfd > listen_sockfd)?(sockfd + 1):(listen_sockfd + 1);           
					  /* valoare maxima file descriptor din multimea read_fds */

	struct timeval *timeout;           /* Necesara pentru a trezi selectul cind avem transfer */

	FD_ZERO(&read_fds);
	FD_ZERO(&tmp_fds);
	FD_ZERO(&write_fds);
	FD_SET(listen_sockfd, &read_fds);
	FD_SET(0, &read_fds);
	while(1) {
		timeout = NULL;
		/* Pentru toate operatiile active */
		bool have_transfer = false;
		for(int i = 0; i < active_PO; ++i) {
			if( PO[i].status == 1 && PO[i].type == send_file ) {
				have_transfer = true;
				/* 
				 * Daca avem cel putin o operatie netermianta (status = 1) de 
				 * trimitere, setam timeout pentru a nu risca sa ne blocam in select  
				 */
				if(timeout == NULL) {
					timeout = (struct timeval *)malloc( sizeof(struct timeval) );
					timeout->tv_sec = 0;
  					timeout->tv_usec = 10000;
				}

				memset((char*)buffer, '\0', SEND_SIZE);
				memset(msgaux, '\0', NAME_LEN);

				int x = fread(buffer, 1, SEND_SIZE, PO[i].file);
				
				if(x > 0) {
					/* Trimitem numarul de octeti cititi */					
					sprintf(msgaux, "%d", x);
					send(PO[i].sockfd, msgaux, NAME_LEN, 0);

					/* Trimitem octeti cititi */
					send(PO[i].sockfd, buffer, SEND_SIZE, 0);
				} else {
					/* Anuntam clientul ca am citit 0 oceti, deci transferul e finisat */
					sprintf(msgaux, "%d", 0);
					send(PO[i].sockfd, msgaux, 32, 0);

					/* Inchidem socketul de transfer, fisierul din care sa citit, setam statusul */
					close(PO[i].sockfd) ;
					fclose(PO[i].file) ;
					PO[i].status = 0;
					FD_CLR(PO[i].sockfd, &read_fds);
				}
			}
		}

/*              //oferim utilizatorului un prompt
		if( !have_transfer ) {
			std::cout << client_name << ">";
			std::cout.flush();
		}
*/
		tmp_fds = read_fds;
		
		if (select(fdmax + 1, &tmp_fds, NULL, NULL, timeout) == -1) {
			std::cout << DBG << " eroare select()\n";
			CLOSE_FILE;
			exit(EXIT_FAILURE);
		} 

		if(FD_ISSET(0, &tmp_fds)) {
			std::cin.getline(msg, BUFLEN, '\n');
			if ( strlen(msg) == 0 ) continue;

			if( strcmp(msg, "quit") == 0 ) {
				std::cout << DBG << "Inchidere client . . .\n";
			
				// Anuntam serverul despre exit
				send(sockfd, msg, NAME_LEN, 0);
				shutdown(sockfd, 2);

				/* DONE termina de trimis fisierele */
				for(int i = 0; i < active_PO; ++i) {
					if( PO[i].status == 1 && PO[i].type == send_file ) {
						int x = fread(buffer, 1, SEND_SIZE, PO[i].file);
						while( x > 0 ) {
							memset(msgaux, '\0', NAME_LEN);
							sprintf(msgaux, "%d", x);
							send(PO[i].sockfd, msgaux, NAME_LEN, 0);
			
							send(PO[i].sockfd, buffer, SEND_SIZE, 0);
							memset((char*)buffer, '\0', SEND_SIZE);
							x = fread(buffer, 1, SEND_SIZE, PO[i].file);
						}
						close(PO[i].sockfd) ;
						fclose(PO[i].file) ;
						PO[i].status = 0;
						FD_CLR(PO[i].sockfd, &read_fds);
					} 

					if( PO[i].status == 1 && PO[i].type == recv_file ) {
						close(PO[i].sockfd) ;
						fclose(PO[i].file) ;
						PO[i].status = 0;
						FD_CLR(PO[i].sockfd, &read_fds);
					}
				}
				/* DONE inchide toate coneiunile */
				close(sockfd);
				close(listen_sockfd);

				CLOSE_FILE;
				exit(EXIT_SUCCESS);
			}


			if( strncmp(msg, "getfile", 7) == 0 ) {

				char arg1[NAME_LEN], arg2[NAME_LEN];	
				extract_arguments(arg1, arg2, msg);

				//DONE verificam daca arg1 este un client valid, cunoscut

				int id = get_client_id(arg1);
				std::cout << arg1 << " " << id << "\n";
	
				if(id == -1) {
					std::cout << "-4 : Client necunoscut\n";
					continue;
				}

				bool ok = true;
				for(int i = 0; i < nr_of_shared_files; i++) {
					if( strcmp(shared[i].name, arg1) == 0 ) {
						ok = false;
					}
				}

				if( !ok ) {
					std::cout << "-6 : Fisier partajat cu acelasi nume\n";
					continue;
				}

				/* Adresa IP a peer-ului */
				struct in_addr peer_in_addr;
				inet_aton(known_clients[id].basic_info.IP, &peer_in_addr);
				//std::cout << "-----------IP-ul peerului " << known_clients[id].basic_info.IP << "\n";
	
				/* Adresa peerului */
				struct sockaddr_in peer_addr;
				peer_addr.sin_family = AF_INET;
				peer_addr.sin_addr   = peer_in_addr;
				peer_addr.sin_port   = htons( (uint16_t)known_clients[id].basic_info.port );

				/* Socket pe care se va face comunicarea ulterioara cu peerul */
				int peer_sockfd = socket(AF_INET, SOCK_STREAM, 0);				
				tmp = connect(peer_sockfd, (struct sockaddr *) &peer_addr, sizeof(struct sockaddr));
				if( tmp == -1 ) {
					std::cout << "-1 : Eroare la conectare\n";
					log_file  << "-1 : Eroare la conectare\n";
					close(peer_sockfd);
					continue;
				}
				FD_SET(peer_sockfd, &read_fds);
				if(fdmax < peer_sockfd + 1) fdmax = peer_sockfd + 1;
				
				send(peer_sockfd, msg, BUFLEN, 0);

				struct pending_operation op;
				memset(PO[active_PO].file_name, '\0', NAME_LEN);
				sprintf(PO[active_PO].file_name, "%s", arg2);
				memset(PO[active_PO].another, '\0', NAME_LEN);
				sprintf(PO[active_PO].another, "%s", arg1);
				PO[active_PO].status = op.status = 1; 
				PO[active_PO].type   = op.type   = recv_file;
				PO[active_PO].file   = op.file   = fopen(arg2, "wb");
				PO[active_PO].sockfd = op.sockfd = peer_sockfd;
				active_PO++;
				std::cout << "Sa pornit o operatie de primire pe " << active_PO-1 << "\n";
				/* //modul blocant
				char buffer[1024];
				memset((char*)buffer, '\0', 1024);
				char msgaux[32];
				memset(msgaux, '\0', 32);
				recv(PO[active_PO-1].sockfd, msgaux, 32, 0);
				std::cout << "Sa receptionat ca sau trimis " << msgaux << "octeti\n";
				
				int x;
				sscanf(msgaux, "%d", &x);
				while(x > 0) {
					std::cout << "Se primeste un bloc de 1024 cu" << x << "\n";
					recv(PO[active_PO-1].sockfd, buffer, 1024, 0);
					
					fwrite(buffer, 1, x, PO[active_PO-1].file);

					memset((char*)buffer, '\0', 1024);
					memset(msgaux, '\0', 32);
					recv(PO[active_PO-1].sockfd, msgaux, 32, 0);
					std::cout << "Sa receptionat ca sau trimis " << msgaux << "octeti\n";
					int y = sscanf(msgaux, "%d", &x);
					std::cout << "Sa receptionat ca sau trimis " << x << "octeti ca variabila int\n";
					if (y < 0) break;
				}
				std::cout << "Comunicarea din pareta receptorului incheiata.\n";
				close(PO[active_PO-1].sockfd) ;
				fclose(PO[active_PO-1].file) ;
				FD_CLR(PO[active_PO-1].sockfd, &read_fds);
				*/
				
			
			} else {
				if( strcmp(msg, "dump") == 0) {
					dump_known_clients();
				} else {			
					digest_user_comand(msg);
				}
			}

			FD_CLR(0, &tmp_fds);
		}

		for(int i = 1; i <= fdmax; ++i) {
			if( FD_ISSET(i, &tmp_fds) && i != sockfd ) {
				/* 
				 * Toata comunicatia cu rerverul se realizeaza doar
				 * in cadrul functiei digest_user_command();
				 * deci aici avem un transfer de fisier sau o cere 
				 * de conexiune pentru un nou transfer
				 */
				if( i == listen_sockfd ) {
					/* A venit o noua cere de transfer */
					struct sockaddr_in new_peer_addr_in;
					socklen_t len = sizeof(struct sockaddr_in);
					int new_peer_sockfd = accept(listen_sockfd,  (struct sockaddr *) &new_peer_addr_in, &len );
					FD_SET(new_peer_sockfd, &read_fds);
					if(fdmax < new_peer_sockfd + 1) fdmax = new_peer_sockfd + 1;

					memset(msg, '\0', BUFLEN);
					recv(new_peer_sockfd, msg, BUFLEN, 0);
					std::cout << "O noua cerere de transfer: " << msg << "\n";

					char arg1[NAME_LEN], arg2[NAME_LEN];	
					extract_arguments(arg1, arg2, msg);
					
					struct pending_operation op;
					PO[active_PO].status = op.status = 1; 
					PO[active_PO].type   = op.type   = send_file;
					PO[active_PO].file   = op.file   = fopen(arg2, "rb");
					PO[active_PO].sockfd = op.sockfd = new_peer_sockfd;
					active_PO++;
					std::cout << "Sa pornit o operatie de trimitere pe " << active_PO-1 << "\n";
					/*   //modul blocant
					char buffer[1024];
					char msgaux[32];
					memset((char*)buffer, '\0', 1024);
					int x = fread(buffer, 1, 1024, PO[active_PO-1].file);
					while( x > 0 ) {
						std::cout << "Sau citit : " << x << " octeti;\n";
						char msgaux[32];
						memset(msgaux, '\0', 32);
						sprintf(msgaux, "%d", x);
						send(PO[active_PO-1].sockfd, msgaux, 32, 0);
						std::cout << "Sa trimis ca sa citit : " << x << " octeti;\n";
			
						std::cout << "Sa trimis un bloc de 1024 cu" << x << "\n";
						send(PO[active_PO-1].sockfd, buffer, 1024, 0);
						memset((char*)buffer, '\0', 1024);
						x = fread(buffer, 1, 1024, PO[active_PO-1].file);
					}

					memset(msgaux, '\0', 32);
					sprintf(msgaux, "%d", 0);
					send(PO[active_PO-1].sockfd, msgaux, 32, 0);
					std::cout << "Comunicarea din pareta trimitatoruli incheiata.\n";
					close(PO[active_PO-1].sockfd) ;
					fclose(PO[active_PO-1].file) ;

					FD_CLR(PO[active_PO-1].sockfd, &read_fds);
					*/
				}
			} 
		}

		for(int i = 0; i < active_PO; i++) {
			if(PO[i].status == 0 || PO[i].type == send_file) continue;
			if( !FD_ISSET(PO[i].sockfd, &read_fds) ) continue;
			
			/* Pentru toate operatiile active de receptionare fisier */
			if(PO[i].status == 1 && PO[i].type == recv_file) {
				char buffer[SEND_SIZE];
				memset(buffer, '\0', SEND_SIZE);
				char msgaux[NAME_LEN];
				memset(msgaux, '\0', NAME_LEN);

				/* Receptionam citi octeti vom primi */
				recv(PO[i].sockfd, msgaux, NAME_LEN, 0);
				//std::cout << "Sa receptionat ca sau trimis " << msgaux << "octeti pe op. " << i << "\n" ;
				
				int x;
				sscanf(msgaux, "%d", &x);

				if(x > 0) {
					//std::cout << "Se primeste un bloc de 1024 cu" << x << "\n";
					recv(PO[i].sockfd, buffer, 1024, 0);
					
					fwrite(buffer, 1, x, PO[active_PO-1].file);

					memset((char*)buffer, '\0', 1024);
					memset(msgaux, '\0', 32);
				} else {
					//std::cout << "Comunicarea din pareta receptorului incheiata.\n";
					close(PO[i].sockfd) ;
					fclose(PO[i].file) ;
					FD_CLR(PO[i].sockfd, &read_fds);
					PO[i].status = 0;
					std::cout << "Succes " << PO[i].file_name << "\n";
					log_file  << client_name << "> getfile " << PO[i].another << " " << PO[i].file_name << "\n";
					log_file  << "Succes " << PO[i].file_name << "\n";

					memset(H[age], '\0', 128);
					sprintf(H[age], "%s %s", PO[i].another, PO[i].file_name);
					age++;
				}
			}	

		}

		
	}

}
Esempio n. 13
0
INT32 register_client
(
 	INT32 major,
 	INT32 minor
)
{
	INT32 cli_id;
	p_atheros_dev dev;
#if defined(OWL_PB42) || defined(PYTHON_EMU)
	UINT32 vendor_id;
#endif
	/* get the client for this device */
	cli_id = get_client_id(major, minor);

	if (cli_id == INVALID_CLIENT) {
		printk("DK::register_client:Device not found \n");
	 	return -ENODEV;
	}

	printk("DK::Regsitering client %d \n",cli_id);
	dev = &dev_table[cli_id];

	if (!VALID_CLIENT(dev)) {
		printk("DK::register_client:Invalid client \n");
	 	return -ENODEV;
    }

	if (BUSY_CLIENT(dev)) {
		printk("DK::register_client:Client alreay in use \n");
		return -EACCES;
	}
#if !defined (P1020)	
	// check whether the device is present
	// by reading the vendor id
#if defined(OWL_PB42) || defined(PYTHON_EMU)
#ifdef WASP_OSPREY
   if(cli_id!=0){ // For DBDC operation, Wasp radio's client ID is zero; 
#endif
	cli_cfg_read(cli_id,0,4,&vendor_id);
	if ((vendor_id & 0xffff) != ATHEROS_VENDOR_ID) {
		printk("DK::Device not present \n");
	 	return -ENODEV;
	}

#ifdef WASP_OSPREY
   }
#endif
#ifndef PYTHON_EMU
	if (bus_dev_init(dev->bus_dev) < 0) {
		printk("DK::register_client:Cannot initialize client \n");
		return -EACCES;
	}
#endif
#endif
#endif
	initEventQueue(&dev->isr_event_q);
	initEventQueue(&dev->trigered_event_q);
	
	reset_device(cli_id);
					
	dev->dev_busy = 1;
				  
	return dev->cli_id;
}