Пример #1
0
void
list (int flag, int non_priv)
{
  FILE *in = NULL;
  FILE *out = NULL;
  int socket = -1;
  if (opensock (&socket, &in, &out))
    {
      client_type_t ctt = DBCTL;
      fwrite (&ctt, sizeof (client_type_t), 1, out);
      fflush (out);

      DbCtlCommand command = { 0 };
      command.command = LIST;
      strcpy (command.parameter, "");
      strcpy (command.options.username, "");
      command.options.cpu = 0;
      command.options.level = 0;
      command.options.read = 0;
      command.options.write = 0;
      command.options.timeout = 0;
      command.options.user_max_connections = 0;

      fwrite_wrapper (&command, sizeof (DbCtlCommand), 1, out);
      fflush (out);

      print_list (in, flag, non_priv);
      closesock (socket, in, out);
    }
  else
    {

      closesock (socket, in, out);
    }
}
Пример #2
0
void readsock(fd_set *fdset,unsigned char *tmp_buf,int tmp_sze) {
  /* reads in a message from a client */         
  int i;
  int byte;

  for (i=0;i<msgmax;i++) 
    if ( (client[i].sock !=-1) && 
         (FD_ISSET(client[i].sock,fdset))) {
         byte=read(client[i].sock,tmp_buf,tmp_sze);
     
         if (byte ==0) closesock(i);
         else if ((byte <0) && (errno !=EAGAIN)) closesock(i);
         else if (byte > 0) {
           if (byte+client[i].in_sze > BUF_SIZE) {
             int delta;
             delta=byte+client[i].in_sze-BUF_SIZE;
             memmove(client[i].in_buf,client[i].in_buf+delta,
                   client[i].in_sze-delta);
             client[i].out_sze=BUF_SIZE-byte;
           } 
           memcpy(client[i].in_buf+client[i].in_sze,tmp_buf,byte);
           client[i].in_sze+=byte;
	 }
    }
}
Пример #3
0
int
main(int argc, char *argv[])
{
	const char *sockname = DEFAULT_SOCKET;
	int ch;

	while ((ch = getopt(argc, argv, ""))!=-1) {
		switch (ch) {
		    default: usage();
		}
	}
	if (optind < argc) {
		sockname = argv[optind++];
	}
	if (optind < argc) {
		usage();
	}

	senders = array_create();
	if (!senders) {
		fprintf(stderr, "hub161: Out of memory\n");
		exit(1);
	}

	opensock(sockname);
	printf("hub161: Listening on %s\n", sockname);
	loop();
	closesock();

	return 0;
}
Пример #4
0
// close sockets and file, if open
void
NetDCC::dcc_stop (void)
{
    if (status == DCC_CHAT)			// if it was a dcc chat
    {
        status = DCC_STOP; // must be changed right now to avoid a possible loop
        s->script.send_partyline ("%s left the partyline.", nick);
    }
    if (socklocal != -1)
    {
#ifdef WINDOZE
        closesocket (socklocal);
#else		// !WINDOZE
        close (socklocal);
#endif		// !WINDOZE
        socklocal = -1;
    }
    closesock ();
    if (dfd != NULL)
    {
        fclose (dfd);
        dfd = NULL;
    }
    filesent = 0;
    status = DCC_STOP;
}
Пример #5
0
void data_sending_handler()
{
	int i;
      	for(i = 0; i < cnt;i++) {
           if ( *(sockid[i]) != -1 && (send(*(sockid[i]), buffer, SIZE, 0) <= 0)   ) {
               closesock(sockid[i],i);
               printf("close sock\n : %d",*sockid[i]);
            }
	}
}
Пример #6
0
void dim_tcpip_stop()
{
#ifdef WIN32
	closesock(DIM_IO_path[0]);
#else
	close(DIM_IO_path[0]);
	close(DIM_IO_path[1]);
#endif
	DIM_IO_path[0] = -1;
	DIM_IO_path[1] = -1;
	DIM_IO_Done = 0;
	init_done = 0;
}
Пример #7
0
/*创建tcp服务*/
int cretcpser(const char *ip, const int port, const int backlog)
{
	int domain = AF_INET;//AF_INET6 目前没有使用IPV6
	int socktype = SOCK_STREAM;
	int protocol = 0;
	
	int sockid = sock(domain, socktype, protocol);
	if (sockid < 0)
	{
		debuginfo("cretcpser->socket failed");
		return -1;
	}

	int flags = 1;
	struct linger linger = {0, 0};
	setsocketopt(sockid, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags));
	setsocketopt(sockid, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
	setsocketopt(sockid, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags));
//	setsocketopt(sockid, IPPROTO_IP, TCP_NODELAY, (void *)&flags, sizeof(flags));
	
	struct sockaddr_in sockaddr;
	memset(&sockaddr, 0, sizeof(struct sockaddr_in));
	setsockaddrin(&sockaddr, domain, port, ip);
	if (bindsock(sockid, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr_in)) != 0)
	{
		debuginfo("cretcpser->bindsock failed %d", errno);
		closesock(sockid);
		return -1;
	}
	
	if (listensock(sockid, backlog) == -1)
	{
		debuginfo("cretcpser->listensock failed");
		closesock(sockid);
		return -1;
	}
	
	return sockid;
}
Пример #8
0
int writesock() {
    int i;
    int byte,cnt=0;
    for (i=0; i<msgmax; i++)
        if ((client[i].sock !=-1) && (client[i].out_sze !=0)) {
            byte=write(client[i].sock,client[i].out_buf,client[i].out_sze);

            if (byte <0) {
                if (errno==EAGAIN) continue;
                else closesock(i);
            } else if (byte >0) {
                memmove(client[i].out_buf,client[i].out_buf+byte,
                        client[i].out_sze-byte);
                client[i].out_sze-=byte;
                if (client[i].out_sze >0) cnt++;
            }
        }
    return cnt; /* returns number of clients with outstanding data */
}
Пример #9
0
static int enable_sig(int conn_id)
{
	int ret = 1, flags = 1;
#ifndef WIN32
	int pid;
#endif

#ifdef DEBUG
	if(!Net_conns[conn_id].channel)
	{
	    printf("Enabling signals on channel 0\n");
	    fflush(stdout);
	}
#endif

	if(!init_done)
	{
		dim_tcpip_init(0);
	}
	if(Threads_on)
	{
#ifdef WIN32
		DIM_IO_valid = 0;
/*
		ret = connect(DIM_IO_path[0], (struct sockaddr*)&DIM_sockname, sizeof(DIM_sockname));
*/
		closesock(DIM_IO_path[0]);
		DIM_IO_path[0] = -1;
		if( (DIM_IO_path[0] = (int)socket(AF_INET, SOCK_STREAM, 0)) == -1 ) 
		{
			perror("socket");
			return(1);
		}		
		ret = ioctl(DIM_IO_path[0], FIONBIO, &flags);
		if(ret != 0)
		{
			perror("ioctlsocket");
		}
		DIM_IO_valid = 1;
#else
		if(DIM_IO_path[1] != -1)
		{
			if(!DIM_IO_Done)
			{
				DIM_IO_Done = 1;
				write(DIM_IO_path[1], &flags, 4);
			}
		}
#endif
	}
#ifndef WIN32
	if(!Threads_on)
	{
	    pid = getpid();

#ifndef __linux__
		ret = ioctl(Net_conns[conn_id].channel, SIOCSPGRP, &pid );
#else
	    ret = fcntl(Net_conns[conn_id].channel,F_SETOWN, pid);
#endif
	    if(ret == -1)
	    {
#ifdef DEBUG
	        printf("ioctl returned -1\n");
#endif
			return(ret);
	    }
	}
	ret = ioctl(Net_conns[conn_id].channel, FIOASYNC, &flags );
	if(ret == -1)
	{
#ifdef DEBUG
		printf("ioctl1 returned -1\n");
#endif
		return(ret);
	}
	
    flags = fcntl(Net_conns[conn_id].channel,F_GETFD,0);
#ifdef DEBUG
    if(flags == -1)
    {
		printf("error\n");
    }
#endif
    ret = fcntl(Net_conns[conn_id].channel,F_SETFD, flags | FD_CLOEXEC );
    if(ret == -1)
    {
#ifdef DEBUG
		printf("ioctl2 returned -1\n");
#endif
		return(ret);
    }
#endif
	return(1);
}
Пример #10
0
static bool spawn_worker_thread(const _TCHAR *ip, socket_t sockfd, dyarr_t workers)
{
    size_t slot = INVALID_SOCKET;
    size_t i;
    worker_context_t *ctx;
    HANDLE thread;
    unsigned thread_id;

    assert(ip != NULL);
    assert(sockfd != INVALID_SOCKET);
    assert(workers != NULL);

    for (i = 0; i < dyarr_size(workers); ++i)
    {
        worker_context_t *elem = dyarr_at(workers, i);
        if (is_idle_worker_context_slot(elem, NULL))
        {
            if (elem->handle != NULL)
            {
                printf("Waiting for worker thread exiting...\n");
                WaitForSingleObject(elem->handle, INFINITE);
                CloseHandle(elem->handle);
                elem->handle = NULL;
            }

            if (elem->sockfd != INVALID_SOCKET)
            {
                closesock(elem->sockfd);
                elem->sockfd = INVALID_SOCKET;
            }

            slot = i;
            break;
        }
    }

    if (slot == INVALID_INDEX)
    {
        if (!dyarr_resize(workers, dyarr_size(workers) + 1))
        {
            fprintf(stderr, "No enough memory!\n");
            return false;
        }

        slot = dyarr_size(workers) - 1;
    }

    ctx = dyarr_at(workers, slot);

    thread = (HANDLE)_beginthreadex(
        NULL,
        0,
        (_beginthreadex_proc_type)worker_routine,
        ctx,
        CREATE_SUSPENDED,
        &thread_id);
    if (thread == NULL)
    {
        fprintf(stderr, "Failed to create worker thread!\n");
        return false;
    }

    ctx->handle = thread;
    ctx->sockfd = sockfd;
    ctx->running = 1;
    _tcscpy(ctx->ipstr, ip);

    printf("Starting working thread [%u]...\n", thread_id);
    printf("Now we have %zu worker threads.\n", dyarr_size(workers));

    ResumeThread(thread);

    return true;
}
Пример #11
0
int start_server(socket_t sockfd)
{
    struct sockaddr_storage client_addr;
    socklen_t sin_size;
    _TCHAR ipstr[INET6_ADDRSTRLEN];
    socket_t client_sockfd;
    dyarr_t workers = dyarr_new(sizeof(worker_context_t), false, true);
    size_t i;
    int error_code = EXIT_SUCCESS;

    assert(sockfd != INVALID_SOCKET);

    if (workers == NULL)
    {
        fprintf(stderr, "No enough memory!\n");
        return EXIT_FAILURE;
    }

    while (true)
    {
        sin_size = sizeof client_addr;
        client_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
        if (client_sockfd == INVALID_SOCKET)
        {
            print_error_ex(_T("accept"));
            continue;
        }

        if (inet_ntop(client_addr.ss_family,
            get_in_addr((struct sockaddr *)&client_addr),
            ipstr, sizeof(ipstr) / sizeof(ipstr[0])) != NULL)
        {
            _ftprintf(stdout, _T("Got connection from: %s.\n"), ipstr);
            if (!spawn_worker_thread(ipstr, client_sockfd, workers))
            {
                error_code = EXIT_FAILURE;
                break;
            }
        }
        else
        {
            fprintf(stderr, "inet_ntop failed.\n");
        }
    }

    for (i = 0; i < dyarr_size(workers); ++i)
    {
        worker_context_t *ctx = dyarr_at(workers, i);

        if (ctx->handle != NULL)
        {
            InterlockedExchange(&(ctx->running), 0);

            printf("Waiting for worker thread exiting...\n");
            WaitForSingleObject(ctx->handle, INFINITE);
            CloseHandle(ctx->handle);
            ctx->handle = NULL;
        }

        if (ctx->sockfd != INVALID_SOCKET)
        {
            closesock(ctx->sockfd);
            ctx->sockfd = INVALID_SOCKET;
        }
    }

    dyarr_free(&workers);

    return error_code;
}
Пример #12
0
int main(int argc, char *argv[]) {
	static struct sigaction act;

	act.sa_handler = get_signal;
	sigfillset(&(act.sa_mask));
	sigaction(SIGSEGV, &act, NULL);

	int no_curses = 0;

	extern char *optarg;
	extern int optind, optopt;
	int c, refresh_tmp_time = 0;
	char *endptr;
	while ((c = getopt(argc, argv, ":r:ch")) != -1) {
		switch (c) {
		case 'r':
			refresh_tmp_time = (int) strtol(optarg, &endptr, 10);
			if ((refresh_tmp_time) && (refresh_tmp_time < 30)) {
				refresh_time = refresh_tmp_time * 1000;
			}
			break;
		case 'c':
			no_curses = 1;
			break;
		case 'h':
			printf("Usage: dbtop [parameters]\n");
			printf("no parameters - standart ncurses mode\n\n");
			printf("parameters:\n");
			printf("-r interval - refresh interval for ncurses mode(in seconds)\n");
			printf("-c - show one time users list (no ncurses)\n");
			exit(0);
			break;
		}
	}
	_socket = connect_to_server_dbtop();
	in = fdopen(_socket, "r+");
	out = fdopen(_socket, "w");
	if (!in || !out) {
		printf("Can't connect to socket. Maybe governor is not started\n");
		exit(-1);
	}
	if (no_curses) {
		client_type_t ctt = DBTOPCL;
		fwrite(&ctt, sizeof(client_type_t), 1, out);
		fflush(out);
		accounts = NULL;
		recv_accounts = NULL;
		read_info();
		printOneScreenNoCurses();
		exit(0);
	}
	
///    
    /*client_type_t ctt = DBTOP;
	fwrite(&ctt, sizeof(client_type_t), 1, out);
	fflush(out);*/
	accounts = NULL;
	recv_accounts = NULL;
	initscr();
	
	signal(SIGALRM, end_pgm);
	signal(SIGHUP, end_pgm);
	signal(SIGPIPE, end_pgm);
	signal(SIGQUIT, end_pgm);
	signal(SIGTERM, end_pgm);
	signal(SIGINT, end_pgm);
	
	noecho();
	nonl();
	intrflush(stdscr, false);
	keypad(stdscr, true);
	curs_set(0);
	if (has_colors()) {
		start_color();
		use_default_colors();
		init_pair(1, COLOR_GREEN, COLOR_BLUE);
		init_pair(2, COLOR_BLUE, -1);
		init_pair(3, COLOR_RED, -1);
	}
	raw();
	halfdelay(5);
    
    read_keys();
	closesock();
}