示例#1
0
int  hash_tbl_add (uint8_t *key, HASH_TABLE *htbl, void *data)
{
    if (hash_add_entry (key , htbl, data) < 0) {
        return -1;
    }
    return 0;
}
示例#2
0
文件: main.c 项目: zhanjl/Myftpserver
int main(int argc, char *argv[])
{

    if (getuid()) {
        fprintf(stderr, "the ftpserver must started by root\n");
        exit(EXIT_FAILURE); 
    }

    if (signal(SIGCHLD, handle_sigchld) == SIG_ERR)
        ERR_EXIT("signal");
    parse_load_file("ftpserver.conf");
    printf("parse_load_file success\n");
//    printconfig();
    
    ip_to_clients = hash_alloc(256, hash_func);
    pid_to_ip = hash_alloc(256, hash_func);
    int listenfd; 
    listenfd = tcp_server(listen_address, listen_port);
    
    printf("tcp_server success, listenfd = %d\n", listenfd);
    int connfd;
    pid_t   pid;
    session_t sess;
    while (1) {
        struct sockaddr_in addr;
        connfd = accept_time_out(listenfd, &addr, accept_timeout);
        if (connfd == -1) {
        //    printf("don't has connection in %d seconds\n", accept_timeout);
            continue;
        }
        
        uint32_t ip = addr.sin_addr.s_addr;
        printf("connect success\n");
        session_init(&sess);
        num_of_clients++;
        sess.curr_clients = num_of_clients;
        sess.curr_ip_clients = add_ip_to_hash(ip_to_clients, ip);
        sess.ip = ip;
        p_sess = &sess;
        pid = fork();
        if (pid == -1) {
            ERR_EXIT("fork error");
        } else if (pid == 0) {   //子进程
            close(listenfd); 
            sess.peerfd = connfd;
            session_begin(&sess);   //建立一个会话
            exit(EXIT_SUCCESS);
        } else {                //父进程
            close(connfd);
            hash_add_entry(pid_to_ip, &pid, sizeof(pid), &ip, sizeof(ip));
        }
    }
    return 0;
}
示例#3
0
/* Entry is eaten.
 * No check is done if entry->tuple is a member of sym->set !
 * This has to be done before.
 */
void symbol_add_entry(Symbol* sym, Entry* entry)
{
   const Tuple* tuple;
   
   assert(symbol_is_valid(sym));
   assert(entry_is_valid(entry));
   
   assert(sym->used <= sym->size);
   
   if (sym->used == sym->size)
   {
      sym->size   += sym->extend;
      sym->extend += sym->extend;
      sym->entry   = realloc(
         sym->entry, (size_t)sym->size * sizeof(*sym->entry));
      
      assert(sym->entry != NULL);
   }
   assert(sym->used < sym->size);

   tuple = entry_get_tuple(entry);

   /* There is no index set for the internal symbol.
    */
   assert(!strcmp(sym->name, SYMBOL_NAME_INTERNAL) || set_lookup(sym->set, tuple));

   if (hash_has_entry(sym->hash, tuple))
   {
      if (stmt_trigger_warning(166))
      {
         fprintf(stderr, "--- Warning 166: Duplicate element ");
         tuple_print(stderr, tuple);
         fprintf(stderr, " for symbol %s rejected\n", sym->name);
      }
      entry_free(entry);
   }
   else
   {
      /* Falls noch nicht geschehen, legen wir hier den Typ des
       * Symbols fest.
       */
      if ((sym->type == SYM_ERR) && (sym->used == 0))
         sym->type = entry_get_type(entry);

      assert(sym->type != SYM_ERR);
      
      hash_add_entry(sym->hash, entry);
      
      sym->entry[sym->used] = entry;      
      sym->used++;
   }
}
示例#4
0
文件: main.c 项目: zhanjl/Myftpserver
unsigned int add_ip_to_hash(hash_t *hash, uint32_t ip)
{
    unsigned int *p_value = (unsigned int*)hash_lookup_value_by_key(hash,&ip,sizeof(ip));
    if (p_value == NULL) {
        unsigned int value = 1;
        hash_add_entry(hash, &ip, sizeof(ip), &value, sizeof(value));
        return 1;
    } else {
        unsigned int value = *p_value;
        value++;
        *p_value = value;
        return value;
    }
}
示例#5
0
文件: parse.c 项目: rfdrake/tac_plus
static void
declare(char *name, int value)
{
    KEYWORD *n;
    KEYWORD *k = (KEYWORD *)tac_malloc(sizeof(KEYWORD));

    k->word = tac_strdup(name);
    k->value = value;

    n = hash_add_entry(wordtable, (void *) k);

    if (n) {
	report(LOG_ERR, "Attempt to multiply define keyword %s", name);
	tac_exit(1);
    }
}
示例#6
0
int main(void)
{
    stu2_t stu_arr[] =
    {
        { 1234, "AAAA", 20 },
        { 4568, "BBBB", 23 },
        { 6729, "AAAA", 19 }
    };

    hash_t *hash = hash_alloc(256, hash_int);

    int size = sizeof(stu_arr) / sizeof(stu_arr[0]);
    int i;
    for (i=0; i<size; i++)
    {
        hash_add_entry(hash, &(stu_arr[i].sno), sizeof(stu_arr[i].sno),
            &stu_arr[i], sizeof(stu_arr[i]));
    }

    int sno = 4568;
    stu2_t *s = (stu2_t *)hash_lookup_entry(hash, &sno, sizeof(sno));
    if (s)
    {
        printf("%d %s %d\n", s->sno, s->name, s->age);
    }
    else
    {
        printf("not found\n");
    }
    
    sno = 1234;
    hash_free_entry(hash, &sno, sizeof(sno));
    s = (stu2_t *)hash_lookup_entry(hash, &sno, sizeof(sno));
    if (s)
    {
        printf("%d %s %d\n", s->sno, s->name, s->age);
    }
    else
    {
        printf("not found\n");
    }

    return 0;
}
示例#7
0
static unsigned int
handle_ip_count(struct vsf_sysutil_ipv4addr* p_accept_addr)
{
    unsigned int* p_count =
        (unsigned int*)hash_lookup_entry(s_p_ip_count_hash, (void*)p_accept_addr);
    unsigned int count;
    if (!p_count)
    {
        count = 1;
        hash_add_entry(s_p_ip_count_hash, (void*)p_accept_addr, (void*)&count);
    }
    else
    {
        count = *p_count;
        count++;
        *p_count = count;
    }
    return count;
}
示例#8
0
static unsigned int
handle_ip_count(void* p_ipaddr)
{
  unsigned int* p_count =
    (unsigned int*)hash_lookup_entry(s_p_ip_count_hash, p_ipaddr);
  unsigned int count;
  if (!p_count)
  {
    count = 1;
    hash_add_entry(s_p_ip_count_hash, p_ipaddr, (void*)&count);
  }
  else
  {
    count = *p_count;
    count++;
    *p_count = count;
  }
  return count;
}
示例#9
0
文件: main.c 项目: JnuSimba/UNP
unsigned int handle_ip_count(void *ip)
{
	unsigned int count;
	unsigned int* ip_count;
	ip_count = hash_lookup_entry(s_ip_count_hash, ip, sizeof(unsigned int));

	if (ip_count == NULL)
	{
		count = 1;
		hash_add_entry(s_ip_count_hash, ip, sizeof(unsigned int), &count, sizeof(unsigned int));
	}
	else
	{
		count = *ip_count;
		count++;
		*ip_count = count;
	}

	return count;
}
示例#10
0
struct vsf_client_launch
vsf_standalone_main(void)
{
  struct vsf_sysutil_sockaddr* p_sockaddr = 0;
  struct vsf_sysutil_ipv4addr listen_ipaddr;
  int listen_sock = vsf_sysutil_get_ipv4_sock();
  int retval;
  s_p_ip_count_hash = hash_alloc(256, sizeof(struct vsf_sysutil_ipv4addr),
                                 sizeof(unsigned int), hash_ip);
  s_p_pid_ip_hash = hash_alloc(256, sizeof(int),
                               sizeof(struct vsf_sysutil_ipv4addr), hash_pid);
  if (tunable_setproctitle_enable)
  {
    vsf_sysutil_setproctitle("LISTENER");
  }
  vsf_sysutil_install_sighandler(kVSFSysUtilSigCHLD, handle_sigchld, 0);
  vsf_sysutil_install_async_sighandler(kVSFSysUtilSigHUP, handle_sighup);

  vsf_sysutil_activate_reuseaddr(listen_sock);
  vsf_sysutil_sockaddr_alloc_ipv4(&p_sockaddr);
  vsf_sysutil_sockaddr_set_port(
      p_sockaddr, vsf_sysutil_ipv4port_from_int(tunable_listen_port));
  if (!tunable_listen_address ||
      vsf_sysutil_inet_aton(tunable_listen_address, &listen_ipaddr) == 0)
  {
    listen_ipaddr = vsf_sysutil_sockaddr_get_any();
  }
  vsf_sysutil_sockaddr_set_ipaddr(p_sockaddr, listen_ipaddr);
  retval = vsf_sysutil_bind(listen_sock, p_sockaddr);
  
  vsf_sysutil_free(p_sockaddr);

  if (vsf_sysutil_retval_is_error(retval))
  {
    die("could not bind listening socket");
  }
  vsf_sysutil_listen(listen_sock, VSFTP_LISTEN_BACKLOG);

  while (1)
  {
    struct vsf_client_launch child_info;
    static struct vsf_sysutil_sockaddr* p_accept_addr;
    int new_child;
    struct vsf_sysutil_ipv4addr ip_addr;
    /* NOTE - wake up every 10 seconds to make sure we notice child exit
     * in a timely manner (the sync signal framework race)
     */
    int new_client_sock = vsf_sysutil_accept_timeout(
        listen_sock, &p_accept_addr, 10);
    if (s_reload_needed)
    {
      s_reload_needed = 0;
      do_reload();
    }
    if (vsf_sysutil_retval_is_error(new_client_sock))
    {
      continue;
    }
    ip_addr = vsf_sysutil_sockaddr_get_ipaddr(p_accept_addr);
    ++s_children;
    child_info.num_children = s_children;
    child_info.num_this_ip = handle_ip_count(&ip_addr);
    new_child = vsf_sysutil_fork_failok();
    if (new_child != 0)
    {
      /* Parent context */
      vsf_sysutil_close(new_client_sock);
      if (new_child > 0)
      {
        hash_add_entry(s_p_pid_ip_hash, (void*)&new_child, (void*)&ip_addr);
      }
      else
      {
        /* fork() failed, clear up! */
        --s_children;
        drop_ip_count(&ip_addr);
      }
      /* Fall through to while() loop and accept() again */
    }
    else
    {
      /* Child context */
      vsf_sysutil_close(listen_sock);
      prepare_child(new_client_sock);
      /* By returning here we "launch" the child process with the same
       * contract as xinetd would provide.
       */
      return child_info;
    }
  }
}
示例#11
0
struct vsf_client_launch
vsf_standalone_main(void)
{
    struct vsf_sysutil_sockaddr* p_accept_addr = 0;
    int listen_sock = -1;
    int retval;
    s_ipaddr_size = vsf_sysutil_get_ipaddr_size();
    if (tunable_listen && tunable_listen_ipv6)
    {
        die("run two copies of vsftpd for IPv4 and IPv6");
    }
    if (tunable_background)
    {
        int forkret = vsf_sysutil_fork();
        if (forkret > 0)
        {
            /* Parent, just exit */
            vsf_sysutil_exit(0);
        }
        vsf_sysutil_make_session_leader();
    }
    if (tunable_listen)
    {
        listen_sock = vsf_sysutil_get_ipv4_sock();
    }
    else
    {
        listen_sock = vsf_sysutil_get_ipv6_sock();
    }
    vsf_sysutil_activate_reuseaddr(listen_sock);

    s_p_ip_count_hash = hash_alloc(256, s_ipaddr_size,
                                   sizeof(unsigned int), hash_ip);
    s_p_pid_ip_hash = hash_alloc(256, sizeof(int),
                                 s_ipaddr_size, hash_pid);
    if (tunable_setproctitle_enable)
    {
        vsf_sysutil_setproctitle("LISTENER");
    }
    vsf_sysutil_install_async_sighandler(kVSFSysUtilSigCHLD, handle_sigchld);
    vsf_sysutil_install_async_sighandler(kVSFSysUtilSigHUP, handle_sighup);
    if (tunable_listen)
    {
        struct vsf_sysutil_sockaddr* p_sockaddr = 0;
        vsf_sysutil_sockaddr_alloc_ipv4(&p_sockaddr);
        vsf_sysutil_sockaddr_set_port(p_sockaddr, tunable_listen_port);
        if (!tunable_listen_address)
        {
            vsf_sysutil_sockaddr_set_any(p_sockaddr);
        }
        else
        {
            if (!vsf_sysutil_inet_aton(tunable_listen_address, p_sockaddr))
            {
                die2("bad listen_address: ", tunable_listen_address);
            }
        }
        retval = vsf_sysutil_bind(listen_sock, p_sockaddr);
        vsf_sysutil_free(p_sockaddr);
        if (vsf_sysutil_retval_is_error(retval))
        {
            die("could not bind listening IPv4 socket");
        }
    }
    else
    {
        struct vsf_sysutil_sockaddr* p_sockaddr = 0;
        vsf_sysutil_sockaddr_alloc_ipv6(&p_sockaddr);
        vsf_sysutil_sockaddr_set_port(p_sockaddr, tunable_listen_port);
        if (!tunable_listen_address6)
        {
            vsf_sysutil_sockaddr_set_any(p_sockaddr);
        }
        else
        {
            struct mystr addr_str = INIT_MYSTR;
            const unsigned char* p_raw_addr;
            str_alloc_text(&addr_str, tunable_listen_address6);
            p_raw_addr = vsf_sysutil_parse_ipv6(&addr_str);
            str_free(&addr_str);
            if (!p_raw_addr)
            {
                die2("bad listen_address6: ", tunable_listen_address6);
            }
            vsf_sysutil_sockaddr_set_ipv6addr(p_sockaddr, p_raw_addr);
        }
        retval = vsf_sysutil_bind(listen_sock, p_sockaddr);
        vsf_sysutil_free(p_sockaddr);
        if (vsf_sysutil_retval_is_error(retval))
        {
            die("could not bind listening IPv6 socket");
        }
    }
    vsf_sysutil_listen(listen_sock, VSFTP_LISTEN_BACKLOG);
    vsf_sysutil_sockaddr_alloc(&p_accept_addr);
    while (1)
    {
        struct vsf_client_launch child_info;
        void* p_raw_addr;
        int new_child;
        int new_client_sock;
        vsf_sysutil_unblock_sig(kVSFSysUtilSigCHLD);
        vsf_sysutil_unblock_sig(kVSFSysUtilSigHUP);
        new_client_sock = vsf_sysutil_accept_timeout(
                              listen_sock, p_accept_addr, 0);
        vsf_sysutil_block_sig(kVSFSysUtilSigCHLD);
        vsf_sysutil_block_sig(kVSFSysUtilSigHUP);
        if (vsf_sysutil_retval_is_error(new_client_sock))
        {
            continue;
        }
        ++s_children;
        child_info.num_children = s_children;
        child_info.num_this_ip = 0;
        p_raw_addr = vsf_sysutil_sockaddr_get_raw_addr(p_accept_addr);
        child_info.num_this_ip = handle_ip_count(p_raw_addr);
        new_child = vsf_sysutil_fork_failok();
        if (new_child != 0)
        {
            /* Parent context */
            vsf_sysutil_close(new_client_sock);
            if (new_child > 0)
            {
                hash_add_entry(s_p_pid_ip_hash, (void*)&new_child, p_raw_addr);
            }
            else
            {
                /* fork() failed, clear up! */
                --s_children;
                drop_ip_count(p_raw_addr);
            }
            /* Fall through to while() loop and accept() again */
        }
        else
        {
            /* Child context */
            vsf_sysutil_close(listen_sock);
            prepare_child(new_client_sock);
            /* By returning here we "launch" the child process with the same
             * contract as xinetd would provide.
             */
            return child_info;
        }
    }
}
示例#12
0
文件: main.c 项目: JnuSimba/UNP
int main(void)
{

	daemon(1, 0);

	parseconf_load_file(MINIFTPD_CONF);

	if (getuid() != 0)
	{
		fprintf(stderr, "must be started by root.\n");
		exit(EXIT_FAILURE);
	}


	int listenfd = tcp_server(tunable_listen_address, tunable_listen_port);
	int conn;
	session_t sess =
	{
		//控制连接
		0, -1, "", "", "",

		// ftp协议进程与nobody进程通信
		-1, -1,
		
		//限速
		0, 0, 0, 0,
		// 数据连接
		NULL, -1, -1, 0,

		// ftp协议控制
		0, 0, NULL, 0,

		//客户端连接数控制
		0, 0
	};

	p_sess = &sess;
	sess.upload_speed_max = tunable_upload_max_rate;
	sess.download_speed_max = tunable_download_max_rate;

	signal(SIGCHLD, handle_sigchld);

	struct sockaddr_in peeraddr;
	
	s_ip_count_hash = hash_alloc(256, hash_func);
	s_pid_ip_hash = hash_alloc(256, hash_func);

	while(1)
	{
		if ((conn = accept_timeout(listenfd, &peeraddr, 0)) < 0)
			ERR_EXIT("accept_timeout");
		
		unsigned int ip = (unsigned int)peeraddr.sin_addr.s_addr;
	
		sess.num_this_ip = handle_ip_count(&ip);

		++cur_childrens;
		sess.num_clients = cur_childrens;
		

		pid_t pid = fork();
		if (pid == -1)
		{
			--cur_childrens;
			ERR_EXIT("fork");
		}

		if (pid == 0)
		{
			sess.ctrl_fd = conn;
			close(listenfd);
			check_clients_limit(&sess);
			signal(SIGCHLD, SIG_IGN);
			begin_session(&sess);
		}
		else if (pid > 0)
		{
			hash_add_entry(s_pid_ip_hash, &pid, sizeof(pid), &ip, sizeof(unsigned int));
			close(conn);
		}
	}

	hash_free(s_ip_count_hash);
	hash_free(s_pid_ip_hash);

	return 0;
}
int main()
{
     stu_t stu_arr[]={
		 {"1","A",20},
		 {"2","B",21},
		 {"3","C",22}
	 };
  hash_t *hash=hash_alloc(256,hash_function);
  int size = sizeof(stu_arr)/sizeof(stu_arr[0]);
  /*用字符串作为关键码
  int i=0;
   for(;i<size;++i)
	{
        hash_add_entry(hash,stu_arr[i].num,strlen(stu_arr[i].num),&stu_arr[i],sizeof(stu_arr[i]));  
    }
	stu_t *s = (stu_t*)hash_lookuo_enty(hash,"1",1);
      if(s != NULL)
	 {
	  printf("%s   %s  %d\n",s->num,s->name,s->age);
	 }
	 else
	{
	 printf("not found\n");
	 }
     hash_free_entry(hash,"1",1);
	 s = (stu_t*)hash_lookuo_enty(hash,"1",1);
      if(s != NULL)
	 {
	  printf("%s   %s  %d\n",s->num,s->name,s->age);
	 }
	 else
	{
	 printf("not found\n");
	 }
*/
//用年龄作为关键码 (用整数作为关键码)
int i=0;
   for(;i<size;++i)
	{
        hash_add_entry(hash,&stu_arr[i].age,sizeof(stu_arr[i].age),&stu_arr[i],sizeof(stu_arr[i]));  
    }
	int key=20;
	stu_t *s = (stu_t*)hash_lookuo_enty(hash,&key,4);
      if(s != NULL)
	 {
	  printf("%s   %s  %d\n",s->num,s->name,s->age);
	 }
	 else
	{
	 printf("not found\n");
	 }
     hash_free_entry(hash,&key,4);
	 s = (stu_t*)hash_lookuo_enty(hash,&key,4);
      if(s != NULL)
	 {
	  printf("%s   %s  %d\n",s->num,s->name,s->age);
	 }
	 else
	{
	 printf("not found\n");
	 }

	 hash_dealloc(hash,256);
    return 0;
}
示例#14
0
void add_pid_ip_to_hash(pid_t pid, uint32_t ip)
{
	hash_add_entry(pid_to_ip, &pid, sizeof(pid), &ip, sizeof(ip));
}