📄 trecvmsg.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/trecvmsg.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: trecvmsg.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:14 tneale * Tornado shuffle * * Revision 1.3 2001/01/19 22:24:55 paul * Update copyright. * * 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:57 paul * Renamed from test_recvmsg.c * * Revision 1.5 2000/03/17 00:14:48 meister * Update copyright message * * Revision 1.4 1999/07/30 21:29:46 paul * enhanced test_fail, test_pass * * Revision 1.2 1999/02/18 04:16:11 wes * Socket Merge: new code (socket tests). * * Revision 1.1.2.6 1999/02/09 18:29:33 paul * fixed empty-buffer tests * * Revision 1.1.2.5 1999/01/06 21:41:54 paul * added configurable server address * * Revision 1.1.2.4 1998/11/10 19:37:17 paul * fixed init of send buffer * * Revision 1.1.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"#ifndef MIN#define MIN(a,b) ((a)<(b)?(a):(b))#endifstatic voidtest_rm(char *proto, char *name, int s, char *snd_buf, size_t snd_len, struct msghdr *msg, int flags, int expected, int expected_errno){ int ret, srclen, i; static int j = 0; char *src; struct iovec *iov; 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 (send(s, snd_buf, snd_len, flags) != snd_len) { test_fail(proto, name, "send"); } } if (msg) { for (i = 0; i < msg->msg_iovlen; ++i) memset(msg->msg_iov[i].iov_base, 0, msg->msg_iov[i].iov_len); } /* 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 = recvmsg(s, msg, flags); test(proto, name, ret, expected, expected_errno); if ((ret != -1) && (ret == expected) && snd_buf && msg) { for (src = snd_buf, srclen = 0, i = 0; srclen < ret; ++i, src += iov->iov_len, srclen += iov->iov_len) { iov = &msg->msg_iov[i]; if (iov->iov_base && (memcmp(src, iov->iov_base, MIN(iov->iov_len, ret - srclen)) != 0)) { test_fail(proto, name, "unexpected data"); break; } } }}static void test_recvmsg_1(int type, char *proto);voidtest_recvmsg(int flags){ if (flags & TEST_UDP) test_recvmsg_1(SOCK_DGRAM, "udp"); if (flags & TEST_TCP) test_recvmsg_1(SOCK_STREAM, "tcp");}static voidtest_recvmsg_1(int type, char *proto){ int s; int opt; struct sockaddr_in snd_si; char snd_buf[63]; struct msghdr msg; struct iovec iov[4]; char buf0[12]; char buf1[3]; char buf2[32]; char buf3[16]; memset(&msg, 0, sizeof(msg)); msg.msg_iov = iov; msg.msg_iovlen = 4; iov[0].iov_base = buf0; iov[0].iov_len = sizeof(buf0); iov[1].iov_base = buf1; iov[1].iov_len = sizeof(buf1); iov[2].iov_base = buf2; iov[2].iov_len = sizeof(buf2); iov[3].iov_base = buf3; iov[3].iov_len = sizeof(buf3); s = socket(PF_INET, type, 0); if (s < 0) { test_fail(proto, "recvmsg", "socket"); return; } 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 */ if (connect(s, (struct sockaddr *) &snd_si, sizeof(snd_si)) != 0) { test_fail(proto, "recvmsg", "connect"); return; } opt = -1; if (ioctl(s, FIONBIO, (char *) &opt) == -1) test_fail(proto, "recvmsg", "ioctl(FIONBIO)"); test_rm(proto, "recvmsg invalid socket descr", -1, 0, 0, &msg, 0, -1, EBADF); test_rm(proto, "recvmsg invalid socket descr (null msg)", -1, 0, 0, 0, 0, -1, EFAULT); test_rm(proto, "recvmsg null msg", s, snd_buf, sizeof(snd_buf), 0, 0, -1, EFAULT); msg.msg_iovlen = 0; test_rm(proto, "recvmsg msg_iovlen=0", s, 0, 0, &msg, 0, 0, 0); msg.msg_iovlen = 4; if (type == SOCK_STREAM) { test_rm(proto, "recvmsg catch-up", s, 0, 0, &msg, 0, sizeof(snd_buf), 0); } test_rm(proto, "recvmsg empty queue", s, 0, 0, &msg, 0, -1, EWOULDBLOCK); msg.msg_iovlen = 0; test_rm(proto, "recvmsg empty queue (msg_iovlen=0)", s, 0, 0, &msg, 0, 0, 0); msg.msg_iovlen = 4; iov[0].iov_len = iov[1].iov_len = iov[2].iov_len = iov[3].iov_len = 0; test_rm(proto, "recvmsg all iov_len=0", s, snd_buf, sizeof(snd_buf), &msg, 0, 0, 0); iov[0].iov_len = sizeof(buf0); iov[1].iov_len = sizeof(buf1); iov[2].iov_len = sizeof(buf2); iov[3].iov_len = sizeof(buf3); if (type == SOCK_STREAM) { test_rm(proto, "recvmsg catch-up", s, 0, 0, &msg, 0, sizeof(snd_buf), 0); } iov[0].iov_len = 0; test_rm(proto, "recvmsg 1/2/3", s, snd_buf, sizeof(snd_buf), &msg, 0, sizeof(buf1) + sizeof(buf2) + sizeof(buf3), 0); iov[0].iov_len = sizeof(buf0); if (type == SOCK_STREAM) { test_rm(proto, "recvmsg catch-up", s, 0, 0, &msg, 0, sizeof(buf0), 0); } msg.msg_iovlen = 3; test_rm(proto, "recvmsg 0/1/2", s, snd_buf, sizeof(snd_buf), &msg, 0, sizeof(buf0) + sizeof(buf1) + sizeof(buf2), 0); msg.msg_iovlen = 4; if (type == SOCK_STREAM) { iov[0].iov_len = iov[1].iov_len = iov[2].iov_len = 0; test_rm(proto, "recvmsg catch-up", s, 0, 0, &msg, 0, sizeof(buf3), 0); iov[0].iov_len = sizeof(buf0); iov[1].iov_len = sizeof(buf1); iov[2].iov_len = sizeof(buf2); } test_rm(proto, "recvmsg 0/1/2/3", s, snd_buf, sizeof(snd_buf), &msg, 0, sizeof(snd_buf), 0); if (shutdown(s, 0) != 0) { test_fail(proto, "recvmsg", "shutdown"); } else { test_rm(proto, "recvmsg shutdown socket", s, snd_buf, sizeof(snd_buf), &msg, 0, 0, 0); } close(s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -