📄 tbind.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/tbind.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-1999 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: tbind.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:14 tneale * Tornado shuffle * * Revision 1.2 2001/01/19 22:24:53 paul * Update copyright. * * Revision 1.1 2000/10/16 19:22:21 paul * Renamed from test_bind.c * * Revision 1.11 2000/03/17 00:14:46 meister * Update copyright message * * Revision 1.10 1999/07/30 21:29:41 paul * enhanced test_fail, test_pass * * Revision 1.9 1999/06/29 18:16:39 paul * Remove invalid-AF test, since Attache behavior diverges from BSD. * * Revision 1.8 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.6 1999/05/03 19:22:57 paul * Removed privileged-port and loopback tests. * * Revision 1.5 1999/03/25 20:10:12 wes * A few cleanups for the REUSEPORT/REUSEADDR tests * * Revision 1.4 1999/02/18 04:16:00 wes * Socket Merge: new code (socket tests). * * Revision 1.3.2.4 1998/11/06 23:36:49 paul * rototill to support tcp * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include "test.h"static void test_bind_1(int type, char *proto);voidtest_bind(int flags){ struct sockaddr_in si; memset(&si, 0, sizeof(si)); test(0, "bind invalid descriptor", bind(-1, (struct sockaddr *) &si, sizeof(si)), -1, EBADF);#ifdef TEST_BSD test(0, "bind file descriptor", bind(1, (struct sockaddr *) &si, sizeof(si)), -1, ENOTSOCK);#endif if (flags & TEST_UDP) test_bind_1(SOCK_DGRAM, "udp"); if (flags & TEST_TCP) test_bind_1(SOCK_STREAM, "tcp");}static voidtest_bind_1(int type, char *proto){ struct sockaddr_in si; int s, s2, opt; s = socket(PF_INET, type, 0); if (s < 0) { test_fail(proto, "bind", "socket"); return; } memset(&si, 0, sizeof(si)); test(proto, "bind null sockaddr", bind(s, 0, 0), -1, EINVAL); test(proto, "bind invalid namelen", bind(s, (struct sockaddr *) &si, 0), -1, EINVAL); si.sin_family = AF_INET; test(proto, "bind zero address/port", bind(s, (struct sockaddr *) &si, sizeof(si)), 0, 0); pr_sockname(s); close(s); s = socket(PF_INET, type, 0); si.sin_port = htons(3456); test(proto, "bind port in user range", bind(s, (struct sockaddr *) &si, sizeof(si)), 0, 0); pr_sockname(s); test(proto, "bind already-bound socket (null sockaddr)", bind(s, 0, 0), -1, EINVAL); pr_sockname(s); test(proto, "bind already-bound socket (same port)", bind(s, (struct sockaddr *) &si, sizeof(si)), -1, EINVAL); pr_sockname(s); si.sin_port = htons(3457); test(proto, "bind already-bound socket (different port)", bind(s, (struct sockaddr *) &si, sizeof(si)), -1, EINVAL); pr_sockname(s); si.sin_port = htons(0); test(proto, "bind already-bound socket (zero port)", bind(s, (struct sockaddr *) &si, sizeof(si)), -1, EINVAL); pr_sockname(s); si.sin_addr.s_addr = htonl(0); test(proto, "bind already-bound socket (zero addr)", bind(s, (struct sockaddr *) &si, sizeof(si)), -1, EINVAL); pr_sockname(s); s2 = socket(PF_INET, type, 0); if (s2 < 0) { test_fail(proto, "bind", "2nd socket"); return; } memset(&si, 0, sizeof(si)); si.sin_family = AF_INET; si.sin_addr.s_addr = htonl(0x7f000001); si.sin_port = htons(3456); test(proto, "bind port already allocated", bind(s2, (struct sockaddr *) &si, sizeof(si)), -1, EADDRINUSE); pr_sockname(s2); opt = -1; if (setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) != 0) test_fail(proto, "bind", "setsockopt(SO_REUSEADDR)"); else { test(proto, "bind port already allocated (SO_REUSEADDR)", bind(s2, (struct sockaddr *) &si, sizeof(si)), 0, 0); pr_sockname(s2); } close(s); s = socket(PF_INET, type, 0); if (s < 0) { test_fail(proto, "bind", "bind: 3rd socket"); return; } close(s2); s2 = socket(PF_INET, type, 0); if (s2 < 0) { test_fail(proto, "bind", "bind: 4th socket"); return; } memset(&si, 0, sizeof(si)); si.sin_family = AF_INET; si.sin_port = htons(3456); test(proto, "bind again", bind(s, (struct sockaddr *) &si, sizeof(si)), 0, 0); opt = -1; if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) != 0) test_fail(proto, "bind", "setsockopt(SO_REUSEPORT"); else if (setsockopt(s2, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) != 0) test_fail(proto, "bind", "setsockopt(SO_REUSEPORT) (s2)"); else { test(proto, "bind port already allocated (SO_REUSEPORT)", bind(s2, (struct sockaddr *) &si, sizeof(si)), 0, 0); pr_sockname(s2); } close(s); s = socket(PF_INET, type, 0); si.sin_addr.s_addr = htonl(0x01020304L); si.sin_port = htons(0); test(proto, "bind 1.2.3.4", bind(s, (struct sockaddr *) &si, sizeof(si)), -1, EADDRNOTAVAIL); pr_sockname(s); si.sin_addr.s_addr = htonl(0xffffffffL); test(proto, "bind 255.255.255.255", bind(s, (struct sockaddr *) &si, sizeof(si)), -1, EADDRNOTAVAIL); pr_sockname(s); si.sin_addr.s_addr = htonl(0x80e00101L); test(proto, "bind 128.224.1.1", bind(s, (struct sockaddr *) &si, sizeof(si)), -1, EADDRNOTAVAIL); pr_sockname(s); close(s); close(s2);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -