📄 trecvfrm.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/trecvfrm.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: trecvfrm.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:14 tneale * Tornado shuffle * * Revision 1.5 2001/01/30 17:18:18 paul * Don't try to turn SO_OOBINLINE off. * * Revision 1.4 2001/01/19 22:24:55 paul * Update copyright. * * Revision 1.3 2000/10/27 18:06:25 paul * Some comments should only be made in verbose mode. * * Revision 1.2 2000/10/20 18:32:35 paul * si.sin_len is only defined for BSD * * Revision 1.1 2000/10/16 19:22:54 paul * Renamed from test_recvfrom.c * * Revision 1.9 2000/03/17 00:14:48 meister * Update copyright message * * Revision 1.8 1999/07/30 21:29:46 paul * enhanced test_fail, test_pass * * Revision 1.6 1999/05/12 19:04:11 paul * Changed MSG_OOB tests from expecting success to expecting failure, * since even BSD doesn't support it. * * Revision 1.5 1999/05/03 19:23:53 paul * Added SO_RCVTIMEO test. * * Revision 1.4 1999/02/18 04:16:10 wes * Socket Merge: new code (socket tests). * * Revision 1.3.2.7 1999/02/09 20:35:38 paul * simplified test_rf * * Revision 1.3.2.6 1999/02/09 18:29:33 paul * fixed empty-buffer tests * * Revision 1.3.2.5 1999/01/06 21:41:54 paul * added configurable server address * * Revision 1.3.2.4 1998/11/10 19:37:17 paul * fixed init of send buffer * * Revision 1.3.2.3 1998/11/06 23:36:52 paul * rototill to support tcp * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include "test.h"static voidtest_rf(char *proto, char *name, int s, char *snd_buf, size_t snd_len, struct sockaddr_in *to, int tolen, void *rcv_buf, size_t rcv_len, int flags, struct sockaddr_in *from, int *fromlen, int expected, int expected_errno){ int ret; int i; static int j = 0; fd_set readfds; struct timeval tv; if (snd_buf) { /* initialize the buffer differently every time, to catch cases * where recv gets a previously-queued packet */ for (i = 0, ++j; i < snd_len; ++i) snd_buf[i] = j; if (sendto(s, snd_buf, snd_len, flags, (struct sockaddr *) to, tolen) != snd_len) { test_fail(proto, name, "sendto"); return; } } if (rcv_buf) memset(rcv_buf, 0, rcv_len); if (from) memset(from, 0, sizeof(struct sockaddr_in)); /* Select here to make sure we get either a valid echo response or * EWOULDBLOCK. Otherwise we could screw some of our 0-buffer tests. */ if (s >= 0) { /* don't try select -1 fd */ FD_ZERO(&readfds); FD_SET(s, &readfds); tv.tv_sec = 1; tv.tv_usec = 0; select(s+1, &readfds, 0, 0, &tv); } ret = recvfrom(s, rcv_buf, rcv_len, flags, (struct sockaddr *)from, fromlen); test(proto, name, ret, expected, expected_errno); if (ret != -1) { if (from) pr_sockaddr_in(from); if (rcv_buf && snd_buf && (memcmp(rcv_buf, snd_buf, ret) != 0)) { if (verbose) printf("%s: ", name); test_fail(proto, name, "unexpected data"); } }}static void test_recvfrom_1(int type, char *proto);voidtest_recvfrom(int flags){ if (flags & TEST_UDP) test_recvfrom_1(SOCK_DGRAM, "udp"); if (flags & TEST_TCP) test_recvfrom_1(SOCK_STREAM, "tcp");}static voidtest_recvfrom_1(int type, char *proto){ int s; struct sockaddr_in snd_si, rcv_si; int si_len = sizeof(struct sockaddr_in); char snd_buf[32], rcv_buf[32]; int opt; int i; struct timeval tv; for (i = 0; i < sizeof(snd_buf); ++i) snd_buf[i] = i; s = socket(PF_INET, type, 0); if (s < 0) { test_fail(proto, "recvfrom", "socket"); return; } tv.tv_sec = 5; tv.tv_usec = 0; if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (void *) &tv, sizeof(tv)) != 0) test_fail(proto, "recvfrom", "setsockopt(SO_RCVTIMEO)"); memset(&snd_si, 0, sizeof(snd_si)); snd_si.sin_family = AF_INET;#if INSTALL_ATTACHE_SOCKETS_44BSD snd_si.sin_len = sizeof(snd_si);#endif snd_si.sin_addr.s_addr = server; snd_si.sin_port = htons(7); /* echo */ test_rf(proto, "recvfrom invalid socket descr", -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, EBADF); test_rf(proto, "recvfrom null buffer", s, snd_buf, sizeof(snd_buf), &snd_si, sizeof(snd_si), 0, sizeof(rcv_buf), 0, &rcv_si, &si_len, -1, EFAULT); test_rf(proto, "recvfrom zero-length null buffer", s, 0, 0, 0, 0, 0, 0, 0, &rcv_si, &si_len, 0, 0); test_rf(proto, "recvfrom zero-length buffer", s, 0, 0, 0, 0, rcv_buf, 0, 0, &rcv_si, &si_len, 0, 0); /* UDP zero-length recv consumes data, but TCP doesn't; sync up here */ if (type == SOCK_STREAM) { test_rf(proto, "recvfrom old data", s, 0, 0, 0, 0, rcv_buf, sizeof(rcv_buf), 0, &rcv_si, &si_len, sizeof(snd_buf), 0); } test_rf(proto, "recvfrom data", s, snd_buf, sizeof(snd_buf), &snd_si, sizeof(snd_si), rcv_buf, sizeof(rcv_buf), 0, &rcv_si, &si_len, sizeof(snd_buf), 0); test_rf(proto, "recvfrom empty queue (blocking)", s, 0, 0, 0, 0, rcv_buf, sizeof(rcv_buf), 0, &rcv_si, &si_len, -1, EWOULDBLOCK); opt = -1; if (ioctl(s, FIONBIO, (char *) &opt) == -1) perror("ioctl(FIONBIO)"); test_rf(proto, "recvfrom empty queue (non-blocking)", s, 0, 0, 0, 0, rcv_buf, sizeof(rcv_buf), 0, &rcv_si, &si_len, -1, EWOULDBLOCK); test_rf(proto, "recvfrom null sockaddr", s, snd_buf, sizeof(snd_buf), &snd_si, sizeof(snd_si), rcv_buf, sizeof(rcv_buf), 0, 0, 0, sizeof(snd_buf), 0); if (type == SOCK_STREAM) { test_rf(proto, "recvfrom MSG_OOB", s, snd_buf, sizeof(snd_buf), &snd_si, sizeof(snd_si), rcv_buf, sizeof(rcv_buf), MSG_OOB, &rcv_si, &si_len, -1, EINVAL); } if (shutdown(s, 0) != 0) { test_fail(proto, "recvfrom", "shutdown"); } else { test_rf(proto, "recvfrom shutdown socket", s, snd_buf, sizeof(snd_buf), &snd_si, sizeof(snd_si), rcv_buf, sizeof(rcv_buf), 0, &rcv_si, &si_len, 0, 0); } close(s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -