예제 #1
0
void test_getsockname_client(cyg_addrword_t pnetdata)
{
	int s, readlen;
	struct sockaddr_in sa, r_sa;
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_GETSOCKNAME_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_GETSOCKNAME_DATA_T*)pnetdata)->pbuf;
	char *pgetsocknamebuf = ((TEST_GETSOCKNAME_DATA_T*)pnetdata)->pgetsocknamebuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_getsockname_client");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	memcpy(&(sa.sin_addr), hp->h_addr_list0, hp->h_length);
	sa.sin_family = AF_INET;
	sa.sin_port = htons(IPPORT_USERRESERVED + port - TEST_GETSOCKNAME_SERVER_NUM);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_getsockname_client");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_getsockname_client");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_getsockname_client");
		cyg_thread_exit();
	}

	if(connect(s, (struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_getsockname_client");
		cyg_thread_exit();
	}
	
	readlen = recv(s, pgetsocknamebuf, TEST_GETSOCKNAME_MSG_LEN, 0, pbuf, RNT_BUFFER_LEN);
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
예제 #2
0
void
sys_exit(void)
{
db_printf("%s called\n", __PRETTY_FUNCTION__);    
    phase = PHASE_DEAD;
    while( ppp_tty.tx_thread_running )
    {
        db_printf("kick tx thread\n");        
        cyg_semaphore_post( &ppp_tty.tx_sem );
        cyg_thread_delay(100);
    }
    ppp_tty.pppd_thread_running = false;
    cyg_thread_exit();
}
예제 #3
0
static void Cyg_DOT1X(cyg_addrword_t param)
#endif //__ECOS//
{
        int ret = 1, i;
        pid_t auth_pid=1;
        char prefix_name[IFNAMSIZ+1];


  	strcpy(prefix_name, "ra");
        DBGPRINT(RT_DEBUG_ERROR,"Start DOT1X Thread....\n");

        // set number of configuration file 1
        interfaces.count = 1;
        interfaces.rtapd = malloc(sizeof(rtapd *));
        if (interfaces.rtapd == NULL)
        {
            DBGPRINT(RT_DEBUG_ERROR,"malloc failed\n");
            exit(1);    
        }
        eloop_init(&interfaces);
        interfaces.rtapd[0] = Apd_init(prefix_name);
        if (!interfaces.rtapd[0])
            goto out;

        if (Apd_setup_interface(interfaces.rtapd[0]))
                goto out;

        eloop_run();

        Apd_free_stas(interfaces.rtapd[0]);
        ret=0;
        
out:
        for (i = 0; i < interfaces.count; i++)
        {
                if (!interfaces.rtapd[i])
                        continue;

                Apd_cleanup(interfaces.rtapd[i]);
                free(interfaces.rtapd[i]);
        }

        free(interfaces.rtapd);
        interfaces.rtapd = NULL;
        eloop_destroy();
        DBGPRINT(RT_DEBUG_ERROR,"DOT1X_exit\n");
        cyg_thread_exit();
        return 0;
}
예제 #4
0
void
KernelLeaveTask
    (
        void
    )
{
    PKERNEL_TASK_MEM_BLOCK          pMem        = NULL;

    KernelAcquireSpinLock(&gMemBlockPoolLock);

    if ( TRUE )
    {
        pMem = gMemBlockPool.Next;

        while ( pMem )
        {
            gMemBlockPool.Next = pMem->Next;

            KernelFreeMemory(pMem);

            pMem = gMemBlockPool.Next;
        }

        gMemBlockPool.Prev = NULL;
        gMemBlockPool.Next = NULL;
    }

    if ( TRUE )
    {
        pMem = (PKERNEL_TASK_MEM_BLOCK)cyg_thread_get_data(0);

        if ( pMem )
        {
            gMemBlockPool.Prev = pMem;
            gMemBlockPool.Next = pMem;
        }
    }

    KernelReleaseSpinLock(&gMemBlockPoolLock);

    cyg_thread_exit();

    return;
}
예제 #5
0
void simple_tcpserver(cyg_addrword_t pnetdata)
{
	int s, new_s, i, len;
	char *msg;
	struct sockaddr_in sa, r_sa;
	int r_sa_l = sizeof(r_sa);
	struct hostent *hp;
	
	struct sockaddr_in peername;
	int socklen;
	
	int threadid;
	int localcount = 0;
		
	int port = ((NET_DATA_T*)pnetdata)->iport;
	char *pbuf = ((NET_DATA_T*)pnetdata)->pbuf;
	
	//s = ((NET_DATA_T*)pnetdata)->fd;
	threadid = cyg_thread_self();
	
	msg = malloc(BUFSIZE);
	if(msg == NULL)
	{
		printf("simple_tcpserver(%d): malloc error\n", threadid);
		cyg_thread_exit();
	}
	
	printf("simple_tcpserver(%d): to gethostbyname\n", threadid);
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		printf("simple_tcpserver(%d): gethostbyname error!!!!!!\n", threadid);
		cyg_thread_exit();
	}
	printf("simple_tcpserver(%d): gethostbyname ok\n", threadid);
	
	//memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_addr.s_addr = inet_addr("10.132.11.10", pbuf, RNT_BUFFER_LEN);
	printf("simple_tcpserver(%d): final addr=%s\n", threadid, inet_ntoa(r_sa.sin_addr, pbuf, RNT_BUFFER_LEN));
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	printf("simple_tcpserver(%d): receive from port %d\n", threadid, ntohs(r_sa.sin_port));
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		printf("simple_tcpserver(%d): socket error!!!!!!\n", threadid);
		cyg_thread_exit();
	}
	printf("simple_tcpserver(%d): create socket success...\n", threadid);
	printf("simple_tcpserver(%d): socket=%d\n", threadid, s);
	
	/* Test for netfcntl & setsockopt */
	/*
	{
		int sock_opt = 1;

		// server socket is nonblocking
		if (netfcntl(s, F_SETFL, O_NONBLOCK, pbuf, RNT_BUFFER_LEN) == -1)
		{
			printf("netfcntl error\n");
			netclose(s, pbuf, RNT_BUFFER_LEN);
			return -1;
		}

		if ((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
						sizeof(sock_opt), pbuf, RNT_BUFFER_LEN)) == -1)
		{
			printf("setsockopt error\n");
			netclose(s, pbuf, RNT_BUFFER_LEN);
			return -1;
		}
	}
	*/
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		printf("simple_tcpserver(%d): bind error!!!!!!\n", threadid);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	printf("simple_tcpserver(%d): bind success...\n", threadid);

	/*
	{
		struct sockaddr_in sockname;
		int socklen;
		
		if(getsockname(s, (struct sockaddr*)&sockname, &socklen, pbuf, RNT_BUFFER_LEN) != -1)
		{
			printf("simple_tcpserver(%d): getsockname:sin_family=%d sin_port=%d sin_addr=%s socklen=%d\n", threadid, 
				sockname.sin_family, sockname.sin_port, inet_ntoa(sockname.sin_addr, pbuf, RNT_BUFFER_LEN), socklen);
		}
		else
		{
			printf("simple_tcpserver(%d): getsockname error\n", threadid);
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
	}
	*/
	
	if(listen(s, 10, pbuf, RNT_BUFFER_LEN) == -1)
	{
		printf("simple_tcpserver(%d): listen error\n", threadid);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	printf("simple_tcpserver(%d): listen success...\n", threadid);
	
	while(1)
	{
		
		if((new_s = accept(s, (struct sockaddr*)&sa, (size_t*)&r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1)
		{
			printf("simple_tcpserver(%d): accept error\n", threadid);
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
		printf("simple_tcpserver(%d): accept success...\n", threadid);
		
		{
			int val, vallen;
			
			vallen = sizeof(val);
			if(getsockopt(new_s, SOL_SOCKET, SO_RCVBUF, &val, &vallen, pbuf, RNT_BUFFER_LEN) < 0)
			{
				printf("get rcvbuf size error!!!!!!\n");
			}
			printf("recv window size = %d\n", val);
			vallen = sizeof(val);
			if(getsockopt(new_s, SOL_SOCKET, SO_SNDBUF, &val, &vallen, pbuf, RNT_BUFFER_LEN) < 0)
			{
				printf("get sndbuf size error!!!!!!\n");
			}
			printf("send window size = %d\n", val);
			
			vallen = sizeof(val);
			val = 50000;
			if(setsockopt(new_s, SOL_SOCKET, SO_RCVBUF, &val, vallen, pbuf, RNT_BUFFER_LEN) < 0)
			{
				printf("set rcvbuf size error!!!!!!\n");
			}
			if(setsockopt(new_s, SOL_SOCKET, SO_SNDBUF, &val, vallen, pbuf, RNT_BUFFER_LEN) < 0)
			{
				printf("set sndbuf size error!!!!!!\n");
			}
			
			vallen = sizeof(val);
			if(getsockopt(new_s, SOL_SOCKET, SO_RCVBUF, &val, &vallen, pbuf, RNT_BUFFER_LEN) < 0)
			{
				printf("get rcvbuf size error!!!!!!\n");
			}
			printf("new recv window size = %d\n", val);
			vallen = sizeof(val);
			if(getsockopt(new_s, SOL_SOCKET, SO_SNDBUF, &val, &vallen, pbuf, RNT_BUFFER_LEN) < 0)
			{
				printf("get sndbuf size error!!!!!!\n");
			}
			printf("new send window size = %d\n", val);
		}
		
		/*
		{
			if(getpeername(new_s, (struct sockaddr*)&peername, &socklen, pbuf, RNT_BUFFER_LEN) != -1)
			{
				printf("simple_tcpserver(%d): getpeername:sin_family=%d sin_port=%d sin_addr=%s socklen=%d\n", threadid, 
					peername.sin_family, peername.sin_port, inet_ntoa(peername.sin_addr, pbuf, RNT_BUFFER_LEN), socklen);
			}
			else
			{
				printf("simple_tcpserver(%d): getpeername error\n", threadid);
				netclose(new_s, pbuf, RNT_BUFFER_LEN);
				netclose(s, pbuf, RNT_BUFFER_LEN);
				cyg_thread_exit();
			}
		}
		*/
		
		i = 0;
		while(1)
		{
			/* Test recvmsg & sendmsg method */
			/*
			{
				struct msghdr msghdr_msg;
				struct iovec *piov;
				int j, n, copylen;
				int perIov_len;
				
				perIov_len = BUFSIZE/4;
				piov = malloc(4*sizeof(struct iovec));
				for(j = 0; j < 4; j++)
				{
					piov[j].iov_base = malloc(perIov_len);
					piov[j].iov_len = perIov_len;
				}
				msghdr_msg.msg_name = NULL;
				msghdr_msg.msg_namelen = 0;
				msghdr_msg.msg_iov = piov;
				msghdr_msg.msg_iovlen = 4;
				msghdr_msg.msg_control = NULL;
				msghdr_msg.msg_controllen = 0;
				msghdr_msg.msg_flags = 0;
				
				if((len = recvmsg(new_s, &msghdr_msg, 0, pbuf, RNT_BUFFER_LEN)) == -1)
				{
					printf("simple_tcpserver(%d): recvmsg error\n", threadid);
					break;
				}
				if(len == 0)
				{
					printf("simple_tcpserver(%d): recvmsg 0, connection broken!!!!!\n", threadid);
					break;
				}
				n = 0;
				for(j = 0; j < 4; j++)
				{
					//printf("simple_tcpserver(%d): recvmsg piov[%d].iovlen=%d\n", threadid, j, piov[j].iov_len);
					copylen = piov[j].iov_len < (len - n) ? piov[j].iov_len : (len - n);
					memcpy(&msg[n], piov[j].iov_base, copylen);
					n += copylen;
					if(n >= len) break;
				}
				msg[len] = '\0';
				//printf("simple_tcpserver(%d): recvmsg: %s\n", threadid, msg);
				
				len = 17408;
				memcpy(msg, g_LargeData, len);
				//len = sprintf(msg, "ack%d", i);
				n = 0;
				for(j = 0; j < 4; j++)
				{
					copylen = piov[j].iov_len < (len - n) ? piov[j].iov_len : (len - n);
					memcpy(piov[j].iov_base, &msg[n], copylen);
					piov[j].iov_len = copylen;
					//printf("simple_tcpserver(%d): sendmsg piov[%d].iov_len=%d\n", threadid, j, piov[j].iov_len);
					n += copylen;
					if(n >= len) break;
				}
				j++;
				for(; j < 4; j++) piov[j].iov_len = 0;
				
				if((len = sendmsg(new_s, &msghdr_msg, 0, pbuf, RNT_BUFFER_LEN)) == -1)
				{
					printf("simple_tcpserver(%d): sendmsg error\n", threadid);
					break;
				}
				if(len == 0)
				{
					printf("simple_tcpserver(%d): sendmsg 0, connection broken!!!!!\n", threadid);
					break;
				}
				for(j = 0; j < 4; j++)
				{
					free(piov[j].iov_base);
				}
				free(piov);
				i++;
			}
			*/
			
			/* Test recv & send method and fdset operation */
			/*
			{
				fd_set readset, exceptset;
				int maxfd;
				int written_bytes;
				int bytes_left;
				int reconnect;
				
				FD_ZERO(&readset);
				FD_SET(new_s, &readset);
				FD_ZERO(&exceptset);
				FD_SET(new_s, &exceptset);
				maxfd = new_s + 1;
				
				if(netselect(maxfd, &readset, NULL, &exceptset, NULL, pbuf, RNT_BUFFER_LEN) == -1)
				{
					printf("simple_tcpserver(%d): netselect error\n", threadid);
					continue;
				}
				
				if(FD_ISSET(new_s, &readset))
				{
					//printf("simple_tcpserver(%d): readset get\n", threadid);
				}
				else if(FD_ISSET(new_s, &exceptset))
				{
					printf("simple_tcpserver(%d): exceptset get\n", threadid);
					break;
				}
			
				if((len = recv(new_s, msg, BUFSIZE, 0, pbuf, RNT_BUFFER_LEN)) == -1)
				{
					printf("simple_tcpserver(%d): recv error\n", threadid);
					break;
				}
				if(len == 0)
				{
					printf("simple_tcpserver(%d): recv 0, connection broken!!!!!\n", threadid);
					break;
				}
				//printf("simple_tcpserver(%d): recv: %s\n", threadid, msg);
				
				//len = sprintf(msg, "ack%d", i);
				len = 40000;
				memcpy(msg, g_LargeData, len);
				bytes_left = len;
				reconnect = 0;
				while(bytes_left > 0)
				{
					written_bytes = send(new_s, msg+(len-bytes_left), bytes_left, 0, pbuf, RNT_BUFFER_LEN);
					if(written_bytes <= 0)
					{
						if(errno == EINTR)
						{
							written_bytes = 0;
						}
						else
						{
							printf("simple_tcpserver(%d): netwrite error, reconnect!!!!!\n", threadid);
							reconnect = 1;
							break;
						}
					}
					bytes_left -= written_bytes;
				}
				if(reconnect == 1) break;
				i++;
			}
			*/
			
			/* Test for netread & netwrite method */
			{
				int written_bytes;
				int bytes_left;
				int reconnect;
				
				if((len = netread(new_s, msg, BUFSIZE, pbuf, RNT_BUFFER_LEN)) == -1)
				{
					printf("simple_tcpserver(%d): netread error %d\n", threadid, errno);
					break;
				}
				if(len == 0)
				{
					printf("simple_tcpserver(%d): netread 0, connection broken, errno=%d!!!!!\n", threadid, errno);
					break;
				}
				msg[len] = '\0';
				//printf("simple_tcpserver(%d): len %d, netread %s\n", threadid, len, msg);
				//printf("simple_tcpserver(%d): len %d\n", threadid, len);
				
				//len = sprintf(msg, "ack%d", i);
				len = 40000;
				memcpy(msg, g_LargeData, len);
				bytes_left = len;
				reconnect = 0;
				while(bytes_left > 0)
				{
					written_bytes = netwrite(new_s, msg+(len-bytes_left), bytes_left, pbuf, RNT_BUFFER_LEN);
					if(written_bytes <= 0)
					{
						printf("errno=%d\n", errno);
						if(errno == EINTR)
						{
							written_bytes = 0;
						}
						else
						{
							printf("simple_tcpserver(%d): netwrite error, reconnect!!!!!\n", threadid);
							reconnect = 1;
							break;
						}
					}
					//printf("simple_tcpserver(%d): write %d\n", threadid, written_bytes);
					bytes_left -= written_bytes;
					if(bytes_left > 0)
					{
						printf("write %d < %d left %d\n", written_bytes, bytes_left+written_bytes, bytes_left);
					}
				}
				if(reconnect == 1) break;
				i++;
			}
			
			totallen += len;
			
			icount++;
			if((icount>=2000) && (icount%2000==0))
			{
				tend = cyg_current_time();
				printf("rate=%d rate1=%03fk\n", (int)(icount/((tend-tbegin)/100)), (float)(totallen/((tend-tbegin)/100)/1024));
			}
			
			localcount++;
			if(localcount > 100 && localcount % 100 == 0)
			{
				printf("simple_tcpserver: %d\n", port);
			}
		}
		
		printf("simple_tcpserver(%d): to close %d with 0x%x\n", threadid, new_s, pbuf);
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
		printf("simple_tcpserver(%d): close connection ok\n", threadid);
	}
	
	netclose(s, pbuf, RNT_BUFFER_LEN);
	free(msg);
	cyg_thread_exit();
}
예제 #6
0
void thread_read_audio(cyg_addrword_t data)
{
	VP_BUFFER_TEST_AUDIO_BITSTREAM_T *ptestaudio;
	int audiocount = 0;
	cyg_tick_count_t timeBegin, timeEnd;
	
	int iRt;
	IO_THREAD_READ_T readarg;
	int i = 0;
	
	timeBegin = cyg_current_time();
	while(1)
	{
		readarg.txlen = BUFFER_SIZE;
		iRt = iothread_ReadAudio(&readarg);
		if(iRt == -1)
		{
			diag_printf("Encode Core quit\n");
			cyg_thread_exit();
	    }
		else if(iRt == 0)
		{
			diag_printf("%d buffer too small\n", readarg.mediatype);
			continue;
        }
		else
        {
			switch(readarg.mediatype)
	        {
               case MEDIA_TYPE_AUDIO:
					audiocount++;
					
					cyg_semaphore_wait(&g_testaudio.pcAudio.producer);
					ptestaudio = bufTestAudioNew();
					if(ptestaudio == NULL)
					{
						diag_printf("No audio read buffer!!!\n");
						while(1);
					}
					
					if( readarg.txlen <= sizeof(ptestaudio->aucAudio))
					{
						memcpy(ptestaudio->aucAudio, readarg.txbuf, readarg.txlen);
						ptestaudio->iAudioLen = readarg.txlen;
						
						listAttach(&g_testaudio.listAudio, &ptestaudio->list);
						cyg_semaphore_post(&g_testaudio.pcAudio.consumer);
					}
					else
					{
						bufTestAudioDecRef(ptestaudio);
						diag_printf("audio too large %d>%d\n", readarg.txlen, sizeof(ptestaudio->aucAudio));
						cyg_semaphore_post(&g_testaudio.pcAudio.producer);
					}
					iothread_ReadAudio_Complete(&readarg);
					//readarg.txbuf = (unsigned char*)g_mpDataBuffer;
					
#if 0
					/* Decode law to pcm */
					{
						DecodeLAW_T decode_info;
						
						decode_info.dfSrcFormat = DecodeLAW_FORMAT_ALAW;
						decode_info.dfDstFormat = DecodeLAW_FORMAT_LINEAR;
						decode_info.ucSrcData = g_mpDataBuffer;
						decode_info.iSrcDataSize = readarg.txlen;
						decode_info.ucDstData = g_mpAudioDataBuffer;
						decode_info.iDstDataSize = sizeof(g_mpAudioDataBuffer);
						
						readarg.txlen = DecodeLAW(&decode_info);
						if(readarg.txlen == -1)
						{
							diag_printf("decode law failed\n");
							continue;
						}
						
						readarg.mediatype = MEDIA_TYPE_AUDIO;
						readarg.txbuf = g_mpAudioDataBuffer;
					}
#endif
					iothread_Write(&readarg);
					//diag_printf("get audio, len=%d\n", readarg.txlen);
                    break;
              default:
					diag_printf("unknown type %d\n", readarg.mediatype);
                    break;
            }
		}
		
		if(audiocount >= 500 && audiocount % 500 == 0)
		{
			int timeoffset;
			timeEnd = cyg_current_time();
			timeoffset = timeEnd-timeBegin;
			diag_printf("audiocount=%d\n", audiocount);
			if(timeoffset != 0 && timeoffset > 100) diag_printf("audiorate=%d\n", audiocount/(timeoffset/100));
        }
	}
	cyg_thread_exit();
}
예제 #7
0
void listener( cyg_addrword_t which )
{
    int s_s1 = -1, e_s1 = 0, s_s2 = -1, e_s2 = 0;
    struct sockaddr_in e_s1_addr,e_s2_addr,local;
    fd_set in_fds;
    int len;
    int num;

    // do we select on multiple sources?
    int dual = (3 == (which & 3)) || (2 == (which & 3));
    // then which is 2,3,6,7 so set up a 2nd listener
    
    CYG_TEST_CHECK( 0 <= which, "which under" );
    CYG_TEST_CHECK( NLISTENERS > which, "which over" );

    diag_printf( "Listener %d alive [%s]\n", which, dual ? "dual" : "single" );

    s_s1 = socket(AF_INET, SOCK_STREAM, 0);
    if (s_s1 < 0) {
        pexit("stream socket 1");
    }   
    memset(&local, 0, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_len = sizeof(local);
    local.sin_port = ntohs(SOURCE_PORT1 + which);
    local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    if(bind(s_s1, (struct sockaddr *) &local, sizeof(local)) < 0) {
        pexit("bind /source_1/ error");
    }
    listen(s_s1, SOMAXCONN);

    if ( dual ) {
        s_s2 = socket(AF_INET, SOCK_STREAM, 0);    
        if (s_s2 < 0) {    
            pexit("stream socket 2");
        }  
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_len = sizeof(local);
        local.sin_port = ntohs(SOURCE_PORT2 + which);
        local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        if(bind(s_s2, (struct sockaddr *) &local, sizeof(local)) < 0) {    
            pexit("bind /source_2/ error");
        }
        listen(s_s2, SOMAXCONN);
    }

    while (true) {
        FD_ZERO(&in_fds);
        FD_SET(s_s1, &in_fds);
        if ( dual )
            FD_SET(s_s2, &in_fds);
        num = select ( max(s_s1,s_s2)+1, &in_fds,0,0,0);
        
        if (FD_ISSET(s_s1,&in_fds)) {
            len = sizeof(e_s1_addr);
            if ((e_s1 = accept(s_s1,(struct sockaddr *)&e_s1_addr,&len))<0) {
                pexit("accept /source_1/");
            }
            diag_printf("TCP SERVER connection from %s: %d\n",
                        inet_ntoa(e_s1_addr.sin_addr),ntohs(e_s1_addr.sin_port));
        }

        if ( dual ) {
            if (FD_ISSET(s_s2,&in_fds)) {
                len = sizeof(e_s2_addr);
                if ((e_s2 = accept(s_s2,(struct sockaddr *)&e_s2_addr,&len))<0) {
                    pexit("accept /source_2/");
                }
                diag_printf("TCP SERVER connection from %s: %d\n",
                            inet_ntoa(e_s2_addr.sin_addr), ntohs(e_s2_addr.sin_port));
            }
        }
        if ((e_s1 != 0) || ( e_s2 != 0)) {
            break;
        }
    }   /* while (true) */ 

    CYG_TEST_CHECK( 0 != e_s1, "No connection made on s1!" );
    
    if ((len = read(e_s1, data_buf1[which], MAX_BUF)) < 0  ) {
        perror("I/O error s1");
        CYG_TEST_FAIL( "Read s1 failed" );
    }
    diag_printf("Listener %d: %s\n", which, data_buf1[which]);

    close( s_s1 );
    if ( dual )
        close( s_s2 );
    if ( 0 != e_s1 )
        close ( e_s1 );
    if ( 0 != e_s2 )
        close ( e_s2 );

    cyg_semaphore_post( &listen_sema[which] ); // Verify that I was here
    cyg_semaphore_post( &recv_sema );          // Count receptions

    cyg_thread_exit(); // explicitly
}
예제 #8
0
void test_tcp_socket_server(cyg_addrword_t pnetdata)
{
	int s, new_s, i;
	struct sockaddr_in sa, r_sa;
	int r_sa_l = sizeof(r_sa);
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_TCP_SOCKET_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_TCP_SOCKET_DATA_T*)pnetdata)->pbuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_tcp_socket_server");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_tcp_socket_server");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_tcp_socket_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_tcp_socket_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}

	if(listen(s, 10, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_tcp_socket_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	for(i = 0; i < TEST_TCP_ACCEPT_SOCKET_TIMES; i++)
	{
		if((new_s = accept(s, (struct sockaddr*)&sa, (size_t*)&r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1)
		{
			test_printf_error("test_tcp_socket_server");
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
	}
	test_printf_success("test_tcp_socket_server");
	
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
예제 #9
0
/****************************************************************************
 Function: disktest

 Description:  This example program streams data to a single SCSI disks, 
               reads it back, and verifies it. 
 Calling Sequence:
 Inputs:
          fd - This parameter contains the open file diescriptor.
 Outputs:
 Returns: 
 Notes: 
 Example:

****************************************************************************/
static void disktest (CYG_ADDRWORD fd)
{
    int                i;
    int                numSects;
    int                numWrites;
    int                seed = rand();
    char               *buffer;
    char               *abuffer;
    int                status = 0;
    unsigned int       count;
    unsigned long long *bufP;
    unsigned long long nextRead = seed;
    unsigned long long nextWrite = seed;

    /* Perform any initialization prior to start of test.
       This example allocates a data buffer for each disk
       being read/written.
    */
    /* Allocate Data Buffer - This example uses malloc to
       allocate data buffers.
    */
    buffer = (char *) malloc (BLOCK_SIZE + HAL_DCACHE_LINE_SIZE);
    if (buffer == NULL)
    {
	printf ("malloc: Error allocating buffer space\n");
        close(fd);
        goto exit;
    }
    /* Align data buffer on a cache line
     */
    abuffer = (char *)roundup(buffer, HAL_DCACHE_LINE_SIZE);

    numSects = BLOCK_SIZE / SECTOR_SIZE;
    numWrites = TOTAL / BLOCK_SIZE;
	
    // Set file offset to beginning of disk
    lseek(fd, 0, SEEK_SET);

    for (count = 0; count < numWrites; count+=1)
    {

#ifdef VERIFY_ALL
        /* Initialize buffer with incrementing data */
        bufP = (unsigned long long *)abuffer;
        for (i = 0; i < BLOCK_SIZE / sizeof(unsigned long long); ++i)
            *bufP++ = nextWrite++;
#endif
#ifdef VERIFY_FIRST
        /* Initialize first location in buffer with incrementing data */
        bufP = (unsigned long long *)abuffer;
        *bufP = nextWrite;
        nextWrite += BLOCK_SIZE;
#endif

        /* Write buffer data to disk
         */
        if ((status = write(fd, abuffer, BLOCK_SIZE)) != BLOCK_SIZE)
        {
            if (status == -1)
            {
                perror("write");
            }
            else
    	        printf("write: Only %d bytes written should be %d\n", 
                       status, BLOCK_SIZE);

            /* Free data buffer */ 
            free(buffer);

            goto exit;
        }
    }
	
    /* Set file offset to beginning of disk */
    lseek(fd, 0, SEEK_SET);

    for (count = 0; count < numWrites; count+= 1)
    {
        /* Read buffer data from disk.
        */
        if ((status = read(fd, abuffer, BLOCK_SIZE)) != BLOCK_SIZE)
        {
            if (status == -1)
                perror("read");
            else
    	        printf("read: Only %d bytes read should be %d\n", 
                       status, BLOCK_SIZE);

            /* Free data buffer */
            free(buffer);

            goto exit;
        }

#ifdef VERIFY_ALL
        bufP = (unsigned long long *)abuffer;
        for (i = 0; i < BLOCK_SIZE / sizeof(unsigned long long); ++i)
        {
            if (*bufP != nextRead)
                printf("%d Buffer mismatch sb %llx is %llx\n", i,
                       nextRead, *bufP);
            ++bufP;
            ++nextRead;
        }
#endif
#ifdef VERIFY_FIRST
        bufP = (unsigned long long *)abuffer;
        if (*bufP != nextRead)
            printf("Buffer mismatch sb %llx is %llx\n", nextRead, *bufP);
        nextRead += BLOCK_SIZE;
#endif
	
    }

    /* Free data buffers */
    free (buffer);

exit:

    /* Notify caller that test has completed */
    cyg_semaphore_post(&test_cmpl_sema);

    /* Exit thread */
    cyg_thread_exit();
}
예제 #10
0
int main(int argc, char *argv[])
{
    int       i;
    int       disk;
    int       fd[NUM_DISKS];

    // Initialize the ISP Fibre Channel Controller
    isp_controller_init(0);

    // Create test complete semaphore
    cyg_semaphore_init(&test_cmpl_sema, 0);

    /* Open a connection to the first NUM_DISKS found.
    */
    for (i = 0, disk = 0; (disk < NUM_DISKS) && (i < MAX_NUM_DISKS); ++i)
    {
        cyg_io_handle_t handle;
        Cyg_ErrNo       status;
        char            device[10];

        sprintf(device, "/dev/sd%d", i);

        /* See if this device is present */
        status = cyg_io_lookup(device, &handle);
        if (status == ENOERR)
        {
            fd[disk] = open(device, O_RDWR, 0);
            if (fd[disk] == -1)
            {
                printf ("disktest: open() failed for %s - ", device);
                perror("open");
                return(-1);
            }
            ++disk;
        }
    }

    printf("\nPerforming disktest on %d disk(s)\n", disk);

    // Create tasks to perform disk testing.  Each task tests one disk.
    for (i = 0; i < disk; ++i)
    {
        cyg_thread_create(15, &disktest, fd[i], "disktest", &stack[i][0],
                          CYGNUM_HAL_STACK_SIZE_TYPICAL, 
                          &disktest_handle[i], &disktest_thread[i]);
        cyg_thread_resume(disktest_handle[i]);
    }

    // Wait for test to complete
    for (i = 0; i < disk; ++i)
        cyg_semaphore_wait(&test_cmpl_sema);

    // Close all the files 
    for (i = 0; i < disk; ++i)
        close(fd[i]);

    // Delete semaphore
    cyg_semaphore_destroy(&test_cmpl_sema);

    printf("disktest completed.\n");

    cyg_thread_exit();

    // Should never get here
    return(0);
}
예제 #11
0
void test_sendmsg_server(cyg_addrword_t pnetdata)
{
	int s, i, sendmsglen;
	struct sockaddr_in sa, r_sa;
	struct hostent *hp;
	struct msghdr msghdr_msg;
	struct iovec *piov;
	int j;
	int perIov_len;
	char tmp[TEST_SENDMSG_MSG_LEN];
	
	int threadid;
		
	int port = ((TEST_SENDMSG_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_SENDMSG_DATA_T*)pnetdata)->pbuf;
	
	threadid = port;
	
    if(inet_aton(TEST_SENDMSG_SERVER_ADDR, &sa.sin_addr, pbuf, RNT_BUFFER_LEN) == 0)
    {
		test_printf_error("test_sendmsg_server");
		cyg_thread_exit();
    }
    sa.sin_family = AF_INET;
    sa.sin_port = htons(IPPORT_USERRESERVED + port);
    
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_sendmsg_server");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_sendmsg_server");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_sendmsg_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_sendmsg_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}

    if(connect(s, (struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN) == -1)
    {
		test_printf_error("test_sendmsg_server");
		cyg_thread_exit();
    }
	
    for(i = 0; i < TEST_SENDMSG_WRITE_TIMES; i++)
	{
		strcpy(tmp, TEST_SENDMSG_MSG);
		
		perIov_len = TEST_SENDMSG_MSG_PART_LEN;
		piov = malloc(TEST_SENDMSG_MSG_PARTS*sizeof(struct iovec));
		if(piov == NULL)
		{
			test_printf_error("test_sendmsg_server");
			goto fail;
		}
		memset(piov, 0, TEST_SENDMSG_MSG_PARTS*sizeof(struct iovec));
		
		for(j = 0; j < TEST_SENDMSG_MSG_PARTS; j++)
		{
			piov[j].iov_base = malloc(perIov_len);
			if(piov[j].iov_base == NULL)
			{
				test_printf_error("test_sendmsg_server");
				for(i = 0; i < j; i++) free(piov[j].iov_base);
				if(piov != NULL) free(piov);
				goto fail;
			}
			piov[j].iov_len = perIov_len;
			memcpy(piov[j].iov_base, tmp + j * TEST_SENDMSG_MSG_PART_LEN, TEST_SENDMSG_MSG_PART_LEN);
		}
		
		msghdr_msg.msg_name = NULL;
		msghdr_msg.msg_namelen = 0;
		msghdr_msg.msg_iov = piov;
		msghdr_msg.msg_iovlen = TEST_SENDMSG_MSG_PARTS;
		msghdr_msg.msg_control = NULL;
		msghdr_msg.msg_controllen = 0;
		msghdr_msg.msg_flags = 0;
		
		sendmsglen = sendmsg(s, &msghdr_msg, 0, pbuf, RNT_BUFFER_LEN);
		if(sendmsglen < 0)
		{
			test_printf_error("test_sendmsg_server");
			for(j = 0; j < TEST_SENDMSG_MSG_PARTS; j++)
			{
				if(piov[j].iov_base != NULL) free(piov[j].iov_base);
			}
			if(piov != NULL) free(piov);
			break;
		}
		
		for(j = 0; j < TEST_SENDMSG_MSG_PARTS; j++)
		{
			if(piov[j].iov_base != NULL) free(piov[j].iov_base);
		}
		if(piov != NULL) free(piov);
	}
	
	test_printf_success("test_sendmsg_server");
	
fail:	
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
예제 #12
0
void test_getsockname_server(cyg_addrword_t pnetdata)
{
	int s, new_s;
	struct sockaddr_in sa, r_sa;
	int r_sa_l = sizeof(r_sa);
	struct hostent *hp;
	
	int port = ((TEST_GETSOCKNAME_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_GETSOCKNAME_DATA_T*)pnetdata)->pbuf;
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_getsockname_entry");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_getsockname_entry");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_getsockname_entry");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_getsockname_entry");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(getsockname(-1, (struct sockaddr*)&sa, &r_sa_l, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("getsockname");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(getsockname(s, NULL, &r_sa_l, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("getsockname");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(getsockname(s, (struct sockaddr*)&sa, NULL, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("getsockname");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	r_sa_l = 0;
	if(getsockname(s, (struct sockaddr*)&sa, &r_sa_l, pbuf, RNT_BUFFER_LEN) != 0)
	{
		test_printf_error("getsockname");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	r_sa_l = sizeof(r_sa);
	
	if(getsockname(s, (struct sockaddr*)&sa, &r_sa_l, NULL, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("getsockname");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(getsockname(s, (struct sockaddr*)&sa, &r_sa_l, pbuf, 0) != -1)
	{
		test_printf_error("getsockname");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(listen(s, 10, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_getsockname_entry");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if((new_s = accept(s, (struct sockaddr*)&sa, (size_t*)&r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_getsockname_entry");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(getpeername(-1, (struct sockaddr*)&sa, &r_sa_l, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("getpeername");
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(getpeername(new_s, NULL, &r_sa_l, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("getpeername");
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(getpeername(new_s, (struct sockaddr*)&sa, NULL, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("getpeername");
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	r_sa_l = 0;
	if(getpeername(new_s, (struct sockaddr*)&sa, &r_sa_l, pbuf, RNT_BUFFER_LEN) != 0)
	{
		test_printf_error("getpeername");
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	r_sa_l = sizeof(r_sa);
	
	if(getpeername(new_s, (struct sockaddr*)&sa, &r_sa_l, NULL, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("getpeername");
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(getpeername(new_s, (struct sockaddr*)&sa, &r_sa_l, pbuf, 0) != -1)
	{
		test_printf_error("getpeername");
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	
	test_printf_success("test_getsockname_entry");
	netclose(new_s, pbuf, RNT_BUFFER_LEN);
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
예제 #13
0
void test_netselect_server(cyg_addrword_t pnetdata)
{
	int s, new_s, readlen;
	struct sockaddr_in sa, r_sa;
	int r_sa_l = sizeof(r_sa);
	struct hostent *hp;
	int ierr = 0;
	
	fd_set readset;
	
	int threadid;
		
	int port = ((TEST_NETSELECT_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_NETSELECT_DATA_T*)pnetdata)->pbuf;
	char *preadbuf = ((TEST_NETSELECT_DATA_T*)pnetdata)->pnetselectbuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_netselect_server");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_netselect_server");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_netselect_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_netselect_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}

	if(listen(s, 10, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_netselect_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if((new_s = accept(s, (struct sockaddr*)&sa, (size_t*)&r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_netselect_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	while(1)
	{
		FD_ZERO(&readset);
		FD_SET(new_s, &readset);
		
		if(netselect(new_s + 1, &readset, NULL, NULL, NULL, pbuf, RNT_BUFFER_LEN) == -1)
		{
			test_printf_error("test_netselect_server");
			ierr = 1;
			break;
		}
		
		if(FD_ISSET(new_s, &readset))
		{
			readlen = netread(new_s, preadbuf, TEST_NETSELECT_MSG_LEN, pbuf, RNT_BUFFER_LEN);
			if(readlen < 0)
			{
				test_printf_error("test_netselect_server");
				ierr = 1;
				break;
			}
			if(readlen == 0)
			{
				break;
			}
			if(readlen > 0)
			{
				if(strcmp(preadbuf, TEST_NETSELECT_MSG) != 0)
				{
					ierr = 1;
					test_printf_error("test_netselect_server");
					break;
				}
			}
		}
	}
	
	if(ierr == 0)
		test_printf_success("test_netselect_server");
	
	netclose(new_s, pbuf, RNT_BUFFER_LEN);
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
예제 #14
0
void simple_udpserver(cyg_addrword_t pnetdata)
{
	int s, new_s, i, len;
	char *msg;
	struct sockaddr_in sa = {0}, r_sa = {0};
	int r_sa_l = sizeof(r_sa);
	struct hostent *hp;
	
	int threadid;
		
	int port = ((NET_DATA_T*)pnetdata)->iport;
	char *pbuf = ((NET_DATA_T*)pnetdata)->pbuf;
	
	threadid = cyg_thread_self();
	
	msg = malloc(BUFSIZE);
	if(msg == NULL)
	{
		printf("simple_udpserver(%d): malloc error\n", threadid);
		cyg_thread_exit();
	}
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		printf("simple_udpserver(%d): gethostbyname error\n", threadid);
		cyg_thread_exit();
	}
	printf("simple_udpserver(%d): gethostbyname ok\n", threadid);
	
	//memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_addr.s_addr = htonl(INADDR_ANY);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	printf("simple_udpserver(%d): final addr=%s\n", threadid, inet_ntoa(r_sa.sin_addr, pbuf, RNT_BUFFER_LEN));
	printf("simple_udpserver(%d): receive from port %d\n", threadid, ntohs(r_sa.sin_port));
	
	if((s = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		printf("simple_udpserver(%d): socket error\n", threadid);
		cyg_thread_exit();
	}
	printf("simple_udpserver(%d): create socket success...\n", threadid);
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		printf("simple_udpserver(%d): bind error\n", threadid);
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	printf("simple_udpserver(%d): bind success...\n", threadid);
	
	{
		struct sockaddr_in sockname;
		int socklen;
		
		if(getsockname(s, (struct sockaddr*)&sockname, &socklen, pbuf, RNT_BUFFER_LEN) != -1)
		{
			printf("simple_udpserver(%d): getsockname sin_family=%d sin_port=%d sin_addr=%s socklen=%d\n", threadid, 
				sockname.sin_family, ntohs(sockname.sin_port), inet_ntoa(sockname.sin_addr, pbuf, RNT_BUFFER_LEN), socklen);
		}
		else
		{
			printf("simple_udpserver(%d): getsockname error\n", threadid);
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
	}
	
	i = 0;
	while(1)
	{
		if((len = recvfrom(s, msg, BUFSIZE, 0, (struct sockaddr*)&sa, &r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1)
		{
			printf("simple_udpserver(%d): recvfrom error\n", threadid);
			continue;
		}
		if(len == 0)
		{
			printf("simple_udpserver(%d): recvfrom 0, connection broken!!!!!\n", threadid);
			continue;
		}
		msg[len] = '\0';
		//printf("simple_udpserver(%d): recvfrom(%s:%d): %s\n", threadid, inet_ntoa(sa.sin_addr, pbuf, RNT_BUFFER_LEN), ntohs(sa.sin_port), msg);
		
		//len = sprintf(msg, "ack%d", i);
		len = 40000;
		memcpy(msg, g_LargeData, len);
		if(sendto(s, msg, len, 0,  (struct sockaddr*)&sa, r_sa_l, pbuf, RNT_BUFFER_LEN) == -1)
		{
			printf("simple_udpserver(%d): sendto error\n", threadid);
			continue;
		}
		if(len == 0)
		{
			printf("simple_udpserver(%d): sendto 0, connection broken!!!!!\n", threadid);
			continue;
		}
		i++;
	}
	
	netclose(s, pbuf, RNT_BUFFER_LEN);
	
	free(msg);
	cyg_thread_exit();
}
예제 #15
0
파일: test_send.c 프로젝트: LucidOne/Rovio
void test_send_server(cyg_addrword_t pnetdata)
{
	int s, i, len, sendlen;
	struct sockaddr_in sa, r_sa;
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_SEND_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_SEND_DATA_T*)pnetdata)->pbuf;
	char *psendbuf = ((TEST_SEND_DATA_T*)pnetdata)->psendbuf;
	
	threadid = port;
	
    if(inet_aton(TEST_SEND_SERVER_ADDR, &sa.sin_addr, pbuf, RNT_BUFFER_LEN) == 0)
    {
		test_printf_error("test_send_server");
		cyg_thread_exit();
    }
    sa.sin_family = AF_INET;
    sa.sin_port = htons(IPPORT_USERRESERVED + port);
    
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_send_server");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_send_server");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_send_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_send_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_send_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}

    if(connect(s, (struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN) == -1)
    {
		test_printf_error("test_send_server");
		cyg_thread_exit();
    }
	
    for(i = 0; i < TEST_SEND_WRITE_TIMES; i++)
	{
		len = sprintf(psendbuf, "%s", TEST_SEND_MSG);
		len++;
		sendlen = send(s, psendbuf, len, 0, pbuf, RNT_BUFFER_LEN);
		if(sendlen < 0)
		{
			test_printf_error("test_send_server");
			break;
		}
	}
	
	if(i == TEST_SEND_WRITE_TIMES)
		test_printf_success("test_send_server");
	
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
예제 #16
0
파일: test_recv.c 프로젝트: LucidOne/Rovio
void test_recv_server(cyg_addrword_t pnetdata)
{
	int s, new_s;
	struct sockaddr_in sa, r_sa;
	int r_sa_l = sizeof(r_sa);
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_RECV_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_RECV_DATA_T*)pnetdata)->pbuf;
	char *precvbuf = ((TEST_RECV_DATA_T*)pnetdata)->precvbuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_recv_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}

	if(listen(s, 10, pbuf, RNT_BUFFER_LEN) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}
	
	if((new_s = accept(s, (struct sockaddr*)&sa, (size_t*)&r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}
	
	if(recv(-1, precvbuf, TEST_RECV_MSG_LEN, 0, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	if(recv(new_s, NULL, TEST_RECV_MSG_LEN, 0, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	// recv 0 will block and wait for client's write. (But netread wouldn't)
	/*
	if(recv(new_s, precvbuf, 0, 0, pbuf, RNT_BUFFER_LEN) != 0)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	*/
	
	if(recv(new_s, precvbuf, -1, 0, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	if(recv(new_s, precvbuf, TEST_RECV_MSG_LEN, -1, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	if(recv(new_s, precvbuf, TEST_RECV_MSG_LEN, 0, NULL, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	if(recv(new_s, precvbuf, TEST_RECV_MSG_LEN, 0, pbuf, 0) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	test_printf_success("test_recv_server");
	
fail:
	netclose(new_s, pbuf, RNT_BUFFER_LEN);
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
예제 #17
0
파일: main.c 프로젝트: VEROS/VEROS
/* --------------------------------------------------------------------------
 *  start_thread
 *
 *  ARM start thread function
 *
 *  @param	: cyg_addrword_t data
 *  @return	: void
 *  @see	: fpga, parse, create_structure, exti_thread, led_thread
 *
 *  Process:
 *    01. Refresh FPGA;
 *    02. Configure GPIO register;
 *    03. Configure EXTI register;
 *    04. Parse binary file;
 *    05. Create data structure;
 *    06. Attach interrupt;
 *    07. Report device address and data.
 * --------------------------------------------------------------------------
 */
void start_thread(cyg_addrword_t data)
{
	HAL_WRITE_UINT32(CYGARC_REG_SYSTICK_BASE+CYGARC_REG_SYSTICK_RELOAD, 2099 ); 

	cyg_tick_count_t aticks = cyg_current_time();
	if (fpga() == 0)
	{
#ifdef MVBPROJ_BASIC_INFO_SUPPORT
		printf("FPGA init :: ERROR :: ARM STOP\n");
#endif // ifdef MVBPROJ_BASIC_INFO_SUPPORT
		cyg_thread_exit();
	}
	
	cyg_tick_count_t bticks = cyg_current_time();
	UNSIGNED32 time = ((UNSIGNED32)bticks - (UNSIGNED32)aticks) / 10;
	printf("FPGA init :: PROGRAMMING TIME :: %d ms\n", time);

#ifdef MVBPROJ_BASIC_INFO_SUPPORT
	printf("...configuring GPIO...\n");
#endif // ifdef MVBPROJ_BASIC_INFO_SUPPORT
	mvb_arm_init_all_gpio();
	
#ifdef MVBPROJ_BASIC_INFO_SUPPORT
	printf("...configuring EXTI...\n");
#endif // ifdef MVBPROJ_BASIC_INFO_SUPPORT
	mvb_arm_init_exti();

	cyg_thread_create(EXTI_WORKTHREAD_PRI, exti_thread, 0, "EXTI CONFIGURATION", &exti_stack, STACK_SIZE, &exti_thread_handle, &exti_thread_data);
	
	aticks = cyg_current_time();
	if (parse() == 0)
	{
#ifdef MVBPROJ_BASIC_INFO_SUPPORT
		printf("Binary init :: ERROR :: Parsing error, no config data.\n");
#endif // ifdef MVBPROJ_BASIC_INFO_SUPPORT
	}
	bticks = cyg_current_time();
	time = ((UNSIGNED32)bticks - (UNSIGNED32)aticks) / 10;
	printf("Binary init :: ELAPSED TIME :: %d ms\n", time);
	
#ifdef MVBPROJ_BASIC_INFO_SUPPORT
	printf("...creating data structure...\n");
#endif // ifdef MVBPROJ_BASIC_INFO_SUPPORT
	create_structure();

#ifdef MVBPROJ_BASIC_INFO_SUPPORT
	printf("...attaching interrupt...\n");
#endif // ifdef MVBPROJ_BASIC_INFO_SUPPORT

#ifdef MVBPROJ_LED_SUPPORT
	cyg_thread_create(LED_WORKTHREAD_PRI, led_thread, 0, "LED SUPPORT", &led_stack, STACK_SIZE, &led_thread_handle, &led_thread_data);
#endif // ifdef MVBPROJ_LED_SUPPORT

    cyg_thread_resume(exti_thread_handle); // Start it

#ifdef MVBPROJ_LED_SUPPORT
	cyg_thread_resume(led_thread_handle);
#endif // ifdef MVBPROJ_LED_SUPPORT

	cyg_thread_exit();
}
예제 #18
0
void test_tcp_socket_client(cyg_addrword_t pnetdata)
{
	int s, i, len;
	char msg;
	struct sockaddr_in sa, r_sa;
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_TCP_SOCKET_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_TCP_SOCKET_DATA_T*)pnetdata)->pbuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_tcp_socket_client");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	memcpy(&(sa.sin_addr), hp->h_addr_list0, hp->h_length);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(IPPORT_USERRESERVED + port - TEST_TCP_SOCKET_SERVER_NUM);
    
    for(i = 0; i < TEST_TCP_ACCEPT_SOCKET_TIMES; i++)
    {
		if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
		{
			test_printf_error("test_tcp_socket_client");
			cyg_thread_exit();
		}
		
		if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
		{
			test_printf_error("test_tcp_socket_client");
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
		
		if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
		{
			test_printf_error("test_tcp_socket_client");
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
    
	    while(connect(s, (struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN) == -1) NULL;
	    
		if((len = netread(s, &msg, sizeof(msg), pbuf, RNT_BUFFER_LEN)) == -1)
		{
			test_printf_error("test_tcp_socket_client");
			break;
		}
		if(len == 0)
		{
			netclose(s, pbuf, RNT_BUFFER_LEN);
			continue;
		}
    }
    
	test_printf_success("test_tcp_socket_client");
	cyg_thread_exit();
}
//os void smbd_process(int ProcSockID, void *nouse1, void *nouse2)
void smbd_process (cyg_addrword_t data)
{
	int ProcSockID = data;
//0704  extern int smb_echo_count;
//0705  time_t last_timeout_processing_time = time(NULL);

/* 0416 from global variable */
//char __inbuf[SMB_BUFFER_SIZE + SAFETY_MARGIN];
//0621char __oubuf[SMB_BUFFER_SIZE + SAFETY_MARGIN];

//Jesse char __inbuf[SMB_BUFFER_SIZE];
//Jesse char __oubuf[MAX_SEND_SIZE];
char __inbuf[SMB_BUFFER_SIZE + 16];
char __oubuf[MAX_SEND_SIZE + 16];
char *InBuffer = __inbuf;
char *OutBuffer = __oubuf;
int i, threadid=0;
int testInbufsize=0, testOutbufsize=0;

	testInbufsize = sizeof(__inbuf);
	testOutbufsize = sizeof(__oubuf);
//  InBuffer += SMB_ALIGNMENT;   // ??? why ??? 7/18/2001 by ron
//  OutBuffer += SMB_ALIGNMENT;
  /* re-initialise the timezone */
//0302  TimeInit();

  for (i =0; i < NUM_SMBTHREAD; i++){
   	if (smbthread[i] == ProcSockID){
		threadid =i;
		break;
	}
  }		

  if (threadid >= NUM_SMBTHREAD)
     threadid = -1;		
  		
  thread_isused[threadid]++;  //for debug
  while (True)
  {
//    int change_notify_timeout = 1 * 1000;
    int got_smb = False;
    
//0705    int select_timeout = SMBD_SELECT_TIMEOUT*1000;//setup_select_timeout();

	check_idle_conn(RELEASE_RESOURCE_TIMEOUT, threadid);  // check Idle Connection ....Ron Add 3/8/2002
    while(!receive_message_or_smb(InBuffer,SMB_BUFFER_SIZE, &got_smb
					,ProcSockID ,threadid) || _forceclosethread[threadid] ==1)
    {
        if(!timeout_processing(SET_SMBD_TIMEOUT, threadid)){
	  	    conn_close_all(threadid);
			pipe_close_all(threadid); //7/19/2001 by ron
		    reset_global_variables(threadid);
//Jesse		    close_s(ProcSockID); //close socket 6/29/2001 by ron
		    close(ProcSockID); //close socket
			cyg_thread_exit();
            return;
	    }	
//0418      num_smbs = 0; /* Reset smb counter. */
//os		kwait(NULL);
		cyg_thread_yield();
    } //while

	if (threadid < 0 ||ProcSockID < 0)
		{
		cyg_thread_exit();			
		return;
		}

    if(got_smb) {
      /*
       * Ensure we do timeout processing if the SMB we just got was
       * only an echo request. This allows us to set the select
       * timeout in 'receive_message_or_smb()' to any value we like
       * without worrying that the client will send echo requests
       * faster than the select timeout, thus starving out the
       * essential processing (change notify, blocking locks) that
       * the timeout code does. JRA.
       */ 
//0704      int num_echos = smb_echo_count;

      process_smb(InBuffer, OutBuffer, ProcSockID, threadid);
    } //if 
//os	kwait(NULL);
	cyg_thread_yield();
  } //while

}