📄 tsendto.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/tsendto.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: tsendto.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:14 tneale * Tornado shuffle * * Revision 1.3 2001/01/19 22:24:56 paul * Update copyright. * * Revision 1.2 2000/10/20 18:32:36 paul * si.sin_len is only defined for BSD * * Revision 1.1 2000/10/16 19:23:11 paul * Renamed from test_sendto.c * * Revision 1.11 2000/03/17 00:14:49 meister * Update copyright message * * Revision 1.10 1999/07/30 21:29:48 paul * enhanced test_fail, test_pass * * Revision 1.9 1999/06/29 18:17:10 paul * Added subnet broadcast test. * * Revision 1.7 1999/05/12 19:04:41 paul * Test for unconnected socket is tcp-specific. * * Revision 1.6 1999/05/03 19:24:11 paul * Removed loopback test. * * Revision 1.5 1999/03/25 20:13:37 wes * sendto attempt to provoke EMSGSIZE failed because socket was connected, * so do it earlier * * Revision 1.4 1999/02/18 04:16:15 wes * Socket Merge: new code (socket tests). * * Revision 1.3.2.7 1999/01/07 22:48:25 wes * Only try for EMSGSIZE on DGRAM socket, as the buffer we're using isn't * 16k long... * * Revision 1.3.2.6 1999/01/06 21:41:55 paul * added configurable server address * * Revision 1.3.2.5 1998/11/10 20:07:45 wes * avoid global variable name collisions * * Revision 1.3.2.4 1998/11/06 23:36:54 paul * rototill to support tcp * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include "test.h"static voidtest_st(char *proto, char *name, int s, void *snd_buf, size_t snd_len, int flags, struct sockaddr_in *to, int tolen, int expected, int expected_errno){ int ret; ret = test(proto, name, sendto(s, snd_buf, snd_len, flags, (struct sockaddr *) to, tolen), expected, expected_errno); if (ret >= 0) pr_sockname(s);}static char buf[32];static void test_sendto_1(int type, char *proto);voidtest_sendto(int flags){ test_st(0, "sendto invalid socket descr", -1, 0, 0, 0, 0, 0, -1, EBADF); if (flags & TEST_UDP) test_sendto_1(SOCK_DGRAM, "udp"); if (flags & TEST_TCP) test_sendto_1(SOCK_STREAM, "tcp");}/* This is a bit more convoluted than most test funcs. Y'see, tcp * sendto implicitly connects on the first sendto, then ignores the * sockaddr on all subsequent calls. */static voidtest_sendto_1(int type, char *proto){ int s; struct sockaddr_in si; int opt; int i, eret, eerr; for (i = 0; i < sizeof(buf); ++i) buf[i] = i; s = socket(PF_INET, type, 0); if (s < 0) { test_fail(proto, "sendto", "socket"); return; } memset(&si, 0, sizeof(si)); if (type == SOCK_DGRAM) eerr = EDESTADDRREQ; else eerr = ENOTCONN; test_st(proto, "sendto null sockaddr", s, 0, 0, 0, 0, 0, -1, eerr); test_st(proto, "sendto sockaddr wrong length", s, 0, 0, 0, &si, 0, -1, EINVAL); test_st(proto, "sendto invalid AF", s, 0, 0, 0, &si, sizeof(si), -1, EAFNOSUPPORT);#if INSTALL_ATTACHE_SOCKETS_44BSD si.sin_len = sizeof(struct sockaddr_in);#endif si.sin_family = AF_INET; si.sin_addr.s_addr = htonl(INADDR_ANY); si.sin_port = htons(0); test_st(proto, "sendto zero address", s, 0, 0, 0, &si, sizeof(si), -1, EADDRNOTAVAIL); if (type == SOCK_DGRAM) { si.sin_addr.s_addr = htonl(0x0a000001); /* bogus */ si.sin_port = htons(9); test_st(proto, "sendto 10.0.0.1", s, buf, sizeof(buf), 0, &si, sizeof(si), sizeof(buf), 0); /* someone eventually sends a host unreachable message */ } if (type == SOCK_STREAM) { close(s); s = socket(PF_INET, type, 0); si.sin_addr.s_addr = server; si.sin_port = htons(9); test_st(proto, "sendto unconnected socket", s, buf, sizeof(buf), 0, &si, sizeof(si), sizeof(buf), 0); /* BSD allows you to sendto an unconnected socket - automatically * connects, and sends data with the SYN packet, so you can send * at least one packet before the connection completes/fails. * SunOS/Solaris does not automatically connect, and returns * ENOTCONN. Attache automatically connects, but does not allow * sending until the connection is completed; it returns * EINPROGRESS on a non-blocking socket. */ } if (type == SOCK_STREAM) { close(s); s = socket(PF_INET, type, 0); } si.sin_port = htons(9); test_st(proto, "sendto port 9", s, buf, sizeof(buf), 0, &si, sizeof(si), sizeof(buf), 0); test_st(proto, "sendto zero-length message", s, 0, 0, 0, &si, sizeof(si), 0, 0); if (type == SOCK_DGRAM) { test_st(proto, "sendto message too big", s, buf, 16383, 0, &si, sizeof(si), -1, EMSGSIZE); } if (type == SOCK_STREAM) { close(s); s = socket(PF_INET, type, 0); } if (connect(s, (struct sockaddr *) &si, sizeof(si)) != 0) test_fail(proto, "sendto", "connect"); test_st(proto, "sendto null sockaddr on connected socket", s, buf, sizeof(buf), 0, 0, 0, sizeof(buf), 0); if (type == SOCK_STREAM) { eret = sizeof(buf); eerr = 0; } else { eret = -1; eerr = EISCONN; } test_st(proto, "sendto connected socket", s, buf, sizeof(buf), 0, &si, sizeof(si), eret, eerr); if (type == SOCK_STREAM) { close(s); s = socket(PF_INET, type, 0); } if (type == SOCK_DGRAM) eerr = EOPNOTSUPP; else eret = sizeof(buf); test_st(proto, "sendto OOB", s, buf, sizeof(buf), MSG_OOB, &si, sizeof(si), eret, eerr); if (type == SOCK_DGRAM) { eerr = EACCES; close(s); s = socket(PF_INET, type, 0); } si.sin_addr.s_addr = htonl(INADDR_BROADCAST); test_st(proto, "sendto broadcast without SO_BROADCAST set", s, buf, sizeof(buf), 0, &si, sizeof(si), eret, eerr); opt = -1; if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (void *) &opt, sizeof(opt)) != 0) test_fail(proto, "sendto", "setsockopt(SO_BROADCAST)"); else { si.sin_addr.s_addr = htonl(INADDR_BROADCAST); /* in bsd, this gets sent to the subnet bcast addr */ test_st(proto, "sendto broadcast", s, buf, sizeof(buf), 0, &si, sizeof(si), sizeof(buf), 0); } /*** this assumes a 24-bit subnet address ***/ si.sin_addr.s_addr = htonl(server | 0xffL); test_st(proto, "sendto subnet broadcast", s, buf, sizeof(buf), 0, &si, sizeof(si), sizeof(buf), 0); if (shutdown(s, 2) != 0) { test_fail(proto, "sendto", "shutdown"); } else {#if defined(SIGPIPE) && defined(SIG_IGN) signal(SIGPIPE, SIG_IGN); /* otherwise bsd aborts */#endif test_st(proto, "sendto shutdown socket", s, buf, sizeof(buf), 0, &si, sizeof(si), -1, EPIPE); } close(s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -