Ejemplo n.º 1
0
int		AcServerHandle::Accept()
{
    //static socklen_t	s_addr_len = sizeof(sockaddr_in);
    int client_fd;
    sockaddr_in	addr;
    client_fd = net_accept(fd_, &addr);
    if(client_fd < 1) {
        ERROR_OUT("Failed to accept");
        return -1;
    }

    DEBUG_MSG("Accept fd: %d", client_fd);
    AcHandle	*handle	= new AcConnectHandle;
    if(NULL == handle) {
        ERROR_OUT("Failed to allocate memory");
        close(client_fd);
        return -1;
    }

    assert(dispatcher_);

    handle->set_fd(client_fd);
    handle->set_address(addr);
    handle->set_dispatcher(dispatcher_);

    dispatcher_->AddEvent(handle,EVENT_READ);

    return 0;
}
Ejemplo n.º 2
0
/* input function: DCC SEND - someone tried to connect to our socket */
static void dcc_send_connected(SEND_DCC_REC *dcc)
{
        GIOChannel *handle;
	IPADDR addr;
	int port;

	/* accept connection */
	handle = net_accept(dcc->handle, &addr, &port);
	if (handle == NULL)
		return;

	/* TODO: some kind of paranoia check would be nice. it would check
	   that the host of the nick who we sent the request matches the
	   address who connected us. */

	net_disconnect(dcc->handle);
	g_source_remove(dcc->tagconn);
        dcc->tagconn = -1;

	dcc->starttime = time(NULL);
	dcc->handle = handle;
	memcpy(&dcc->addr, &addr, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;

	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_send_read_size, dcc);
	dcc->tagwrite = g_input_add(handle, G_INPUT_WRITE,
				    (GInputFunction) dcc_send_data, dcc);

	signal_emit("dcc connected", 1, dcc);
}
Ejemplo n.º 3
0
/* input function: DCC CHAT - someone tried to connect to our socket */
static void dcc_chat_listen(CHAT_DCC_REC *dcc)
{
	IPADDR ip;
        GIOChannel *handle;
	int port;

	g_return_if_fail(IS_DCC_CHAT(dcc));

	/* accept connection */
	handle = net_accept(dcc->handle, &ip, &port);
	if (handle == NULL)
		return;

	/* TODO: add paranoia check - see dcc-files.c */

	net_disconnect(dcc->handle);
	g_source_remove(dcc->tagconn);
	dcc->tagconn = -1;

	dcc->starttime = time(NULL);
	dcc->handle = handle;
	dcc->sendbuf = net_sendbuffer_create(handle, 0);
	memcpy(&dcc->addr, &ip, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;
	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_chat_input, dcc);

	signal_emit("dcc connected", 1, dcc);
}
Ejemplo n.º 4
0
int check_for_clients(IRCD *ircd)
{
	fd_set rfds;
	struct timeval tv;
	int retval;

	/* Watch stdin (fd 0) to see when it has input. */
	FD_ZERO(&rfds);
	FD_SET(ircd->socks.ircd.fd, &rfds);

	/* Wait up to one second. */
	tv.tv_sec = 1;
	tv.tv_usec = 0;

	retval = select(1, &rfds, NULL, NULL, &tv);
	if (retval == -1) {
		fprintf(stderr, "[check_for_clients] select()\n");
	} else if (retval == 0) {
		printf("[check_for_clients] Timeout occured, no new connection.\n");
	} else {
		printf("[check_for_clients] Client is available now.\n");
	}

	if ( net_accept(ircd->socks.ircd, ircd->users[ircd->numusers].sock) != -1 ) {
		++ircd->numusers;
		printf("[check_for_clients] At %d users. [%d]\n", ircd->numusers, ircd->users[ircd->numusers].sock.fd);
	}
	return 0;
}
Ejemplo n.º 5
0
static void sig_botnet_listen(BOTNET_REC *botnet)
{
	BOT_CONNECT_REC *rec;
	IPADDR ip;
	int handle;

	g_return_if_fail(botnet != NULL);

	/* accept connection */
	handle = net_accept(botnet->listen_handle, &ip, NULL);
	if (handle == -1)
		return;

	rec = g_new0(BOT_CONNECT_REC, 1);
	rec->botnet = g_strdup(botnet->name);
	memcpy(&rec->ip, &ip, sizeof(IPADDR));
        rec->handle = handle;

	if (!net_gethostbyaddr_nonblock(&ip, (NET_HOST_CALLBACK) sig_host_got, rec)) {
		/* failed for some reason, try without host */
		connect_downlink(botnet, handle, &ip, NULL);
                g_free(rec->botnet);
		g_free(rec);
	}
}
Ejemplo n.º 6
0
            static
            void *
            proto_server_event_listen(void *arg)
            {
              int fd = Proto_Server.EventListenFD;
              int connfd;

              if (net_listen(fd)<0) {
                exit(-1);
              }

              for (;;) {
                connfd = net_accept(fd);
                if (connfd < 0) {
                  fprintf(stderr, "Error: EventListen accept failed (%d)\n", errno);
                } else {
                  int i;
                  fprintf(stderr, "EventListen: connfd=%d -> ", connfd);

                  if (proto_server_record_event_subscriber(connfd, &i) <0) {
            	fprintf(stderr, "oops no space for any more event subscribers\n");
            	close(connfd);
                  } else {
                  }
                } 
              }
            }
Ejemplo n.º 7
0
int
main(int argc, char **argv)
{
  int listenfd, port=0;
  long connfd;
  pthread_t tid;

  bzero(&globals, sizeof(globals));

  if (net_setup_listen_socket(&listenfd,&port)!=1) {
    fprintf(stderr, "net_setup_listen_socket FAILED!\n");
    exit(-1);
  }

  printf("listening on port=%d\n", port);

  if (net_listen(listenfd) < 0) {
    fprintf(stderr, "Error: server listen failed (%d)\n", errno);
    exit(-1);
  }

  for (;;) {
    connfd = net_accept(listenfd);
    if (connfd < 0) {
      fprintf(stderr, "Error: server accept failed (%d)\n", errno);
    } else {
      //EXPLAIN WHAT IS HAPPENING HERE IN YOUR LOG
      pthread_create(&tid, NULL, &doit, (void *)connfd);
    }
  }

  VPRINTF("Exiting\n");
}
Ejemplo n.º 8
0
void network_init()
{
	struct sockaddr_in my_name;

	my_name.sin_family = AF_INET;
	my_name.sin_port = htons(SERVER_PORT);
	my_name.sin_addr.s_addr = htonl(INADDR_ANY);

	net_init();

	server_socket = net_socket(AF_INET, SOCK_STREAM, 0);
	int yes = 1;
	net_setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

	while(net_bind(server_socket, (struct sockaddr*)&my_name, sizeof(my_name)) < 0)
	{
	}

	net_listen(server_socket, 0);

	struct sockaddr_in client_info;
	socklen_t ssize = sizeof(client_info);
	client_socket = net_accept(server_socket, (struct sockaddr*)&client_info, &ssize);

	network_printf("Hello world!\n");
}
Ejemplo n.º 9
0
static void accept_new(int accept_fd)
{
	int from, to, feid, teid;

	while (1) {
		from = net_accept(cos_spd_id(), accept_fd);
		assert(from != accept_fd);
		if (-EAGAIN == from) {
			return;
		} else if (from < 0) {
			BUG();
			return;
		}
		feid = evt_get();
		assert(feid > 0);
		if (0 < net_accept_data(cos_spd_id(), from, feid)) BUG();

		teid = evt_get();
		assert(teid > 0);
		to = tsplit(cos_spd_id(), td_root, "", 0, TOR_RW, teid);
		if (to < 0) {
			printc("torrent split returned %d", to);
			BUG();
		}

		mapping_add(from, to, feid, teid);
	}
}
Ejemplo n.º 10
0
/* input function: DCC CHAT - someone tried to connect to our socket */
static void dcc_chat_listen(DCC_REC *dcc)
{
	IPADDR ip;
	int handle, port;

	g_return_if_fail(dcc != NULL);

	/* accept connection */
	handle = net_accept(dcc->handle, &ip, &port);
	if (handle == -1)
		return;

	/* TODO: add paranoia check - see dcc-files.c */

	g_source_remove(dcc->tagread);
	close(dcc->handle);

	dcc->starttime = time(NULL);
	dcc->handle = handle;
	memcpy(&dcc->addr, &ip, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;
	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_chat_input, dcc);

	signal_emit("dcc connected", 1, dcc);
}
Ejemplo n.º 11
0
int main(int argc, char *argv[]) {
#ifdef TCP
    int s = net_connect_tcp("0","2334",1);
#else
    int s = net_connect_udp("2334",1);
#endif
    if (s < 0) {
        printf("Failed to serve (%d)\n",s);
        return 0;
    }
    printf("%d serving...\n",s);
#ifdef TCP
    int c = net_accept(s);
    if (c < 0) {
        printf("Client not found (%d)\n",c);
        return 0;
    }
    printf("Connected %d\n",c);
    char *str = net_receive(c);
    if (str == NULL)
        printf("Receive error (%d)\n",errno);
    else
        printf("Received: %s\nSending: %s\n",str,"Goodbye Cruel World");
    char *msg = "Goodbye Cruel World";
    if (net_send(c,msg,strlen(msg)+1) < 0)
        printf("Send error (%d)\n",errno);
    closesocket(c);
#else
    net_bounce(s);
#endif
    closesocket(s);
    printf("Session terminated\n");
}
Ejemplo n.º 12
0
static
void *
proto_server_rpc_listen(void *arg)
{
    int fd = Proto_Server.RPCListenFD;
    unsigned long connfd;
    pthread_t tid;

    if (net_listen(fd) < 0)
    {
        fprintf(stderr, "Error: proto_server_rpc_listen listen failed (%d)\n", errno);
        exit(-1);
    }

    for (;;)
    {
        connfd = net_accept(fd);
        if (connfd < 0)
        {
            fprintf(stderr, "Error: proto_server_rpc_listen accept failed (%d)\n", errno);
        }
        else
        {
            pthread_create(&tid, NULL, &proto_server_req_dispatcher,
                           (void *)connfd);
        }
    }
}
Ejemplo n.º 13
0
/* input function: DCC SERVER - someone tried to connect to our socket */
static void dcc_server_listen(SERVER_DCC_REC *dcc)
{
	SERVER_DCC_REC *newdcc;
	IPADDR ip;
	GIOChannel *handle;
	int port;

	g_return_if_fail(IS_DCC_SERVER(dcc));

	/* accept connection */
	handle = net_accept(dcc->handle, &ip, &port);
	if (handle == NULL)
		return;

	/* Create a new DCC SERVER to handle this connection */
	newdcc = dcc_server_clone(dcc);

	newdcc->starttime = time(NULL);
	newdcc->handle = handle;
	newdcc->sendbuf = net_sendbuffer_create(handle, 0);
	memcpy(&newdcc->addr, &ip, sizeof(IPADDR));
	net_ip2host(&newdcc->addr, newdcc->addrstr);
	newdcc->port = port;
	newdcc->tagread = g_input_add(handle, G_INPUT_READ,
				      (GInputFunction) dcc_server_input, newdcc);

	signal_emit("dcc connected", 1, newdcc);
}
Ejemplo n.º 14
0
/****************************************************************************
 * NetworkWait
 ***************************************************************************/
int NetworkWait() {

    if (!checkincomming)
        return -3;

    struct sockaddr_in sin;
    struct sockaddr_in client_address;
    socklen_t addrlen = sizeof(client_address);

    //Open socket
    socket = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP);

    if (socket == INVALID_SOCKET) {
        return socket;
    }

    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = htonl(INADDR_ANY);

    if (net_bind(socket, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        net_close(socket);
        return -1;
    }

    if (net_listen(socket, 3) < 0) {
        net_close(socket);
        return -1;
    }

    connection = net_accept(socket, (struct sockaddr*)&client_address, &addrlen);

    sprintf(incommingIP, "%s", inet_ntoa(client_address.sin_addr));

    if (connection < 0) {
        net_close(connection);
        net_close(socket);
        return -4;

    } else {

        unsigned char haxx[9];
        //skip haxx
        net_read(connection, &haxx, 8);
		wiiloadVersion[0] = haxx[4];
		wiiloadVersion[1] = haxx[5];

        net_read(connection, &infilesize, 4);

		if (haxx[4] > 0 || haxx[5] > 4) {
			net_read(connection, &uncfilesize, 4); // Compressed protocol, read another 4 bytes
		}
        waitforanswer = true;
        checkincomming = false;
        networkHalt = true;
    }

    return 1;
}
Ejemplo n.º 15
0
void ReactorAccept(int listenFd, short listenEv, void * arg)
{
    NetReactor * nrc = (NetReactor *)arg;
    if(nrc->isStop())
    {
        event lis = nrc->getListenEv();
        event_del(&lis);
        Log::NOTICE("SVR is STOP, don not accept");
        return;
    }

    struct sockaddr sa;
    socklen_t slen = sizeof(sa);
    int connfd = net_accept(listenFd, &sa, &slen);

    struct bufferevent ** pool;
    pool = nrc->getFdPool();
    int maxConnected = nrc->getMaxConnected(); 
    in_addr ipaddr = ((struct sockaddr_in *)&sa)->sin_addr;
    char *peer_ip = inet_ntoa(ipaddr);

    if(connfd > maxConnected)
    {
        Log::ERROR("Too much user, connfd : %d", connfd);
        close(connfd);
        return;
    }
   
    NetConnManager * cMag = NetConnManager::getInstance();  

    if(cMag->IsUserConnExist(connfd))
    {
        Log::ERROR("assign a working fd, connfd : %d, will clean it", 
                   connfd);
    }

    struct bufferevent * bev;
    set_fd_noblock(connfd);
    bev = bufferevent_new(connfd, 
                          EvReadCallback, 
                          NULL, 
                          EvErrorCallback, 
                          nrc);
    bev->timeout_read = nrc->getReadTo();
    bev->timeout_write = nrc->getWriteTo();
    bufferevent_enable(bev, EV_READ|EV_WRITE);    


    std::unique_ptr<Iconn> con(new UserConn(nrc, bev)); 
    cMag->setConn(connfd, std::move(con));

    /* use for test */
    //cMag->addAppConnFd(connfd); 

    Log::NOTICE("ACCEPT Connect SUCC, connfd : %d, UserIP : %s", connfd, peer_ip); 
}
Ejemplo n.º 16
0
Socket ServerSocket::Accept()
{
  AMJU_CALL_STACK;

#ifdef GEKKO
  return net_accept(m_socket, 0, 0);
#else
  return accept(m_socket, 0, 0);
#endif
}
Ejemplo n.º 17
0
td_t
tsplit(spdid_t spdid, td_t tid, char *param, int len,
       tor_flags_t tflags, long evtid)
{
	td_t ret = -EINVAL;
	struct torrent *t;
	net_connection_t nc = 0;
	int accept = 0;

	if (tor_isnull(tid)) return -EINVAL;

	NET_LOCK_TAKE();
	/* creating a new connection */
	if (tid == td_root || len == 0 || strstr(param, "accept")) {
		if (tid == td_root) { 	/* new connection */
			nc = net_create_tcp_connection(spdid, cos_get_thd_id(), evtid);
			if (nc <= 0) ERR_THROW(-ENOMEM, done);
		} else { /* len == 0 || strstr(param, "accept"), accept on connection */
			t = tor_lookup(tid);
			if (!t) goto done;
			nc = net_accept(spdid, (net_connection_t)t->data);
			if (nc == -EAGAIN) {
				/* printc("net accept return EAGAIN\n"); */
				ERR_THROW(-EAGAIN, done);
			}
			if (nc < 0) ERR_THROW(-EINVAL, done);
			if (0 < net_accept_data(spdid, nc, evtid)) BUG();
			accept = 1;
		}
		t = tor_alloc((void*)nc, tflags);
		if (!t) ERR_THROW(-ENOMEM, free);
		ret = t->td;
	} else { 		/* modifying an existing connection */
		t = tor_lookup(tid);
		if (!t) goto done;
		nc = (net_connection_t)t->data;
	}

	if (!accept && len != 0) {
		int r;

		NET_LOCK_RELEASE();
		r = modify_connection(spdid, nc, param, len);
		if (r < 0) ret = r;
		NET_LOCK_TAKE();
	}
done:
	NET_LOCK_RELEASE();
	assert(lock_contested(&net_lock) != cos_get_thd_id());
	return ret;
free:
	net_close(spdid, nc);
	goto done;
}
Ejemplo n.º 18
0
static void *worker_loop(void *p)
{
    struct work_info *wi = p;
    struct channel ch;

    while (wi->active) {
        if (net_accept(wi->li, &ch))
            break;
        ht_process(&ch);
        ch_close(&ch);
    }
    return NULL;
}
Ejemplo n.º 19
0
int WaitForConnection(int& server_socket)
{
	int addrlen;
	struct sockaddr_in my_name, peer_name;
	int status;

	server_socket = net_socket(AF_INET, SOCK_STREAM, 0);
	if (server_socket == -1)
	{
		printf("Failed to create server socket\n");
	}
	int yes = 1;
	net_setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

	memset(&my_name, 0, sizeof(my_name));
	my_name.sin_family = AF_INET;
	my_name.sin_port = htons(DFF_CONN_PORT);
	my_name.sin_addr.s_addr = htonl(INADDR_ANY);

	status = net_bind(server_socket, (struct sockaddr*)&my_name, sizeof(my_name));
	if (status == -1)
	{
		printf("Failed to bind server socket\n");
	}

	status = net_listen(server_socket, 5); // TODO: Change second parameter..
	if (status == -1)
	{
		printf("Failed to listen on server socket\n");
	}
	printf("Listening now!\n");

	int client_socket = -1;

	struct sockaddr_in client_info;
	socklen_t ssize = sizeof(client_info);
	int new_socket = net_accept(server_socket, (struct sockaddr*)&client_info, &ssize);
	if (new_socket < 0)
	{
		printf("accept failed!\n");
	}
	else
	{
		client_socket = new_socket;
		printf("accept succeeded and returned %d\n", client_socket);
	}

	return client_socket;
}
Ejemplo n.º 20
0
static void *worker_loop(void *p)
{
	struct work_info *wi = p;
	struct channel ch;
	char desc[40];
	struct net_socket sock;

	while (wi->active) {
		if (net_accept(&wi->li->sock, &sock, sizeof(desc), desc))
			break;
		ch_init(&ch, sock, desc);
		ht_process(&ch);
		ch_close(&ch);
	}
	return NULL;
}
Ejemplo n.º 21
0
void net_on_accept(struct net_connection* con, int event, void *arg)
{
    struct hub_info* hub = (struct hub_info*) arg;
    struct hub_probe* probe = 0;
    struct ip_addr_encap ipaddr;
    int server_fd = net_con_get_sd(con);
#ifdef PLUGIN_SUPPORT
    plugin_st status;
#endif

    for (;;)
    {
        int fd = net_accept(server_fd, &ipaddr);
        if (fd == -1)
        {
            if (net_error() == EWOULDBLOCK)
            {
                break;
            }
            else
            {
                LOG_ERROR("Accept error: %d %s", net_error(), strerror(net_error()));
                break;
            }
        }

#ifdef PLUGIN_SUPPORT
        status = plugin_check_ip_early(hub, &ipaddr);
        if (status == st_deny)
        {
            plugin_log_connection_denied(hub, &ipaddr);
            net_close(fd);
            continue;
        }

        plugin_log_connection_accepted(hub, &ipaddr);
#endif

        probe = probe_create(hub, fd, &ipaddr);
        if (!probe)
        {
            LOG_ERROR("Unable to create probe after socket accepted. Out of memory?");
            net_close(fd);
            break;
        }
    }
}
Ejemplo n.º 22
0
/* input function: DCC SEND - someone tried to connect to our socket */
static void dcc_send_init(DCC_REC *dcc)
{
	int handle, port;
	IPADDR addr;

	g_return_if_fail(dcc != NULL);

	/* accept connection */
	handle = net_accept(dcc->handle, &addr, &port);
	if (handle == -1)
		return;

	/* TODO: some kind of paranoia check would be nice. it would check
	   that the host of the nick who we sent the request matches the
	   address who connected us. */

	g_source_remove(dcc->tagread);
	close(dcc->handle);

	dcc->starttime = time(NULL);
	dcc->fastsend = settings_get_bool("dcc_fast_send");
	dcc->handle = handle;
	memcpy(&dcc->addr, &addr, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;

	dcc->databufsize = settings_get_int("dcc_block_size");
        if (dcc->databufsize <= 0) dcc->databufsize = 2048;
	dcc->databuf = g_malloc(dcc->databufsize);

	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_send_read_size, dcc);
	dcc->tagwrite = !dcc->fastsend ? -1 :
		g_input_add(handle, G_INPUT_WRITE, (GInputFunction) dcc_send_data, dcc);

	signal_emit("dcc connected", 1, dcc);

	if (!dcc->fastsend) {
		/* send first block */
		dcc->gotalldata = TRUE;
		dcc_send_data(dcc);
	}
}
Ejemplo n.º 23
0
/*
 * accept a connection and start a thread to manage it
 */
int server_accept (XML *xml, NETCON *conn, SSL_CTX *ctx, TASKQ *q)
{
  SERVERPARM *s;
  NETCON *c;

  debug ("accepting connection\n");
  if ((c = net_accept (conn, ctx)) == NULL)
  {
    debug ("failed to successfully accept socket\n");
    return (-1);
  }
  s = (SERVERPARM *) malloc (sizeof (SERVERPARM));
  s->conn = c;
  s->xml = xml;
  if (!task_available (q))
    warn ("No available server threads for request\n");
  task_add (q, server_request, s);
  return (0);
}
Ejemplo n.º 24
0
static void sig_listen(LISTEN_REC *listen)
{
	CLIENT_REC *rec;
	IPADDR ip;
	NET_SENDBUF_REC *sendbuf;
        GIOChannel *handle;
	char host[MAX_IP_LEN];
	int port;

	g_return_if_fail(listen != NULL);

	/* accept connection */
	handle = net_accept(listen->handle, &ip, &port);
	if (handle == NULL)
		return;
	net_ip2host(&ip, host);
	sendbuf = net_sendbuffer_create(handle, 0);
	rec = g_new0(CLIENT_REC, 1);
	rec->listen = listen;
	rec->handle = sendbuf;
        rec->host = g_strdup(host);
	rec->port = port;
	if (g_strcmp0(listen->ircnet, "*") == 0) {
		rec->proxy_address = g_strdup("irc.proxy");
		rec->server = servers == NULL ? NULL : IRC_SERVER(servers->data);
	} else {
		rec->proxy_address = g_strdup_printf("%s.proxy", listen->ircnet);
		rec->server = servers == NULL ? NULL :
			IRC_SERVER(server_find_chatnet(listen->ircnet));
	}
	rec->recv_tag = g_input_add(handle, G_INPUT_READ,
			       (GInputFunction) sig_listen_client, rec);

	proxy_clients = g_slist_prepend(proxy_clients, rec);
	rec->listen->clients = g_slist_prepend(rec->listen->clients, rec);

	signal_emit("proxy client connecting", 1, rec);
	printtext(rec->server, NULL, MSGLEVEL_CLIENTNOTICE,
		  "Proxy: New client %s:%d on port %d (%s)",
		  rec->host, rec->port, listen->port, listen->ircnet);
}
Ejemplo n.º 25
0
int net_server_serverClass::acceptNewRequest()
{
    if(!isStart) return -1;
    if(isShuttingDown) return -1; // don't accept new request when shutting down
    net_sockHandle hnd = net_accept(sock);
    if(hnd.sock != INVALID_SOCKET) { /** Connection recieved */
        net_ext_sock client = net_ext_createSock();
        client.sockHandle = hnd;
        client.status = NET_EXT_SOCK_ONLINE;
        char cc[100]; sprintf(cc,"%s connected",net_getIpFromHandle(hnd).c_str());
        if(debugFunc != NULL) (*debugFunc)(string(cc));
        clientList.push_back(client);
        if(accFunc != NULL) if((*accFunc)(client) == false) {
            // reject this acception
            disconnect(clientList.size()-1);
            return 2; // connection rejected
        }
        return 1; // new connection recieved
    }
    return 0; // nothing new
}
Ejemplo n.º 26
0
static void sig_listen(PLUGIN_DATA *data, gint handle)
{
    CLIENT_REC *rec;
    IPADDR ip;
    gint port;

    g_return_if_fail(data != NULL);
    if (servers == NULL) return;

    /* accept connection */
    handle = net_accept(handle, &ip, &port);
    if (handle == -1)
        return;

    rec = g_new0(CLIENT_REC, 1);
    rec->handle = handle;
    rec->server = servers == NULL ? NULL : servers->data;
    rec->tag = g_input_add(handle, G_INPUT_READ, (GInputFunction) sig_listen_client, rec);

    data->clients = g_slist_append(data->clients, rec);
}
Ejemplo n.º 27
0
static
s32 helper_opentcpip( DebugHelper_t *helper)
{
	struct sockaddr_in client;
	u32 clientlen = sizeof(client);

	if ( helper->listensock >=0 && helper->clientsock<0 ) {

		memset (&client, 0, sizeof (client));
	
		DEBUG_PRINT(( NULL,0, "helper_opentcpip(%p), try net_accept(), listensock=%d.\n", helper, helper->listensock));

                helper->clientsock = net_accept (helper->listensock, (struct sockaddr *) &client, &clientlen);

		DEBUG_PRINT(( NULL,0, "helper_opentcpip(%p), clientsock=%d.\n", helper, helper->clientsock));

		return helper->clientsock ;
	}
	return -1;

}
Ejemplo n.º 28
0
gint accept_connection(gint in_fd, gchar ** location)
{
	int fd;
	gchar *error_message;
	gchar *port;

	fd = net_accept(in_fd, &error_message);
	if (fd < 0) {
		log_message(MSG_ERROR, "%s\n", error_message);
		g_free(error_message);
		return -1;
	}

	g_assert(location != NULL);
	if (!net_get_peer_name(fd, location, &port, &error_message)) {
		log_message(MSG_ERROR, "%s\n", error_message);
		g_free(error_message);
	}
	g_free(port);
	return fd;
}
Ejemplo n.º 29
0
/**
 * \brief           Accept a connection from a remote client
 *
 * \param bind_fd   Relevant socket
 * \param client_fd Will contain the connected client socket
 * \param client_ip Will contain the client IP address
 *
 * \return          0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
 *                  POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
 *                  non-blocking and accept() is blocking.
 */
int net_accept_proxy( int bind_fd, int *client_fd, void *client_ip )
{
    mute();
    int ret = net_accept(bind_fd, client_fd, client_ip);
    unmute();

    SymN("socket", 0);
    Nondet();
    assume_intype("bitstring");
    size_t len = sizeof(*client_fd);
    assume_len(&len, false, sizeof(len));
    store_buf(client_fd);

    // Let the attacker decide what this function returns.
    input("client_ip", MAX_PRINCIPAL_LENGTH);
    store_buf(client_ip);
    input("net_accept_result", sizeof(ret));
    store_buf(&ret);

    return ret;

}
Ejemplo n.º 30
0
int tcp_accept(int fd)
{
	/* Accept incoming connection. */
	int incoming = net_accept(fd, NULL);

	/* Evaluate connection. */
	if (incoming >= 0) {
#ifdef SO_RCVTIMEO
		struct timeval tv;
		rcu_read_lock();
		conf_val_t *val = &conf()->cache.srv_tcp_idle_timeout;
		tv.tv_sec = conf_int(val);
		rcu_read_unlock();
		tv.tv_usec = 0;
		if (setsockopt(incoming, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
			log_warning("TCP, failed to set up watchdog timer"
			            ", fd %d", incoming);
		}
#endif
	}

	return incoming;
}