コード例 #1
0
int Lw_GetHostAddr(void *pBuff, int buffSize)
{
	memset(pBuff, 0, buffSize);
	
	if (gethostaddr(0, pBuff))
	{
		return 1;
	}
	
	return 0;
}
コード例 #2
0
ya_result
tcp_input_output_stream_connect_ex(const char *server, u16 port, input_stream *istream_, output_stream *ostream_, struct sockaddr *bind_from, u8 to_sec)
{
    ya_result return_code;
    socketaddress sa;

    /*
     * If the client interface is specified, then use its family.
     * Else use the unspecified familly to let the algorithm choose the first available one.
     */

    int family = (bind_from != NULL) ? bind_from->sa_family : AF_UNSPEC;

    if(ISOK(return_code = gethostaddr(server, port, &sa.sa, family)))
    {
        return_code = tcp_input_output_stream_connect_sockaddr(&sa.sa, istream_, ostream_, bind_from, to_sec);
    }

    return return_code;
}
コード例 #3
0
void
lwip_sysboot_threadtype_RunFunction(void **inPtr)
{
	char  ip[32];

        // Enable the Periodic Semaphore
	VDK_MakePeriodic(kPeriodic, SAMPLE_PERIOD, SAMPLE_PERIOD);


	/* Initializes the TCP/IP Stack and returns */
	if(system_init() == -1)
	{
		printf("Failed to initialize system\n");
		return;
	}

	/* start stack */
	start_stack();

	/*
	 * For debug purposes, printf() the IP address to the VisualDSP++
	 * console window.  Likely not needed in final application.
	 */

	memset(ip,0,sizeof(ip));
	if(gethostaddr(0,ip))
	{
		printf("IP ADDRESS: %s\n",ip);
	}

	/**
	 *  Add Application Code here
	 **/	 
	 LoopBackTest();

	/* Put the thread's exit from "main" HERE */
	/* A thread is automatically Destroyed when it exits its run function */
}
コード例 #4
0
void
lwip_sysboot_threadtype_RunFunction(void **inPtr)
{
	char  ip[32];
	
	// hack
	// *pVR_CTL = 0x4000 | (*pVR_CTL);
	
	/* Initializes the TCP/IP Stack and returns */	
	// true for fake init
	if(system_init(false) == -1)
	{
		printf("Failed to initialize system\n");
		return;
	}

	/* start stack */
	start_stack();

	/*
	 * For debug purposes, printf() the IP address to the VisualDSP++
	 * console window.  Likely not needed in final application.
	 */

	memset(ip,0,sizeof(ip));
	if(gethostaddr(0,ip))
	{
		printf("IP ADDRESS: %s\n",ip);
	}

	/**
	 *  Add Application Code here
	 **/
    RunSimpleUdpTest();

	/* Put the thread's exit from "main" HERE */
	/* A thread is automatically Destroyed when it exits its run function */
}
コード例 #5
0
ファイル: serv.cpp プロジェクト: NankaiZBP/nynn
int main(){
	char buff[INET_ADDRSTRLEN],hostname[32];
	sockaddr_in saddr;
	memset(&saddr,0,sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_addr.s_addr = gethostaddr(hostname,32);
	saddr.sin_port = htons(30001);
	Socket so(saddr);
	so.listen(10);
	cout<<"host "<<hostname<<" is listenning..."<<endl;
	while(true){
		Socket so1=so.accept();
		int pid=fork();
		if (pid>0){
			so1.close();
		}else if (pid==0){
			//cout<<"local host:"<<so1.getlocalhost(buff)<<endl;
			//cout<<"local port:"<<so1.getlocalport()<<endl;
			//cout<<"remote host:"<<so1.getremotehost(buff,INET_ADDRSTRLEN)<<endl;
			//cout<<"remote port:"<<so1.getremoteport()<<endl;

			int shmid;
			so1.recv((char*)&shmid,4);
			//cout<<"shmid="<<shmid<<endl;
			char *shm;
			//sleep(1);
			nynn_shmat(shmid,(void**)&shm,4096,true);
			cout<<shm<<endl;
			nynn_shmdt(shm);
			return 0;
		}else{
			cout<<"failed to fork a process"<<endl;
		}
	}
	return 0;

}
コード例 #6
0
/** Initialize the BACnet/IP services at the given interface.
 * @ingroup DLBIP
 * -# Gets the local IP address and local broadcast address from the system,
 *  and saves it into the BACnet/IP data structures.
 * -# Opens a UDP socket
 * -# Configures the socket for sending and receiving
 * -# Configures the socket so it can send broadcasts
 * -# Binds the socket to the local IP address at the specified port for
 *    BACnet/IP (by default, 0xBAC0 = 47808).
 *
 * @note For Windows, ifname is the dotted ip address of the interface.
 *
 * @param ifname [in] The named interface to use for the network layer.
 *        If NULL, the "eth0" interface is assigned.
 * @return True if the socket is successfully opened for BACnet/IP,
 *         else False if the socket functions fail.
 */
bool bip_init(
    char *ifname)
{
    int rv = 0; /* return from socket lib calls */
    struct sockaddr_in sin = { -1 };
    int value = 1;
    int sock_fd = -1;
    int Result;
    int Code;
    WSADATA wd;
    struct in_addr address;
    struct in_addr broadcast_address;

    Result = WSAStartup((1 << 8) | 1, &wd);
    /*Result = WSAStartup(MAKEWORD(2,2), &wd); */
    if (Result != 0) {
        Code = WSAGetLastError();
        printf("TCP/IP stack initialization failed\n" " error code: %i %s\n",
            Code, winsock_error_code_text(Code));
        exit(1);
    }
    atexit(bip_cleanup);

    if (ifname)
        bip_set_interface(ifname);
    /* has address been set? */
    address.s_addr = bip_get_addr();
    if (address.s_addr == 0) {
        address.s_addr = gethostaddr();
        if (address.s_addr == (unsigned) -1) {
            Code = WSAGetLastError();
            printf("Get host address failed\n" " error code: %i %s\n", Code,
                winsock_error_code_text(Code));
            exit(1);
        }
        bip_set_addr(address.s_addr);
    }
    if (BIP_Debug) {
        fprintf(stderr, "IP Address: %s\n", inet_ntoa(address));
    }
    /* has broadcast address been set? */
    if (bip_get_broadcast_addr() == 0) {
        set_broadcast_address(address.s_addr);
    }
    if (BIP_Debug) {
        broadcast_address.s_addr = bip_get_broadcast_addr();
        fprintf(stderr, "IP Broadcast Address: %s\n",
            inet_ntoa(broadcast_address));
        fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", ntohs(bip_get_port()),
            ntohs(bip_get_port()));
    }
    /* assumes that the driver has already been initialized */
    sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    bip_set_socket(sock_fd);
    if (sock_fd < 0) {
        fprintf(stderr, "bip: failed to allocate a socket.\n");
        return false;
    }
    /* Allow us to use the same socket for sending and receiving */
    /* This makes sure that the src port is correct when sending */
    rv = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &value,
        sizeof(value));
    if (rv < 0) {
        fprintf(stderr, "bip: failed to set REUSEADDR socket option.\n");
        close(sock_fd);
        bip_set_socket(-1);
        return false;
    }
    /* Enables transmission and receipt of broadcast messages on the socket. */
    rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, (char *) &value,
        sizeof(value));
    if (rv < 0) {
        fprintf(stderr, "bip: failed to set BROADCAST socket option.\n");
        close(sock_fd);
        bip_set_socket(-1);
        return false;
    }
#if 0
    /* probably only for Apple... */
    /* rebind a port that is already in use.
       Note: all users of the port must specify this flag */
    rv = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEPORT, (char *) &value,
        sizeof(value));
    if (rv < 0) {
        fprintf(stderr, "bip: failed to set REUSEPORT socket option.\n");
        close(sock_fd);
        bip_set_socket(-1);
        return false;
    }
#endif
    /* bind the socket to the local port number and IP address */
    sin.sin_family = AF_INET;
#if defined(USE_INADDR) && USE_INADDR
    /* by setting sin.sin_addr.s_addr to INADDR_ANY,
       I am telling the IP stack to automatically fill
       in the IP address of the machine the process
       is running on.

       Some server computers have multiple IP addresses.
       A socket bound to one of these will not accept
       connections to another address. Frequently you prefer
       to allow any one of the computer's IP addresses
       to be used for connections.  Use INADDR_ANY (0L) to
       allow clients to connect using any one of the host's
       IP addresses. */
    sin.sin_addr.s_addr = htonl(INADDR_ANY);
#else
    /* or we could use the specific adapter address
       note: already in network byte order */
    sin.sin_addr.s_addr = address.s_addr;
#endif
    sin.sin_port = bip_get_port();
    memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
    rv = bind(sock_fd, (const struct sockaddr *) &sin,
        sizeof(struct sockaddr));
    if (rv < 0) {
        fprintf(stderr, "bip: failed to bind to %s port %hu\n",
            inet_ntoa(sin.sin_addr), ntohs(bip_get_port()));
        close(sock_fd);
        bip_set_socket(-1);
        return false;
    }

    return true;
}
コード例 #7
0
void
lwip_sysboot_threadtype_RunFunction(void **inPtr)
{
	char  ip[32];
	int i;
	
	/* Initializes the TCP/IP Stack and returns */
	if(system_init() == -1)
	{
		printf("Failed to initialize system\n");
		return;
	}
	
	
	user_net_config_info[0].use_dhcp = 0;
	
	/* start stack */
	start_stack();
	
	sethostaddr(0,"192.168.0.5");
	
	/*
	 * For debug purposes, printf() the IP address to the VisualDSP++
	 * console window.  Likely not needed in final application.
	 */
	 
	memset(ip,0,sizeof(ip));
	if(gethostaddr(0,ip))
	{
		printf("IP ADDRESS: %s\n",ip);
	}
	
	/**
	 *  Add Application Code here 
	 **/
   {
  struct VDK_ThreadCreationBlock TCB = {
                                         (VDK_ThreadType)0,
                                         (VDK_ThreadID)0,
                                         0,
                                         (VDK_Priority)0
                                        };
    // Create application threads.
    TCB.template_id = kCharGen_Server_ThreadType;
    if (VDK_CreateThreadEx(&TCB) == UINT_MAX)
    {
      fprintf(stderr, "boot: failed to create CharGen server thread\n");
      exit(905);
    }
    TCB.template_id = kDiscard_Server_ThreadType;
    if (VDK_CreateThreadEx(&TCB) == UINT_MAX)
    {
      fprintf(stderr, "boot: failed to create Discard server thread\n");
      exit(906);
    }
    TCB.template_id = kEcho_Server_ThreadType;
    if (VDK_CreateThreadEx(&TCB) == UINT_MAX)
    {
      fprintf(stderr, "boot: failed to create Echo server thread\n");
      exit(907);
    }
  }

    /* Put the thread's exit from "main" HERE */
    /* A thread is automatically Destroyed when it exits its run function */
}
コード例 #8
0
ファイル: remote.c プロジェクト: 2dot4/Psiphon3-for-Linux
int authRsh(/*int *bg*/)
{	const char *home;
	CStr(path,1024);
	FILE *fp;
	CStr(paddr,1024);
	CStr(saddr,1024);
	CStr(line,1024);
	CStr(hosts,1024);
	CStr(phost1,1024); /* client's host */
	const char *paddr1;
	CStr(shost1,1024); /* server's host (optional) */
	const char *saddr1;
	int ok;

	if( (home = getenv("HOME")) == NULL ){
		fprintf(stderr,"Permission denied: no $HOME set\n");
		return -1;
	}

	sprintf(path,"%s/%s",home,RHOSTS);
	fp = fopen(path,"r");
	if( fp == NULL ){
		fprintf(stderr,"Permission denied: no %s\n",RHOSTS);
		return 1;
	}

	getpeerAddr(0,AVStr(paddr));
	gethostAddr(0,AVStr(saddr));

	ok = 0;
	while( fgets(line,sizeof(line),fp) ){
		wordScan(line,hosts);
		if( hosts[0] == 0 )
			continue;
		phost1[0] = shost1[0] = 0;
		if( Xsscanf(hosts,"%[^:]:%s",AVStr(phost1),AVStr(shost1)) < 1 )
			continue;

		if( phost1[0] && strcmp(phost1,"*") != 0 ){
			if( (paddr1 = gethostaddr(phost1)) == NULL )
				continue;
			if( strcmp(paddr,paddr1) != 0 )
				continue;
		}
		if( shost1[0] && strcmp(shost1,"*") != 0 ){
			if( (saddr1 = gethostaddr(shost1)) == NULL )
				continue;
			if( strcmp(saddr,saddr1) != 0 )
				continue;
		}
		ok = 1;
		break;
	}
	fclose(fp);
	if( ok ){
		return 0;
	}else{
		fprintf(stderr,"Permission denied: not in %s\n",RHOSTS);
		return -1;
	}
}
コード例 #9
0
ファイル: udprelay.c プロジェクト: 2dot4/Psiphon3-for-Linux
void udp_relayX(Connection *Conn,int csc,int csv[])
{	int svsock;
	int sockv[1024],readyv[1024],sx; /* must be lt/eq FD_SESIZE */
	int nsock; /* number of clients */
	CStr(buff,0x8000);
	int rcc,wcc;
	int nready;
	CStr(ihost,64);
	int iport;
	CStr(svhost,64);
	int svport;
	const char *clhost;
	int clport;
	UDP_Assoc *ua;
	UDP_Assoc *udpav[MAXASSOC*4]; /**/
	int udpxv[MAXASSOC*4];
	int lastrelay,idle;
	const char *aaddr;
	int svportmap;
	int portnumv[64];
	int csi;
	int istcp[MAXASSOC];
	int ccc = 0; /* tcp clients */
	int usocks = 0; /* UDP SOCKS */
	int update = 0;
	int ai;

	/* is this necessary or effective ? */
	socks_addservers();

	if( MAXASSOC < UDPRELAY_MAXASSOC ){
		UDPRELAY_MAXASSOC = MAXASSOC;
	}
	sv1log("MAXIMA=udprelay:%d (max. udp assoc.)\n",UDPRELAY_MAXASSOC);

	if( (aaddr = gethostaddr(DST_HOST)) == NULL ){
		sv1log("#### ERROR: bad destination host [%s]\n",DST_HOST);
		return;
	}
	strcpy(svhost,aaddr);
	svport = DST_PORT;
	svportmap = DFLT_PORTMAP;
	if( svportmap ){
		/* get port numbers of incoming ports ... */
		for( csi = 0; csi < csc; csi++ ){
			portnumv[csi] = sockPort(csv[csi]) + svportmap;
			portnumv[csi] &= ~0x40000000; /* clear IS_PORTMAP */
		}
	}

	expand_fdset(MAXASSOC);
	uassocv = (UDP_Assoc**)calloc(MAXASSOC+1,sizeof(UDP_Assoc*));
	for( csi = 0; csi < csc; csi++ )
		sockv[csi] = csv[csi];
	for( csi = 0; csi < csc; csi++ )
		istcp[csi] = !isUDPsock(sockv[csi]);
	nsock = 0;
	lastrelay = 0;

	for(;;){
UPDATE:
		if( update ){
			int ai;
			update = 0;
			ccc = 0;
			for( ai = 0; ua = uassocv[ai]; ai++ ){
				if( ua->ua_clpriv ){
				udpxv[csc + ccc] = ai;
				udpav[csc + ccc] = ua;
				sockv[csc + ccc++] = ua->ua_clsock;
				}
			}
			nsock = 0;
			for( ai = 0; ua = uassocv[ai]; ai++ ){
				udpxv[csc+ccc + nsock] = ai;
				udpav[csc+ccc + nsock] = ua;
				sockv[csc+ccc + nsock++] = ua->ua_svsock;
			}
			usocks = 0;
			for( ai = 0; ua = uassocv[ai]; ai++ ){
				int ns = csc+ccc+nsock;
				if( 0 <= ua->ua_svSOCKS ){
					udpxv[ns+usocks] = ai;
					udpav[ns+usocks] = ua;
					sockv[ns+usocks] = ua->ua_svSOCKS;
					usocks++;
				}
			}
		}
		/*
		nready = PollIns(10*1000,csc+nsock+ccc,sockv,readyv);
		*/
		nready = PollIns(10*1000,csc+ccc+nsock+usocks,sockv,readyv);
		if( nready < 0 ){
			/*
			sv1log("UDPRELAY: ABORT PollIns(%d) = %d\n",nready);
			*/
			sv1log("UDPRELAY: ABORT PollIns(%d+%d+%d+%d)=%d\n",
				csc,ccc,nsock,usocks,nready);
			for( ai = 0; ai < csc+ccc+nsock+usocks; ai++ ){
				sv1log("[%2d] %d\n",ai,sockv[ai]);
			}
			break;
		}
		if( nready == 0 ){
			idle = time(0) - lastrelay;
			if( SERVER_TIMEOUT && lastrelay )
			if( SERVER_TIMEOUT < idle){
				sv1log("UDPRELAY: SERVER TIMEOUT (idle %ds)\n",
					idle);
				break;
			}
			killTimeouts(uassocv);
			/*
			nsock = getsocks(uassocv,&sockv[csc]);
			*/
			update = 1;
			continue;
		}
		lastrelay = time(0);

		for( sx = 0; sx < csc; sx++ )
		if( 0 < readyv[sx] && istcp[sx] ){
			CStr(local,256);
			CStr(remote,256);
			int clsk;
			strcpy(remote,"*:*");
			strcpy(local,"*:*");
			clsk = VSocket(Conn,"ACPT/",sockv[sx],AVStr(local),AVStr(remote),"");
/*
			setNonblockingIO("UDPRELAY-CL",clsk,1);
*/
			SetNonblockingIO("UDPRELAY-CL",clsk,1);
			iport = getpeerAddr(clsk,AVStr(ihost));
			if( clsk <= 0 || iport <= 0 ){
				sv1log("UDPRELAY: accept() errno=%d\n",errno);
				continue;
			}
			if( svportmap ){
				svport = portnumv[sx];
			}
			ua = newUA(Conn,uassocv,ihost,iport,svhost,svport);
			ua->ua_clsock = clsk;
			ua->ua_clpriv = 1;
			update = 1;
		}else
		if( 0 < readyv[sx] ){
			rcc = recvFromA(sockv[sx],AVStr(buff),sizeof(buff),0,AVStr(ihost),&iport);
			if( rcc <= 0 ){
				sv1log("UDPRELAY: recv() == 0, errno=%d\n",errno);
				break;
			}
			if( svportmap ){
				svport = portnumv[sx];
			}
			ua = findUAbysrc(uassocv,ihost,iport,svhost,svport);
			if( ua == NULL ){
				ua = newUA(Conn,uassocv,ihost,iport,svhost,svport);
				if( ua == NULL ){
					continue;
				}
				ua->ua_clsock = sockv[sx];
				if( ua->ua_svsock < 0 )
					continue;
				/*
				nsock = getsocks(uassocv,&sockv[csc]);
				*/
				update = 1;
			}
			toServ(ua,buff,rcc,svhost,svport,ihost,iport);
			/*
			ua->ua_mtime = time(0);
			ua->ua_upcnt++;
			ua->ua_upbytes += rcc;
			if( UDPRELAY_RPORT_FIX )
				wcc = Send(ua->ua_svsock,buff,rcc);
			else	wcc = SendTo(ua->ua_svsock,buff,rcc,svhost,svport);
			Verbose("TO SERV#%d: %s:%d %3d> %s:%d\n",
				ua->ua_id,ihost,iport,rcc,svhost,svport);
			*/
			if( nready == 1 )
				continue;
		}

		for( sx = csc; sx < csc+ccc; sx++ )
		if( 0 < readyv[sx] ){
			rcc = recv(sockv[sx],buff,sizeof(buff),0);
			if( rcc <= 0 ){
				int ux = getUAx(uassocv,udpav[sx]);
				if( ux < 0 ){
					sv1log("## delUA-CL(%d)?\n",ux);
					continue;
				}
				delUA(uassocv,ux,"TCPreset-CL",1);
				/* here udpxv[] becomes inconsistent */
				update = 1;
			}else{
				ua = udpav[sx];
				toServ(ua,buff,rcc,svhost,svport,NULL,0);
			}
		}

		/*
		for( sx = csc; sx < csc+nsock; sx++ ){
		*/
		for( sx = csc+ccc; sx < csc+ccc+nsock; sx++ ){
			if( readyv[sx] <= 0 )
				continue;
			ua = udpav[sx];
			svsock = sockv[sx];

			if( 0 <= ua->ua_svSOCKS )
			rcc = RecvFrom(svsock,buff,sizeof(buff),AVStr(ihost),&iport);
			else
			rcc = recvFromA(svsock,AVStr(buff),sizeof(buff),0,AVStr(ihost),&iport);

			if( rcc <= 0 ){
				if( ua->ua_svtcp ){
					int ux = getUAx(uassocv,udpav[sx]);
					if( ux < 0 ){
						sv1log("## delUA-SV(%d)?\n",ux);
						continue;
					}
					delUA(uassocv,ux,"TCPreset-SV",1);
					update = 1;
				}
				readyv[sx] = -1;
				continue;
			}

			/*
			ua = findUAbysock(uassocv,svsock);
			*/
			ua->ua_mtime = time(0);
			ua->ua_downcnt++;
			ua->ua_downbytes += rcc;
			clhost = ua->ua_clhost;
			clport = ua->ua_clport;
			/*
			wcc = SendTo(ua->ua_clsock,buff,rcc,clhost,clport);
			*/
			wcc = sendToA(ua->ua_clsock,buff,rcc,0,clhost,clport);

			Verbose("TO CLNT#%d: %s:%d <%-3d %s:%d\n",
				ua->ua_id,clhost,clport,rcc,ihost,iport);
		}
		for( sx = csc+ccc+nsock; sx < csc+ccc+nsock+usocks; sx++ ){
			int ux;
			if( readyv[sx] <= 0 )
				continue;
			ua = udpav[sx];
			ux = getUAx(uassocv,udpav[sx]);
			if( ux < 0 ){
				sv1log("## delUA-CTL(%d)?\n",ux);
				continue;
			}
			sv1log("## detected disconn. by SOCKS CTL [%d]\n",ux);
			delUA(uassocv,ux,"SOCKSCTLreset-SV",1);
			update = 1;
		}
	}
}