⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t_bind.c

📁 T-kernel Tcp/ip Protocol Stack Sample
💻 C
字号:
/* *  Copyright (C) 1999-2003 MITSUBISHI ELECTRIC CORPORATION and *  RENESAS SOLUTIONS CORPORATION and *  RENESAS TECHNOLOGY CORPORATION *  All rights reserved. * *  TCP/IP Test Application program [API]. */#include <tk/tkernel.h>#include <renesas_tcpip.h>#include <renesas_tcpip_common.h>#include "config.h"voidtest_task(int stacd, void *exinf){	int	i, fd, s, s2;	int	ret;	struct sockaddr_in name;	int	namelen;	int num;	int testno, testno2, result[21];	num = 1;	testno = 0;	memset((char *)result, 0, sizeof(result));	/* for setup: get normal socket */	errno = 0;	if ((s = unix_socket(AF_INET, SOCK_STREAM, 0)) < 0) {		goto testend;	}	memset((char *)&name, 0, sizeof(name));	name.sin_family = AF_INET;	name.sin_addr.s_addr = inet_addr(TGT_ADDR);	name.sin_port = htons(TGT_PORT);	name.sin_len = sizeof(struct sockaddr_in);	namelen = sizeof(name);			/* Test 1 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 1;	ret = unix_bind(-1, (struct sockaddr *)&name, namelen);	if (ret < 0) {		printf("OK! : Bad socket fd(-1) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! : Bad socket fd(-1) errno=%d\n",errno);		result[testno] = 1;	}	/* Test 2 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 2;	ret = unix_bind(10, (struct sockaddr *)&name, namelen);	if (ret < 0) {		printf("OK! : Bad socket fd(10) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! : Bad socket fd(10) errno=%d\n",errno);		result[testno] = 1;	}	/* Test 3 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 3;	ret = unix_bind(s, (struct sockaddr *)&name, namelen);	if (ret == 0) {		printf("OK! : Port number(%d)\n", TGT_PORT);		result[testno] = 0;	}	else {		printf("Fail! : Port number(%d) errno=%d\n", TGT_PORT, errno);		result[testno] = 1;	}	/* we close this socket for next test and create a new socket*/	unix_close(s);	errno = 0;	if ((s = unix_socket(AF_INET, SOCK_STREAM, 0)) < 0) {		printf("socket fail\n");		goto	dgramtest;	}	/* Test 4 */	printf("Bind Test %d: \n",num++);		name.sin_port = 0;	name.sin_addr.s_addr = inet_addr(BAD_ADDR);	if (name.sin_addr.s_addr == (u_long)-1)		printf("BAD_ADDR is bad\n");	else {		errno = 0;testno = 4;		ret = unix_bind(s, (struct sockaddr *)&name, namelen);		if (ret < 0) {			printf("OK! : IP address bad %s errno=%d\n",BAD_ADDR,errno);			result[testno] = 0;		}		else {			printf("Fail! : IP address bad %s errno=%d\n",BAD_ADDR,errno);			result[testno] = 1;		}	}	name.sin_addr.s_addr = INADDR_ANY;	/* Test 5 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 5;	ret = unix_bind(s, (struct sockaddr *)NULL, namelen);	if (ret < 0) {		printf("OK! :bad sockaddr_in address(NULL) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :sockaddr_in address(NULL) errno=%d\n",errno);		result[testno] = 1;	}	/* Test 6 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 6;	ret = unix_bind(s, (struct sockaddr *)-1, namelen);	if (ret < 0) {		printf("OK! :bad sockaddr_in address(-1) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :bad sockaddr_in address(-1) but 0 returned\n");		result[testno] = 1;	}	/* Test 7 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 7;	ret = unix_bind(s, (struct sockaddr *)&name, 0);	if (ret < 0) {		printf("OK! :bad name length(0) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :bad name length(0) errno=%d\n",errno);		result[testno] = 1;	}	/* Test 8 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 8;	ret = unix_bind(s, (struct sockaddr *)&name, -1);	if (ret < 0) {		printf("OK! :bad name length(-1) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :bad name length(-1) errno=%d\n",errno);		result[testno] = 1;	}	name.sin_port = htons(TGT_PORT);	/* Test 9 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 9;	ret = unix_bind(s, (struct sockaddr *)&name, namelen);	if (ret < 0) {		printf("normal bind failed\n");		result[testno] = 1;	} else {		printf("OK! :nomal bind");		errno = 0;		if ((s2 = unix_socket(AF_INET, SOCK_STREAM, 0)) < 0) {			printf("socket fail\n");		} else {			errno = 0;testno = 10;			ret = unix_bind(s2, (struct sockaddr *)&name, namelen);			if (ret < 0) {				printf("OK! :address reused errno=%d\n",errno);				result[testno] = 0;			}			else {				printf("Fail! :address reused errno=%d\n",errno);				result[testno] = 1;			}		}		unix_close(s2);	}	name.sin_port = 0;	/* Test 10 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 11;	ret = unix_bind(s, (struct sockaddr *)&name, namelen);	if (ret < 0) {		printf("OK! :2nd bind to same socket (port=0) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :2nd bind to same socket (port=0) errno=%d\n",errno);		result[testno] = 1;	}	unix_close(s);dgramtest:	printf("\nDGRAM socket abnormal case\n");	num = 1;	/* for setup: get normal socket */	errno = 0;	if ((s = unix_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {		goto testend;	}	name.sin_family = AF_INET;	name.sin_port = 0;	name.sin_addr.s_addr = INADDR_ANY;	name.sin_len = sizeof(struct sockaddr_in);	namelen = sizeof(name);	name.sin_port = htons(1);	/* Test 1 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 12;	ret = unix_bind(s, (struct sockaddr *)&name, namelen);	if (ret == 0) {		printf("OK! :port address(1) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :port address(1) errno=%d\n",errno);		result[testno] = 1;	}	/* we close this socket for next test and create a new socket*/	unix_close(s);	errno = 0;	if ((s = unix_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {		goto testend;	}	/* Test 2 */	printf("Bind Test %d: \n",num++);		name.sin_port = 0;	name.sin_addr.s_addr = inet_addr(BAD_ADDR);	if (name.sin_addr.s_addr == (u_long)-1)		printf("BAD_ADDR is bad\n");	else {		errno = 0;testno = 13;		ret = unix_bind(s, (struct sockaddr *)&name, namelen);		if (ret < 0) {			printf("OK! :bad IP address %s errno=%d\n",BAD_ADDR,errno);			result[testno] = 0;		}		else {			printf("Fail! :bad IP address %s errno=%d\n",BAD_ADDR,errno);			result[testno] = 1;		}	}	name.sin_addr.s_addr = INADDR_ANY;	/* Test 3 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 14;	ret = unix_bind(s, (struct sockaddr *)NULL, namelen);	if (ret < 0)		printf("OK! :bad sockaddr_in address(NULL) errno=%d\n",errno);	else		printf("Fail! :bad sockaddr_in address(NULL) errno=%d\n",errno);	/* Test 4 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 15;	ret = unix_bind(s, (struct sockaddr *)-1, namelen);	if (ret < 0) {		printf("OK! :bad sockaddr_in address(-1) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :bad sockaddr_in address(-1) errno=%d\n",errno);		result[testno] = 1;	}	/* Test 5 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 16;	ret = unix_bind(s, (struct sockaddr *)&name, 0);	if (ret < 0) {		printf("OK! :bad name length(0) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :bad name length(0) errno=%d\n",errno);		result[testno] = 1;	}	/* we close this socket for next test and create a new socket*/	unix_close(s);	errno = 0;	if ((s = unix_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {		goto testend;	}	/* Test 6 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 17;	ret = unix_bind(s, (struct sockaddr *)&name, -1);	if (ret < 0) {		printf("OK! :bad name length (-1) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :bad name length (-1) errno=%d\n",errno);		result[testno] = 1;	}	name.sin_port = htons(TGT_PORT);	/* Test 7 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 18;	ret = unix_bind(s, (struct sockaddr *)&name, namelen);	if (ret < 0) {		result[testno] = 1;	} else {		printf("OK! :nomal bind");		errno = 0;		if ((s2 = unix_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {			goto testend;		} else {			errno = 0;testno = 19;			ret = unix_bind(s2, (struct sockaddr *)&name, namelen);			if (ret < 0) {				printf("OK! :address reused errno=%d\n",errno);				result[testno] = 0;			}			else {				printf("Fail! :address reused errno=%d\n",errno);				result[testno] = 1;			}		}		unix_close(s2);	}	name.sin_port = 0;	/* Test 8 */	printf("Bind Test %d: \n",num++);		errno = 0;testno = 20;	ret = unix_bind(s, (struct sockaddr *)&name, namelen);	if (ret < 0) {		printf("OK! :2nd bind to same socket (port=0) errno=%d\n",errno);		result[testno] = 0;	}	else {		printf("Fail! :2nd bind to same socket (port=0) errno=%d\n",errno);		result[testno] = 1;	}testend:	unix_close(s);	/* check the results */	testno2 = 20;	for (i=1; i <= testno; i++) {		if (result[i] == 1)			break;	}	if (i > testno2)		printf("Pass.\n");	else		printf("Error.\n");#if defined(T_KERNEL)	tk_slp_tsk(TMO_FEVR);#endif}/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -