Пример #1
0
int tcp_data_handler::init(base_reactor* reactor,const char* host,int port)
{
    if(m_id.fd >= 0 || host == NULL ) return -1 ;

    if( m_rbuf.resize(INIT_BUF_SIZE) != 0 ) return -2;
    if( m_sbuf.resize(INIT_BUF_SIZE) != 0 ) return -2;
    
    sa_in_t service_addr ;
    init_sa_in(&service_addr,host,port) ;

    int fd = create_tcp_client(&service_addr,0) ;
    if(fd < 0 ) return -3 ;

    if( reactor->add_handler(fd,this,base_reactor::EVENT_WRITE)!=0 )
    {
        close(fd) ;
        return -3 ;
    }
    m_write_flag = 1 ;

    m_reactor = reactor ;

    m_id.fd = fd ;
    m_id.timestamp = time(0) ;

    update_status(STATUS_CONNECTING);

    return 0 ;
}
Пример #2
0
int main(int argc, char *argv[])
{
	if (argc <= 1) {
		usage(argv[0]);
	}

	int epoll_fd = create_epoll_reactor();
	assert(epoll_fd > 0);

	int c;
	while ((c = getopt(argc, argv, "chs")) != EOF) {
		switch (c) {
		case 'c':
			create_tcp_client(epoll_fd);
			break;
		case 's':
			create_tcp_server(epoll_fd);
			break;
		default:
			usage(argv[0]);
		}
	}

	pthread_t tid = pthread_create(&tid, NULL, &start_reactor, (void*)&epoll_fd);

	read_from_cli();

	return 0;
}
Пример #3
0
static int rpc_ping_proto(const char *host,
			  unsigned long nfs_version,
			  const char *proto,
			  long seconds, long micros)
{
	struct conn_info info;
	CLIENT *client;
	enum clnt_stat stat;
	struct protoent *prot;

	prot = getprotobyname(proto);
	if (!prot)
		return 1;

	info.host = host;
	info.program = NFS_PROGRAM;
	info.version = nfs_version;
	info.proto = prot;
	info.send_sz = 0;
	info.recv_sz = 0;
	info.timeout.tv_sec = seconds;
	info.timeout.tv_usec = micros;

	info.port = portmap_getport(&info);
	if (!info.port)
		return 0;

	if (prot->p_proto == IPPROTO_UDP) {
		info.send_sz = UDPMSGSIZE;
		info.recv_sz = UDPMSGSIZE;
		client = create_udp_client(&info);
	} else
		client = create_tcp_client(&info);

	if (!client)
		return 0;

	clnt_control(client, CLSET_TIMEOUT, (char *) &info.timeout);
	clnt_control(client, CLSET_RETRY_TIMEOUT, (char *) &info.timeout);

	stat = clnt_call(client, NFSPROC_NULL,
			 (xdrproc_t) xdr_void, 0, (xdrproc_t) xdr_void, 0,
			 info.timeout);

	clnt_destroy(client);

	if (stat != RPC_SUCCESS)
		return 0;

	return 1;
}
Пример #4
0
void run_client(std::string server, int port, int verbose_flag) {
	verbose = verbose_flag;	

	signal(SIGINT, &sigterm_client);
	vars = new nw_var_t[PAYLOAD_SIZE-1]; //Can't be more that this many vars

	sck = create_tcp_client(server.c_str(), port);

	if(sck != -1) {
		printf("Client ready\n");
		while(run) {
			read();
		}
	} else {
		printf("Connection failed\n");
	}
}
Пример #5
0
static unsigned short portmap_getport(struct conn_info *info)
{
	struct conn_info pmap_info;
	unsigned short port = 0;
	CLIENT *client;
	enum clnt_stat stat;
	struct pmap parms;

	pmap_info.host = info->host;
	pmap_info.port = PMAPPORT;
	pmap_info.program = PMAPPROG;
	pmap_info.version = PMAPVERS;
	pmap_info.proto = info->proto;
	pmap_info.send_sz = RPCSMALLMSGSIZE;
	pmap_info.recv_sz = RPCSMALLMSGSIZE;
	pmap_info.timeout.tv_sec = PMAP_TOUT_UDP;
	pmap_info.timeout.tv_usec = 0;

	if (info->proto->p_proto == IPPROTO_TCP) {
		pmap_info.timeout.tv_sec = PMAP_TOUT_TCP;
		client = create_tcp_client(&pmap_info);
	} else
		client = create_udp_client(&pmap_info);

	if (!client)
		return 0;
	
	parms.pm_prog = info->program;
	parms.pm_vers = info->version;
	parms.pm_prot = info->proto->p_proto;
	parms.pm_port = 0;

	stat = clnt_call(client, PMAPPROC_GETPORT,
			 (xdrproc_t) xdr_pmap, (caddr_t) &parms,
			 (xdrproc_t) xdr_u_short, (caddr_t) &port,
			 pmap_info.timeout);

	clnt_destroy(client);

	if (stat != RPC_SUCCESS)
		return 0;

	return port;
}
Пример #6
0
Файл: main.c Проект: c-ber/cber
int main(int argc,char * argv[])
{
    printf("\ntest start :\n");
    //int udp_fd = -1;
    create_tcp_client(&tcp_fd);

    //create_udp_service(&udp_fd);

    char errBuf[PCAP_ERRBUF_SIZE], * devStr;


    /* get a device */
    devStr = pcap_lookupdev(errBuf);

    if(devStr)
    {
        //printf("success: device: %s\n", devStr);
    }
    else
    {
        printf("error: %s\n", errBuf);
        exit(1);
    }

    /* open a device, wait until a packet arrives */
    char ifdev[] = "eth3";
    pcap_t * device = pcap_open_live(ifdev, 65535, 1, 0, errBuf);
    //pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf);
    if(!device)
    {
        printf("error: pcap_open_live(): %s\n", errBuf);
        exit(1);
    }

    /* wait loop forever */
    int id = 0;
    printf("Start to listen data now...\n");
    pcap_loop(device, -1, getPacket, (u_char*)&id);

    close(tcp_fd);
    return 0;
} 
Пример #7
0
/*
 *
 *  req_server()
 *
 *  CA server task
 *
 *  Waits for connections at the CA port and spawns a task to
 *  handle each of them
 *
 */
static void req_server (void *pParm)
{
    unsigned priorityOfSelf = epicsThreadGetPrioritySelf ();
    unsigned priorityOfBeacons;
    epicsThreadBooleanStatus tbs;
    struct sockaddr_in serverAddr;  /* server's address */
    osiSocklen_t addrSize;
    int status;
    SOCKET clientSock;
    epicsThreadId tid;
    int portChange;

    epicsSignalInstallSigPipeIgnore ();

    taskwdInsert ( epicsThreadGetIdSelf (), NULL, NULL );
    
    rsrvCurrentClient = epicsThreadPrivateCreate ();

    if ( envGetConfigParamPtr ( &EPICS_CAS_SERVER_PORT ) ) {
        ca_server_port = envGetInetPortConfigParam ( &EPICS_CAS_SERVER_PORT, 
            (unsigned short) CA_SERVER_PORT );
    }
    else {
        ca_server_port = envGetInetPortConfigParam ( &EPICS_CA_SERVER_PORT, 
            (unsigned short) CA_SERVER_PORT );
    }

    if (IOC_sock != 0 && IOC_sock != INVALID_SOCKET) {
        epicsSocketDestroy ( IOC_sock );
    }
    
    /*
     * Open the socket. Use ARPA Internet address format and stream
     * sockets. Format described in <sys/socket.h>.
     */
    if ( ( IOC_sock = epicsSocketCreate (AF_INET, SOCK_STREAM, 0) ) == INVALID_SOCKET ) {
        errlogPrintf ("CAS: Socket creation error\n");
        epicsThreadSuspendSelf ();
    }

    epicsSocketEnableAddressReuseDuringTimeWaitState ( IOC_sock );

    /* Zero the sock_addr structure */
    memset ( (void *) &serverAddr, 0, sizeof ( serverAddr ) );
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = htonl (INADDR_ANY); 
    serverAddr.sin_port = htons ( ca_server_port );

    /* get server's Internet address */
    status = bind ( IOC_sock, (struct sockaddr *) &serverAddr, sizeof ( serverAddr ) );
	if ( status < 0 ) {
		if ( SOCKERRNO == SOCK_EADDRINUSE ) {
			/*
			 * enable assignment of a default port
			 * (so the getsockname() call below will
			 * work correctly)
			 */
			serverAddr.sin_port = ntohs (0);
			status = bind ( IOC_sock, 
                (struct sockaddr *) &serverAddr, sizeof ( serverAddr ) );
		}
		if ( status < 0 ) {
            char sockErrBuf[64];
            epicsSocketConvertErrnoToString ( 
                sockErrBuf, sizeof ( sockErrBuf ) );
            errlogPrintf ( "CAS: Socket bind error was \"%s\"\n",
                sockErrBuf );
            epicsThreadSuspendSelf ();
		}
        portChange = 1;
	}
    else {
        portChange = 0;
    }

	addrSize = ( osiSocklen_t ) sizeof ( serverAddr );
	status = getsockname ( IOC_sock, 
			(struct sockaddr *)&serverAddr, &addrSize);
	if ( status ) {
        char sockErrBuf[64];
        epicsSocketConvertErrnoToString ( 
            sockErrBuf, sizeof ( sockErrBuf ) );
		errlogPrintf ( "CAS: getsockname() error %s\n", 
			sockErrBuf );
        epicsThreadSuspendSelf ();
	}

    ca_server_port = ntohs (serverAddr.sin_port);

    if ( portChange ) {
        errlogPrintf ( "cas warning: Configured TCP port was unavailable.\n");
        errlogPrintf ( "cas warning: Using dynamically assigned TCP port %hu,\n", 
            ca_server_port );
        errlogPrintf ( "cas warning: but now two or more servers share the same UDP port.\n");
        errlogPrintf ( "cas warning: Depending on your IP kernel this server may not be\n" );
        errlogPrintf ( "cas warning: reachable with UDP unicast (a host's IP in EPICS_CA_ADDR_LIST)\n" );
    }

    /* listen and accept new connections */
    if ( listen ( IOC_sock, 20 ) < 0 ) {
        errlogPrintf ("CAS: Listen error\n");
        epicsSocketDestroy (IOC_sock);
        epicsThreadSuspendSelf ();
    }

    tbs  = epicsThreadHighestPriorityLevelBelow ( priorityOfSelf, &priorityOfBeacons );
    if ( tbs != epicsThreadBooleanStatusSuccess ) {
        priorityOfBeacons = priorityOfSelf;
    }

    beacon_startStopEvent = epicsEventMustCreate(epicsEventEmpty);
    beacon_ctl = ctlPause;

    tid = epicsThreadCreate ( "CAS-beacon", priorityOfBeacons,
        epicsThreadGetStackSize (epicsThreadStackSmall),
        rsrv_online_notify_task, 0 );
    if ( tid == 0 ) {
        epicsPrintf ( "CAS: unable to start beacon thread\n" );
    }

    epicsEventMustWait(beacon_startStopEvent);
    epicsEventSignal(castcp_startStopEvent);

    while (TRUE) {
        struct sockaddr     sockAddr;
        osiSocklen_t        addLen = sizeof(sockAddr);

        while (castcp_ctl == ctlPause) {
            epicsThreadSleep(0.1);
        }

        clientSock = epicsSocketAccept ( IOC_sock, &sockAddr, &addLen );
        if ( clientSock == INVALID_SOCKET ) {
            char sockErrBuf[64];
            epicsSocketConvertErrnoToString ( 
                sockErrBuf, sizeof ( sockErrBuf ) );
            errlogPrintf("CAS: Client accept error was \"%s\"\n",
                sockErrBuf );
            epicsThreadSleep(15.0);
            continue;
        } 
        else {
            epicsThreadId id;
            struct client *pClient;

            /* socket passed in is closed if unsuccessful here */
            pClient = create_tcp_client ( clientSock );
            if ( ! pClient ) {
                epicsThreadSleep ( 15.0 );
                continue;
            }

            LOCK_CLIENTQ;
            ellAdd ( &clientQ, &pClient->node );
            UNLOCK_CLIENTQ;

            id = epicsThreadCreate ( "CAS-client", epicsThreadPriorityCAServerLow,
                    epicsThreadGetStackSize ( epicsThreadStackBig ),
                    camsgtask, pClient );
            if ( id == 0 ) {
                LOCK_CLIENTQ;
                ellDelete ( &clientQ, &pClient->node );
                UNLOCK_CLIENTQ;
                destroy_tcp_client ( pClient );
                errlogPrintf ( "CAS: task creation for new client failed\n" );
                epicsThreadSleep ( 15.0 );
                continue;
            }
        }
    }
}
Пример #8
0
int main(int argc, char const *argv[])
{
	int tcp_server_fd;
	int tcp_client_fd;
	int unix_server_fd;
	int temp_client_fd;
	int rv;
	int test_fd;
    fd_set readfds;
    fd_set testfds;

	if ((rv = init()) < 0) {
		perror("failed to init send message");
		exit(EXIT_FAILURE);
	}


	tcp_server_fd = create_tcp_server(local_tcp_server_ip, local_tcp_server_port);
	if (tcp_server_fd < 0) {
		perror("Failed to create tcp server");
		exit(EXIT_FAILURE);
	}

	tcp_client_fd = create_tcp_client();
	if (tcp_client_fd < 0) {
		perror("Failed to create tcp client");
		exit(EXIT_FAILURE);
	}


	unix_server_fd = create_unix_server(unix_server_path);
	if (unix_server_fd < 0) {
		perror("Failed to create unix server");
		exit(EXIT_FAILURE);
	}

    FD_ZERO(&readfds);
    FD_SET(tcp_server_fd, &readfds);
    FD_SET(unix_server_fd, &readfds);   

    while (1) {

        testfds = readfds;
        rv = select(FD_SETSIZE, &testfds, (fd_set *)NULL, (fd_set *)NULL, (struct timeval *)NULL);
        switch (rv)
        {
    
        case -1:
            perror("select");
            break;
        default:

        	for (test_fd = 0; test_fd < FD_SETSIZE; test_fd++) {
        		if (FD_ISSET(test_fd, &testfds)) {
        			if (test_fd == tcp_server_fd) {
            			temp_client_fd = accept_tcp_client(tcp_server_fd);
            			if (temp_client_fd < 0) {
            				continue;
            			}
            			printf("a client connected\n");
            			FD_SET(temp_client_fd, &readfds);
            	
            		}
            		else if (test_fd == unix_server_fd) {
            			rv = forward_server_command(unix_server_fd, tcp_client_fd);
            		} 

            		else {
            			rv = receive_iridium_msgs(test_fd);
            			if (rv <= 0) {
            				printf("a client disconnect\n");
            				FD_CLR(test_fd, &readfds);
            				close(test_fd);
            			}
            		}
        		}
	
        	}

        }
    }
	
	close(tcp_server_fd);
	close(unix_server_fd);
	close(tcp_client_fd);

	exit(EXIT_SUCCESS);
}
Пример #9
0
Client::Client(const char * hostname, int port) {
	_vars = new nw_var_t[PAYLOAD_SIZE-1]; //Can't be more that this many vars
	players = std::map<int, Player*>();
	_sockfd = create_tcp_client(hostname, port);
	me = NULL;
}