📄 tconnect.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/tconnect.c,v 1.1.1.1 2001/11/05 17:49:14 tneale Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: tconnect.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:14 tneale * Tornado shuffle * * Revision 1.3 2001/02/09 20:13:56 paul * Added INADDR_ANY test. * * Revision 1.2 2001/01/19 22:24:54 paul * Update copyright. * * Revision 1.1 2000/10/16 19:22:30 paul * Renamed from test_connect.c * * Revision 1.9 2000/03/17 00:14:47 meister * Update copyright message * * Revision 1.8 1999/07/30 21:29:41 paul * enhanced test_fail, test_pass * * Revision 1.7 1999/05/21 19:33:59 niqbal * PNA isn;t exactly like BSD, atleast in returning the error * numbers. So removing the TEST_PNA flag from code that should be tested * on BSD only. * * Revision 1.5 1999/05/03 19:23:10 paul * Removed loopback test, adding blocking timeout test. * * Revision 1.4 1999/02/18 04:16:01 wes * Socket Merge: new code (socket tests). * * Revision 1.3.2.8 1999/01/07 22:47:28 wes * whitespace tweak for legibility. * * Revision 1.3.2.7 1999/01/06 21:41:53 paul * added configurable server address * * Revision 1.3.2.6 1998/11/16 23:21:31 paul * added loopback tests * * Revision 1.3.2.5 1998/11/06 23:36:50 paul * rototill to support tcp * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include "test.h"static void test_connect_1(int type, char *proto);static void test_connect_udp(int s, char *proto);static void test_connect_tcp(int s, char *proto);voidtest_connect(int flags){ struct sockaddr_in si; memset(&si, 0, sizeof(si)); test(0, "connect invalid descriptor", connect(-1, (struct sockaddr *) &si, sizeof(si)), -1, EBADF);#ifdef TEST_BSD test(0, "connect file descriptor", connect(1, (struct sockaddr *) &si, sizeof(si)), -1, ENOTSOCK);#endif if (flags & TEST_UDP) test_connect_1(SOCK_DGRAM, "udp"); if (flags & TEST_TCP) test_connect_1(SOCK_STREAM, "tcp");}static voidtest_connect_1(int type, char *proto){ int s; struct sockaddr_in si; int eret, eerr; s = socket(PF_INET, type, 0); if (s < 0) { test_fail(proto, "connect", "socket"); return; } memset(&si, 0, sizeof(si)); test(proto, "connect null sockaddr", connect(s, 0, 0), -1, EINVAL); test(proto, "connect sockaddr wrong length", connect(s, (struct sockaddr *) &si, 0), -1, EINVAL); si.sin_family = 0; test(proto, "connect invalid AF", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EAFNOSUPPORT); si.sin_family = AF_INET; test(proto, "connect 0.0.0.0", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EADDRNOTAVAIL); si.sin_addr.s_addr = htonl(INADDR_BROADCAST); si.sin_port = htons(9); if (type == SOCK_DGRAM) { eret = 0; eerr = 0; } else { eret = -1; eerr = EACCES; } test(proto, "connect broadcast", connect(s, (struct sockaddr *) &si, sizeof(si)), eret, eerr); if (type == SOCK_DGRAM) test_connect_udp(s, proto); else test_connect_tcp(s, proto);}static voidtest_connect_udp(int s, char *proto){ struct sockaddr_in si; memset(&si, 0, sizeof(si)); si.sin_family = AF_INET;#if 0 si.sin_addr.s_addr = htonl(0x7f000001L); si.sin_port = htons(9); test(proto, "connect 127.0.0.1", connect(s, (struct sockaddr *) &si, sizeof(si)), 0, 0);#endif si.sin_addr.s_addr = htonl(0x0a000001); /* 10.0.0.1 */ si.sin_port = htons(9); test(proto, "connect 10.0.0.1", connect(s, (struct sockaddr *) &si, sizeof(si)), 0, 0); si.sin_addr.s_addr = htonl(0x0a000002); /* 10.0.0.2 */ si.sin_port = htons(9); test(proto, "connect 10.0.0.2", connect(s, (struct sockaddr *) &si, sizeof(si)), 0, 0); if (shutdown(s, 2) != 0) { test_fail(proto, "connect", "shutdown"); } else { si.sin_addr.s_addr = htonl(0x0a000003); /* 10.0.0.3 */ test(proto, "connect shutdown socket", connect(s, (struct sockaddr *) &si, sizeof(si)), 0, 0); } close(s);}static voidtest_connect_tcp(int s, char *proto){ int opt; struct sockaddr_in si; memset(&si, 0, sizeof(si)); si.sin_family = AF_INET; si.sin_addr.s_addr = htonl(0x0a000001); /* 10.0.0.1 */ si.sin_port = htons(9); if (verbose) printf("please stand by for blocking connect timeout...\n"); test(proto, "connect 10.0.0.1 (blocking)", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, ETIMEDOUT); close(s); s = socket(PF_INET, SOCK_STREAM, 0); opt = -1; if (ioctl(s, FIONBIO, (char *) &opt) == -1) perror("ioctl(FIONBIO)"); test(proto, "connect 10.0.0.1 (non-blocking)", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EINPROGRESS); test(proto, "connect 10.0.0.1 (again)", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EALREADY); si.sin_port = htons(99); test(proto, "connect connecting socket, different port", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EALREADY); close(s); s = socket(PF_INET, SOCK_STREAM, 0); si.sin_addr.s_addr = server; si.sin_port = htons(9999); test(proto, "connect port 9999", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, ECONNREFUSED); si.sin_port = htons(9); test(proto, "connect same host, different port", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EINVAL); close(s); s = socket(PF_INET, SOCK_STREAM, 0); si.sin_port = htons(9); test(proto, "connect new socket, port 9", connect(s, (struct sockaddr *) &si, sizeof(si)), 0, 0); si.sin_port = htons(9); test(proto, "connect port 9 (again)", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EISCONN); si.sin_port = htons(99); test(proto, "connect connected socket, different port", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EISCONN); close(s); s = socket(PF_INET, SOCK_STREAM, 0); if (shutdown(s, 2) != 0) { test_fail(proto, "connect", "shutdown"); } else { test(proto, "connect shutdown socket", connect(s, (struct sockaddr *) &si, sizeof(si)), -1, EINVAL); } close(s); s = socket(PF_INET, SOCK_STREAM, 0); memset(&si, 0, sizeof(si)); si.sin_family = AF_INET; si.sin_port = htons(9); test(proto, "connect INADDR_ANY", connect(s, (struct sockaddr *) &si, sizeof(si)),#if INSTALL_ATTACHE_SOCKETS_TCP_LOOPBACK_CONNECT 0, 0);#else -1, EADDRNOTAVAIL);#endif /* try to connect a listening socket */ close(s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -